|
1 /* |
|
2 * Copyright (c) 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 */ |
|
23 |
|
24 import java.net.URI; |
|
25 import java.net.URISyntaxException; |
|
26 import java.util.Arrays; |
|
27 import javax.tools.DiagnosticCollector; |
|
28 import javax.tools.JavaCompiler; |
|
29 import javax.tools.JavaFileObject; |
|
30 import javax.tools.SimpleJavaFileObject; |
|
31 import javax.tools.ToolProvider; |
|
32 import javax.tools.JavaCompiler.CompilationTask; |
|
33 |
|
34 public class Helper { |
|
35 |
|
36 enum ContentVars { |
|
37 IMPORTCONTAINERSTMTS("\nimport java.lang.annotation.ContainedBy;\n" + |
|
38 "\nimport java.lang.annotation.ContainerFor;\n"), |
|
39 IMPORTDEPRECATED("import java.lang.Deprecated;\n"), |
|
40 IMPORTDOCUMENTED("import java.lang.annotation.Documented;\n"), |
|
41 IMPORTINHERITED("import java.lang.annotation.Inherited;\n"), |
|
42 IMPORTRETENTION("import java.lang.annotation.Retention;\n" + |
|
43 "\nimport java.lang.annotation.RetentionPolicy;\n"), |
|
44 CONTAINEDBY("\n@ContainedBy(FooContainer.class)\n"), |
|
45 CONTAINERFOR("@ContainerFor(Foo.class)\n"), |
|
46 CONTAINER("@interface FooContainer {\n" +" Foo[] value();\n}\n"), |
|
47 BASE("@interface Foo {}\n"), |
|
48 REPEATABLEANNO("\n@Foo() @Foo()"), |
|
49 DEPRECATED("\n@Deprecated"), |
|
50 DOCUMENTED("\n@Documented"), |
|
51 INHERITED("\n@Inherited"), |
|
52 RETENTION("@Retention(RetentionPolicy.#VAL)\n"); |
|
53 |
|
54 private String val; |
|
55 |
|
56 |
|
57 private ContentVars(String val) { |
|
58 this.val = val; |
|
59 } |
|
60 |
|
61 public String getVal() { |
|
62 return val; |
|
63 } |
|
64 } |
|
65 |
|
66 /* String template where /*<TYPE>*/ /*gets replaced by repeating anno |
|
67 * Used to generate test src for combo tests |
|
68 * - BasicSyntaxCombo.java |
|
69 * - TargetAnnoCombo.java |
|
70 */ |
|
71 public static final String template = |
|
72 "/*PACKAGE*/\n" + |
|
73 "//pkg test;\n\n" + |
|
74 "/*TYPE*/ //class\n" + |
|
75 "class #ClassName {\n" + |
|
76 " /*FIELD*/ //instance var\n" + |
|
77 " public int x = 0;\n\n" + |
|
78 " /*FIELD*/ //Enum constants\n" + |
|
79 " TestEnum testEnum;\n\n" + |
|
80 " /*FIELD*/ // Static field\n" + |
|
81 " public static int num;\n\n" + |
|
82 " /*STATIC_INI*/\n" + |
|
83 " static { \n" + "num = 10; \n }\n\n" + |
|
84 " /*CONSTRUCTOR*/\n" + |
|
85 " #ClassName() {}\n\n" + |
|
86 " /*INSTANCE_INI*/\n" + |
|
87 " { \n x = 10; \n }" + |
|
88 " /*INNER_CLASS*/\n" + |
|
89 " class innerClass {}\n" + |
|
90 " /*METHOD*/\n" + |
|
91 " void bar(/*PARAMETER*/ int baz) {\n" + |
|
92 " /*LOCAL_VARIABLE*/\n" + |
|
93 " int y = 0;\n" + |
|
94 " }\n" + |
|
95 "}\n\n" + |
|
96 "/*TYPE*/ //Enum\n" + |
|
97 "enum TestEnum {}\n\n" + |
|
98 "/*TYPE*/ //Interface\n" + |
|
99 "interface TestInterface {}\n\n" + |
|
100 "/*TYPE*/\n" + |
|
101 "/*ANNOTATION_TYPE*/\n" + |
|
102 "@interface TestAnnotationType{}\n"; |
|
103 |
|
104 // Create and compile FileObject using values for className and contents |
|
105 public static boolean compileCode(String className, String contents, |
|
106 DiagnosticCollector<JavaFileObject> diagnostics) { |
|
107 boolean ok = false; |
|
108 JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); |
|
109 if (compiler == null) { |
|
110 throw new RuntimeException("can't get javax.tools.JavaCompiler!"); |
|
111 } |
|
112 |
|
113 JavaFileObject file = getFile(className, contents); |
|
114 Iterable<? extends JavaFileObject> compilationUnit = Arrays.asList(file); |
|
115 |
|
116 CompilationTask task = compiler.getTask(null, null, diagnostics, null, null, compilationUnit); |
|
117 ok = task.call(); |
|
118 return ok; |
|
119 |
|
120 } |
|
121 |
|
122 // Compile a list of FileObjects |
|
123 public static boolean compileCode(DiagnosticCollector<JavaFileObject> diagnostics, Iterable<? extends JavaFileObject> files) { |
|
124 JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); |
|
125 if (compiler == null) { |
|
126 throw new RuntimeException("can't get javax.tools.JavaCompiler!"); |
|
127 } |
|
128 |
|
129 CompilationTask task = compiler.getTask(null, null, diagnostics, null, null, files); |
|
130 boolean ok = task.call(); |
|
131 return ok; |
|
132 } |
|
133 |
|
134 static JavaFileObject getFile(String name, String code) { |
|
135 JavaFileObject o = null; |
|
136 try { |
|
137 o = new JavaStringFileObject(name, code); |
|
138 } catch (URISyntaxException e) { |
|
139 throw new RuntimeException(e); |
|
140 } |
|
141 return o; |
|
142 } |
|
143 static class JavaStringFileObject extends SimpleJavaFileObject { |
|
144 final String theCode; |
|
145 public JavaStringFileObject(String fileName, String theCode) throws URISyntaxException { |
|
146 super(new URI("string:///" + fileName.replace('.','/') + ".java"), Kind.SOURCE); |
|
147 this.theCode = theCode; |
|
148 } |
|
149 @Override |
|
150 public CharSequence getCharContent(boolean ignoreEncodingErrors) { |
|
151 return theCode; |
|
152 } |
|
153 } |
|
154 } |