CompileEvent.java revision 2599:50b448c5be54
1/* 2 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24/* 25 * @test 26 * @bug 8033414 27 * @summary Verify that the TaskEvent.COMPILATION is fired properly. 28 * @run main CompileEvent 29 */ 30 31import java.io.*; 32import java.util.*; 33 34import javax.tools.*; 35 36import com.sun.source.util.*; 37import com.sun.tools.javac.Main; 38import com.sun.tools.javac.api.BasicJavacTask; 39import com.sun.tools.javac.util.Context; 40import com.sun.tools.javac.util.Log; 41import com.sun.tools.javac.util.Log.WriterKind; 42 43public class CompileEvent { 44 45 public static void main(String... args) throws IOException { 46 new CompileEvent().run(); 47 } 48 49 void run() throws IOException { 50 String testClasses = System.getProperty("test.classes"); 51 File pluginRegistration = 52 new File(testClasses + "/META-INF/services/com.sun.source.util.Plugin"); 53 pluginRegistration.getParentFile().mkdirs(); 54 try (Writer metaInfRegistration = new FileWriter(pluginRegistration)) { 55 metaInfRegistration.write("CompileEvent$PluginImpl"); 56 } 57 File test = new File(testClasses + "/Test.java"); 58 test.getParentFile().mkdirs(); 59 try (Writer testFileWriter = new FileWriter(test)) { 60 testFileWriter.write("public class Test { }"); 61 } 62 63 StringWriter out; 64 65 //test events fired to listeners registered from plugins 66 //when starting compiler using Main.compile 67 out = new StringWriter(); 68 int mainResult = Main.compile(new String[] { 69 "-Xplugin:compile-event", "-processorpath", testClasses, test.getAbsolutePath() 70 }, new PrintWriter(out, true)); 71 if (mainResult != 0) 72 throw new AssertionError("Compilation failed unexpectedly, exit code: " + mainResult); 73 assertOutput(out.toString()); 74 75 JavaCompiler comp = ToolProvider.getSystemJavaCompiler(); 76 StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null); 77 Iterable<? extends JavaFileObject> testFileObjects = fm.getJavaFileObjects(test); 78 79 //test events fired to listeners registered from plugins 80 //when starting compiler using JavaCompiler.getTask(...).call 81 List<String> options = 82 Arrays.asList("-Xplugin:compile-event", "-processorpath", testClasses); 83 out = new StringWriter(); 84 boolean compResult = comp.getTask(out, null, null, options, null, testFileObjects).call(); 85 if (!compResult) 86 throw new AssertionError("Compilation failed unexpectedly."); 87 assertOutput(out.toString()); 88 } 89 90 void assertOutput(String found) { 91 String lineSeparator = System.getProperty("line.separator"); 92 if (!found.trim().replace(lineSeparator, "\n").equals(EXPECTED)) { 93 System.err.println("Expected: " + EXPECTED); 94 System.err.println("Found: " + found); 95 throw new AssertionError("Unexpected events: " + found); 96 } 97 } 98 99 private static final String EXPECTED = 100 "started(COMPILATION)\n" + 101 "started(PARSE:Test.java)\n" + 102 "finished(PARSE:Test.java)\n" + 103 "started(ENTER:Test.java)\n" + 104 "finished(ENTER:Test.java)\n" + 105 "started(ANALYZE:Test.java:Test)\n" + 106 "finished(ANALYZE:Test.java:Test)\n" + 107 "started(GENERATE:Test.java:Test)\n" + 108 "finished(GENERATE:Test.java:Test)\n" + 109 "finished(COMPILATION)"; 110 111 private static class TaskListenerImpl implements TaskListener { 112 private final PrintWriter out; 113 public TaskListenerImpl(PrintWriter out) { 114 this.out = out; 115 } 116 @Override public void started(TaskEvent e) { 117 dumpTaskEvent("started", e); 118 } 119 @Override public void finished(TaskEvent e) { 120 dumpTaskEvent("finished", e); 121 } 122 private void dumpTaskEvent(String type, TaskEvent e) { 123 StringBuilder data = new StringBuilder(); 124 data.append(type); 125 data.append("("); 126 data.append(e.getKind()); 127 if (e.getSourceFile() != null) { 128 data.append(":"); 129 data.append(new File(e.getSourceFile().getName()).getName()); 130 } 131 if (e.getTypeElement()!= null) { 132 data.append(":"); 133 data.append(e.getTypeElement().getQualifiedName()); 134 } 135 data.append(")"); 136 out.println(data); 137 } 138 } 139 140 public static final class PluginImpl implements Plugin { 141 @Override public String getName() { 142 return "compile-event"; 143 } 144 @Override public void init(JavacTask task, String... args) { 145 Context context = ((BasicJavacTask) task).getContext(); 146 Log log = Log.instance(context); 147 task.addTaskListener(new TaskListenerImpl(log.getWriter(WriterKind.NOTICE))); 148 } 149 } 150} 151