1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/tools/javac/annotations/typeAnnotations/classfile/CombinationsTargetTest1.java Wed Apr 27 01:34:52 2016 +0800 1.3 @@ -0,0 +1,381 @@ 1.4 +/* 1.5 + * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + */ 1.26 + 1.27 +/* 1.28 + * @test 1.29 + * @bug 8005085 8005877 8004829 8005681 8006734 8006775 1.30 + * @summary Combinations of Target ElementTypes on (repeated)type annotations. 1.31 + */ 1.32 + 1.33 +import com.sun.tools.classfile.*; 1.34 +import java.io.File; 1.35 + 1.36 +public class CombinationsTargetTest1 extends ClassfileTestHelper { 1.37 + 1.38 + // Test count helps identify test case in event of failure. 1.39 + int testcount = 0; 1.40 + 1.41 + // Base test case template descriptions 1.42 + enum srce { 1.43 + src1("(repeating) type annotations at class level"), 1.44 + src2("(repeating) type annotations on method"), 1.45 + src3("(repeating) type annotations on wildcard, type arguments in anonymous class"), 1.46 + src4("(repeating) type annotations on type parameters, bounds and type arguments on class decl"), 1.47 + src5("(repeating) type annotations on type parameters, bounds and type arguments on method"), 1.48 + src6("(repeating) type annotations on type parameters, bounds and type arguments in method"); 1.49 + 1.50 + String description; 1.51 + 1.52 + srce(String desc) { 1.53 + this.description = this + ": " +desc; 1.54 + } 1.55 + } 1.56 + 1.57 + String[] ETypes={"TYPE", "FIELD", "METHOD", "PARAMETER", "CONSTRUCTOR", 1.58 + "LOCAL_VARIABLE", "ANNOTATION_TYPE", "PACKAGE"}; 1.59 + 1.60 + // local class tests will have an inner class. 1.61 + Boolean hasInnerClass=false; 1.62 + String innerClassname=""; 1.63 + 1.64 + public static void main(String[] args) throws Exception { 1.65 + new CombinationsTargetTest1().run(); 1.66 + } 1.67 + 1.68 + void run() throws Exception { 1.69 + // Determines which repeat and order in source(ABMix). 1.70 + Boolean As= false, BDs=true, ABMix=false; 1.71 + int testrun=0; 1.72 + Boolean [][] bRepeat = new Boolean[][]{{false,false,false},//no repeats 1.73 + {true,false,false}, //repeat @A 1.74 + {false,true,false}, //repeat @B 1.75 + {true,true,false}, //repeat both 1.76 + {false,false,true} //repeat mix 1.77 + }; 1.78 + for(Boolean[] bCombo : bRepeat) { 1.79 + As=bCombo[0]; BDs=bCombo[1]; ABMix=bCombo[2]; 1.80 + for(String et : ETypes) { 1.81 + switch(et) { 1.82 + case "METHOD": 1.83 + test( 8, 0, 2, 0, As, BDs, ABMix, "CLASS", et, ++testrun, srce.src1); 1.84 + test(10, 0, 2, 0, As, BDs, ABMix, "CLASS", et, ++testrun, srce.src2); 1.85 + test( 6, 0, 0, 0, As, BDs, ABMix, "CLASS", et, ++testrun, srce.src3); 1.86 + test(10, 0, 2, 0, As, BDs, ABMix, "CLASS", et, ++testrun, srce.src5); 1.87 + test( 0, 8, 0, 2, As, BDs, ABMix, "RUNTIME", et, ++testrun, srce.src1); 1.88 + test( 0, 10, 0, 2, As, BDs, ABMix, "RUNTIME", et, ++testrun, srce.src2); 1.89 + test( 0, 6, 0, 0, As, BDs, ABMix, "RUNTIME", et, ++testrun, srce.src3); 1.90 + test( 0, 10, 0, 2, As, BDs, ABMix, "RUNTIME", et, ++testrun, srce.src5); 1.91 + break; 1.92 + case "CONSTRUCTOR": 1.93 + case "FIELD": 1.94 + test( 8, 0, 4, 0, As, BDs, ABMix, "CLASS", et, ++testrun, srce.src1); 1.95 + test( 6, 0, 3, 0, As, BDs, ABMix, "CLASS", et, ++testrun, srce.src4); 1.96 + test( 9, 0, 0, 0, As, BDs, ABMix, "CLASS", et, ++testrun, srce.src6); 1.97 + test( 0, 8, 0, 4, As, BDs, ABMix, "RUNTIME", et, ++testrun, srce.src1); 1.98 + test( 0, 6, 0, 3, As, BDs, ABMix, "RUNTIME", et, ++testrun, srce.src4); 1.99 + test( 0, 9, 0, 0, As, BDs, ABMix, "RUNTIME", et, ++testrun, srce.src6); 1.100 + break; 1.101 + default:/*TYPE,PARAMETER,LOCAL_VARIABLE,ANNOTATION_TYPE,PACKAGE*/ 1.102 + test( 8, 0, 2, 0, As, BDs, ABMix, "CLASS", et, ++testrun, srce.src1); 1.103 + test( 6, 0, 3, 0, As, BDs, ABMix, "CLASS", et, ++testrun, srce.src4); 1.104 + test( 0, 8, 0, 2, As, BDs, ABMix, "RUNTIME", et, ++testrun, srce.src1); 1.105 + test( 0, 6, 0, 3, As, BDs, ABMix, "RUNTIME", et, ++testrun, srce.src4); 1.106 + } 1.107 + } 1.108 + } 1.109 + } 1.110 + 1.111 + public void test(int tinv, int tvis, int inv, int vis, Boolean Arepeats, 1.112 + Boolean BDrepeats, Boolean ABmix, String rtn, String et2, 1.113 + Integer N, srce source) throws Exception { 1.114 + ++testcount; 1.115 + expected_tvisibles = tvis; 1.116 + expected_tinvisibles = tinv; 1.117 + expected_visibles = vis; 1.118 + expected_invisibles = inv; 1.119 + File testFile = null; 1.120 + String tname="Test" + N.toString(); 1.121 + hasInnerClass=false; 1.122 + String testDef = "Test " + testcount + " parameters: tinv=" + tinv + 1.123 + ", tvis=" + tvis + ", inv=" + inv + ", vis=" + vis + 1.124 + ", Arepeats=" + Arepeats + ", BDrepeats=" + BDrepeats + 1.125 + ", ABmix=" + ABmix + ", retention: " + rtn + ", anno2: " + 1.126 + et2 + ", src=" + source; 1.127 + 1.128 + println(testDef); 1.129 + // Create test source and File. 1.130 + String sourceString = sourceString(tname, rtn, et2, Arepeats, 1.131 + BDrepeats, ABmix, source); 1.132 + testFile = writeTestFile(tname+".java", sourceString); 1.133 + // Compile test source and read classfile. 1.134 + File classFile = null; 1.135 + try { 1.136 + classFile = compile(testFile); 1.137 + } catch (Error err) { 1.138 + System.err.println("Failed compile. Source:\n" + sourceString); 1.139 + throw err; 1.140 + } 1.141 + //if sourcString() set hasInnerClass it also set innerClassname. 1.142 + if(hasInnerClass) { 1.143 + StringBuffer sb = new StringBuffer(classFile.getAbsolutePath()); 1.144 + classFile=new File(sb.insert(sb.lastIndexOf(".class"), 1.145 + innerClassname).toString()); 1.146 + } 1.147 + ClassFile cf = ClassFile.read(classFile); 1.148 + 1.149 + //Test class,fields and method counts. 1.150 + test(cf); 1.151 + 1.152 + for (Field f : cf.fields) { 1.153 + test(cf, f); 1.154 + } 1.155 + for (Method m: cf.methods) { 1.156 + test(cf, m); 1.157 + } 1.158 + countAnnotations(); 1.159 + if (errors > 0) { 1.160 + System.err.println( testDef ); 1.161 + System.err.println( "Source:\n" + sourceString ); 1.162 + throw new Exception( errors + " errors found" ); 1.163 + } 1.164 + println("Pass"); 1.165 + } 1.166 + 1.167 + // 1.168 + // Source for test cases 1.169 + // 1.170 + String sourceString(String testname, String retentn, String annot2, 1.171 + Boolean Arepeats, Boolean BDrepeats, Boolean ABmix, 1.172 + srce src) { 1.173 + 1.174 + String As = "@A", Bs = "@B", Ds = "@D"; 1.175 + if(Arepeats) As = "@A @A"; 1.176 + if(BDrepeats) { 1.177 + Bs = "@B @B"; 1.178 + Ds = "@D @D"; 1.179 + } 1.180 + if(ABmix) { As = "@A @B"; Bs = "@A @B"; Ds = "@D @D"; } 1.181 + 1.182 + // Source to check for TYPE_USE and TYPE_PARAMETER annotations. 1.183 + // Source base (annotations) is same for all test cases. 1.184 + String source = new String(); 1.185 + String imports = new String("import java.lang.annotation.*; \n" + 1.186 + "import static java.lang.annotation.RetentionPolicy.*; \n" + 1.187 + "import static java.lang.annotation.ElementType.*; \n" + 1.188 + "import java.util.List; \n" + 1.189 + "import java.util.HashMap; \n" + 1.190 + "import java.util.Map; \n\n"); 1.191 + 1.192 + String sourceBase = new String("@Retention("+retentn+")\n" + 1.193 + "@Target({TYPE_USE,_OTHER_})\n" + 1.194 + "@Repeatable( AC.class )\n" + 1.195 + "@interface A { }\n\n" + 1.196 + 1.197 + "@Retention("+retentn+")\n" + 1.198 + "@Target({TYPE_USE,_OTHER_})\n" + 1.199 + "@interface AC { A[] value(); }\n\n" + 1.200 + 1.201 + "@Retention("+retentn+")\n" + 1.202 + "@Target({TYPE_USE,_OTHER_})\n" + 1.203 + "@Repeatable( BC.class )\n" + 1.204 + "@interface B { }\n\n" + 1.205 + 1.206 + "@Retention("+retentn+")\n" + 1.207 + "@Target({TYPE_USE,_OTHER_})\n" + 1.208 + "@interface BC { B[] value(); } \n\n" + 1.209 + 1.210 + "@Retention("+retentn+")\n" + 1.211 + "@Target({TYPE_PARAMETER,_OTHER_})\n" + 1.212 + "@interface C { }\n\n" + 1.213 + 1.214 + "@Retention("+retentn+")\n" + 1.215 + "@Target({TYPE_USE,TYPE_PARAMETER,_OTHER_})\n" + 1.216 + "@Repeatable(DC.class)\n" + 1.217 + "@interface D { }\n\n" + 1.218 + 1.219 + "@Retention("+retentn+")\n" + 1.220 + "@Target({TYPE_USE,TYPE_PARAMETER,_OTHER_})\n" + 1.221 + "@interface DC { D[] value(); }\n"); 1.222 + 1.223 + // Test case sources with sample generated source. 1.224 + switch(src) { 1.225 + case src1: // repeating type annotations at class level 1.226 + /* 1.227 + * @A @B class Test1 { 1.228 + * @A @B Test1(){} 1.229 + * @A @B Integer i1 = 0; 1.230 + * String @A @B [] @A @B [] sa = null; 1.231 + * // type usage in method body 1.232 + * String test(Test1 this, String param, String ... vararg) { 1.233 + * Object o = new String [3]; 1.234 + * return (String) null; 1.235 + * }} 1.236 + */ 1.237 + source = new String( 1.238 + "// " + src.description + "\n" + 1.239 + "_As_ _Bs_ class " + testname + " {\n" + 1.240 + "_As_ _Bs_ " + testname +"(){} \n" + 1.241 + "_As_ _Bs_ Integer i1 = 0; \n" + 1.242 + "String _As_ _Bs_ [] _As_ _Bs_ [] sa = null; \n" + 1.243 + "// type usage in method body \n" + 1.244 + "String test("+testname+" this, " + 1.245 + "String param, String ... vararg) { \n" + 1.246 + " Object o = new String [3]; \n" + 1.247 + " return (String) null; \n" + 1.248 + "}\n" + 1.249 + "}\n\n").concat(sourceBase).replace("_OTHER_", annot2).replace("_As_",As).replace("_Bs_",Bs) + 1.250 + "\n"; 1.251 + break; 1.252 + case src2: // (repeating) type annotations on method. 1.253 + /* 1.254 + * class Test12 { 1.255 + * Test12(){} 1.256 + * // type usage on method 1.257 + * @A @B String test(@A @B Test12 this, @A @B String param, @A @B String @A @B ... vararg) { 1.258 + * Object o = new String [3]; 1.259 + * return (String) null; 1.260 + * }} 1.261 + */ 1.262 + source = new String( 1.263 + "// " + src.description + "\n" + 1.264 + "class " + testname + " {\n" + 1.265 + testname +"(){} \n" + 1.266 + "// type usage on method \n" + 1.267 + "_As_ _Bs_ String test(_As_ _Bs_ "+testname+" this, " + 1.268 + "_As_ _Bs_ String param, _As_ _Bs_ String _As_ _Bs_ ... vararg) { \n" + 1.269 + " Object o = new String [3]; \n" + 1.270 + " return (String) null; \n" + 1.271 + "}\n" + 1.272 + "}\n\n").concat(sourceBase).replace("_OTHER_", annot2).replace("_As_",As).replace("_Bs_",Bs) + 1.273 + "\n"; 1.274 + break; 1.275 + case src3: //(repeating) annotations on wildcard, type arguments in anonymous class. 1.276 + /* 1.277 + * class Test13<T extends Object> { 1.278 + * public T data = null; 1.279 + * T getData() { return data;} 1.280 + * String mtest( Test13<String> t){ return t.getData(); } 1.281 + * public void test() { 1.282 + * mtest( new Test13<String>() { 1.283 + * void m1(List<@A @B ? extends @A @B Object> lst) {} 1.284 + * void m2() throws@A @B Exception { } 1.285 + * }); 1.286 + * } 1.287 + * } 1.288 + */ 1.289 + source = new String( source + 1.290 + "// " + src.description + "\n" + 1.291 + "class " + testname + "<T extends Object> {\n" + 1.292 + " public T data = null;\n" + 1.293 + " T getData() { return data;}\n" + 1.294 + " String mtest( " + testname + "<String> t){ return t.getData(); }\n" + 1.295 + " public void test() {\n" + 1.296 + " mtest( new " + testname + "<String>() {\n" + 1.297 + " void m1(List<_As_ _Bs_ ? extends _As_ _Bs_ Object> lst) {}\n" + 1.298 + " void m2() throws_As_ _Bs_ Exception { }\n" + 1.299 + " });\n" + 1.300 + " }\n" + 1.301 + "}\n\n").concat(sourceBase).replace("_OTHER_", annot2).replace("_As_",As).replace("_Bs_",Bs) + 1.302 + "\n"; 1.303 + hasInnerClass=true; 1.304 + innerClassname="$1"; 1.305 + break; 1.306 + case src4: // (repeating)annotations on type parameters, bounds and type arguments on class decl. 1.307 + /* 1.308 + * @A @B @D 1.309 + * class Test2<@A @B @C @D T extends @A @B Object> { 1.310 + * Map<List<String>, Integer> map = 1.311 + * new HashMap<List< String>, Integer>(); 1.312 + * Map<List<String>,Integer> map2 = new HashMap<>(); 1.313 + * String test(Test2<T> this) { return null;} 1.314 + * <T> String genericMethod(T t) { return null; } 1.315 + * } 1.316 + */ 1.317 + source = new String( source + 1.318 + "// " + src.description + "\n" + 1.319 + "_As_ _Bs_ _Ds_\n" + //8004829: A and B on type parameter below. 1.320 + "class " + testname + "<_As_ _Bs_ @C _Ds_ T extends _As_ _Bs_ Object> {\n" + 1.321 + " Map<List<String>, Integer> map =\n" + 1.322 + " new HashMap<List< String>, Integer>();\n" + 1.323 + " Map<List<String>,Integer> map2 = new HashMap<>();\n" + 1.324 + " String test(" + testname + "<T> this) { return null;}\n" + 1.325 + " <T> String genericMethod(T t) { return null; }\n" + 1.326 + "}\n\n").concat(sourceBase).replace("_OTHER_", annot2).replace("_As_",As).replace("_Bs_",Bs).replace("_Ds_",Ds) + 1.327 + "\n"; 1.328 + break; 1.329 + case src5: // (repeating) annotations on type parameters, bounds and type arguments on method. 1.330 + /* 1.331 + * class Test14<T extends Object> { 1.332 + * Map<List<String>, Integer> map = 1.333 + * new HashMap<List<String>, Integer>(); 1.334 + * Map<List<String>, Integer> map2 = new HashMap<>(); 1.335 + * String test(@A @B Test14<@D T> this) { return null;} 1.336 + * <@C @D T> @A @B String genericMethod(@A @B @D T t) { return null; } 1.337 + * } 1.338 + */ 1.339 + source = new String( source + 1.340 + "// " + src.description + "\n" + 1.341 + "class " + testname + "<T extends Object> {\n" + 1.342 + " Map<List<String>, Integer> map =\n" + 1.343 + " new HashMap<List<String>, Integer>();\n" + 1.344 + " Map<List<String>, Integer> map2 = new HashMap<>();\n" + 1.345 + " String test(_As_ _Bs_ " + testname + "<_Ds_ T> this) { return null;}\n" + 1.346 + " <@C _Ds_ T> _As_ _Bs_ String genericMethod(_As_ _Bs_ _Ds_ T t) { return null; }\n" + 1.347 + "}\n\n").concat(sourceBase).replace("_OTHER_", annot2).replace("_As_",As).replace("_Bs_",Bs).replace("_Ds_",Ds) + 1.348 + "\n"; 1.349 + break; 1.350 + case src6: // repeating annotations on type parameters, bounds and type arguments in method. 1.351 + /* 1.352 + * class Test7{ 1.353 + * <E extends Comparable> Map<List<E>, E > foo(E e) { 1.354 + * class maptest <@A @B @D E> { 1.355 + * Map<List<@A @B @D E>,@A @B @D E> getMap() { 1.356 + * return new HashMap<List<E>,E>(); 1.357 + * } 1.358 + * } 1.359 + * return new maptest<E>().getMap(); 1.360 + * } 1.361 + * Map<List<String>,String> shm = foo(new String("hello")); 1.362 + * } 1.363 + */ 1.364 + source = new String( source + 1.365 + "// " + src.description + "\n" + 1.366 + "class "+ testname + "{\n" + 1.367 + " <E extends Comparable> Map<List<E>, E > foo(E e) {\n" + 1.368 + " class maptest <_As_ _Bs_ _Ds_ E> {\n" + // inner class $1maptest 1.369 + " Map<List<_As_ _Bs_ _Ds_ E>,_As_ _Bs_ _Ds_ E> getMap() { \n" + 1.370 + " return new HashMap<List<E>,E>();\n" + 1.371 + " }\n" + 1.372 + " }\n" + 1.373 + " return new maptest<E>().getMap();\n" + 1.374 + " }\n" + 1.375 + " Map<List<String>,String> shm = foo(new String(\"hello\"));\n" + 1.376 + "}\n\n").concat(sourceBase).replace("_OTHER_", annot2).replace("_As_",As).replace("_Bs_",Bs).replace("_Ds_",Ds) + 1.377 + "\n"; 1.378 + hasInnerClass=true; 1.379 + innerClassname="$1maptest"; 1.380 + break; 1.381 + } 1.382 + return imports + source; 1.383 + } 1.384 +}