Search code examples
javasonarqubesonarqube-scansonar-runnersonarlint

sonar custom rule check java collection type is unknownSymbol


Collection type always return 'unknownSymbol' in a custom sonar check rule class.

//demo class

public class SaasConstantNameCheckCase {
    private static final long serialVersionUID;
    private final String title;
    public static final Integer maxSize = 1111;// Noncompliant
    private static List list = new ArrayList<>();
    private static HashMap<String, String> map = new HashMap<>();
}

// custom rule class

@Override
public void visitVariable(VariableTree tree) {
    VariableSymbol symbol = (VariableSymbol) tree.symbol();
    String type = symbol.type().name();
    System.out.println("type->"+type);
}

// test result

type->long
type->String
type->Integer
type->!unknownSymbol!
type->!unknownSymbol!

Why?


Solution

  • SonarJava will show this unknownSymbol! string when it fails to resolve the actual type symbols.

    In this particular case, you should either use fully qualified types java.util.List and java.util.HashMap, or import them.