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();
  48     }
  49 
  50     void run() throws Exception {
  51         // Determines which repeat and order in source(ABMix).
  52         Boolean As= false, BDs=true, ABMix=false;
  53         int testrun=0;
  54         // A repeats and/or B/D repeats, ABMix for order of As and Bs.
  55         Boolean [][] bRepeat = new Boolean[][]{{false,false,false},//no repeats
  56                                                {true,false,false}, //repeat @A
  57                                                {false,true,false}, //repeat @B
  58                                                {true,true,false},  //repeat both
  59                                                {false,false,true}  //repeat mix
  60         };
  61         for(Boolean[] bCombo : bRepeat) {
  62             As=bCombo[0]; BDs=bCombo[1]; ABMix=bCombo[2];
  63             for(String et : ETypes) {
  64                switch(et) {
  65                    case "METHOD":
  66                        test( 8,  0, 0, 0, As, BDs, ABMix, "CLASS",   et, ++testrun, src3);
  67                        test( 0,  8, 0, 0, As, BDs, ABMix, "RUNTIME", et, ++testrun, src3);
  68                        break;
  69                    case "CONSTRUCTOR":
  70                    case "FIELD":
  71                        test( 8,  0, 0, 0, As, BDs, ABMix, "CLASS",   et, ++testrun, src3);
  72                        test( 8,  0, 0, 0, As, BDs, ABMix, "CLASS",   et, ++testrun, src8);
  73                        test( 6,  0, 0, 0, As, BDs, ABMix, "CLASS",   et, ++testrun, src9);
  74                        test( 0,  8, 0, 0, As, BDs, ABMix, "RUNTIME", et, ++testrun, src3);
  75                        test( 0,  8, 0, 0, As, BDs, ABMix, "RUNTIME", et, ++testrun, src8);
  76                        test( 0,  6, 0, 0, As, BDs, ABMix, "RUNTIME", et, ++testrun, src9);
  77                        break;
  78                    default:/*TYPE,PARAMETER,LOCAL_VARIABLE,ANNOTATION_TYPE,PACKAGE*/
  79                        break;
  80                }
  81             }
  82         }
  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);
 128 
 129         //Test class,fields and method counts.
 130         test(cf);
 131 
 132         for (Field f : cf.fields) {
 133             test(cf, f);
 134         }
 135         for (Method m: cf.methods) {
 136             test(cf, m);
 137         }
 138         countAnnotations();
 139         if (errors > 0) {
 140             System.err.println( testDef );
 141             System.err.println( "Source:\n" + sourceString );
 142             throw new Exception( errors + " errors found" );
 143         }
 144         println("Pass");
 145     }
 146 
 147     //
 148     // Source for test cases
 149     //
 150     String sourceString(String testname, String retentn, String annot2,
 151                         Boolean Arepeats, Boolean BDrepeats, Boolean ABmix,
 152                         int src) {
 153 
 154         String As = "@A", Bs = "@B", Ds = "@D";
 155         if(Arepeats) As = "@A @A";
 156         if(BDrepeats) {
 157             Bs = "@B @B";
 158             Ds = "@D @D";
 159         }
 160         if(ABmix) { As = "@A @B"; Bs = "@A @B"; Ds = "@D @D"; }
 161 
 162         // Source to check for TYPE_USE and TYPE_PARAMETER annotations.
 163         // Source base (annotations) is same for all test cases.
 164         String source = new String();
 165         String imports = new String("import java.lang.annotation.*; \n" +
 166             "import static java.lang.annotation.RetentionPolicy.*; \n" +
 167             "import static java.lang.annotation.ElementType.*; \n" +
 168             "import java.util.List; \n" +
 169             "import java.util.HashMap; \n" +
 170             "import java.util.Map; \n\n");
 171 
 172             String sourceBase = new String("@Retention("+retentn+")\n" +
 173             "@Target({TYPE_USE,_OTHER_})\n" +
 174             "@Repeatable( AC.class )\n" +
 175             "@interface A { }\n\n" +
 176 
 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 }