Skip to content

Instantly share code, notes, and snippets.

@bsorrentino
Last active December 23, 2015 10:29
Show Gist options
  • Save bsorrentino/6621348 to your computer and use it in GitHub Desktop.
Save bsorrentino/6621348 to your computer and use it in GitHub Desktop.
JSR269 - Java Annotation Processor - Find method usage
@SuppressWarnings("restriction")
@SupportedSourceVersion(SourceVersion.RELEASE_6)
@SupportedAnnotationTypes( "*" )
@SupportedOptions( {"method"} )
public class CodeAnalisysProcessor extends AbstractProcessor {
public class Analisys extends TreePathScanner<Object,Trees> {
final String method;
public Analisys() {
final java.util.Map<String,String> optionMap = processingEnv.getOptions();
method = optionMap.get("method");
}
@Override
public Object visitMethodInvocation(MethodInvocationTree tree, Trees trees) {
if( tree.toString().startsWith(method)) {
System.out.print('\t');
System.out.println( tree.toString() );
}
return super.visitMethodInvocation(tree, trees);
}
}
@Override
public boolean process(Set<? extends TypeElement> annotation, RoundEnvironment roundEnvironment) {
if( roundEnvironment.processingOver() ) {
return false;
}
final Trees trees = Trees.instance(processingEnv);
final Analisys visitor = new Analisys();
for (Element e : roundEnvironment.getRootElements()) {
// Add code here to analyze each root element
final TreePath tp = trees.getPath(e);
visitor.scan(tp, trees);
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment