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

Thu, 07 Feb 2013 20:47:06 -0800

author
darcy
date
Thu, 07 Feb 2013 20:47:06 -0800
changeset 1554
5125b9854d07
parent 1492
df694c775e8a
child 1576
63872da94576
permissions
-rw-r--r--

7195131: Update 2 compiler combo tests for repeating annotations to include package and default use cases
Reviewed-by: darcy
Contributed-by: sonali.goel@oracle.com

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

mercurial