!未知符号!为sonarqube编写java类时
大家好,我的社区 我不熟悉用SonarQube编写java自定义规则 我正在编写一个自定义规则来检测通过maven使用的某些接口。然而,在测试期间,“!unknownSymbol!”出现,而不是测试用例实现的接口类 在我的测试用例中,我编写了一个实现2个接口的类:- -java.util.List -org.osgi.framework.BundleActivator 该测试可以检测两个接口,但当我将其转换为符号树,然后使用fullyQualifiedName()方法时,它返回“!unknownSymbol!”。但是,当我执行一个简单的toString()时,它会返回接口名 我想知道如何正确地将其转换为symbolTree对象,并在不遇到“!UnknownsSymbol!”的情况下获得正确的接口 多谢各位 守则:!未知符号!为sonarqube编写java类时,java,maven,sonarqube,Java,Maven,Sonarqube,大家好,我的社区 我不熟悉用SonarQube编写java自定义规则 我正在编写一个自定义规则来检测通过maven使用的某些接口。然而,在测试期间,“!unknownSymbol!”出现,而不是测试用例实现的接口类 在我的测试用例中,我编写了一个实现2个接口的类:- -java.util.List -org.osgi.framework.BundleActivator 该测试可以检测两个接口,但当我将其转换为符号树,然后使用fullyQualifiedName()方法时,它返回“!unknown
package org.sonar.samples.java.checks;
import com.google.common.collect.ImmutableList;
import org.sonar.check.Priority;
import org.sonar.check.Rule;
import org.sonar.plugins.java.api.IssuableSubscriptionVisitor;
import org.sonar.plugins.java.api.tree.ClassTree;
import org.sonar.plugins.java.api.tree.ListTree;
import org.sonar.plugins.java.api.tree.Tree;
import org.sonar.plugins.java.api.tree.TypeTree;
import java.util.List;
@Rule(
key = "AvoidInterfaceClass",
name = "No Interfaces",
description = "Not allowed to implement any interface(s)",
priority = Priority.MAJOR
)
public class AvoidSuperInterfacesRule extends IssuableSubscriptionVisitor {
public static final List<String> SUPER_INTERFACE_AVOID = ImmutableList.of("org.osgi.framework.BundleActivator","java.util.List");
@Override
public List<Tree.Kind> nodesToVisit() {
return ImmutableList.of(Tree.Kind.CLASS);
}
public void visitNode(Tree tree){
//System.out.println("Here");
ClassTree treeClass = (ClassTree) tree;
if (treeClass.superInterfaces().isEmpty()) {
return;
}
ListTree<TypeTree> superInterfaceNames = treeClass.superInterfaces();
System.out.println(superInterfaceNames.size()); //Number of interfaces
System.out.println();
for (TypeTree t:superInterfaceNames){
String name = t.symbolType().fullyQualifiedName();
System.out.println(name);
System.out.println(t.toString());
System.out.println();
if (SUPER_INTERFACE_AVOID.contains(name)){
reportIssue(t, "This interface is not allowed to be implemented.");
}
}
}
}
你能把问题的代码和确切的错误信息分享给大家吗?尤其是当消息指出问题的确切位置时。当您这样做时,请尽可能多地复制粘贴文本,而不是截图。我已将代码与错误消息一起插入。非常感谢。
09:18:36.332 [main] INFO org.sonar.squidbridge.ProgressReport - 1 source files to be analyzed
09:18:36.439 [main] DEBUG org.sonar.java.bytecode.ClassLoaderBuilder - ----- Classpath analyzed by Squid:
09:18:36.439 [main] DEBUG org.sonar.java.bytecode.ClassLoaderBuilder - C:\Users\attanyg1\Desktop\Sonarqube\java-custom-rules\target\test-jars\commons-collections4-4.0.jar
09:18:36.439 [main] DEBUG org.sonar.java.bytecode.ClassLoaderBuilder - C:\Users\attanyg1\Desktop\Sonarqube\java-custom-rules\target\test-jars\javaee-api-6.0.jar
09:18:36.439 [main] DEBUG org.sonar.java.bytecode.ClassLoaderBuilder - C:\Users\attanyg1\Desktop\Sonarqube\java-custom-rules\target\test-jars\spring-context-4.3.3.RELEASE.jar
09:18:36.440 [main] DEBUG org.sonar.java.bytecode.ClassLoaderBuilder - C:\Users\attanyg1\Desktop\Sonarqube\java-custom-rules\target\test-jars\spring-web-4.3.3.RELEASE.jar
09:18:36.440 [main] DEBUG org.sonar.java.bytecode.ClassLoaderBuilder - C:\Users\attanyg1\Desktop\Sonarqube\java-custom-rules\target\test-jars\spring-webmvc-4.3.3.RELEASE.jar
09:18:36.440 [main] DEBUG org.sonar.java.bytecode.ClassLoaderBuilder - C:\Users\attanyg1\Desktop\Sonarqube\java-custom-rules\target\test-classes
09:18:36.440 [main] DEBUG org.sonar.java.bytecode.ClassLoaderBuilder - -----
2 -> Number of Interfaces
!unknownSymbol! -> symbolType.getFullyQualifiedName()
BundleActivator -> toString()
!unknownSymbol! -> symbolType.getFullyQualifiedName()
List -> toString()
09:18:36.798 [Report about progress of Java AST analyzer] INFO org.sonar.squidbridge.ProgressReport - 1/1 source files have been analyzed