test/tools/javac/annotations/repeatingAnnotations/combo/Helper.java

Mon, 14 Jan 2013 13:50:01 -0800

author
jjg
date
Mon, 14 Jan 2013 13:50:01 -0800
changeset 1492
df694c775e8a
parent 1401
2986e7052952
child 1554
5125b9854d07
permissions
-rw-r--r--

8006119: update javac to follow latest spec for repeatable annotations
Reviewed-by: darcy

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

mercurial