Mon, 21 Jan 2013 01:27:42 -0500
8004182: Add support for profiles in javac
Reviewed-by: mcimadamore
1 /*
2 * Copyright (c) 2011, 2012, 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 */
24 /*
25 * @test
26 * @bug 8004182
27 * @summary Add support for profiles in javac
28 */
30 import java.io.PrintWriter;
31 import java.io.StringWriter;
32 import java.lang.annotation.Annotation;
33 import java.lang.annotation.Retention;
34 import java.lang.annotation.RetentionPolicy;
35 import java.lang.reflect.InvocationTargetException;
36 import java.lang.reflect.Method;
37 import java.net.URI;
38 import java.util.ArrayList;
39 import java.util.Arrays;
40 import java.util.Collections;
41 import java.util.EnumMap;
42 import java.util.List;
43 import java.util.Map;
45 import javax.tools.Diagnostic;
46 import javax.tools.DiagnosticCollector;
47 import javax.tools.JavaCompiler;
48 import javax.tools.JavaFileObject;
49 import javax.tools.SimpleJavaFileObject;
50 import javax.tools.StandardJavaFileManager;
52 import com.sun.source.util.JavacTask;
53 import com.sun.tools.javac.api.JavacTool;
54 import com.sun.tools.javac.jvm.Profile;
55 import com.sun.tools.javac.jvm.Target;
58 public class ProfileOptionTest {
59 public static void main(String... args) throws Exception {
60 new ProfileOptionTest().run();
61 }
63 private final JavaCompiler javac = JavacTool.create();
64 private final StandardJavaFileManager fm = javac.getStandardFileManager(null, null, null);
67 // ---------- Test cases, invoked reflectively via run. ----------
69 @Test
70 void testInvalidProfile_CommandLine() throws Exception {
71 JavaFileObject fo = new StringJavaFileObject("Test.java", "class Test { }");
72 String badName = "foo";
73 List<String> opts = Arrays.asList("-profile", badName);
74 StringWriter sw = new StringWriter();
75 try {
76 JavacTask task = (JavacTask) javac.getTask(sw, fm, null, opts, null,
77 Arrays.asList(fo));
78 throw new Exception("expected exception not thrown");
79 } catch (IllegalArgumentException e) {
80 // expected
81 }
82 }
84 @Test
85 void testInvalidProfile_API() throws Exception {
86 String badName = "foo";
87 String[] opts = { "-profile", badName };
88 StringWriter sw = new StringWriter();
89 PrintWriter pw = new PrintWriter(sw);
90 int rc = com.sun.tools.javac.Main.compile(opts, pw);
92 // sadly, command line errors are not (yet?) reported to
93 // the diag listener
94 String out = sw.toString();
95 if (!out.isEmpty())
96 System.err.println(out.trim());
98 if (!out.contains("invalid profile: " + badName)) {
99 error("expected message not found");
100 }
101 }
103 @Test
104 void testTargetProfileCombinations() throws Exception {
105 JavaFileObject fo = new StringJavaFileObject("Test.java", "class Test { }");
106 for (Target t: Target.values()) {
107 switch (t) {
108 case JDK1_1: case JDK1_2: // no equivalent -source
109 case JDK1_4_1: case JDK1_4_2: case JSR14: // transitional values
110 continue;
111 }
113 for (Profile p: Profile.values()) {
114 List<String> opts = new ArrayList<String>();
115 opts.addAll(Arrays.asList("-source", t.name, "-target", t.name));
116 opts.add("-Xlint:-options"); // dont warn about no -bootclasspath
117 if (p != Profile.DEFAULT)
118 opts.addAll(Arrays.asList("-profile", p.name));
119 StringWriter sw = new StringWriter();
120 JavacTask task = (JavacTask) javac.getTask(sw, fm, null, opts, null,
121 Arrays.asList(fo));
122 task.analyze();
124 // sadly, command line errors are not (yet?) reported to
125 // the diag listener
126 String out = sw.toString();
127 if (!out.isEmpty())
128 System.err.println(out.trim());
130 switch (t) {
131 case JDK1_8:
132 if (!out.isEmpty())
133 error("unexpected output from compiler");
134 break;
135 default:
136 if (p != Profile.DEFAULT
137 && !out.contains("profile " + p.name
138 + " is not valid for target release " + t.name)) {
139 error("expected message not found");
140 }
141 }
142 }
143 }
144 }
146 @Test
147 void testClassesInProfiles() throws Exception {
148 for (Profile p: Profile.values()) {
149 for (Map.Entry<Profile, List<JavaFileObject>> e: testClasses.entrySet()) {
150 for (JavaFileObject fo: e.getValue()) {
151 DiagnosticCollector<JavaFileObject> dl =
152 new DiagnosticCollector<JavaFileObject>();
153 List<String> opts = (p == Profile.DEFAULT)
154 ? Collections.<String>emptyList()
155 : Arrays.asList("-profile", p.name);
156 JavacTask task = (JavacTask) javac.getTask(null, fm, dl, opts, null,
157 Arrays.asList(fo));
158 task.analyze();
160 List<String> expectDiagCodes = (p.value >= e.getKey().value)
161 ? Collections.<String>emptyList()
162 : Arrays.asList("compiler.err.not.in.profile");
164 checkDiags(opts + " " + fo.getName(), dl.getDiagnostics(), expectDiagCodes);
165 }
166 }
167 }
168 }
170 Map<Profile, List<JavaFileObject>> testClasses =
171 new EnumMap<Profile, List<JavaFileObject>>(Profile.class);
173 void initTestClasses() {
174 // The following table assumes the existence of specific classes
175 // in specific profiles, as defined in the Java SE 8 spec.
176 init(Profile.COMPACT1,
177 java.lang.String.class);
179 init(Profile.COMPACT2,
180 javax.xml.XMLConstants.class);
182 init(Profile.COMPACT3,
183 javax.script.Bindings.class,
184 com.sun.security.auth.PolicyFile.class); // specifically included in 3
186 init(Profile.DEFAULT,
187 java.beans.BeanInfo.class,
188 javax.management.remote.rmi._RMIServer_Stub.class); // specifically excluded in 3
189 }
191 void init(Profile p, Class<?>... classes) {
192 List<JavaFileObject> srcs = new ArrayList<JavaFileObject>();
193 for (Class<?> c: classes) {
194 String name = "T" + c.getSimpleName();
195 String src =
196 "class T" + name + "{" + "\n" +
197 " Class<?> c = " + c.getName() + ".class;\n" +
198 "}";
199 srcs.add(new StringJavaFileObject(name + ".java", src));
200 }
201 testClasses.put(p, srcs);
202 }
204 void checkDiags(String msg, List<Diagnostic<? extends JavaFileObject>> diags, List<String> expectDiagCodes) {
205 System.err.print(msg);
206 if (diags.isEmpty())
207 System.err.println(" OK");
208 else {
209 System.err.println();
210 System.err.println(diags);
211 }
213 List<String> foundDiagCodes = new ArrayList<String>();
214 for (Diagnostic<? extends JavaFileObject> d: diags)
215 foundDiagCodes.add(d.getCode());
217 if (!foundDiagCodes.equals(expectDiagCodes)) {
218 System.err.println("Found diag codes: " + foundDiagCodes);
219 System.err.println("Expected diag codes: " + expectDiagCodes);
220 error("expected diagnostics not found");
221 }
222 }
224 /** Marker annotation for test cases. */
225 @Retention(RetentionPolicy.RUNTIME)
226 @interface Test { }
228 /** Run all test cases. */
229 void run() throws Exception {
230 initTestClasses();
232 for (Method m: getClass().getDeclaredMethods()) {
233 Annotation a = m.getAnnotation(Test.class);
234 if (a != null) {
235 System.err.println(m.getName());
236 try {
237 m.invoke(this, new Object[] { });
238 } catch (InvocationTargetException e) {
239 Throwable cause = e.getCause();
240 throw (cause instanceof Exception) ? ((Exception) cause) : e;
241 }
242 System.err.println();
243 }
244 }
246 if (errors > 0)
247 throw new Exception(errors + " errors occurred");
248 }
250 void error(String msg) {
251 System.err.println("Error: " + msg);
252 errors++;
253 }
255 int errors;
257 private static class StringJavaFileObject extends SimpleJavaFileObject {
258 StringJavaFileObject(String name, String text) {
259 super(URI.create(name), JavaFileObject.Kind.SOURCE);
260 this.text = text;
261 }
262 @Override
263 public CharSequence getCharContent(boolean b) {
264 return text;
265 }
266 private String text;
267 }
268 }