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
What am I supposed to do now? I’m stuck here