1/* 2 * Copyright (c) 2014, 2015, 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 * @modules jdk.compiler/com.sun.tools.javac.api 29 * jdk.compiler/com.sun.tools.javac.util 30 * @run main CompileEvent 31 */ 32 33import java.io.*; 34import java.util.*; 35 36import javax.tools.*; 37 38import com.sun.source.util.*; 39import com.sun.tools.javac.Main; 40import com.sun.tools.javac.api.BasicJavacTask; 41import com.sun.tools.javac.util.Context; 42import com.sun.tools.javac.util.Log; 43import com.sun.tools.javac.util.Log.WriterKind; 44 45public class CompileEvent { 46 47 public static void main(String... args) throws IOException { 48 new CompileEvent().run(); 49 } 50 51 void run() throws IOException { 52 String testClasses = System.getProperty("test.classes"); 53 File pluginRegistration = 54 new File(testClasses + "/META-INF/services/com.sun.source.util.Plugin"); 55 pluginRegistration.getParentFile().mkdirs(); 56 try (Writer metaInfRegistration = new FileWriter(pluginRegistration)) { 57 metaInfRegistration.write("CompileEvent$PluginImpl"); 58 } 59 File test = new File(testClasses + "/Test.java"); 60 test.getParentFile().mkdirs(); 61 try (Writer testFileWriter = new FileWriter(test)) { 62 testFileWriter.write("public class Test { }"); 63 } 64 65 StringWriter out; 66 67 //test events fired to listeners registered from plugins 68 //when starting compiler using Main.compile 69 out = new StringWriter(); 70 int mainResult = Main.compile(new String[] { 71 "-XDaccessInternalAPI", "-Xplugin:compile-event", "-processorpath", testClasses, test.getAbsolutePath() 72 }, new PrintWriter(out, true)); 73 if (mainResult != 0) 74 throw new AssertionError("Compilation failed unexpectedly, exit code: " + mainResult); 75 assertOutput(out.toString()); 76 77 JavaCompiler comp = ToolProvider.getSystemJavaCompiler(); 78 try (StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null)) { 79 Iterable<? extends JavaFileObject> testFileObjects = fm.getJavaFileObjects(test); 80 81 //test events fired to listeners registered from plugins 82 //when starting compiler using JavaCompiler.getTask(...).call 83 List<String> options = 84 Arrays.asList("-XDaccessInternalAPI", "-Xplugin:compile-event", "-processorpath", testClasses); 85 out = new StringWriter(); 86 boolean compResult = comp.getTask(out, null, null, options, null, testFileObjects).call(); 87 if (!compResult) 88 throw new AssertionError("Compilation failed unexpectedly."); 89 assertOutput(out.toString()); 90 } 91 } 92 93 void assertOutput(String found) { 94 String lineSeparator = System.getProperty("line.separator"); 95 if (!found.trim().replace(lineSeparator, "\n").equals(EXPECTED)) { 96 System.err.println("Expected: " + EXPECTED); 97 System.err.println("Found: " + found); 98 throw new AssertionError("Unexpected events: " + found); 99 } 100 } 101 102 private static final String EXPECTED = 103 "started(COMPILATION)\n" + 104 "started(PARSE:Test.java)\n" + 105 "finished(PARSE:Test.java)\n" + 106 "started(ENTER:Test.java)\n" + 107 "finished(ENTER:Test.java)\n" + 108 "started(ANALYZE:Test.java:Test)\n" + 109 "finished(ANALYZE:Test.java:Test)\n" + 110 "started(GENERATE:Test.java:Test)\n" + 111 "finished(GENERATE:Test.java:Test)\n" + 112 "finished(COMPILATION)"; 113 114 private static class TaskListenerImpl implements TaskListener { 115 private final PrintWriter out; 116 public TaskListenerImpl(PrintWriter out) { 117 this.out = out; 118 } 119 @Override public void started(TaskEvent e) { 120 dumpTaskEvent("started", e); 121 } 122 @Override public void finished(TaskEvent e) { 123 dumpTaskEvent("finished", e); 124 } 125 private void dumpTaskEvent(String type, TaskEvent e) { 126 StringBuilder data = new StringBuilder(); 127 data.append(type); 128 data.append("("); 129 data.append(e.getKind()); 130 if (e.getSourceFile() != null) { 131 data.append(":"); 132 data.append(new File(e.getSourceFile().getName()).getName()); 133 } 134 if (e.getTypeElement()!= null) { 135 data.append(":"); 136 data.append(e.getTypeElement().getQualifiedName()); 137 } 138 data.append(")"); 139 out.println(data); 140 } 141 } 142 143 public static final class PluginImpl implements Plugin { 144 @Override public String getName() { 145 return "compile-event"; 146 } 147 @Override public void init(JavacTask task, String... args) { 148 Context context = ((BasicJavacTask) task).getContext(); 149 Log log = Log.instance(context); 150 task.addTaskListener(new TaskListenerImpl(log.getWriter(WriterKind.NOTICE))); 151 } 152 } 153} 154