8033414: javac Plugin to receive notification (before and) after the compilation.
authorjlahoda
Mon, 09 Jun 2014 10:17:16 +0200
changeset 24898 88fa65d2ac87
parent 24897 655b72d7b96e
child 24899 ead62a035849
8033414: javac Plugin to receive notification (before and) after the compilation. Summary: Introducing TaskEvent.Kind.COMPILATION, sending this event before and after compilation. Reviewed-by: jjg, jfranck Contributed-by: joel.franck@oracle.com, jan.lahoda@oracle.com
langtools/src/share/classes/com/sun/source/util/TaskEvent.java
langtools/src/share/classes/com/sun/tools/javac/main/JavaCompiler.java
langtools/test/tools/javac/T6358024.java
langtools/test/tools/javac/T6395974.java
langtools/test/tools/javac/T6403466.out
langtools/test/tools/javac/api/taskListeners/CompileEvent.java
langtools/test/tools/javac/api/taskListeners/TestSimpleAddRemove.java
--- a/langtools/src/share/classes/com/sun/source/util/TaskEvent.java	Sun Jun 08 15:02:34 2014 -0700
+++ b/langtools/src/share/classes/com/sun/source/util/TaskEvent.java	Mon Jun 09 10:17:16 2014 +0200
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2005, 2014, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -68,7 +68,15 @@
         /**
          * For events relating to an individual annotation processing round.
          **/
-        ANNOTATION_PROCESSING_ROUND
+        ANNOTATION_PROCESSING_ROUND,
+        /**
+         * Sent before parsing first source file, and after writing the last output file.
+         * This event is not sent when using {@link JavacTask#parse()},
+         * {@link JavacTask#analyze()} or {@link JavacTask#generate()}.
+         *
+         * @since 1.9
+         */
+        COMPILATION,
     }
 
     public TaskEvent(Kind kind) {
--- a/langtools/src/share/classes/com/sun/tools/javac/main/JavaCompiler.java	Sun Jun 08 15:02:34 2014 -0700
+++ b/langtools/src/share/classes/com/sun/tools/javac/main/JavaCompiler.java	Mon Jun 09 10:17:16 2014 +0200
@@ -832,6 +832,10 @@
                         List<String> classnames,
                         Iterable<? extends Processor> processors)
     {
+        if (!taskListener.isEmpty()) {
+            taskListener.started(new TaskEvent(TaskEvent.Kind.COMPILATION));
+        }
+
         if (processors != null && processors.iterator().hasNext())
             explicitAnnotationProcessingRequested = true;
         // as a JavaCompiler can only be used once, throw an exception if
@@ -905,6 +909,9 @@
                 printCount("error", errorCount());
                 printCount("warn", warningCount());
             }
+            if (!taskListener.isEmpty()) {
+                taskListener.finished(new TaskEvent(TaskEvent.Kind.COMPILATION));
+            }
             close();
             if (procEnvImpl != null)
                 procEnvImpl.close();
