Anonymous avatar Anonymous committed 215e589

Splitting the AP invoker to NB and JDK part.

Comments (0)

Files changed (3)

analysis/ap/src/j1/tutorial/javac/analysis/infrastructure/APInvoker.java

-package j1.tutorial.javac.analysis.infrastructure;
-
-import com.sun.source.util.TaskEvent;
-import com.sun.source.util.TaskEvent.Kind;
-import com.sun.source.util.TaskListener;
-import com.sun.source.util.Trees;
-import com.sun.tools.javac.api.MultiTaskListener;
-import com.sun.tools.javac.processing.JavacProcessingEnvironment;
-import com.sun.tools.javac.util.Context;
-import j1.tutorial.javac.analysis.api.AnalysisContext;
-import j1.tutorial.javac.analysis.api.Rule;
-import java.util.ServiceLoader;
-import java.util.Set;
-import javax.annotation.processing.AbstractProcessor;
-import javax.annotation.processing.Processor;
-import javax.annotation.processing.RoundEnvironment;
-import javax.annotation.processing.SupportedAnnotationTypes;
-import javax.lang.model.SourceVersion;
-import javax.lang.model.element.TypeElement;
-import org.openide.util.lookup.ServiceProvider;
-
-/**Only works in NetBeans or JDK8.
- * 
- * @author lahvac
- */
-@SupportedAnnotationTypes("*")
-@ServiceProvider(service=Processor.class)
-public class APInvoker extends AbstractProcessor {
-
-    @Override
-    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
-        if (roundEnv.processingOver()) {
-            if (SourceVersion.latest().compareTo(SourceVersion.RELEASE_7) > 0) {
-                //JDK8:
-                MultiTaskListener.instance(((JavacProcessingEnvironment) processingEnv).getContext()).add(new TaskListenerImpl(Trees.instance(processingEnv), null));
-            } else {
-                //NetBeans 7.2:
-                Context context = ((JavacProcessingEnvironment) processingEnv).getContext();
-                TaskListener origTL = context.get(TaskListener.class);
-                if (origTL != null) context.put(TaskListener.class, (TaskListener) null);
-                context.put(TaskListener.class, new TaskListenerImpl(Trees.instance(processingEnv), origTL));
-            }
-        }
-        
-        return false;
-    }
-    
-    private final class TaskListenerImpl implements TaskListener {
-
-        private final Trees trees;
-        private final TaskListener origTL;
-
-        public TaskListenerImpl(Trees trees, TaskListener origTL) {
-            this.trees = trees;
-            this.origTL = origTL;
-        }
-        
-        @Override public void started(TaskEvent te) {
-            if (origTL != null) origTL.started(te);
-        }
-
-        @Override public void finished(TaskEvent te) {
-            if (origTL != null) origTL.finished(te);
-            if (te.getKind() == Kind.ANALYZE) {
-                for (Rule r : ServiceLoader.load(Rule.class, APInvoker.class.getClassLoader())) {
-                    r.analyseFile(new AnalysisContext(processingEnv, trees), te.getCompilationUnit());
-                }
-            }
-        }
-        
-    }
-    
-}

analysis/ap/src/j1/tutorial/javac/analysis/infrastructure/JDK8APInvoker.java

