1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/tools/javac/processing/loader/testClose/TestClose.java Wed Sep 21 21:56:53 2011 -0700 1.3 @@ -0,0 +1,229 @@ 1.4 +/* 1.5 + * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + */ 1.26 + 1.27 +/* 1.28 + * @test 1.29 + * @bug 7092965 1.30 + * @summary javac should not close processorClassLoader before end of compilation 1.31 + */ 1.32 + 1.33 +import com.sun.source.util.JavacTask; 1.34 +import com.sun.source.util.TaskEvent; 1.35 +import com.sun.source.util.TaskListener; 1.36 +import com.sun.tools.javac.api.ClientCodeWrapper.Trusted; 1.37 +import com.sun.tools.javac.api.JavacTool; 1.38 +import com.sun.tools.javac.processing.JavacProcessingEnvironment; 1.39 +import com.sun.tools.javac.util.Context; 1.40 +import java.io.ByteArrayOutputStream; 1.41 +import java.io.File; 1.42 +import java.io.IOException; 1.43 +import java.io.PrintStream; 1.44 +import java.lang.reflect.Field; 1.45 +import java.net.URI; 1.46 +import java.util.ArrayList; 1.47 +import java.util.Arrays; 1.48 +import java.util.Collections; 1.49 +import java.util.List; 1.50 +import javax.annotation.processing.ProcessingEnvironment; 1.51 +import javax.tools.JavaFileObject; 1.52 +import javax.tools.SimpleJavaFileObject; 1.53 +import javax.tools.StandardJavaFileManager; 1.54 +import javax.tools.StandardLocation; 1.55 +import javax.tools.ToolProvider; 1.56 + 1.57 +/* 1.58 + * The test compiles an annotation processor and a helper class into a 1.59 + * custom classes directory. 1.60 + * 1.61 + * It then uses them while compiling a dummy file, with the custom classes 1.62 + * directory on the processor path, thus guaranteeing that references to 1.63 + * these class are satisfied by the processor class loader. 1.64 + * 1.65 + * The annotation processor uses the javac TaskListener to run code 1.66 + * after annotation processing has completed, to verify that the classloader 1.67 + * is not closed until the end of the compilation. 1.68 + */ 1.69 + 1.70 +@Trusted // avoids use of ClientCodeWrapper 1.71 +public class TestClose implements TaskListener { 1.72 + public static final String annoProc = 1.73 + "import java.util.*;\n" + 1.74 + "import javax.annotation.processing.*;\n" + 1.75 + "import javax.lang.model.*;\n" + 1.76 + "import javax.lang.model.element.*;\n" + 1.77 + "import com.sun.source.util.*;\n" + 1.78 + "import com.sun.tools.javac.processing.*;\n" + 1.79 + "import com.sun.tools.javac.util.*;\n" + 1.80 + "@SupportedAnnotationTypes(\"*\")\n" + 1.81 + "public class AnnoProc extends AbstractProcessor {\n" + 1.82 + " @Override\n" + 1.83 + " public SourceVersion getSupportedSourceVersion() {\n" + 1.84 + " return SourceVersion.latest();\n" + 1.85 + " }\n" + 1.86 + " @Override\n" + 1.87 + " public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {\n" + 1.88 + " System.out.println(\"in AnnoProc.process\");\n" + 1.89 + " final ClassLoader cl = getClass().getClassLoader();\n" + 1.90 + " if (roundEnv.processingOver()) {\n" + 1.91 + " TestClose.add(processingEnv, new Runnable() {\n" + 1.92 + " public void run() {\n" + 1.93 + " System.out.println(getClass().getName() + \": run()\");\n" + 1.94 + " try {\n" + 1.95 + " cl.loadClass(\"Callback\")\n" + 1.96 + " .asSubclass(Runnable.class)\n" + 1.97 + " .newInstance()\n" + 1.98 + " .run();\n" + 1.99 + " } catch (ReflectiveOperationException e) {\n" + 1.100 + " throw new Error(e);\n" + 1.101 + " }\n" + 1.102 + " }\n" + 1.103 + " });\n" + 1.104 + " }\n" + 1.105 + " return true;\n" + 1.106 + " }\n" + 1.107 + "}\n"; 1.108 + 1.109 + public static final String callback = 1.110 + "public class Callback implements Runnable {\n" + 1.111 + " public void run() {\n" + 1.112 + " System.out.println(getClass().getName() + \": run()\");\n" + 1.113 + " }\n" + 1.114 + "}"; 1.115 + 1.116 + public static void main(String... args) throws Exception { 1.117 + new TestClose().run(); 1.118 + } 1.119 + 1.120 + void run() throws IOException { 1.121 + JavacTool tool = (JavacTool) ToolProvider.getSystemJavaCompiler(); 1.122 + StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null); 1.123 + 1.124 + File classes = new File("classes"); 1.125 + classes.mkdirs(); 1.126 + File extraClasses = new File("extraClasses"); 1.127 + extraClasses.mkdirs(); 1.128 + 1.129 + System.out.println("compiling classes to extraClasses"); 1.130 + { // setup class in extraClasses 1.131 + fm.setLocation(StandardLocation.CLASS_OUTPUT, 1.132 + Collections.singleton(extraClasses)); 1.133 + List<? extends JavaFileObject> files = Arrays.asList( 1.134 + new MemFile("AnnoProc.java", annoProc), 1.135 + new MemFile("Callback.java", callback)); 1.136 + JavacTask task = tool.getTask(null, fm, null, null, null, files); 1.137 + check(task.call()); 1.138 + } 1.139 + 1.140 + System.out.println("compiling dummy to classes with anno processor"); 1.141 + { // use that class in a TaskListener after processing has completed 1.142 + PrintStream prev = System.out; 1.143 + String out; 1.144 + ByteArrayOutputStream baos = new ByteArrayOutputStream(); 1.145 + try (PrintStream ps = new PrintStream(baos)) { 1.146 + System.setOut(ps); 1.147 + File testClasses = new File(System.getProperty("test.classes")); 1.148 + fm.setLocation(StandardLocation.CLASS_OUTPUT, 1.149 + Collections.singleton(classes)); 1.150 + fm.setLocation(StandardLocation.ANNOTATION_PROCESSOR_PATH, 1.151 + Arrays.asList(extraClasses, testClasses)); 1.152 + List<? extends JavaFileObject> files = Arrays.asList( 1.153 + new MemFile("my://dummy", "class Dummy { }")); 1.154 + List<String> options = Arrays.asList("-processor", "AnnoProc"); 1.155 + JavacTask task = tool.getTask(null, fm, null, options, null, files); 1.156 + task.setTaskListener(this); 1.157 + check(task.call()); 1.158 + } finally { 1.159 + System.setOut(prev); 1.160 + out = baos.toString(); 1.161 + if (!out.isEmpty()) 1.162 + System.out.println(out); 1.163 + } 1.164 + check(out.contains("AnnoProc$1: run()")); 1.165 + check(out.contains("Callback: run()")); 1.166 + } 1.167 + } 1.168 + 1.169 + @Override 1.170 + public void started(TaskEvent e) { 1.171 + System.out.println("Started: " + e); 1.172 + } 1.173 + 1.174 + @Override 1.175 + public void finished(TaskEvent e) { 1.176 + System.out.println("Finished: " + e); 1.177 + if (e.getKind() == TaskEvent.Kind.ANALYZE) { 1.178 + for (Runnable r: runnables) { 1.179 + System.out.println("running " + r); 1.180 + r.run(); 1.181 + } 1.182 + } 1.183 + } 1.184 + 1.185 + void check(boolean b) { 1.186 + if (!b) 1.187 + throw new AssertionError(); 1.188 + } 1.189 + 1.190 + 1.191 + public static void add(ProcessingEnvironment env, Runnable r) { 1.192 + try { 1.193 + Context c = ((JavacProcessingEnvironment) env).getContext(); 1.194 + Object o = c.get(TaskListener.class); 1.195 + // The TaskListener is an instanceof TestClose, but when using the 1.196 + // default class loaders. the taskListener uses a different 1.197 + // instance of Class<TestClose> than the anno processor. 1.198 + // If you try to evaluate 1.199 + // TestClose tc = (TestClose) (o). 1.200 + // you get the following somewhat confusing error: 1.201 + // java.lang.ClassCastException: TestClose cannot be cast to TestClose 1.202 + // The workaround is to access the fields of TestClose with reflection. 1.203 + Field f = o.getClass().getField("runnables"); 1.204 + @SuppressWarnings("unchecked") 1.205 + List<Runnable> runnables = (List<Runnable>) f.get(o); 1.206 + runnables.add(r); 1.207 + } catch (Throwable t) { 1.208 + System.err.println(t); 1.209 + } 1.210 + } 1.211 + 1.212 + public List<Runnable> runnables = new ArrayList<>(); 1.213 + 1.214 + class MemFile extends SimpleJavaFileObject { 1.215 + public final String text; 1.216 + 1.217 + MemFile(String name, String text) { 1.218 + super(URI.create(name), JavaFileObject.Kind.SOURCE); 1.219 + this.text = text; 1.220 + } 1.221 + 1.222 + @Override 1.223 + public String getName() { 1.224 + return uri.toString(); 1.225 + } 1.226 + 1.227 + @Override 1.228 + public String getCharContent(boolean ignoreEncodingErrors) { 1.229 + return text; 1.230 + } 1.231 + } 1.232 +}