--- a/langtools/test/tools/javac/T6358024.java	Sun Jun 08 15:02:34 2014 -0700
+++ b/langtools/test/tools/javac/T6358024.java	Mon Jun 09 10:17:16 2014 +0200
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2006, 2014, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -54,13 +54,13 @@
 
         test(fm, f,
              new Option[] { new Option("-d", ".")},
-             7);
+             8);
 
         test(fm, f,
              new Option[] { new XOption("-XprintRounds"),
                             new Option("-processorpath", "."),
                             new Option("-processor", self) },
-             12);
+             13);
     }
 
     static void test(JavacFileManager fm, JavaFileObject f, Option[] opts, int expect) throws Throwable {
--- a/langtools/test/tools/javac/T6395974.java	Sun Jun 08 15:02:34 2014 -0700
+++ b/langtools/test/tools/javac/T6395974.java	Mon Jun 09 10:17:16 2014 +0200
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2006, 2014, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -29,8 +29,11 @@
 
 import java.io.*;
 import java.util.*;
+
 import javax.tools.*;
+
 import com.sun.source.util.*;
+import com.sun.source.util.TaskEvent.Kind;
 import com.sun.tools.javac.api.*;
 
 
@@ -59,16 +62,13 @@
         task.setTaskListener(tl);
 
         task.call();
-
-        if (tl.event != null)
-            throw new AssertionError("Unexpected TaskListener event: " + tl.event);
     }
 
     static class MyTaskListener implements TaskListener {
         public void started(TaskEvent e) {
-            System.err.println("Started: " + e);
-            if (event == null)
-                event = e;
+            if (e.getKind() != Kind.COMPILATION) {
+                throw new AssertionError("Unexpected TaskListener event: " + e);
+            }
         }
         public void finished(TaskEvent e) {
         }
--- a/langtools/test/tools/javac/T6403466.out	Sun Jun 08 15:02:34 2014 -0700
+++ b/langtools/test/tools/javac/T6403466.out	Mon Jun 09 10:17:16 2014 +0200
@@ -1,3 +1,4 @@
+Started TaskEvent[COMPILATION,null,null]
 Started TaskEvent[ANNOTATION_PROCESSING,null,null]
 Started TaskEvent[PARSE,T6403466.java,null]
 Finished TaskEvent[PARSE,T6403466.java,null]
@@ -40,3 +41,4 @@
 Finished TaskEvent[ANALYZE,T6403466Wrapper.java,T6403466Wrapper]
 Started TaskEvent[GENERATE,T6403466Wrapper.java,T6403466Wrapper]
 Finished TaskEvent[GENERATE,T6403466Wrapper.java,T6403466Wrapper]
+Finished TaskEvent[COMPILATION,null,null]
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/api/taskListeners/CompileEvent.java	Mon Jun 09 10:17:16 2014 +0200
@@ -0,0 +1,148 @@
+/*
+ * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+ * @test
+ * @bug 8033414
+ * @summary Verify that the TaskEvent.COMPILATION is fired properly.
+ * @run main CompileEvent
+ */
+
+import java.io.*;
+import java.util.*;
+
+import javax.tools.*;
+
+import com.sun.source.util.*;
+import com.sun.tools.javac.Main;
+import com.sun.tools.javac.api.BasicJavacTask;
+import com.sun.tools.javac.util.Context;
+import com.sun.tools.javac.util.Log;
+import com.sun.tools.javac.util.Log.WriterKind;
+
+public class CompileEvent {
+
+    public static void main(String... args) throws IOException {
+        new CompileEvent().run();
+    }
+
+    void run() throws IOException {
+        String testClasses = System.getProperty("test.classes");
+        File pluginRegistration =
+                new File(testClasses + "/META-INF/services/com.sun.source.util.Plugin");
+        pluginRegistration.getParentFile().mkdirs();
+        try (Writer metaInfRegistration = new FileWriter(pluginRegistration)) {
+            metaInfRegistration.write("CompileEvent$PluginImpl");
+        }
+        File test = new File(testClasses + "/Test.java");
+        test.getParentFile().mkdirs();
+        try (Writer testFileWriter = new FileWriter(test)) {
+            testFileWriter.write("public class Test { }");
+        }
+
+        StringWriter out;
+
+        //test events fired to listeners registered from plugins
+        //when starting compiler using Main.compile
+        out = new StringWriter();
+        int mainResult = Main.compile(new String[] {
+            "-Xplugin:compile-event", "-processorpath", testClasses, test.getAbsolutePath()
+        }, new PrintWriter(out, true));
+        if (mainResult != 0)
+            throw new AssertionError("Compilation failed unexpectedly, exit code: " + mainResult);
+        assertOutput(out);
+
+        JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
+        StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null);
+        Iterable<? extends JavaFileObject> testFileObjects = fm.getJavaFileObjects(test);
+
+        //test events fired to listeners registered from plugins
+        //when starting compiler using JavaCompiler.getTak(...).call
+        List<String> options =
+                Arrays.asList("-Xplugin:compile-event", "-processorpath", testClasses);
+        out = new StringWriter();
+        boolean compResult = comp.getTask(out, null, null, options, null, testFileObjects).call();
+        if (!compResult)
+            throw new AssertionError("Compilation failed unexpectedly.");
+        assertOutput(out);
+    }
+
+    void assertOutput(StringWriter out) {
+        String lineSeparator = System.getProperty("line.separator");
+        if (!out.toString().trim().replace(lineSeparator, "\n").equals(EXPECTED)) {
+            throw new AssertionError("Unexpected events: " + out.toString());
+        }
+    }
+
+    private static final String EXPECTED =
+        "started(COMPILATION)\n" +
+        "started(PARSE:Test.java)\n" +
+        "finished(PARSE:Test.java)\n" +
+        "started(ENTER:Test.java)\n" +
+        "finished(ENTER:Test.java)\n" +
+        "started(ANALYZE:Test.java:Test)\n" +
+        "finished(ANALYZE:Test.java:Test)\n" +
+        "started(GENERATE:Test.java:Test)\n" +
+        "finished(GENERATE:Test.java:Test)\n" +
+        "finished(COMPILATION)";
+
+    private static class TaskListenerImpl implements TaskListener {
+        private final PrintWriter out;
+        public TaskListenerImpl(PrintWriter out) {
+            this.out = out;
+        }
+        @Override public void started(TaskEvent e) {
+            dumpTaskEvent("started", e);
+        }
+        @Override public void finished(TaskEvent e) {
+            dumpTaskEvent("finished", e);
+        }
+        private void dumpTaskEvent(String type, TaskEvent e) {
+            StringBuilder data = new StringBuilder();
+            data.append(type);
+            data.append("(");
+            data.append(e.getKind());
+            if (e.getSourceFile() != null) {
+                data.append(":");
+                data.append(new File(e.getSourceFile().getName()).getName());
+            }
+            if (e.getTypeElement()!= null) {
+                data.append(":");
+                data.append(e.getTypeElement().getQualifiedName());
+            }
+            data.append(")");
+            out.println(data);
+        }
+    }
+
+    public static final class PluginImpl implements Plugin {
+        @Override public String getName() {
+            return "compile-event";
+        }
+        @Override public void init(JavacTask task, String... args) {
+            Context context = ((BasicJavacTask) task).getContext();
+            Log log = Log.instance(context);
+            task.addTaskListener(new TaskListenerImpl(log.getWriter(WriterKind.NOTICE)));
+        }
+    }
+}
--- a/langtools/test/tools/javac/api/taskListeners/TestSimpleAddRemove.java	Sun Jun 08 15:02:34 2014 -0700
+++ b/langtools/test/tools/javac/api/taskListeners/TestSimpleAddRemove.java	Mon Jun 09 10:17:16 2014 +0200
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2011, 2014, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -287,10 +287,16 @@
                         found = "{}";
                         break;
                     case REMOVE_IN_PROCESSOR:
