author | darcy |
Thu, 16 Jan 2014 18:00:25 -0800 | |
changeset 22444 | c79ee1e6742f |
parent 7681 | 1f0819a3341f |
child 27319 | 030080f03e4f |
permissions | -rw-r--r-- |
10 | 1 |
/* |
7681 | 2 |
* Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved. |
10 | 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 |
* |
|
5520 | 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. |
|
10 | 22 |
*/ |
23 |
||
24 |
/** |
|
25 |
* @test |
|
26 |
* @bug 6341866 |
|
27 |
* @summary Source files loaded from source path are not subject to annotation processing |
|
28 |
* @build Anno T6341866 |
|
29 |
* @run main T6341866 |
|
30 |
*/ |
|
31 |
||
32 |
import java.io.*; |
|
33 |
import java.util.*; |
|
34 |
import javax.annotation.processing.*; |
|
35 |
import javax.tools.*; |
|
36 |
||
37 |
/** |
|
38 |
* For each of a number of implicit compilation scenarios, |
|
39 |
* and for each of a set of annotation processing scenarios, |
|
40 |
* verify that a class file is generated, or not, for an |
|
41 |
* implicitly compiled source file and that the correct |
|
42 |
* warning message is given for implicitly compiled files |
|
43 |
* when annotation processing. |
|
44 |
*/ |
|
45 |
public class T6341866 { |
|
46 |
static final String testSrc = System.getProperty("test.src", "."); |
|
47 |
static final String testClasses = System.getProperty("test.classes", "."); |
|
48 |
static final File a_java = new File(testSrc, "A.java"); |
|
49 |
static final File a_class = new File("A.class"); |
|
50 |
static final File b_java = new File(testSrc, "B.java"); |
|
51 |
static final File b_class = new File("B.class"); |
|
52 |
static final File processorServices = services(Processor.class); |
|
53 |
||
54 |
enum ImplicitType { |
|
55 |
NONE(null), // don't use implicit compilation |
|
56 |
OPT_UNSET(null), // implicit compilation, but no -implicit option |
|
57 |
OPT_NONE("-implicit:none"), // implicit compilation wiith -implicit:none |
|
58 |
OPT_CLASS("-implicit:class"); // implicit compilation wiith -implicit:class |
|
59 |
||
60 |
ImplicitType(String opt) { |
|
61 |
this.opt = opt; |
|
62 |
} |
|
63 |
final String opt; |
|
64 |
}; |
|
65 |
||
66 |
enum AnnoType { |
|
67 |
NONE, // no annotation processing |
|
68 |
SERVICE, // implicit annotation processing, via ServiceLoader |
|
69 |
SPECIFY // explicit annotation processing |
|
70 |
}; |
|
71 |
||
72 |
||
73 |
public static void main(String ... args) throws Exception { |
|
74 |
boolean ok = true; |
|
75 |
||
76 |
// iterate over all combinations |
|
77 |
for (ImplicitType implicitType: EnumSet.allOf(ImplicitType.class)) { |
|
78 |
for (AnnoType annoType: EnumSet.allOf(AnnoType.class)) { |
|
79 |
ok &= test(implicitType, annoType); |
|
80 |
} |
|
81 |
} |
|
82 |
||
83 |
if (!ok) |
|
84 |
throw new AssertionError("test failed"); |
|
85 |
} |
|
86 |
||
87 |
/** |
|
88 |
* Verify that a class file is generated, or not, for an implicitly compiled source file, |
|
89 |
* and that the correct warning message is given for implicitly compiled files when annotation processing. |
|
90 |
*/ |
|
91 |
static boolean test(ImplicitType implicitType, AnnoType annoType) throws IOException { |
|
92 |
System.err.println("test implicit=" + implicitType + " anno=" + annoType); |
|
93 |
||
94 |
// ensure clean start |
|
95 |
a_class.delete(); |
|
96 |
b_class.delete(); |
|
97 |
processorServices.delete(); |
|
98 |
||
99 |
List<String> opts = new ArrayList<String>(); |
|
22444
c79ee1e6742f
8031745: Remove unneeded/obsolete -source/-target options in javac tests, part 1
darcy
parents:
7681
diff
changeset
|
100 |
opts.addAll(Arrays.asList("-d", ".", "-sourcepath", testSrc, "-classpath", testClasses, "-Xlint:-options")); |
10 | 101 |
if (implicitType.opt != null) |
102 |
opts.add(implicitType.opt); |
|
103 |
||
104 |
switch (annoType) { |
|
105 |
case SERVICE: |
|
106 |
createProcessorServices(Anno.class.getName()); |
|
107 |
break; |
|
108 |
case SPECIFY: |
|
109 |
opts.addAll(Arrays.asList("-processor", Anno.class.getName())); |
|
110 |
break; |
|
111 |
} |
|
112 |
||
113 |
||
114 |
JavaCompiler javac = ToolProvider.getSystemJavaCompiler(); |
|
115 |
MyDiagListener dl = new MyDiagListener(); |
|
116 |
StandardJavaFileManager fm = javac.getStandardFileManager(dl, null, null); |
|
117 |
||
118 |
// Note: class A references class B, so compile A if we want implicit compilation |
|
119 |
File file = (implicitType != ImplicitType.NONE) ? a_java : b_java; |
|
120 |
Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(file); |
|
121 |
||
122 |
//System.err.println("compile: " + opts + " " + files); |
|
123 |
||
124 |
boolean ok = javac.getTask(null, fm, dl, opts, null, files).call(); |
|
125 |
if (!ok) { |
|
126 |
error("compilation failed"); |
|
127 |
return false; |
|
128 |
} |
|
129 |
||
130 |
// check implicit compilation results if necessary |
|
131 |
if (implicitType != ImplicitType.NONE) { |
|
132 |
boolean expectClass = (implicitType != ImplicitType.OPT_NONE); |
|
133 |
if (b_class.exists() != expectClass) { |
|
134 |
if (b_class.exists()) |
|
135 |
error("B implicitly compiled unexpectedly"); |
|
136 |
else |
|
137 |
error("B not impliictly compiled"); |
|
138 |
return false; |
|
139 |
} |
|
140 |
} |
|
141 |
||
142 |
// check message key results |
|
143 |
String expectKey = null; |
|
144 |
if (implicitType == ImplicitType.OPT_UNSET) { |
|
145 |
switch (annoType) { |
|
146 |
case SERVICE: |
|
147 |
expectKey = "compiler.warn.proc.use.proc.or.implicit"; |
|
148 |
break; |
|
149 |
case SPECIFY: |
|
150 |
expectKey = "compiler.warn.proc.use.implicit"; |
|
151 |
break; |
|
152 |
} |
|
153 |
} |
|
154 |
||
155 |
if (expectKey == null) { |
|
156 |
if (dl.diagCodes.size() != 0) { |
|
157 |
error("no diagnostics expected"); |
|
158 |
return false; |
|
159 |
} |
|
160 |
} else { |
|
161 |
if (!(dl.diagCodes.size() == 1 && dl.diagCodes.get(0).equals(expectKey))) { |
|
162 |
error("unexpected diagnostics generated"); |
|
163 |
return false; |
|
164 |
} |
|
165 |
} |
|
166 |
||
167 |
return true; |
|
168 |
} |
|
169 |
||
170 |
static void createProcessorServices(String name) throws IOException { |
|
171 |
processorServices.getParentFile().mkdirs(); |
|
172 |
||
173 |
BufferedWriter out = new BufferedWriter(new FileWriter(processorServices)); |
|
174 |
out.write(name); |
|
175 |
out.newLine(); |
|
176 |
out.close(); |
|
177 |
} |
|
178 |
||
179 |
static class MyDiagListener implements DiagnosticListener<JavaFileObject> { |
|
180 |
public void report(Diagnostic d) { |
|
181 |
diagCodes.add(d.getCode()); |
|
182 |
System.err.println(d); |
|
183 |
} |
|
184 |
||
185 |
List<String> diagCodes = new ArrayList<String>(); |
|
186 |
} |
|
187 |
||
188 |
static void error(String msg) { |
|
168 | 189 |
System.err.println("ERROR: " + msg); |
10 | 190 |
} |
191 |
||
192 |
static File services(Class<?> service) { |
|
193 |
String[] dirs = { testClasses, "META-INF", "services" }; |
|
194 |
File dir = null; |
|
195 |
for (String d: dirs) |
|
196 |
dir = (dir == null ? new File(d) : new File(dir, d)); |
|
197 |
||
198 |
return new File(dir, service.getName()); |
|
199 |
} |
|
200 |
} |