+package j1.tutorial.javac.analysis.infrastructure;
+
+import com.sun.source.util.JavacTask;
+import com.sun.source.util.TaskEvent;
+import com.sun.source.util.TaskEvent.Kind;
+import com.sun.source.util.TaskListener;
+import com.sun.source.util.Trees;
+import j1.tutorial.javac.analysis.api.AnalysisContext;
+import j1.tutorial.javac.analysis.api.Rule;
+import java.util.ServiceLoader;
+import java.util.Set;
+import javax.annotation.processing.AbstractProcessor;
+import javax.annotation.processing.Processor;
+import javax.annotation.processing.RoundEnvironment;
+import javax.annotation.processing.SupportedAnnotationTypes;
+import javax.lang.model.SourceVersion;
+import javax.lang.model.element.TypeElement;
+import org.openide.util.lookup.ServiceProvider;
+
+/**Only works in very recent builds of JDK8.
+ * 
+ * @author lahvac
+ */
+@SupportedAnnotationTypes("*")
+@ServiceProvider(service=Processor.class)
+public class JDK8APInvoker extends AbstractProcessor {
+
+    @Override
+    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
+        if (roundEnv.processingOver()) {
+            if (SourceVersion.latest().compareTo(SourceVersion.RELEASE_7) > 0) {
+                JavacTask.instance(processingEnv).addTaskListener(new TaskListenerImpl(Trees.instance(processingEnv), null));
+            }
+        }
+        
+        return false;
+    }
+    
+    private final class TaskListenerImpl implements TaskListener {
+
+        private final Trees trees;
+        private final TaskListener origTL;
+
+        public TaskListenerImpl(Trees trees, TaskListener origTL) {
+            this.trees = trees;
+            this.origTL = origTL;
+        }
+        
+        @Override public void started(TaskEvent te) {
+            if (origTL != null) origTL.started(te);
+        }
+
+        @Override public void finished(TaskEvent te) {
+            if (origTL != null) origTL.finished(te);
+            if (te.getKind() == Kind.ANALYZE) {
+                for (Rule r : ServiceLoader.load(Rule.class, JDK8APInvoker.class.getClassLoader())) {
+                    r.analyseFile(new AnalysisContext(processingEnv, trees), te.getCompilationUnit());
+                }
+            }
+        }
+        
+    }
+    
+}

analysis/ap/src/j1/tutorial/javac/analysis/infrastructure/NBAPInvoker.java

+package j1.tutorial.javac.analysis.infrastructure;
+
+import com.sun.source.util.TaskEvent;
+import com.sun.source.util.TaskEvent.Kind;
+import com.sun.source.util.TaskListener;
+import com.sun.source.util.Trees;
+import com.sun.tools.javac.processing.JavacProcessingEnvironment;
+import com.sun.tools.javac.util.Context;
+import com.sun.tools.javac.util.Options;
+import j1.tutorial.javac.analysis.api.AnalysisContext;
+import j1.tutorial.javac.analysis.api.Rule;
+import java.util.ServiceLoader;
+import java.util.Set;
+import javax.annotation.processing.AbstractProcessor;
+import javax.annotation.processing.Processor;
+import javax.annotation.processing.RoundEnvironment;
+import javax.annotation.processing.SupportedAnnotationTypes;
+import javax.lang.model.SourceVersion;
+import javax.lang.model.element.TypeElement;
+import org.openide.util.lookup.ServiceProvider;
+
+/**Only for NetBeans (7.3)
+ * 
+ * @author lahvac
+ */
+@SupportedAnnotationTypes("*")
+@ServiceProvider(service=Processor.class)
+public class NBAPInvoker extends AbstractProcessor {
+
+    @Override
+    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
+        if (roundEnv.processingOver()) {
+            if (SourceVersion.latest().compareTo(SourceVersion.RELEASE_7) <= 0) {
+                //NetBeans 7.2:
+                Context context = ((JavacProcessingEnvironment) processingEnv).getContext();
+                if (Options.instance(context).isSet("ide")) {
+                    TaskListener origTL = context.get(TaskListener.class);
+                    if (origTL != null) context.put(TaskListener.class, (TaskListener) null);
+                    context.put(TaskListener.class, new TaskListenerImpl(Trees.instance(processingEnv), origTL));
+                }
+            }
+        }
+        
+        return false;
+    }
+    
+    private final class TaskListenerImpl implements TaskListener {
+
+        private final Trees trees;
+        private final TaskListener origTL;
+
+        public TaskListenerImpl(Trees trees, TaskListener origTL) {
+            this.trees = trees;
+            this.origTL = origTL;
+        }
+        
+        @Override public void started(TaskEvent te) {
+            if (origTL != null) origTL.started(te);
+        }
+
+        @Override public void finished(TaskEvent te) {
+            if (origTL != null) origTL.finished(te);
+            if (te.getKind() == Kind.ANALYZE) {
+                for (Rule r : ServiceLoader.load(Rule.class, NBAPInvoker.class.getClassLoader())) {
+                    r.analyseFile(new AnalysisContext(processingEnv, trees), te.getCompilationUnit());
+                }
+            }
+        }
+        
+    }
+    
+}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.