Mon, 18 Feb 2013 14:29:40 -0800
8008339: Test TargetAnnoCombo.java is broken
Reviewed-by: jjh
1 /*
2 * Copyright (c) 2012, 2013, 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 import java.io.File;
25 import java.io.IOException;
26 import java.net.URI;
27 import java.net.URISyntaxException;
28 import java.util.Arrays;
29 import java.util.Iterator;
31 import javax.tools.Diagnostic;
32 import javax.tools.DiagnosticCollector;
33 import javax.tools.JavaCompiler;
34 import javax.tools.JavaCompiler.CompilationTask;
35 import javax.tools.JavaFileObject;
36 import javax.tools.SimpleJavaFileObject;
37 import javax.tools.StandardJavaFileManager;
38 import javax.tools.StandardLocation;
39 import javax.tools.ToolProvider;
41 import com.sun.source.util.JavacTask;
43 public class Helper {
45 enum ContentVars {
47 IMPORTCONTAINERSTMTS("\nimport java.lang.annotation.Repeatable;\n"),
48 IMPORTDEPRECATED("import java.lang.Deprecated;\n"),
49 IMPORTDOCUMENTED("import java.lang.annotation.Documented;\n"),
50 IMPORTINHERITED("import java.lang.annotation.Inherited;\n"),
51 IMPORTRETENTION("import java.lang.annotation.Retention;\n"
52 + "\nimport java.lang.annotation.RetentionPolicy;\n"),
53 IMPORTSTMTS("import java.lang.annotation.*;\n"),
54 IMPORTEXPECTED("import expectedFiles.*;\n"),
55 REPEATABLE("\n@Repeatable(FooContainer.class)\n"),
56 CONTAINER("@interface FooContainer {\n" + " Foo[] value();\n}\n"),
57 BASE("@interface Foo {}\n"),
58 BASEANNO("@Foo"),
59 LEGACYCONTAINER("@FooContainer(value = {@Foo, @Foo})\n"),
60 REPEATABLEANNO("\n@Foo() @Foo()"),
61 DEPRECATED("\n@Deprecated"),
62 DOCUMENTED("\n@Documented"),
63 INHERITED("\n@Inherited"),
64 TARGET("\n@Target(#VAL)\n"),
65 RETENTION("@Retention(RetentionPolicy.#VAL)\n"),
66 RETENTIONRUNTIME("@Retention(RetentionPolicy.RUNTIME)\n");
67 private String val;
69 private ContentVars(String val) {
70 this.val = val;
71 }
73 public String getVal() {
74 return val;
75 }
76 }
78 // Create and compile FileObject using values for className and contents
79 public static boolean compileCode(String className, String contents,
80 DiagnosticCollector<JavaFileObject> diagnostics) {
81 boolean ok = false;
82 JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
83 if (compiler == null) {
84 throw new RuntimeException("can't get javax.tools.JavaCompiler!");
85 }
87 JavaFileObject file = getFile(className, contents);
88 Iterable<? extends JavaFileObject> compilationUnit = Arrays.asList(file);
90 CompilationTask task = compiler.getTask(null, null, diagnostics, null, null, compilationUnit);
91 ok = task.call();
92 return ok;
93 }
94 // Compile a list of FileObjects
95 // Used when packages are needed and classes need to be loaded at runtime
96 static File destDir = new File(System.getProperty("user.dir"));
98 public static boolean compileCode(DiagnosticCollector<JavaFileObject> diagnostics, Iterable<? extends JavaFileObject> files) {
99 boolean ok = false;
100 JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
101 if (compiler == null) {
102 throw new RuntimeException("can't get javax.tools.JavaCompiler!");
103 }
105 StandardJavaFileManager fm = compiler.getStandardFileManager(null, null, null);
107 // Assuming filesCount can maximum be 2 and if true, one file is package-info.java
108 if (isPkgInfoPresent(files)) {
109 JavacTask task = (JavacTask) compiler.getTask(null, fm, diagnostics, null, null, files);
110 try {
111 fm.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(destDir));
112 task.generate();
113 } catch (IOException ioe) {
114 throw new RuntimeException("Compilation failed for package level tests", ioe);
115 }
116 int err = 0;
117 for (Diagnostic<? extends JavaFileObject> d : diagnostics.getDiagnostics()) {
118 if(d.getKind() == Diagnostic.Kind.ERROR) {
119 err++;
120 }
121 }
122 ok = (err == 0);
123 } else {
124 CompilationTask task = compiler.getTask(null, null, diagnostics, null, null, files);
125 ok = task.call();
126 }
127 return ok;
128 }
130 static private boolean isPkgInfoPresent(Iterable<? extends JavaFileObject> files) {
131 Iterator<? extends JavaFileObject> itr = files.iterator();
132 while (itr.hasNext()) {
133 String name = itr.next().getName();
134 if (name.contains("package-info")) {
135 return true;
136 }
137 }
138 return false;
139 }
140 /* String template where /*<TYPE>*/ /*gets replaced by repeating anno
141 * Used to generate test src for combo tests
142 * - BasicSyntaxCombo.java
143 * - TargetAnnoCombo.java
144 */
146 public static final String template =
147 "/*PACKAGE*/\n"
148 + "//pkg test;\n\n"
149 + "/*TYPE*/ //class\n"
150 + "class #ClassName {\n"
151 + " /*FIELD*/ //instance var\n"
152 + " public int x = 0;\n\n"
153 + " /*FIELD*/ //Enum constants\n"
154 + " TestEnum testEnum;\n\n"
155 + " /*FIELD*/ // Static field\n"
156 + " public static int num;\n\n"
157 + " /*STATIC_INI*/\n"
158 + " static { \n" + "num = 10; \n }\n\n"
159 + " /*CONSTRUCTOR*/\n"
160 + " #ClassName() {}\n\n"
161 + " /*INSTANCE_INI*/\n"
162 + " { \n x = 10; \n }"
163 + " /*INNER_CLASS*/\n"
164 + " class innerClass {}\n"
165 + " /*METHOD*/\n"
166 + " void bar(/*PARAMETER*/ int baz) {\n"
167 + " /*LOCAL_VARIABLE*/\n"
168 + " int y = 0;\n"
169 + " }\n"
170 + "}\n\n"
171 + "/*TYPE*/ //Enum\n"
172 + "enum TestEnum {}\n\n"
173 + "/*TYPE*/ //Interface\n"
174 + "interface TestInterface {}\n\n"
175 + "/*TYPE*/\n"
176 + "/*ANNOTATION_TYPE*/\n"
177 + "@interface TestAnnotationType{}\n"
178 + "class TestPkg {}\n"
179 + "class TestTypeAnno </*TYPE_PARAMETER*/ T extends Object> {\n"
180 + " String /*TYPE_USE*/[] arr;\n"
181 + "}";
183 static JavaFileObject getFile(String name, String code) {
184 JavaFileObject o = null;
185 try {
186 o = new JavaStringFileObject(name, code);
187 } catch (URISyntaxException e) {
188 throw new RuntimeException(e);
189 }
190 return o;
191 }
193 static class JavaStringFileObject extends SimpleJavaFileObject {
195 final String theCode;
197 public JavaStringFileObject(String fileName, String theCode) throws URISyntaxException {
198 super(new URI("string:///" + fileName.replace('.', '/') + ".java"), Kind.SOURCE);
199 this.theCode = theCode;
200 }
202 @Override
203 public CharSequence getCharContent(boolean ignoreEncodingErrors) {
204 return theCode;
205 }
206 }
207 }