hi, i write a custom rule for java.
i want to check annotation’s value.
For example,
my java source like that
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.persistence.Entity;
@RequestMapping(value = "/pjtname/v1")
@Controller
public class HelloController {
@Entity
public class Order {
String ordered;
}
public class Client {
String cliendId;
}
}
and i want check
that value of annotation @RequestMapping (in my source code “/pjtname/v1” )
contains the word “v1” .
i show below examples but i don’t know how to check it.
- sonar-java/AbstractCallToDeprecatedCodeChecker.java at 97bac5cf4d75cf4f2da8faddd532fb225c6d9422 · SonarSource/sonar-java · GitHub
- Obtaining arguments and values inside a Spring Boot Annotation
this is my source code for this rule.
i don’t know hot to get String value of “/pjtname/v1” (annoatation value) .
for (SymbolMetadata.AnnotationInstance annotation : tree.symbol().metadata().annotations()){
String aname = annotation.symbol().name();
if (annotation.symbol().name().equals(name)) {
List<SymbolMetadata.AnnotationValue> valuesForAnnotations = tree.symbol().metadata().valuesForAnnotation("org.springframework.web.bind.annotation.RequestMapping");
for( SymbolMetadata.AnnotationValue valuesForAnnotation :valuesForAnnotations ) {
if(valuesForAnnotation.name().equals("value") ) {
Object value = valuesForAnnotation.value();
// i don't know how to get Sting type .
}
}
}
}
plese your advice for it. thank you!