gpt4 book ai didi

java - mvn Sonar :sonar running Error: cannot be cast to org. sonar.java.resolve.JavaSymbol$TypeJavaSymbol

转载 作者:搜寻专家 更新时间:2023-11-01 03:32:12 28 4
gpt4 key购买 nike

我正在编写一个 Sonarqube Java 自定义规则,但在我尝试分析我的代码时一直出现此错误:

"SonarQube is unable to analyze file 'File Path' org.sonar.java.resolve.JavaSymbol$TypeJavaSymbol cannot be cast to org.sonar.java.resolve.JavaSymbol$TypeJavaSymbol"

如您所见,原始类型和转换后的类型具有相同的限定名称。因此,我认为这只能是 ClassLoader 着色问题。

您可以在此处找到完整的开源代码: https://github.com/oasp-forge/sonarqube-devon-plugin (如果您想检查 POM 或规则类)。

这是导致问题的规则代码:

package io.oasp.ide.sonarqube.common.impl;

import io.oasp.module.basic.common.api.reflect.OaspPackage;

@Rule(key = "ArchitecturePackageCheck", name = "My Package Check", description = "Verify that the code is following My package conventions.", //
priority = Priority.MAJOR, tags = { "bug" })
public class ArchitecturePackageCheck extends BaseTreeVisitor implements JavaFileScanner {

private List<String> issues;

private String fullyQualifiedName;

/**
* The constructor.
*/
public DevonArchitecturePackageCheck() {
super();
this.issues = new ArrayList<>();
}

/**
* @return fullyQualifiedName
*/
public String getFullyQualifiedName() {

return this.fullyQualifiedName;
}

@Override
public void scanFile(JavaFileScannerContext context) {

this.issues.clear();
scan(context.getTree());
for (String issue : this.issues) {
int lineNumber = 1;
context.addIssue(lineNumber, this, issue);
}
}

@Override
public void visitIdentifier(IdentifierTree tree) {

// TODO Auto-generated method stub
super.visitIdentifier(tree);
}

@Override
public void visitClass(ClassTree tree) {

this.fullyQualifiedName = ((JavaSymbol.TypeJavaSymbol) tree.symbol()).getFullyQualifiedName();
String packageName = " ";

int lastDot = this.fullyQualifiedName.lastIndexOf('.');
if (lastDot > 0) {
packageName = this.fullyQualifiedName.substring(0, lastDot);
}
if (packageName.isEmpty()) {
this.issues.add("Invalid Package IS EMPTY!" + packageName + " !");
} else {
OaspPackage pkg = OaspPackage.of(packageName);
if (!pkg.isValid()) {
this.issues.add("Invalid Package IS VALID" + packageName + " !");
}
}
}

}

最佳答案

是的,这很可能与类加载器问题有关,您不应使用未公开 API 的类,即包 org.sonar.plugins.java.api 之外的任何类。

ClassTree 获取完全限定名称的正确方法是使用 Type 类,它可以像这样从符号中访问

classTree.symbol().type().fullyQualifiedName()

关于java - mvn Sonar :sonar running Error: cannot be cast to org. sonar.java.resolve.JavaSymbol$TypeJavaSymbol,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47451223/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com