Failed to get method annotation value

I’m developing a custom rule that needs to fetch the annotation value of methods, but I seem to not get what I want when analyzing some projects.

sonarqube.version = Community Edition 8.9.2 (build 46101)
sonarjava.version = 6.15.1.26024
we use sonnar-scanner to do analysis

my method definition is as follows:

interface MyInterface extends Iface {
    @MyAnno(name = "name", param = MyEnum.MyEnumValue)
    void method(String str);
}

my annoatation definition:

public @interface MyAnno {
    String name();
    MyEnum param();
}

MyEnum definition:

public enum MyEnum {
    MyEnumValue(1);

    private int code;
    MyEnum(int code) {
        this.code = code;
    }
}

I find that param can be resolved as VariableSymbol, so I’m doing this to get the variable name

public String getVarName(Symbol methodSymbol) {
    for (AnnotationValue annotationValue : methodSymbol.metadata().valuesForAnnotation("MyAnno")) {
        if (annotationValue.name().equals("param") && annotationValue.value() instance of VariableSymbol) {
            VariableSymbol variableSymbol = (VariableSymbol) annotationValue.value();
            if (null != variableSymbol) {
                return variableSymbol.name();
            }
        }
    }
    return null;
}

when I call getVarName, I always get null instead of MyEnumValue in the above case. what makes it more strange is that I run my rule on multiple projects, but only one project has this problem. I’m a little bit confused, what may be the difference between these projects?

I’m facing this problem on the production sonarqube server, I cannot reproduce the problem when I run analysis locally on the same project(I use gradle to run analysis on my developing machine, not sure whether it matters or not), strange too :sob:

What am I supposed to do now? I’m stuck here :joy:

I find that when I build my project with gradle-2.14,the problem is gone and when I use gradle 4.10,the problem shows。I add some logs and find that methodSymbol is unknown, the difference between gradle2.14 and gradle4.10 building classes is that gradle4.10’s file path is something like build/classes/java/main/... while gradle2.14 file path is like build/classes/main/....

UPDATE
after some digging, I find that sonarqube use ASTParser from eclipse.jdt to build semantic tree, and it takes sonar.java.binaries as the project class file path, our online config is **/build/c;asses/*, and this can only match the first level directory under the base directory. and the file generated by gradle4.10 is under the secondary directory, which will lead to the missing of the project binaries/

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.