check if annotation processing suppressed

This commit is contained in:
Eugene Zhuravlev
2012-02-05 12:35:15 +01:00
parent ab368c4c90
commit 4572ada668
2 changed files with 87 additions and 3 deletions
@@ -1,9 +1,16 @@
package org.jetbrains.jps.javac;
import com.sun.source.tree.*;
import com.sun.source.util.TreeScanner;
import com.sun.source.util.Trees;
import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.ProcessingEnvironment;
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.lang.model.element.Element;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.Name;
import javax.lang.model.element.TypeElement;
import java.util.Set;
@@ -15,12 +22,80 @@ import java.util.Set;
//@SupportedSourceVersion(SourceVersion.RELEASE_7)
@SupportedAnnotationTypes("*")
public class JavacASTAnalyser extends AbstractProcessor{
private Trees myTrees;
private final boolean mySuppressOtherProcessors;
public JavacASTAnalyser(boolean suppressOtherProcessors) {
mySuppressOtherProcessors = suppressOtherProcessors;
}
@Override
public void init(ProcessingEnvironment processingEnv) {
super.init(processingEnv);
myTrees = Trees.instance(processingEnv);
}
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
MyAnalyzer scaner = new MyAnalyzer();
for (Element element : roundEnv.getRootElements()) {
System.out.println("Element: " + element.getSimpleName());
final Tree tree = myTrees.getTree(element);
scaner.scan(tree, myTrees);
}
return false;
return mySuppressOtherProcessors;
}
private static class MyAnalyzer extends TreeScanner<Object, Trees> {
@Override
public Object visitImport(ImportTree node, Trees trees) {
return null/*super.visitImport(node, trees)*/;
}
@Override
public Object visitClass(ClassTree node, Trees trees) {
return scan(node.getMembers(), trees);
}
@Override
public Object visitVariable(VariableTree node, Trees trees) {
final ModifiersTree modifiers = node.getModifiers();
final Set<Modifier> flags = modifiers.getFlags();
if (flags.contains(Modifier.STATIC) && flags.contains(Modifier.FINAL)) {
final Name variableName = node.getName();
// todo register constant
final ConstantRefsFinder finder = new ConstantRefsFinder();
finder.scan(node.getInitializer(), trees);
// todo: process found refs
}
return null;
}
@Override
public Object visitMethod(MethodTree node, Trees trees) {
final ConstantRefsFinder finder = new ConstantRefsFinder();
finder.scan(node.getBody(), trees);
// todo: process found refs
return null;
}
}
private static class ConstantRefsFinder extends TreeScanner<Object, Trees> {
@Override
public Object visitMethodInvocation(MethodInvocationTree node, Trees trees) {
return scan(node.getArguments(), trees);
}
@Override
public Object visitMemberSelect(MemberSelectTree node, Trees trees) {
return super.visitMemberSelect(node, trees);
}
@Override
public Object visitIdentifier(IdentifierTree node, Trees trees) {
return super.visitIdentifier(node, trees);
}
}
}
@@ -68,7 +68,7 @@ public class JavacMain {
final JavaCompiler.CompilationTask task = compiler.getTask(
out, fileManager, outConsumer, filterOptionList(options), null, fileManager.toJavaFileObjects(sources)
);
//final JavacASTAnalyser analyzer = new JavacASTAnalyser();
//final JavacASTAnalyser analyzer = new JavacASTAnalyser(shouldSuppressAnnotationProcessing(options));
//task.setProcessors(Collections.singleton(analyzer));
return task.call();
}
@@ -77,6 +77,15 @@ public class JavacMain {
}
}
private static boolean shouldSuppressAnnotationProcessing(final Collection<String> options) {
for (String option : options) {
if ("-proc:none".equals(option)) {
return true;
}
}
return false;
}
private static Collection<String> filterOptionList(final Collection<String> options) {
if (options.isEmpty()) {
return options;