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