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

Print this page


   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  */
  23 
  24 /*
  25  * @test
  26  * @bug 8005085 8005877 8004829 8005681 8006734 8006775
  27  * @ignore
  28  * @summary Combinations of Target ElementTypes on (repeated)type annotations.
  29  */
  30 
  31 import com.sun.tools.classfile.*;
  32 import java.io.File;
  33 
  34 public class CombinationsTargetTest2 extends ClassfileTestHelper {
  35     // Helps identify test case in event of failure.
  36     int testcount = 0;
  37     int src3 = 3, src8 = 8, src9 = 9;
  38 
  39     String[] ETypes={"TYPE", "FIELD", "METHOD", "PARAMETER", "CONSTRUCTOR",
  40                      "LOCAL_VARIABLE", "ANNOTATION_TYPE", "PACKAGE"};
  41 
  42     // local class tests will have an inner class.
  43     Boolean hasInnerClass=false;
  44     String innerClassname="";
  45 
  46     public static void main(String[] args) throws Exception {
  47         new CombinationsTargetTest2().run();


  83     }
  84 
  85     public void test(int tinv, int tvis, int inv, int vis, Boolean Arepeats,
  86                      Boolean BDrepeats, Boolean ABmix, String rtn, String et2,
  87                      Integer N, int source) throws Exception {
  88         ++testcount;
  89         expected_tvisibles = tvis;
  90         expected_tinvisibles = tinv;
  91         expected_visibles = vis;
  92         expected_invisibles = inv;
  93         File testFile = null;
  94         String tname="Test" + N.toString();
  95         hasInnerClass=false;
  96         String testDef = "Test " + testcount + " parameters: tinv=" + tinv +
  97                 ", tvis=" + tvis + ", inv=" + inv + ", vis=" + vis +
  98                 ", Arepeats=" + Arepeats + ", BDrepeats=" + BDrepeats +
  99                 ", ABmix=" + ABmix + ", retention: " + rtn + ", anno2: " +
 100                 et2 + ", src=" + source;
 101 
 102 // Uncomment this block to run the tests but skip failing scenarios.
 103 //        // 8005681 - skip cases with repeated annotations on new, array, cast.
 104 //        if((source==3 || source==8 || source==9) && (ABmix || (Arepeats && BDrepeats))) {
 105 //            System.out.println(testDef+"\n8005681-skip repeated annotations on new,array,cast");
 106 //            return;
 107 //        }
 108 
 109         println(testDef);
 110         // Create test source and File.
 111         String sourceString = sourceString(tname, rtn, et2, Arepeats,
 112                                            BDrepeats, ABmix, source);
 113         testFile = writeTestFile(tname+".java", sourceString);
 114         // Compile test source and read classfile.
 115         File classFile = null;
 116         try {
 117             classFile = compile(testFile);
 118         } catch (Error err) {
 119             System.err.println("Failed compile. Source:\n" + sourceString);
 120             throw err;
 121         }
 122         //if sourcString() set hasInnerClass it also set innerClassname.
 123         if(hasInnerClass) {
 124             StringBuffer sb = new StringBuffer(classFile.getAbsolutePath());
 125             classFile=new File(sb.insert(sb.lastIndexOf(".class"),innerClassname).toString());
 126         }
 127         ClassFile cf = ClassFile.read(classFile);


 177             "@Retention("+retentn+")\n" +
 178             "@Target({TYPE_USE,_OTHER_})\n" +
 179             "@interface AC { A[] value(); }\n\n" +
 180 
 181             "@Retention("+retentn+")\n" +
 182             "@Target({TYPE_USE,_OTHER_})\n" +
 183             "@Repeatable( BC.class )\n" +
 184             "@interface B { }\n\n" +
 185 
 186             "@Retention("+retentn+")\n" +
 187             "@Target({TYPE_USE,_OTHER_})\n" +
 188             "@interface BC { B[] value(); } \n\n" +
 189 
 190             "@Retention("+retentn+")\n" +
 191             "@Target({TYPE_USE,TYPE_PARAMETER,_OTHER_})\n" +
 192             "@Repeatable(DC.class)\n" +
 193             "@interface D { }\n\n" +
 194 
 195             "@Retention("+retentn+")\n" +
 196             "@Target({TYPE_USE,TYPE_PARAMETER,_OTHER_})\n" +
 197             "@interface DC { D[] value(); }\n\n");
 198 
 199         // Test case sources with sample generated source
 200         switch(src) {
 201             case 3: // (repeating) type annotations on field in method body
 202                     /*
 203                      * class Test1 {
 204                      * Test1(){}
 205                      * // type usage in method body
 206                      * String test(Test1 this, String param, String ... vararg) {
 207                      *     @A @B
 208                      *     Object o = new @A @B  String @A @B  [3];
 209                      *         return (@A @B  String) null;
 210                      * }}
 211                       */
 212                 source = new String(

 213                 "class " + testname + " {\n" +
 214                 "" + testname +"(){} \n" +
 215                 "// type usage in method body \n" +
 216                 "String test("+testname+" this, " +
 217                    "String param, String ... vararg) { \n" +
 218                 "    _As_ _Bs_\n    Object o = new _As_ _Bs_  String _As_ _Bs_  [3]; \n" +
 219                 "        return (_As_ _Bs_  String) null; \n" +
 220                 "} \n" +
 221                 "} \n").concat(sourceBase).replace("_OTHER_", annot2).replace("_As_",As).replace("_Bs_",Bs) +
 222                 "\n\n";
 223                 break;
 224             case 8: // (repeating) annotations on type parameters, bounds and  type arguments in new statement.
 225                     /*
 226                      * class Test2<T extends Object> {
 227                      *     Map<List<String>, Integer> map =
 228                      *         new HashMap<@A @B List<@A @B String>, @A @B Integer>();
 229                      *     Map<List<String>, Integer> map2 = new @A @B HashMap<>();
 230                      *     String test(Test2<T> this) { return null;}
 231                      *     <T> String genericMethod(T t) { return null; }
 232                      * }
 233                      */
 234                 source = new String( source +
 235                 "// (repeating) annotations on type parameters, bounds and  type arguments. \n" +
 236                 "class " + testname + "<T extends Object> {\n" +
 237                 "    Map<List<String>, Integer> map =\n" +
 238                 "        new HashMap<_As_ _Bs_ List<_As_ _Bs_ String>, _As_ _Bs_ Integer>();\n" +
 239                 "    Map<List<String>, Integer> map2 = new _As_ _Bs_ HashMap<>();\n" +
 240                 "    String test(" + testname + "<T> this) { return null;}\n" +
 241                 "    <T> String genericMethod(T t) { return null; }\n" +
 242                 "}\n").concat(sourceBase).replace("_OTHER_", annot2).replace("_As_",As).replace("_Bs_",Bs) +
 243                 "\n\n";
 244             break;
 245             case 9: // (repeating)annotations on type parameters of class, method return value in method.
 246                     /*
 247                      * class Test3{
 248                      *     <E extends Comparable> Map<List<E>, E > foo(E e) {
 249                      *         class maptest <E> {
 250                      *             Map<List<E>,E> getMap() {
 251                      *                 Map<List<E>,E> Em = new HashMap<List<@A @B @D E>,@A @B @D E>();
 252                      *                 return Em;
 253                      *             }
 254                      *         }
 255                      *         return new maptest<E>().getMap();
 256                      *    }
 257                      *    Map<List<String>,String> shm = foo(new String("hello"));
 258                      * }
 259                      */
 260                 source = new String( source +
 261                 "// (repeating)annotations on type parameters of class, method return value in method. \n" +
 262                 "class "+ testname + "{\n" +
 263                 "    <E extends Comparable> Map<List<E>, E > foo(E e) {\n" +
 264                 "        class maptest <E> {\n" +                  // inner class $1maptest
 265                 "            Map<List<E>,E> getMap() { \n" +
 266                 "                Map<List<E>,E> Em = new HashMap<List<_As_ _Bs_ _Ds_ E>,_As_ _Bs_ _Ds_ E>();\n" +
 267                 "                return Em;\n" +
 268                 "            }\n" +
 269                 "        }\n" +
 270                 "        return new maptest<E>().getMap();\n" +
 271                 "   }\n" +
 272                 "   Map<List<String>,String> shm = foo(new String(\"hello\"));\n" +
 273                 "}\n").concat(sourceBase).replace("_OTHER_", annot2).replace("_As_",As).replace("_Bs_",Bs).replace("_Ds_",Ds) +
 274                 "\n\n";
 275                 hasInnerClass=true;
 276                 innerClassname="$1maptest";
 277             break;
 278 
 279         }
 280         return imports + source;
 281     }
 282 }
   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  */
  23 
  24 /*
  25  * @test
  26  * @bug 8005085 8005877 8004829 8005681 8006734 8006775

  27  * @summary Combinations of Target ElementTypes on (repeated)type annotations.
  28  */
  29 
  30 import com.sun.tools.classfile.*;
  31 import java.io.File;
  32 
  33 public class CombinationsTargetTest2 extends ClassfileTestHelper {
  34     // Helps identify test case in event of failure.
  35     int testcount = 0;
  36     int src3 = 3, src8 = 8, src9 = 9;
  37 
  38     String[] ETypes={"TYPE", "FIELD", "METHOD", "PARAMETER", "CONSTRUCTOR",
  39                      "LOCAL_VARIABLE", "ANNOTATION_TYPE", "PACKAGE"};
  40 
  41     // local class tests will have an inner class.
  42     Boolean hasInnerClass=false;
  43     String innerClassname="";
  44 
  45     public static void main(String[] args) throws Exception {
  46         new CombinationsTargetTest2().run();


  82     }
  83 
  84     public void test(int tinv, int tvis, int inv, int vis, Boolean Arepeats,
  85                      Boolean BDrepeats, Boolean ABmix, String rtn, String et2,
  86                      Integer N, int source) throws Exception {
  87         ++testcount;
  88         expected_tvisibles = tvis;
  89         expected_tinvisibles = tinv;
  90         expected_visibles = vis;
  91         expected_invisibles = inv;
  92         File testFile = null;
  93         String tname="Test" + N.toString();
  94         hasInnerClass=false;
  95         String testDef = "Test " + testcount + " parameters: tinv=" + tinv +
  96                 ", tvis=" + tvis + ", inv=" + inv + ", vis=" + vis +
  97                 ", Arepeats=" + Arepeats + ", BDrepeats=" + BDrepeats +
  98                 ", ABmix=" + ABmix + ", retention: " + rtn + ", anno2: " +
  99                 et2 + ", src=" + source;
 100 
 101 // Uncomment this block to run the tests but skip failing scenarios.
 102         // 8005681 - skip cases with repeated annotations on new, array, cast.
 103         if((source==3 || source==8 || source==9) && (ABmix || (Arepeats && BDrepeats))) {
 104             System.out.println(testDef+"\n8005681-skip repeated annotations on new,array,cast");
 105             return;
 106         }
 107 
 108         println(testDef);
 109         // Create test source and File.
 110         String sourceString = sourceString(tname, rtn, et2, Arepeats,
 111                                            BDrepeats, ABmix, source);
 112         testFile = writeTestFile(tname+".java", sourceString);
 113         // Compile test source and read classfile.
 114         File classFile = null;
 115         try {
 116             classFile = compile(testFile);
 117         } catch (Error err) {
 118             System.err.println("Failed compile. Source:\n" + sourceString);
 119             throw err;
 120         }
 121         //if sourcString() set hasInnerClass it also set innerClassname.
 122         if(hasInnerClass) {
 123             StringBuffer sb = new StringBuffer(classFile.getAbsolutePath());
 124             classFile=new File(sb.insert(sb.lastIndexOf(".class"),innerClassname).toString());
 125         }
 126         ClassFile cf = ClassFile.read(classFile);


 176             "@Retention("+retentn+")\n" +
 177             "@Target({TYPE_USE,_OTHER_})\n" +
 178             "@interface AC { A[] value(); }\n\n" +
 179 
 180             "@Retention("+retentn+")\n" +
 181             "@Target({TYPE_USE,_OTHER_})\n" +
 182             "@Repeatable( BC.class )\n" +
 183             "@interface B { }\n\n" +
 184 
 185             "@Retention("+retentn+")\n" +
 186             "@Target({TYPE_USE,_OTHER_})\n" +
 187             "@interface BC { B[] value(); } \n\n" +
 188 
 189             "@Retention("+retentn+")\n" +
 190             "@Target({TYPE_USE,TYPE_PARAMETER,_OTHER_})\n" +
 191             "@Repeatable(DC.class)\n" +
 192             "@interface D { }\n\n" +
 193 
 194             "@Retention("+retentn+")\n" +
 195             "@Target({TYPE_USE,TYPE_PARAMETER,_OTHER_})\n" +
 196             "@interface DC { D[] value(); }\n");
 197 
 198         // Test case sources with sample generated source
 199         switch(src) {
 200             case 3: // (repeating) type annotations new,array level and cast
 201                     /*
 202                      * class Test1 {
 203                      * Test1(){}
 204                      * // type usage in method body
 205                      * String test(Test1 this, String param, String ... vararg) {
 206                      *     @A @B
 207                      *     Object o = new @A @B  String @A @B  [3];
 208                      *         return (@A @B  String) null;
 209                      * }}
 210                       */
 211                 source = new String( source +
 212                 "//case3: (repeating) type annotations new,array level and cast\n" +
 213                 "class " + testname + " {\n" +
 214                 "" + testname +"(){} \n" +
 215                 "// type usage in method body \n" +
 216                 "String test("+testname+" this, " +
 217                    "String param, String ... vararg) { \n" +
 218                 "    _As_ _Bs_\n    Object o = new _As_ _Bs_  String _As_ _Bs_  [3]; \n" +
 219                 "        return (_As_ _Bs_  String) null; \n" +
 220                 "} \n" +
 221                 "}\n\n").concat(sourceBase).replace("_OTHER_", annot2).replace("_As_",As).replace("_Bs_",Bs) +
 222                 "\n";
 223                 break;
 224             case 8: // (repeating) annotations on type parameters, bounds and  type arguments in new statement.
 225                     /*
 226                      * class Test2<T extends Object> {
 227                      *     Map<List<String>, Integer> map =
 228                      *         new HashMap<@A @B List<@A @B String>, @A @B Integer>();
 229                      *     Map<List<String>, Integer> map2 = new @A @B HashMap<>();
 230                      *     String test(Test2<T> this) { return null;}
 231                      *     <T> String genericMethod(T t) { return null; }
 232                      * }
 233                      */
 234                 source = new String( source +
 235                 "//case8: (repeating) annotations on type parameters, bounds and  type arguments. \n" +
 236                 "class " + testname + "<T extends Object> {\n" +
 237                 "    Map<List<String>, Integer> map =\n" +
 238                 "        new HashMap<_As_ _Bs_ List<_As_ _Bs_ String>, _As_ _Bs_ Integer>();\n" +
 239                 "    Map<List<String>, Integer> map2 = new _As_ _Bs_ HashMap<>();\n" +
 240                 "    String test(" + testname + "<T> this) { return null;}\n" +
 241                 "    <T> String genericMethod(T t) { return null; }\n" +
 242                 "}\n\n").concat(sourceBase).replace("_OTHER_", annot2).replace("_As_",As).replace("_Bs_",Bs) +
 243                 "\n";
 244             break;
 245             case 9: // (repeating)annotations on type parameters of class, method return value in method.
 246                     /*
 247                      * class Test3{
 248                      *     <E extends Comparable> Map<List<E>, E > foo(E e) {
 249                      *         class maptest <E> {
 250                      *             Map<List<E>,E> getMap() {
 251                      *                 Map<List<E>,E> Em = new HashMap<List<@A @B @D E>,@A @B @D E>();
 252                      *                 return Em;
 253                      *             }
 254                      *         }
 255                      *         return new maptest<E>().getMap();
 256                      *    }
 257                      *    Map<List<String>,String> shm = foo(new String("hello"));
 258                      * }
 259                      */
 260                 source = new String( source +
 261                 "//case9: (repeating)annotations on type parameters of class, method return value in method. \n" +
 262                 "class "+ testname + "{\n" +
 263                 "    <E extends Comparable> Map<List<E>, E > foo(E e) {\n" +
 264                 "        class maptest <E> {\n" +                  // inner class $1maptest
 265                 "            Map<List<E>,E> getMap() { \n" +
 266                 "                Map<List<E>,E> Em = new HashMap<List<_As_ _Bs_ _Ds_ E>,_As_ _Bs_ _Ds_ E>();\n" +
 267                 "                return Em;\n" +
 268                 "            }\n" +
 269                 "        }\n" +
 270                 "        return new maptest<E>().getMap();\n" +
 271                 "   }\n" +
 272                 "   Map<List<String>,String> shm = foo(new String(\"hello\"));\n" +
 273                 "}\n\n").concat(sourceBase).replace("_OTHER_", annot2).replace("_As_",As).replace("_Bs_",Bs).replace("_Ds_",Ds) +
 274                 "\n";
 275                 hasInnerClass=true;
 276                 innerClassname="$1maptest";
 277             break;

 278         }
 279         return imports + source;
 280     }
 281 }