Mvn sonar: sonar running Error: cannot be added to org.sonar.java.resolve.JavaSymbol $ TypeJavaSymbol

I am writing a special Sonarqube Java rule, but I continue this error when I try to parse my code:

"SonarQube cannot parse file. File Path" org.sonar.java.resolve.JavaSymbol $ TypeJavaSymbol cannot be dropped org.sonar.java.resolve.JavaSymbol $ TypeJavaSymbol "

As you can see, the source type and cast type have the same qualified name. Therefore, I assume that this could only be a problem with ClassLoader shading.

Here you can find all the open source code: https://github.com/oasp-forge/sonarqube-devon-plugin (if you want to check the POM class or rules).

Here is the rule code causing the problem:

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 + " !");
      }
    }
  }

}
+4
source share
1 answer

Yes, this is most likely due to the classloader problem, you should not use classes that are not exposed to the public API, i.e. anything outside the package org.sonar.plugins.java.api.

The correct way to get the full name from ClassTreeis to use a class Typethat can be accessed from such a character

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

+1
source

Source: https://habr.com/ru/post/1689725/


All Articles