1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/tools/javac/annotations/repeatingAnnotations/combo/Helper.java Wed Nov 07 17:01:19 2012 -0800 1.3 @@ -0,0 +1,154 @@ 1.4 +/* 1.5 + * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + */ 1.26 + 1.27 +import java.net.URI; 1.28 +import java.net.URISyntaxException; 1.29 +import java.util.Arrays; 1.30 +import javax.tools.DiagnosticCollector; 1.31 +import javax.tools.JavaCompiler; 1.32 +import javax.tools.JavaFileObject; 1.33 +import javax.tools.SimpleJavaFileObject; 1.34 +import javax.tools.ToolProvider; 1.35 +import javax.tools.JavaCompiler.CompilationTask; 1.36 + 1.37 +public class Helper { 1.38 + 1.39 + enum ContentVars { 1.40 + IMPORTCONTAINERSTMTS("\nimport java.lang.annotation.ContainedBy;\n" + 1.41 + "\nimport java.lang.annotation.ContainerFor;\n"), 1.42 + IMPORTDEPRECATED("import java.lang.Deprecated;\n"), 1.43 + IMPORTDOCUMENTED("import java.lang.annotation.Documented;\n"), 1.44 + IMPORTINHERITED("import java.lang.annotation.Inherited;\n"), 1.45 + IMPORTRETENTION("import java.lang.annotation.Retention;\n" + 1.46 + "\nimport java.lang.annotation.RetentionPolicy;\n"), 1.47 + CONTAINEDBY("\n@ContainedBy(FooContainer.class)\n"), 1.48 + CONTAINERFOR("@ContainerFor(Foo.class)\n"), 1.49 + CONTAINER("@interface FooContainer {\n" +" Foo[] value();\n}\n"), 1.50 + BASE("@interface Foo {}\n"), 1.51 + REPEATABLEANNO("\n@Foo() @Foo()"), 1.52 + DEPRECATED("\n@Deprecated"), 1.53 + DOCUMENTED("\n@Documented"), 1.54 + INHERITED("\n@Inherited"), 1.55 + RETENTION("@Retention(RetentionPolicy.#VAL)\n"); 1.56 + 1.57 + private String val; 1.58 + 1.59 + 1.60 + private ContentVars(String val) { 1.61 + this.val = val; 1.62 + } 1.63 + 1.64 + public String getVal() { 1.65 + return val; 1.66 + } 1.67 + } 1.68 + 1.69 + /* String template where /*<TYPE>*/ /*gets replaced by repeating anno 1.70 + * Used to generate test src for combo tests 1.71 + * - BasicSyntaxCombo.java 1.72 + * - TargetAnnoCombo.java 1.73 + */ 1.74 + public static final String template = 1.75 + "/*PACKAGE*/\n" + 1.76 + "//pkg test;\n\n" + 1.77 + "/*TYPE*/ //class\n" + 1.78 + "class #ClassName {\n" + 1.79 + " /*FIELD*/ //instance var\n" + 1.80 + " public int x = 0;\n\n" + 1.81 + " /*FIELD*/ //Enum constants\n" + 1.82 + " TestEnum testEnum;\n\n" + 1.83 + " /*FIELD*/ // Static field\n" + 1.84 + " public static int num;\n\n" + 1.85 + " /*STATIC_INI*/\n" + 1.86 + " static { \n" + "num = 10; \n }\n\n" + 1.87 + " /*CONSTRUCTOR*/\n" + 1.88 + " #ClassName() {}\n\n" + 1.89 + " /*INSTANCE_INI*/\n" + 1.90 + " { \n x = 10; \n }" + 1.91 + " /*INNER_CLASS*/\n" + 1.92 + " class innerClass {}\n" + 1.93 + " /*METHOD*/\n" + 1.94 + " void bar(/*PARAMETER*/ int baz) {\n" + 1.95 + " /*LOCAL_VARIABLE*/\n" + 1.96 + " int y = 0;\n" + 1.97 + " }\n" + 1.98 + "}\n\n" + 1.99 + "/*TYPE*/ //Enum\n" + 1.100 + "enum TestEnum {}\n\n" + 1.101 + "/*TYPE*/ //Interface\n" + 1.102 + "interface TestInterface {}\n\n" + 1.103 + "/*TYPE*/\n" + 1.104 + "/*ANNOTATION_TYPE*/\n" + 1.105 + "@interface TestAnnotationType{}\n"; 1.106 + 1.107 + // Create and compile FileObject using values for className and contents 1.108 + public static boolean compileCode(String className, String contents, 1.109 + DiagnosticCollector<JavaFileObject> diagnostics) { 1.110 + boolean ok = false; 1.111 + JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); 1.112 + if (compiler == null) { 1.113 + throw new RuntimeException("can't get javax.tools.JavaCompiler!"); 1.114 + } 1.115 + 1.116 + JavaFileObject file = getFile(className, contents); 1.117 + Iterable<? extends JavaFileObject> compilationUnit = Arrays.asList(file); 1.118 + 1.119 + CompilationTask task = compiler.getTask(null, null, diagnostics, null, null, compilationUnit); 1.120 + ok = task.call(); 1.121 + return ok; 1.122 + 1.123 + } 1.124 + 1.125 + // Compile a list of FileObjects 1.126 + public static boolean compileCode(DiagnosticCollector<JavaFileObject> diagnostics, Iterable<? extends JavaFileObject> files) { 1.127 + JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); 1.128 + if (compiler == null) { 1.129 + throw new RuntimeException("can't get javax.tools.JavaCompiler!"); 1.130 + } 1.131 + 1.132 + CompilationTask task = compiler.getTask(null, null, diagnostics, null, null, files); 1.133 + boolean ok = task.call(); 1.134 + return ok; 1.135 + } 1.136 + 1.137 + static JavaFileObject getFile(String name, String code) { 1.138 + JavaFileObject o = null; 1.139 + try { 1.140 + o = new JavaStringFileObject(name, code); 1.141 + } catch (URISyntaxException e) { 1.142 + throw new RuntimeException(e); 1.143 + } 1.144 + return o; 1.145 + } 1.146 + static class JavaStringFileObject extends SimpleJavaFileObject { 1.147 + final String theCode; 1.148 + public JavaStringFileObject(String fileName, String theCode) throws URISyntaxException { 1.149 + super(new URI("string:///" + fileName.replace('.','/') + ".java"), Kind.SOURCE); 1.150 + this.theCode = theCode; 1.151 + } 1.152 + @Override 1.153 + public CharSequence getCharContent(boolean ignoreEncodingErrors) { 1.154 + return theCode; 1.155 + } 1.156 + } 1.157 +}