-                        found = "{PARSE=1:1, ENTER=2:2, ANNOTATION_PROCESSING=1:0, ANNOTATION_PROCESSING_ROUND=2:1}";
+                        if (ck == CompileKind.CALL)
+                            found = "{PARSE=1:1, ENTER=2:2, ANNOTATION_PROCESSING=1:0, ANNOTATION_PROCESSING_ROUND=2:1, COMPILATION=1:0}";
+                        else
+                            found = "{PARSE=1:1, ENTER=2:2, ANNOTATION_PROCESSING=1:0, ANNOTATION_PROCESSING_ROUND=2:1}";
                         break;
                     case REMOVE_IN_LISTENER:
-                        found = "{PARSE=1:1, ENTER=3:3, ANALYZE=1:1, GENERATE=1:0, ANNOTATION_PROCESSING=1:1, ANNOTATION_PROCESSING_ROUND=2:2}";
+                        if (ck == CompileKind.CALL)
+                            found = "{PARSE=1:1, ENTER=3:3, ANALYZE=1:1, GENERATE=1:0, ANNOTATION_PROCESSING=1:1, ANNOTATION_PROCESSING_ROUND=2:2, COMPILATION=1:0}";
+                        else
+                            found = "{PARSE=1:1, ENTER=3:3, ANALYZE=1:1, GENERATE=1:0, ANNOTATION_PROCESSING=1:1, ANNOTATION_PROCESSING_ROUND=2:2}";
                         break;
                     default:
                         throw new IllegalStateException();
@@ -302,7 +308,10 @@
                 switch (rk) {
                     // Remove will fail (too early), so events to end will be recorded
                     case REMOVE_IN_TASK:
-                        found = "{ENTER=2:2, ANALYZE=1:1, GENERATE=1:1, ANNOTATION_PROCESSING=0:1, ANNOTATION_PROCESSING_ROUND=1:2}";
+                        if (ck == CompileKind.CALL)
+                            found = "{ENTER=2:2, ANALYZE=1:1, GENERATE=1:1, ANNOTATION_PROCESSING=0:1, ANNOTATION_PROCESSING_ROUND=1:2, COMPILATION=0:1}";
+                        else
+                            found = "{ENTER=2:2, ANALYZE=1:1, GENERATE=1:1, ANNOTATION_PROCESSING=0:1, ANNOTATION_PROCESSING_ROUND=1:2}";
                         break;
                     case REMOVE_IN_PROCESSOR:
                         found = "{ENTER=1:1, ANNOTATION_PROCESSING_ROUND=1:1}";
@@ -321,7 +330,10 @@
                     // Remove will fail (too early, so events to end will be recorded
                     case REMOVE_IN_TASK:
                     case REMOVE_IN_PROCESSOR:
-                        found = "{ANALYZE=0:1, GENERATE=1:1}";
+                        if (ck == CompileKind.CALL)
+                            found = "{ANALYZE=0:1, GENERATE=1:1, COMPILATION=0:1}";
+                        else
+                            found = "{ANALYZE=0:1, GENERATE=1:1}";
                         break;
                     // Remove will succeed during "GENERATE.finished" event
                     case REMOVE_IN_LISTENER: