test/tools/javac/annotations/typeAnnotations/classfile/CombinationsTargetTest1.java

Fri, 03 May 2013 09:56:56 -0700

author
jjg
date
Fri, 03 May 2013 09:56:56 -0700
changeset 1721
abd153854f16
parent 1534
bec996065c45
child 1755
ddb4a2bfcd82
permissions
-rw-r--r--

8012728: Normalize @ignore comments on langtools tests
Reviewed-by: vromero, mcimadamore

     1 /*
     2  * Copyright (c) 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 /*
    25  * @test
    26  * @bug 8005085 8005877 8004829 8005681 8006734 8006775
    27  * @ignore 8013409: test failures for type annotations
    28  * @summary Combinations of Target ElementTypes on (repeated)type annotations.
    29  */
    31 import com.sun.tools.classfile.*;
    32 import java.io.File;
    34 public class CombinationsTargetTest1 extends ClassfileTestHelper {
    35     // Helps identify test case in event of failure.
    36     int testcount = 0;
    37     int src1 = 1, src2 = 2, src4 = 4,
    38         src5 = 5, src6 = 6, src7 = 7;
    40     String[] ETypes={"TYPE", "FIELD", "METHOD", "PARAMETER", "CONSTRUCTOR",
    41                      "LOCAL_VARIABLE", "ANNOTATION_TYPE", "PACKAGE"};
    43     // local class tests will have an inner class.
    44     Boolean hasInnerClass=false;
    45     String innerClassname="";
    47     public static void main(String[] args) throws Exception {
    48         new CombinationsTargetTest1().run();
    49     }
    51     void run() throws Exception {
    52         // Determines which repeat and order in source(ABMix).
    53         Boolean As= false, BDs=true, ABMix=false;
    54         int testrun=0;
    55         // A repeats and/or B/D repeats, ABMix for order of As and Bs.
    56         Boolean [][] bRepeat = new Boolean[][]{{false,false,false},//no repeats
    57                                                {true,false,false}, //repeat @A
    58                                                {false,true,false}, //repeat @B
    59                                                {true,true,false},  //repeat both
    60                                                {false,false,true}  //repeat mix
    61         };
    62         for(Boolean[] bCombo : bRepeat) {
    63             As=bCombo[0]; BDs=bCombo[1]; ABMix=bCombo[2];
    64             for(String et : ETypes) {
    65                switch(et) {
    66                    case "METHOD":
    67                        test( 8,  0, 2, 0, As, BDs, ABMix, "CLASS",   et, ++testrun, src1);
    68                        test(10,  0, 2, 0, As, BDs, ABMix, "CLASS",   et, ++testrun, src2);
    69                        test( 8,  0, 0, 0, As, BDs, ABMix, "CLASS",   et, ++testrun, src4);
    70                        test(10,  0, 2, 0, As, BDs, ABMix, "CLASS",   et, ++testrun, src6);
    71                        test( 0,  8, 0, 2, As, BDs, ABMix, "RUNTIME", et, ++testrun, src1);
    72                        test( 0, 10, 0, 2, As, BDs, ABMix, "RUNTIME", et, ++testrun, src2);
    73                        test( 0,  8, 0, 0, As, BDs, ABMix, "RUNTIME", et, ++testrun, src4);
    74                        test( 0, 10, 0, 2, As, BDs, ABMix, "RUNTIME", et, ++testrun, src6);
    75                        break;
    76                    case "CONSTRUCTOR":
    77                    case "FIELD":
    78                        test( 8,  0, 4, 0, As, BDs, ABMix, "CLASS",   et, ++testrun, src1);
    79                        test( 6,  0, 3, 0, As, BDs, ABMix, "CLASS",   et, ++testrun, src5);
    80                        test( 9,  0, 0, 0, As, BDs, ABMix, "CLASS",   et, ++testrun, src7);
    81                        test( 0,  8, 0, 4, As, BDs, ABMix, "RUNTIME", et, ++testrun, src1);
    82                        test( 0,  6, 0, 3, As, BDs, ABMix, "RUNTIME", et, ++testrun, src5);
    83                        test( 0,  9, 0, 0, As, BDs, ABMix, "RUNTIME", et, ++testrun, src7);
    84                        break;
    85                    default:/*TYPE,PARAMETER,LOCAL_VARIABLE,ANNOTATION_TYPE,PACKAGE*/
    86                        test( 8,  0, 2, 0, As, BDs, ABMix, "CLASS",   et, ++testrun, src1);
    87                        test( 6,  0, 3, 0, As, BDs, ABMix, "CLASS",   et, ++testrun, src5);
    88                        test( 0,  8, 0, 2, As, BDs, ABMix, "RUNTIME", et, ++testrun, src1);
    89                        test( 0,  6, 0, 3, As, BDs, ABMix, "RUNTIME", et, ++testrun, src5);
    90                }
    91             }
    92         }
    93     }
    95     public void test(int tinv, int tvis, int inv, int vis, Boolean Arepeats,
    96                      Boolean BDrepeats, Boolean ABmix, String rtn, String et2,
    97                      Integer N, int source) throws Exception {
    98         ++testcount;
    99         expected_tvisibles = tvis;
   100         expected_tinvisibles = tinv;
   101         expected_visibles = vis;
   102         expected_invisibles = inv;
   103         File testFile = null;
   104         String tname="Test" + N.toString();
   105         hasInnerClass=false;
   106         String testDef = "Test " + testcount + " parameters: tinv=" + tinv +
   107                 ", tvis=" + tvis + ", inv=" + inv + ", vis=" + vis +
   108                 ", Arepeats=" + Arepeats + ", BDrepeats=" + BDrepeats +
   109                 ", ABmix=" + ABmix + ", retention: " + rtn + ", anno2: " +
   110                 et2 + ", src=" + source;
   112         println(testDef);
   113         // Create test source and File.
   114         String sourceString = sourceString(tname, rtn, et2, Arepeats,
   115                                            BDrepeats, ABmix, source);
   116         testFile = writeTestFile(tname+".java", sourceString);
   117         // Compile test source and read classfile.
   118         File classFile = null;
   119         try {
   120             classFile = compile(testFile);
   121         } catch (Error err) {
   122             System.err.println("Failed compile. Source:\n" + sourceString);
   123             throw err;
   124         }
   125         //if sourcString() set hasInnerClass it also set innerClassname.
   126         if(hasInnerClass) {
   127             StringBuffer sb = new StringBuffer(classFile.getAbsolutePath());
   128             classFile=new File(sb.insert(sb.lastIndexOf(".class"),innerClassname).toString());
   129         }
   130         ClassFile cf = ClassFile.read(classFile);
   132         //Test class,fields and method counts.
   133         test(cf);
   135         for (Field f : cf.fields) {
   136             test(cf, f);
   137         }
   138         for (Method m: cf.methods) {
   139             test(cf, m);
   140         }
   141         countAnnotations();
   142         if (errors > 0) {
   143             System.err.println( testDef );
   144             System.err.println( "Source:\n" + sourceString );
   145             throw new Exception( errors + " errors found" );
   146         }
   147         println("Pass");
   148     }
   150     //
   151     // Source for test cases
   152     //
   153     String sourceString(String testname, String retentn, String annot2,
   154                         Boolean Arepeats, Boolean BDrepeats, Boolean ABmix,
   155                         int src) {
   157         String As = "@A", Bs = "@B", Ds = "@D";
   158         if(Arepeats) As = "@A @A";
   159         if(BDrepeats) {
   160             Bs = "@B @B";
   161             Ds = "@D @D";
   162         }
   163         if(ABmix) { As = "@A @B"; Bs = "@A @B"; Ds = "@D @D"; }
   165         // Source to check for TYPE_USE and TYPE_PARAMETER annotations.
   166         // Source base (annotations) is same for all test cases.
   167         String source = new String();
   168         String imports = new String("import java.lang.annotation.*; \n" +
   169             "import static java.lang.annotation.RetentionPolicy.*; \n" +
   170             "import static java.lang.annotation.ElementType.*; \n" +
   171             "import java.util.List; \n" +
   172             "import java.util.HashMap; \n" +
   173             "import java.util.Map; \n\n");
   175             String sourceBase = new String("@Retention("+retentn+")\n" +
   176             "@Target({TYPE_USE,_OTHER_})\n" +
   177             "@Repeatable( AC.class )\n" +
   178             "@interface A { }\n\n" +
   180             "@Retention("+retentn+")\n" +
   181             "@Target({TYPE_USE,_OTHER_})\n" +
   182             "@interface AC { A[] value(); }\n\n" +
   184             "@Retention("+retentn+")\n" +
   185             "@Target({TYPE_USE,_OTHER_})\n" +
   186             "@Repeatable( BC.class )\n" +
   187             "@interface B { }\n\n" +
   189             "@Retention("+retentn+")\n" +
   190             "@Target({TYPE_USE,_OTHER_})\n" +
   191             "@interface BC { B[] value(); } \n\n" +
   193             "@Retention("+retentn+")\n" +
   194             "@Target({TYPE_PARAMETER,_OTHER_})\n" +
   195             "@interface C { }\n\n" +
   197             "@Retention("+retentn+")\n" +
   198             "@Target({TYPE_USE,TYPE_PARAMETER,_OTHER_})\n" +
   199             "@Repeatable(DC.class)\n" +
   200             "@interface D { }\n\n" +
   202             "@Retention("+retentn+")\n" +
   203             "@Target({TYPE_USE,TYPE_PARAMETER,_OTHER_})\n" +
   204             "@interface DC { D[] value(); }\n\n");
   206         // Test case sources with sample generated source.
   207         switch(src) {
   208             case 1: // repeating type annotations at class level
   209                     /*
   210                      * @A @B class Test1 {
   211                      * @A @B Test1(){}
   212                      * @A @B Integer i1 = 0;
   213                      * String @A @B [] @A @B [] sa = null;
   214                      * // type usage in method body
   215                      * String test(Test1 this, String param, String ... vararg) {
   216                      *     Object o = new  String  [3];
   217                      *     return (String) null;
   218                      * }}
   219                      */
   220                 source = new String(
   221                 "// (repeating) type annotations at class level. \n" +
   222                 "_As_ _Bs_ class " + testname + " {\n" +
   223                 "_As_ _Bs_ " + testname +"(){} \n" +
   224                 "_As_ _Bs_ Integer i1 = 0; \n" +
   225                 "String _As_ _Bs_ [] _As_ _Bs_ [] sa = null; \n" +
   226                 "// type usage in method body \n" +
   227                 "String test("+testname+" this, " +
   228                    "String param, String ... vararg) { \n" +
   229                 "    Object o = new  String  [3]; \n" +
   230                 "    return (String) null; \n" +
   231                 "} \n" +
   232                 "} \n").concat(sourceBase).replace("_OTHER_", annot2).replace("_As_",As).replace("_Bs_",Bs) +
   233                 "\n\n";
   234                 break;
   235             case 2: // (repeating) type annotations on method.
   236                     /*
   237                      * class Test12 {
   238                      * Test12(){}
   239                      * // type usage on method
   240                      * @A @B String test(@A @B  Test12 this, @A @B  String param, @A @B  String @A @B  ... vararg) {
   241                      *     Object o = new String [3];
   242                      *     return (String) null;
   243                      * }}
   244                      */
   245                 source = new String(
   246                 "// (repeating) type annotations on method. \n" +
   247                 "class " + testname + " {\n" +
   248                 testname +"(){} \n" +
   249                 "// type usage on method \n" +
   250                 "_As_ _Bs_ String test(_As_ _Bs_  "+testname+" this, " +
   251                    "_As_ _Bs_  String param, _As_ _Bs_  String _As_ _Bs_  ... vararg) { \n" +
   252                 "    Object o = new String [3]; \n" +
   253                 "    return (String) null; \n" +
   254                 "} \n" +
   255                 "} \n").concat(sourceBase).replace("_OTHER_", annot2).replace("_As_",As).replace("_Bs_",Bs) +
   256                 "\n\n";
   257                 break;
   258             case 4: //(repeating) annotations on wildcard, type arguments in anonymous class.
   259                     /*
   260                      * class Test13<T extends Object> {
   261                      *     public T data = null;
   262                      *     T getData() { return data;}
   263                      *     String mtest( Test13<String> t){ return t.getData(); }
   264                      *     public void test() {
   265                      *         mtest( new Test13<@A @B String>() {
   266                      *                  void m1(List<@A @B ? extends @A @B  Object> lst) {}
   267                      *                  void m2() throws@A @B Exception { }
   268                      *                });
   269                      *     }
   270                      * }
   271                      */
   272                 source = new String( source +
   273                 "// (repeating) annotations on wildcard, type arguments in anonymous class. \n" +
   274                 "class " + testname + "<T extends Object> {\n" +
   275                 "    public T data = null;\n" +
   276                 "    T getData() { return data;}\n" +
   277                 "    String mtest( " + testname + "<String> t){ return t.getData(); }\n" +
   278                 "    public void test() {\n" +
   279                 "        mtest( new " + testname + "<_As_ _Bs_ String>() {\n" +
   280                 "                 void m1(List<_As_ _Bs_ ? extends _As_ _Bs_  Object> lst) {}\n" +
   281                 "                 void m2() throws_As_ _Bs_ Exception { }\n" +
   282                 "               });\n" +
   283                 "    }\n" +
   284                 "}\n").concat(sourceBase).replace("_OTHER_", annot2).replace("_As_",As).replace("_Bs_",Bs) + "\n\n";
   285                 hasInnerClass=true;
   286                 innerClassname="$1";
   287             break;
   288             case 5: // (repeating)annotations on type parameters, bounds and  type arguments on class decl.
   289                     /*
   290                      * @A @B @D
   291                      * class Test2<@A @B @C @D T extends @A @B Object> {
   292                      *     Map<List<String>, Integer> map =
   293                      *         new HashMap<List< String>, Integer>();
   294                      *     Map<List<String>,Integer> map2 = new HashMap<>();
   295                      *     String test(Test2<T> this) { return null;}
   296                      *     <T> String genericMethod(T t) { return null; }
   297                      * }
   298                      */
   299                 source = new String( source +
   300                 "// (repeating)annotations on type parameters, bounds and  type arguments on class decl. \n" +
   301                 "_As_ _Bs_ _Ds_\n" +  //8004829: A and B on type parameter below.
   302                 "class " + testname + "<_As_ _Bs_ @C _Ds_ T extends _As_ _Bs_ Object> {\n" +
   303                 "    Map<List<String>, Integer> map =\n" +
   304                 "        new HashMap<List< String>, Integer>();\n" +
   305                 "    Map<List<String>,Integer> map2 = new HashMap<>();\n" +
   306                 "    String test(" + testname + "<T> this) { return null;}\n" +
   307                 "    <T> String genericMethod(T t) { return null; }\n" +
   308                 "}\n").concat(sourceBase).replace("_OTHER_", annot2).replace("_As_",As).replace("_Bs_",Bs).replace("_Ds_",Ds) +
   309                 "\n\n";
   310             break;
   311             case 6: // (repeating) annotations on type parameters, bounds and  type arguments on method.
   312                     /*
   313                      * class Test14<T extends Object> {
   314                      *     Map<List<String>, Integer> map =
   315                      *         new HashMap<List<String>, Integer>();
   316                      *     Map<List<String>, Integer> map2 = new HashMap<>();
   317                      *     String test(@A @B Test14<@D T> this) { return null;}
   318                      *     <@C @D T> @A @B String genericMethod(@A @B @D T t) { return null; }
   319                      * }
   320                      */
   321                 source = new String( source +
   322                 "// (repeating) annotations on type parameters, bounds and  type arguments on method. \n" +
   323                 "class " + testname + "<T extends Object> {\n" +
   324                 "    Map<List<String>, Integer> map =\n" +
   325                 "        new HashMap<List<String>, Integer>();\n" +
   326                 "    Map<List<String>, Integer> map2 = new HashMap<>();\n" +
   327                 "    String test(_As_ _Bs_ " + testname + "<_Ds_ T> this) { return null;}\n" +
   328                 "    <@C _Ds_ T> _As_ _Bs_ String genericMethod(_As_ _Bs_ _Ds_ T t) { return null; }\n" +
   329                 "}\n").concat(sourceBase).replace("_OTHER_", annot2).replace("_As_",As).replace("_Bs_",Bs).replace("_Ds_",Ds) +
   330                 "\n\n";
   331             break;
   332             case 7: // repeating annotations on type parameters, bounds and  type arguments in method.
   333                     /*
   334                      * class Test7{
   335                      *     <E extends Comparable> Map<List<E>, E > foo(E e) {
   336                      *         class maptest <@A @B @D E> {
   337                      *             Map<List<@A @B @D E>,@A @B @D E> getMap() {
   338                      *                 return new HashMap<List<E>,E>();
   339                      *             }
   340                      *         }
   341                      *         return new maptest<E>().getMap();
   342                      *    }
   343                      *    Map<List<String>,String> shm = foo(new String("hello"));
   344                      * }
   345                      */
   346                 source = new String( source +
   347                 "// (repeating)annotations on type parameters of class, method return value in method. \n" +
   348                 "class "+ testname + "{\n" +
   349                 "    <E extends Comparable> Map<List<E>, E > foo(E e) {\n" +
   350                 "        class maptest <_As_ _Bs_ _Ds_ E> {\n" +                  // inner class $1maptest
   351                 "            Map<List<_As_ _Bs_ _Ds_ E>,_As_ _Bs_ _Ds_ E> getMap() { \n" +
   352                 "                return new HashMap<List<E>,E>();\n" +
   353                 "            }\n" +
   354                 "        }\n" +
   355                 "        return new maptest<E>().getMap();\n" +
   356                 "   }\n" +
   357                 "   Map<List<String>,String> shm = foo(new String(\"hello\"));\n" +
   358                 "}\n").concat(sourceBase).replace("_OTHER_", annot2).replace("_As_",As).replace("_Bs_",Bs).replace("_Ds_",Ds) +
   359                 "\n\n";
   360                 hasInnerClass=true;
   361                 innerClassname="$1maptest";
   362             break;
   363         }
   364         return imports + source;
   365     }
   366 }

mercurial