aoqi@0: /* aoqi@0: * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: /* aoqi@0: * @test aoqi@0: * @bug 7092965 aoqi@0: * @summary javac should not close processorClassLoader before end of compilation aoqi@0: */ aoqi@0: aoqi@0: import com.sun.source.util.JavacTask; aoqi@0: import com.sun.source.util.TaskEvent; aoqi@0: import com.sun.source.util.TaskListener; aoqi@0: import com.sun.tools.javac.api.JavacTool; aoqi@0: import com.sun.tools.javac.file.JavacFileManager; aoqi@0: import com.sun.tools.javac.util.Context; aoqi@0: import java.io.File; aoqi@0: import java.io.IOException; aoqi@0: import java.net.URL; aoqi@0: import java.net.URLClassLoader; aoqi@0: import java.util.Arrays; aoqi@0: import java.util.Collections; aoqi@0: import java.util.List; aoqi@0: import java.util.Set; aoqi@0: import javax.annotation.processing.AbstractProcessor; aoqi@0: import javax.annotation.processing.Messager; aoqi@0: import javax.annotation.processing.RoundEnvironment; aoqi@0: import javax.annotation.processing.SupportedAnnotationTypes; aoqi@0: import javax.lang.model.SourceVersion; aoqi@0: import javax.lang.model.element.TypeElement; aoqi@0: import javax.tools.Diagnostic; aoqi@0: import javax.tools.JavaFileObject; aoqi@0: import javax.tools.StandardJavaFileManager; aoqi@0: import javax.tools.StandardLocation; aoqi@0: import javax.tools.ToolProvider; aoqi@0: aoqi@0: @SupportedAnnotationTypes("*") aoqi@0: public class TestClose2 extends AbstractProcessor implements TaskListener { aoqi@0: aoqi@0: public static void main(String... args) throws Exception { aoqi@0: new TestClose2().run(); aoqi@0: } aoqi@0: aoqi@0: void run() throws IOException { aoqi@0: File testSrc = new File(System.getProperty("test.src")); aoqi@0: File testClasses = new File(System.getProperty("test.classes")); aoqi@0: aoqi@0: JavacTool tool = (JavacTool) ToolProvider.getSystemJavaCompiler(); aoqi@0: final ClassLoader cl = getClass().getClassLoader(); aoqi@0: Context c = new Context(); aoqi@0: StandardJavaFileManager fm = new JavacFileManager(c, true, null) { aoqi@0: @Override aoqi@0: protected ClassLoader getClassLoader(URL[] urls) { aoqi@0: return new URLClassLoader(urls, cl) { aoqi@0: @Override aoqi@0: public void close() throws IOException { aoqi@0: System.err.println(getClass().getName() + " closing"); aoqi@0: TestClose2.this.closedCount++; aoqi@0: TestClose2.this.closedIsLast = true; aoqi@0: super.close(); aoqi@0: } aoqi@0: }; aoqi@0: } aoqi@0: }; aoqi@0: aoqi@0: fm.setLocation(StandardLocation.CLASS_OUTPUT, aoqi@0: Collections.singleton(new File("."))); aoqi@0: fm.setLocation(StandardLocation.ANNOTATION_PROCESSOR_PATH, aoqi@0: Collections.singleton(testClasses)); aoqi@0: Iterable files = aoqi@0: fm.getJavaFileObjects(new File(testSrc, TestClose2.class.getName() + ".java")); aoqi@0: List options = Arrays.asList( aoqi@0: "-processor", TestClose2.class.getName()); aoqi@0: aoqi@0: JavacTask task = tool.getTask(null, fm, null, options, null, files); aoqi@0: task.setTaskListener(this); aoqi@0: aoqi@0: if (!task.call()) aoqi@0: throw new Error("compilation failed"); aoqi@0: aoqi@0: if (closedCount == 0) aoqi@0: throw new Error("no closing message"); aoqi@0: else if (closedCount > 1) aoqi@0: throw new Error(closedCount + " closed messages"); aoqi@0: aoqi@0: if (!closedIsLast) aoqi@0: throw new Error("closing message not last"); aoqi@0: } aoqi@0: aoqi@0: // AbstractProcessor methods aoqi@0: aoqi@0: @Override aoqi@0: public SourceVersion getSupportedSourceVersion() { aoqi@0: return SourceVersion.latest(); aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public boolean process(Set annotations, RoundEnvironment roundEnv) { aoqi@0: Messager messager = processingEnv.getMessager(); aoqi@0: messager.printMessage(Diagnostic.Kind.NOTE, "processing"); aoqi@0: return true; aoqi@0: } aoqi@0: aoqi@0: // TaskListener methods aoqi@0: aoqi@0: @Override aoqi@0: public void started(TaskEvent e) { aoqi@0: System.err.println("Started: " + e); aoqi@0: closedIsLast = false; aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public void finished(TaskEvent e) { aoqi@0: System.err.println("Finished: " + e); aoqi@0: closedIsLast = false; aoqi@0: } aoqi@0: aoqi@0: // aoqi@0: aoqi@0: int closedCount = 0; aoqi@0: boolean closedIsLast = false; aoqi@0: }