Mon, 18 Feb 2013 14:29:40 -0800
8008339: Test TargetAnnoCombo.java is broken
Reviewed-by: jjh
jjg@1401 | 1 | /* |
jjg@1492 | 2 | * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. |
jjg@1401 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
jjg@1401 | 4 | * |
jjg@1401 | 5 | * This code is free software; you can redistribute it and/or modify it |
jjg@1401 | 6 | * under the terms of the GNU General Public License version 2 only, as |
jjg@1401 | 7 | * published by the Free Software Foundation. |
jjg@1401 | 8 | * |
jjg@1401 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
jjg@1401 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
jjg@1401 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
jjg@1401 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
jjg@1401 | 13 | * accompanied this code). |
jjg@1401 | 14 | * |
jjg@1401 | 15 | * You should have received a copy of the GNU General Public License version |
jjg@1401 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
jjg@1401 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
jjg@1401 | 18 | * |
jjg@1401 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
jjg@1401 | 20 | * or visit www.oracle.com if you need additional information or have any |
jjg@1401 | 21 | * questions. |
jjg@1401 | 22 | */ |
jjg@1401 | 23 | |
darcy@1576 | 24 | import java.io.File; |
darcy@1576 | 25 | import java.io.IOException; |
jjg@1401 | 26 | import java.net.URI; |
jjg@1401 | 27 | import java.net.URISyntaxException; |
jjg@1401 | 28 | import java.util.Arrays; |
darcy@1576 | 29 | import java.util.Iterator; |
darcy@1576 | 30 | |
darcy@1576 | 31 | import javax.tools.Diagnostic; |
jjg@1401 | 32 | import javax.tools.DiagnosticCollector; |
jjg@1401 | 33 | import javax.tools.JavaCompiler; |
darcy@1576 | 34 | import javax.tools.JavaCompiler.CompilationTask; |
jjg@1401 | 35 | import javax.tools.JavaFileObject; |
jjg@1401 | 36 | import javax.tools.SimpleJavaFileObject; |
darcy@1576 | 37 | import javax.tools.StandardJavaFileManager; |
darcy@1576 | 38 | import javax.tools.StandardLocation; |
jjg@1401 | 39 | import javax.tools.ToolProvider; |
darcy@1576 | 40 | |
darcy@1576 | 41 | import com.sun.source.util.JavacTask; |
jjg@1401 | 42 | |
jjg@1401 | 43 | public class Helper { |
jjg@1401 | 44 | |
jjg@1401 | 45 | enum ContentVars { |
darcy@1576 | 46 | |
jjg@1492 | 47 | IMPORTCONTAINERSTMTS("\nimport java.lang.annotation.Repeatable;\n"), |
jjg@1401 | 48 | IMPORTDEPRECATED("import java.lang.Deprecated;\n"), |
jjg@1401 | 49 | IMPORTDOCUMENTED("import java.lang.annotation.Documented;\n"), |
jjg@1401 | 50 | IMPORTINHERITED("import java.lang.annotation.Inherited;\n"), |
darcy@1576 | 51 | IMPORTRETENTION("import java.lang.annotation.Retention;\n" |
darcy@1576 | 52 | + "\nimport java.lang.annotation.RetentionPolicy;\n"), |
darcy@1554 | 53 | IMPORTSTMTS("import java.lang.annotation.*;\n"), |
darcy@1576 | 54 | IMPORTEXPECTED("import expectedFiles.*;\n"), |
jjg@1492 | 55 | REPEATABLE("\n@Repeatable(FooContainer.class)\n"), |
darcy@1576 | 56 | CONTAINER("@interface FooContainer {\n" + " Foo[] value();\n}\n"), |
jjg@1401 | 57 | BASE("@interface Foo {}\n"), |
darcy@1554 | 58 | BASEANNO("@Foo"), |
darcy@1576 | 59 | LEGACYCONTAINER("@FooContainer(value = {@Foo, @Foo})\n"), |
jjg@1401 | 60 | REPEATABLEANNO("\n@Foo() @Foo()"), |
jjg@1401 | 61 | DEPRECATED("\n@Deprecated"), |
jjg@1401 | 62 | DOCUMENTED("\n@Documented"), |
jjg@1401 | 63 | INHERITED("\n@Inherited"), |
darcy@1576 | 64 | TARGET("\n@Target(#VAL)\n"), |
darcy@1554 | 65 | RETENTION("@Retention(RetentionPolicy.#VAL)\n"), |
darcy@1576 | 66 | RETENTIONRUNTIME("@Retention(RetentionPolicy.RUNTIME)\n"); |
jjg@1401 | 67 | private String val; |
jjg@1401 | 68 | |
jjg@1401 | 69 | private ContentVars(String val) { |
jjg@1401 | 70 | this.val = val; |
jjg@1401 | 71 | } |
jjg@1401 | 72 | |
jjg@1401 | 73 | public String getVal() { |
jjg@1401 | 74 | return val; |
jjg@1401 | 75 | } |
jjg@1401 | 76 | } |
jjg@1401 | 77 | |
jjg@1401 | 78 | // Create and compile FileObject using values for className and contents |
jjg@1401 | 79 | public static boolean compileCode(String className, String contents, |
jjg@1401 | 80 | DiagnosticCollector<JavaFileObject> diagnostics) { |
jjg@1401 | 81 | boolean ok = false; |
jjg@1401 | 82 | JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); |
jjg@1401 | 83 | if (compiler == null) { |
jjg@1401 | 84 | throw new RuntimeException("can't get javax.tools.JavaCompiler!"); |
jjg@1401 | 85 | } |
jjg@1401 | 86 | |
jjg@1401 | 87 | JavaFileObject file = getFile(className, contents); |
jjg@1401 | 88 | Iterable<? extends JavaFileObject> compilationUnit = Arrays.asList(file); |
jjg@1401 | 89 | |
jjg@1401 | 90 | CompilationTask task = compiler.getTask(null, null, diagnostics, null, null, compilationUnit); |
jjg@1401 | 91 | ok = task.call(); |
jjg@1401 | 92 | return ok; |
darcy@1576 | 93 | } |
darcy@1576 | 94 | // Compile a list of FileObjects |
darcy@1576 | 95 | // Used when packages are needed and classes need to be loaded at runtime |
darcy@1576 | 96 | static File destDir = new File(System.getProperty("user.dir")); |
jjg@1401 | 97 | |
jjg@1401 | 98 | public static boolean compileCode(DiagnosticCollector<JavaFileObject> diagnostics, Iterable<? extends JavaFileObject> files) { |
darcy@1576 | 99 | boolean ok = false; |
jjg@1401 | 100 | JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); |
jjg@1401 | 101 | if (compiler == null) { |
jjg@1401 | 102 | throw new RuntimeException("can't get javax.tools.JavaCompiler!"); |
jjg@1401 | 103 | } |
jjg@1401 | 104 | |
darcy@1576 | 105 | StandardJavaFileManager fm = compiler.getStandardFileManager(null, null, null); |
darcy@1576 | 106 | |
darcy@1576 | 107 | // Assuming filesCount can maximum be 2 and if true, one file is package-info.java |
darcy@1576 | 108 | if (isPkgInfoPresent(files)) { |
darcy@1576 | 109 | JavacTask task = (JavacTask) compiler.getTask(null, fm, diagnostics, null, null, files); |
darcy@1576 | 110 | try { |
darcy@1576 | 111 | fm.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(destDir)); |
darcy@1576 | 112 | task.generate(); |
darcy@1576 | 113 | } catch (IOException ioe) { |
darcy@1576 | 114 | throw new RuntimeException("Compilation failed for package level tests", ioe); |
darcy@1576 | 115 | } |
darcy@1576 | 116 | int err = 0; |
darcy@1576 | 117 | for (Diagnostic<? extends JavaFileObject> d : diagnostics.getDiagnostics()) { |
darcy@1576 | 118 | if(d.getKind() == Diagnostic.Kind.ERROR) { |
darcy@1576 | 119 | err++; |
darcy@1576 | 120 | } |
darcy@1576 | 121 | } |
darcy@1576 | 122 | ok = (err == 0); |
darcy@1576 | 123 | } else { |
darcy@1576 | 124 | CompilationTask task = compiler.getTask(null, null, diagnostics, null, null, files); |
darcy@1576 | 125 | ok = task.call(); |
darcy@1576 | 126 | } |
jjg@1401 | 127 | return ok; |
jjg@1401 | 128 | } |
jjg@1401 | 129 | |
darcy@1576 | 130 | static private boolean isPkgInfoPresent(Iterable<? extends JavaFileObject> files) { |
darcy@1576 | 131 | Iterator<? extends JavaFileObject> itr = files.iterator(); |
darcy@1576 | 132 | while (itr.hasNext()) { |
darcy@1576 | 133 | String name = itr.next().getName(); |
darcy@1576 | 134 | if (name.contains("package-info")) { |
darcy@1576 | 135 | return true; |
darcy@1576 | 136 | } |
darcy@1576 | 137 | } |
darcy@1576 | 138 | return false; |
darcy@1576 | 139 | } |
darcy@1576 | 140 | /* String template where /*<TYPE>*/ /*gets replaced by repeating anno |
darcy@1576 | 141 | * Used to generate test src for combo tests |
darcy@1576 | 142 | * - BasicSyntaxCombo.java |
darcy@1576 | 143 | * - TargetAnnoCombo.java |
darcy@1576 | 144 | */ |
darcy@1576 | 145 | |
darcy@1576 | 146 | public static final String template = |
darcy@1576 | 147 | "/*PACKAGE*/\n" |
darcy@1576 | 148 | + "//pkg test;\n\n" |
darcy@1576 | 149 | + "/*TYPE*/ //class\n" |
darcy@1576 | 150 | + "class #ClassName {\n" |
darcy@1576 | 151 | + " /*FIELD*/ //instance var\n" |
darcy@1576 | 152 | + " public int x = 0;\n\n" |
darcy@1576 | 153 | + " /*FIELD*/ //Enum constants\n" |
darcy@1576 | 154 | + " TestEnum testEnum;\n\n" |
darcy@1576 | 155 | + " /*FIELD*/ // Static field\n" |
darcy@1576 | 156 | + " public static int num;\n\n" |
darcy@1576 | 157 | + " /*STATIC_INI*/\n" |
darcy@1576 | 158 | + " static { \n" + "num = 10; \n }\n\n" |
darcy@1576 | 159 | + " /*CONSTRUCTOR*/\n" |
darcy@1576 | 160 | + " #ClassName() {}\n\n" |
darcy@1576 | 161 | + " /*INSTANCE_INI*/\n" |
darcy@1576 | 162 | + " { \n x = 10; \n }" |
darcy@1576 | 163 | + " /*INNER_CLASS*/\n" |
darcy@1576 | 164 | + " class innerClass {}\n" |
darcy@1576 | 165 | + " /*METHOD*/\n" |
darcy@1576 | 166 | + " void bar(/*PARAMETER*/ int baz) {\n" |
darcy@1576 | 167 | + " /*LOCAL_VARIABLE*/\n" |
darcy@1576 | 168 | + " int y = 0;\n" |
darcy@1576 | 169 | + " }\n" |
darcy@1576 | 170 | + "}\n\n" |
darcy@1576 | 171 | + "/*TYPE*/ //Enum\n" |
darcy@1576 | 172 | + "enum TestEnum {}\n\n" |
darcy@1576 | 173 | + "/*TYPE*/ //Interface\n" |
darcy@1576 | 174 | + "interface TestInterface {}\n\n" |
darcy@1576 | 175 | + "/*TYPE*/\n" |
darcy@1576 | 176 | + "/*ANNOTATION_TYPE*/\n" |
darcy@1576 | 177 | + "@interface TestAnnotationType{}\n" |
darcy@1576 | 178 | + "class TestPkg {}\n" |
darcy@1576 | 179 | + "class TestTypeAnno </*TYPE_PARAMETER*/ T extends Object> {\n" |
darcy@1576 | 180 | + " String /*TYPE_USE*/[] arr;\n" |
darcy@1576 | 181 | + "}"; |
darcy@1576 | 182 | |
jjg@1401 | 183 | static JavaFileObject getFile(String name, String code) { |
jjg@1401 | 184 | JavaFileObject o = null; |
jjg@1401 | 185 | try { |
jjg@1401 | 186 | o = new JavaStringFileObject(name, code); |
jjg@1401 | 187 | } catch (URISyntaxException e) { |
jjg@1401 | 188 | throw new RuntimeException(e); |
jjg@1401 | 189 | } |
jjg@1401 | 190 | return o; |
jjg@1401 | 191 | } |
darcy@1576 | 192 | |
jjg@1401 | 193 | static class JavaStringFileObject extends SimpleJavaFileObject { |
darcy@1576 | 194 | |
jjg@1401 | 195 | final String theCode; |
darcy@1576 | 196 | |
jjg@1401 | 197 | public JavaStringFileObject(String fileName, String theCode) throws URISyntaxException { |
darcy@1576 | 198 | super(new URI("string:///" + fileName.replace('.', '/') + ".java"), Kind.SOURCE); |
jjg@1401 | 199 | this.theCode = theCode; |
jjg@1401 | 200 | } |
darcy@1576 | 201 | |
jjg@1401 | 202 | @Override |
jjg@1401 | 203 | public CharSequence getCharContent(boolean ignoreEncodingErrors) { |
jjg@1401 | 204 | return theCode; |
jjg@1401 | 205 | } |
jjg@1401 | 206 | } |
jjg@1401 | 207 | } |