1 /* 2 * Copyright (c) 2011, 2016, 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 7092965 27 * @summary javac should not close processorClassLoader before end of compilation 28 * @modules jdk.compiler/com.sun.tools.javac.api 29 * jdk.compiler/com.sun.tools.javac.file 30 * jdk.compiler/com.sun.tools.javac.util 31 */ 32 33 import com.sun.source.util.JavacTask; 34 import com.sun.source.util.TaskEvent; 35 import com.sun.source.util.TaskListener; 36 import com.sun.tools.javac.api.JavacTool; 37 import com.sun.tools.javac.file.JavacFileManager; 38 import com.sun.tools.javac.util.Context; 39 import java.io.File; 40 import java.io.IOException; 41 import java.net.URL; 42 import java.net.URLClassLoader; 43 import java.util.Arrays; 44 import java.util.Collections; 45 import java.util.List; 46 import java.util.Set; 47 import javax.annotation.processing.AbstractProcessor; 48 import javax.annotation.processing.Messager; 49 import javax.annotation.processing.RoundEnvironment; 50 import javax.annotation.processing.SupportedAnnotationTypes; 51 import javax.lang.model.SourceVersion; 52 import javax.lang.model.element.TypeElement; 53 import javax.tools.Diagnostic; 54 import javax.tools.JavaFileObject; 55 import javax.tools.StandardJavaFileManager; 56 import javax.tools.StandardLocation; 57 import javax.tools.ToolProvider; 58 59 @SupportedAnnotationTypes("*") 60 public class TestClose2 extends AbstractProcessor implements TaskListener { 61 62 public static void main(String... args) throws Exception { 63 new TestClose2().run(); 64 } 65 66 void run() throws IOException { 67 File testSrc = new File(System.getProperty("test.src")); 68 File testClasses = new File(System.getProperty("test.classes")); 69 70 JavacTool tool = (JavacTool) ToolProvider.getSystemJavaCompiler(); 71 final ClassLoader cl = getClass().getClassLoader(); 72 Context c = new Context(); 73 StandardJavaFileManager fm = new JavacFileManager(c, true, null) { 74 @Override 75 protected ClassLoader getClassLoader(URL[] urls) { 76 return new URLClassLoader(urls, cl) { 77 @Override 78 public void close() throws IOException { 79 System.err.println(getClass().getName() + " closing"); 80 TestClose2.this.closedCount++; 81 TestClose2.this.closedIsLast = true; 82 super.close(); 83 } 84 }; 85 } 86 }; 87 88 fm.setLocation(StandardLocation.CLASS_OUTPUT, 89 Collections.singleton(new File("."))); 90 fm.setLocation(StandardLocation.ANNOTATION_PROCESSOR_PATH, 91 Collections.singleton(testClasses)); 92 Iterable<? extends JavaFileObject> files = 93 fm.getJavaFileObjects(new File(testSrc, TestClose2.class.getName() + ".java")); 94 List<String> options = Arrays.asList( 95 "--add-exports", "jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED", 96 "--add-exports", "jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED", 97 "--add-exports", "jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED", 98 "-processor", TestClose2.class.getName()); 99 100 JavacTask task = tool.getTask(null, fm, null, options, null, files); 101 task.setTaskListener(this); 102 103 if (!task.call()) 104 throw new Error("compilation failed"); 105 106 if (closedCount == 0) 107 throw new Error("no closing message"); 108 else if (closedCount > 1) 109 throw new Error(closedCount + " closed messages"); 110 111 if (!closedIsLast) 112 throw new Error("closing message not last"); 113 } 114 115 // AbstractProcessor methods 116 117 @Override 118 public SourceVersion getSupportedSourceVersion() { 119 return SourceVersion.latest(); 120 } 121 122 @Override 123 public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { 124 Messager messager = processingEnv.getMessager(); 125 messager.printMessage(Diagnostic.Kind.NOTE, "processing"); 126 return true; 127 } 128 129 // TaskListener methods 130 131 @Override 132 public void started(TaskEvent e) { 133 System.err.println("Started: " + e); 134 closedIsLast = false; 135 } 136 137 @Override 138 public void finished(TaskEvent e) { 139 System.err.println("Finished: " + e); 140 closedIsLast = false; 141 } 142 143 // 144 145 int closedCount = 0; 146 boolean closedIsLast = false; 147 }