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

Wed, 27 Apr 2016 01:34:52 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:34:52 +0800
changeset 0
959103a6100f
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/langtools/
changeset: 2573:53ca196be1ae
tag: jdk8u25-b17

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 */
aoqi@0 23
aoqi@0 24 /*
aoqi@0 25 * @test
aoqi@0 26 * @bug 8005085 8005877 8004829 8005681 8006734 8006775
aoqi@0 27 * @summary Combinations of Target ElementTypes on (repeated)type annotations.
aoqi@0 28 */
aoqi@0 29
aoqi@0 30 import com.sun.tools.classfile.*;
aoqi@0 31 import java.io.File;
aoqi@0 32
aoqi@0 33 public class CombinationsTargetTest1 extends ClassfileTestHelper {
aoqi@0 34
aoqi@0 35 // Test count helps identify test case in event of failure.
aoqi@0 36 int testcount = 0;
aoqi@0 37
aoqi@0 38 // Base test case template descriptions
aoqi@0 39 enum srce {
aoqi@0 40 src1("(repeating) type annotations at class level"),
aoqi@0 41 src2("(repeating) type annotations on method"),
aoqi@0 42 src3("(repeating) type annotations on wildcard, type arguments in anonymous class"),
aoqi@0 43 src4("(repeating) type annotations on type parameters, bounds and type arguments on class decl"),
aoqi@0 44 src5("(repeating) type annotations on type parameters, bounds and type arguments on method"),
aoqi@0 45 src6("(repeating) type annotations on type parameters, bounds and type arguments in method");
aoqi@0 46
aoqi@0 47 String description;
aoqi@0 48
aoqi@0 49 srce(String desc) {
aoqi@0 50 this.description = this + ": " +desc;
aoqi@0 51 }
aoqi@0 52 }
aoqi@0 53
aoqi@0 54 String[] ETypes={"TYPE", "FIELD", "METHOD", "PARAMETER", "CONSTRUCTOR",
aoqi@0 55 "LOCAL_VARIABLE", "ANNOTATION_TYPE", "PACKAGE"};
aoqi@0 56
aoqi@0 57 // local class tests will have an inner class.
aoqi@0 58 Boolean hasInnerClass=false;
aoqi@0 59 String innerClassname="";
aoqi@0 60
aoqi@0 61 public static void main(String[] args) throws Exception {
aoqi@0 62 new CombinationsTargetTest1().run();
aoqi@0 63 }
aoqi@0 64
aoqi@0 65 void run() throws Exception {
aoqi@0 66 // Determines which repeat and order in source(ABMix).
aoqi@0 67 Boolean As= false, BDs=true, ABMix=false;
aoqi@0 68 int testrun=0;
aoqi@0 69 Boolean [][] bRepeat = new Boolean[][]{{false,false,false},//no repeats
aoqi@0 70 {true,false,false}, //repeat @A
aoqi@0 71 {false,true,false}, //repeat @B
aoqi@0 72 {true,true,false}, //repeat both
aoqi@0 73 {false,false,true} //repeat mix
aoqi@0 74 };
aoqi@0 75 for(Boolean[] bCombo : bRepeat) {
aoqi@0 76 As=bCombo[0]; BDs=bCombo[1]; ABMix=bCombo[2];
aoqi@0 77 for(String et : ETypes) {
aoqi@0 78 switch(et) {
aoqi@0 79 case "METHOD":
aoqi@0 80 test( 8, 0, 2, 0, As, BDs, ABMix, "CLASS", et, ++testrun, srce.src1);
aoqi@0 81 test(10, 0, 2, 0, As, BDs, ABMix, "CLASS", et, ++testrun, srce.src2);
aoqi@0 82 test( 6, 0, 0, 0, As, BDs, ABMix, "CLASS", et, ++testrun, srce.src3);
aoqi@0 83 test(10, 0, 2, 0, As, BDs, ABMix, "CLASS", et, ++testrun, srce.src5);
aoqi@0 84 test( 0, 8, 0, 2, As, BDs, ABMix, "RUNTIME", et, ++testrun, srce.src1);
aoqi@0 85 test( 0, 10, 0, 2, As, BDs, ABMix, "RUNTIME", et, ++testrun, srce.src2);
aoqi@0 86 test( 0, 6, 0, 0, As, BDs, ABMix, "RUNTIME", et, ++testrun, srce.src3);
aoqi@0 87 test( 0, 10, 0, 2, As, BDs, ABMix, "RUNTIME", et, ++testrun, srce.src5);
aoqi@0 88 break;
aoqi@0 89 case "CONSTRUCTOR":
aoqi@0 90 case "FIELD":
aoqi@0 91 test( 8, 0, 4, 0, As, BDs, ABMix, "CLASS", et, ++testrun, srce.src1);
aoqi@0 92 test( 6, 0, 3, 0, As, BDs, ABMix, "CLASS", et, ++testrun, srce.src4);
aoqi@0 93 test( 9, 0, 0, 0, As, BDs, ABMix, "CLASS", et, ++testrun, srce.src6);
aoqi@0 94 test( 0, 8, 0, 4, As, BDs, ABMix, "RUNTIME", et, ++testrun, srce.src1);
aoqi@0 95 test( 0, 6, 0, 3, As, BDs, ABMix, "RUNTIME", et, ++testrun, srce.src4);
aoqi@0 96 test( 0, 9, 0, 0, As, BDs, ABMix, "RUNTIME", et, ++testrun, srce.src6);
aoqi@0 97 break;
aoqi@0 98 default:/*TYPE,PARAMETER,LOCAL_VARIABLE,ANNOTATION_TYPE,PACKAGE*/
aoqi@0 99 test( 8, 0, 2, 0, As, BDs, ABMix, "CLASS", et, ++testrun, srce.src1);
aoqi@0 100 test( 6, 0, 3, 0, As, BDs, ABMix, "CLASS", et, ++testrun, srce.src4);
aoqi@0 101 test( 0, 8, 0, 2, As, BDs, ABMix, "RUNTIME", et, ++testrun, srce.src1);
aoqi@0 102 test( 0, 6, 0, 3, As, BDs, ABMix, "RUNTIME", et, ++testrun, srce.src4);
aoqi@0 103 }
aoqi@0 104 }
aoqi@0 105 }
aoqi@0 106 }
aoqi@0 107
aoqi@0 108 public void test(int tinv, int tvis, int inv, int vis, Boolean Arepeats,
aoqi@0 109 Boolean BDrepeats, Boolean ABmix, String rtn, String et2,
aoqi@0 110 Integer N, srce source) throws Exception {
aoqi@0 111 ++testcount;
aoqi@0 112 expected_tvisibles = tvis;
aoqi@0 113 expected_tinvisibles = tinv;
aoqi@0 114 expected_visibles = vis;
aoqi@0 115 expected_invisibles = inv;
aoqi@0 116 File testFile = null;
aoqi@0 117 String tname="Test" + N.toString();
aoqi@0 118 hasInnerClass=false;
aoqi@0 119 String testDef = "Test " + testcount + " parameters: tinv=" + tinv +
aoqi@0 120 ", tvis=" + tvis + ", inv=" + inv + ", vis=" + vis +
aoqi@0 121 ", Arepeats=" + Arepeats + ", BDrepeats=" + BDrepeats +
aoqi@0 122 ", ABmix=" + ABmix + ", retention: " + rtn + ", anno2: " +
aoqi@0 123 et2 + ", src=" + source;
aoqi@0 124
aoqi@0 125 println(testDef);
aoqi@0 126 // Create test source and File.
aoqi@0 127 String sourceString = sourceString(tname, rtn, et2, Arepeats,
aoqi@0 128 BDrepeats, ABmix, source);
aoqi@0 129 testFile = writeTestFile(tname+".java", sourceString);
aoqi@0 130 // Compile test source and read classfile.
aoqi@0 131 File classFile = null;
aoqi@0 132 try {
aoqi@0 133 classFile = compile(testFile);
aoqi@0 134 } catch (Error err) {
aoqi@0 135 System.err.println("Failed compile. Source:\n" + sourceString);
aoqi@0 136 throw err;
aoqi@0 137 }
aoqi@0 138 //if sourcString() set hasInnerClass it also set innerClassname.
aoqi@0 139 if(hasInnerClass) {
aoqi@0 140 StringBuffer sb = new StringBuffer(classFile.getAbsolutePath());
aoqi@0 141 classFile=new File(sb.insert(sb.lastIndexOf(".class"),
aoqi@0 142 innerClassname).toString());
aoqi@0 143 }
aoqi@0 144 ClassFile cf = ClassFile.read(classFile);
aoqi@0 145
aoqi@0 146 //Test class,fields and method counts.
aoqi@0 147 test(cf);
aoqi@0 148
aoqi@0 149 for (Field f : cf.fields) {
aoqi@0 150 test(cf, f);
aoqi@0 151 }
aoqi@0 152 for (Method m: cf.methods) {
aoqi@0 153 test(cf, m);
aoqi@0 154 }
aoqi@0 155 countAnnotations();
aoqi@0 156 if (errors > 0) {
aoqi@0 157 System.err.println( testDef );
aoqi@0 158 System.err.println( "Source:\n" + sourceString );
aoqi@0 159 throw new Exception( errors + " errors found" );
aoqi@0 160 }
aoqi@0 161 println("Pass");
aoqi@0 162 }
aoqi@0 163
aoqi@0 164 //
aoqi@0 165 // Source for test cases
aoqi@0 166 //
aoqi@0 167 String sourceString(String testname, String retentn, String annot2,
aoqi@0 168 Boolean Arepeats, Boolean BDrepeats, Boolean ABmix,
aoqi@0 169 srce src) {
aoqi@0 170
aoqi@0 171 String As = "@A", Bs = "@B", Ds = "@D";
aoqi@0 172 if(Arepeats) As = "@A @A";
aoqi@0 173 if(BDrepeats) {
aoqi@0 174 Bs = "@B @B";
aoqi@0 175 Ds = "@D @D";
aoqi@0 176 }
aoqi@0 177 if(ABmix) { As = "@A @B"; Bs = "@A @B"; Ds = "@D @D"; }
aoqi@0 178
aoqi@0 179 // Source to check for TYPE_USE and TYPE_PARAMETER annotations.
aoqi@0 180 // Source base (annotations) is same for all test cases.
aoqi@0 181 String source = new String();
aoqi@0 182 String imports = new String("import java.lang.annotation.*; \n" +
aoqi@0 183 "import static java.lang.annotation.RetentionPolicy.*; \n" +
aoqi@0 184 "import static java.lang.annotation.ElementType.*; \n" +
aoqi@0 185 "import java.util.List; \n" +
aoqi@0 186 "import java.util.HashMap; \n" +
aoqi@0 187 "import java.util.Map; \n\n");
aoqi@0 188
aoqi@0 189 String sourceBase = new String("@Retention("+retentn+")\n" +
aoqi@0 190 "@Target({TYPE_USE,_OTHER_})\n" +
aoqi@0 191 "@Repeatable( AC.class )\n" +
aoqi@0 192 "@interface A { }\n\n" +
aoqi@0 193
aoqi@0 194 "@Retention("+retentn+")\n" +
aoqi@0 195 "@Target({TYPE_USE,_OTHER_})\n" +
aoqi@0 196 "@interface AC { A[] value(); }\n\n" +
aoqi@0 197
aoqi@0 198 "@Retention("+retentn+")\n" +
aoqi@0 199 "@Target({TYPE_USE,_OTHER_})\n" +
aoqi@0 200 "@Repeatable( BC.class )\n" +
aoqi@0 201 "@interface B { }\n\n" +
aoqi@0 202
aoqi@0 203 "@Retention("+retentn+")\n" +
aoqi@0 204 "@Target({TYPE_USE,_OTHER_})\n" +
aoqi@0 205 "@interface BC { B[] value(); } \n\n" +
aoqi@0 206
aoqi@0 207 "@Retention("+retentn+")\n" +
aoqi@0 208 "@Target({TYPE_PARAMETER,_OTHER_})\n" +
aoqi@0 209 "@interface C { }\n\n" +
aoqi@0 210
aoqi@0 211 "@Retention("+retentn+")\n" +
aoqi@0 212 "@Target({TYPE_USE,TYPE_PARAMETER,_OTHER_})\n" +
aoqi@0 213 "@Repeatable(DC.class)\n" +
aoqi@0 214 "@interface D { }\n\n" +
aoqi@0 215
aoqi@0 216 "@Retention("+retentn+")\n" +
aoqi@0 217 "@Target({TYPE_USE,TYPE_PARAMETER,_OTHER_})\n" +
aoqi@0 218 "@interface DC { D[] value(); }\n");
aoqi@0 219
aoqi@0 220 // Test case sources with sample generated source.
aoqi@0 221 switch(src) {
aoqi@0 222 case src1: // repeating type annotations at class level
aoqi@0 223 /*
aoqi@0 224 * @A @B class Test1 {
aoqi@0 225 * @A @B Test1(){}
aoqi@0 226 * @A @B Integer i1 = 0;
aoqi@0 227 * String @A @B [] @A @B [] sa = null;
aoqi@0 228 * // type usage in method body
aoqi@0 229 * String test(Test1 this, String param, String ... vararg) {
aoqi@0 230 * Object o = new String [3];
aoqi@0 231 * return (String) null;
aoqi@0 232 * }}
aoqi@0 233 */
aoqi@0 234 source = new String(
aoqi@0 235 "// " + src.description + "\n" +
aoqi@0 236 "_As_ _Bs_ class " + testname + " {\n" +
aoqi@0 237 "_As_ _Bs_ " + testname +"(){} \n" +
aoqi@0 238 "_As_ _Bs_ Integer i1 = 0; \n" +
aoqi@0 239 "String _As_ _Bs_ [] _As_ _Bs_ [] sa = null; \n" +
aoqi@0 240 "// type usage in method body \n" +
aoqi@0 241 "String test("+testname+" this, " +
aoqi@0 242 "String param, String ... vararg) { \n" +
aoqi@0 243 " Object o = new String [3]; \n" +
aoqi@0 244 " return (String) null; \n" +
aoqi@0 245 "}\n" +
aoqi@0 246 "}\n\n").concat(sourceBase).replace("_OTHER_", annot2).replace("_As_",As).replace("_Bs_",Bs) +
aoqi@0 247 "\n";
aoqi@0 248 break;
aoqi@0 249 case src2: // (repeating) type annotations on method.
aoqi@0 250 /*
aoqi@0 251 * class Test12 {
aoqi@0 252 * Test12(){}
aoqi@0 253 * // type usage on method
aoqi@0 254 * @A @B String test(@A @B Test12 this, @A @B String param, @A @B String @A @B ... vararg) {
aoqi@0 255 * Object o = new String [3];
aoqi@0 256 * return (String) null;
aoqi@0 257 * }}
aoqi@0 258 */
aoqi@0 259 source = new String(
aoqi@0 260 "// " + src.description + "\n" +
aoqi@0 261 "class " + testname + " {\n" +
aoqi@0 262 testname +"(){} \n" +
aoqi@0 263 "// type usage on method \n" +
aoqi@0 264 "_As_ _Bs_ String test(_As_ _Bs_ "+testname+" this, " +
aoqi@0 265 "_As_ _Bs_ String param, _As_ _Bs_ String _As_ _Bs_ ... vararg) { \n" +
aoqi@0 266 " Object o = new String [3]; \n" +
aoqi@0 267 " return (String) null; \n" +
aoqi@0 268 "}\n" +
aoqi@0 269 "}\n\n").concat(sourceBase).replace("_OTHER_", annot2).replace("_As_",As).replace("_Bs_",Bs) +
aoqi@0 270 "\n";
aoqi@0 271 break;
aoqi@0 272 case src3: //(repeating) annotations on wildcard, type arguments in anonymous class.
aoqi@0 273 /*
aoqi@0 274 * class Test13<T extends Object> {
aoqi@0 275 * public T data = null;
aoqi@0 276 * T getData() { return data;}
aoqi@0 277 * String mtest( Test13<String> t){ return t.getData(); }
aoqi@0 278 * public void test() {
aoqi@0 279 * mtest( new Test13<String>() {
aoqi@0 280 * void m1(List<@A @B ? extends @A @B Object> lst) {}
aoqi@0 281 * void m2() throws@A @B Exception { }
aoqi@0 282 * });
aoqi@0 283 * }
aoqi@0 284 * }
aoqi@0 285 */
aoqi@0 286 source = new String( source +
aoqi@0 287 "// " + src.description + "\n" +
aoqi@0 288 "class " + testname + "<T extends Object> {\n" +
aoqi@0 289 " public T data = null;\n" +
aoqi@0 290 " T getData() { return data;}\n" +
aoqi@0 291 " String mtest( " + testname + "<String> t){ return t.getData(); }\n" +
aoqi@0 292 " public void test() {\n" +
aoqi@0 293 " mtest( new " + testname + "<String>() {\n" +
aoqi@0 294 " void m1(List<_As_ _Bs_ ? extends _As_ _Bs_ Object> lst) {}\n" +
aoqi@0 295 " void m2() throws_As_ _Bs_ Exception { }\n" +
aoqi@0 296 " });\n" +
aoqi@0 297 " }\n" +
aoqi@0 298 "}\n\n").concat(sourceBase).replace("_OTHER_", annot2).replace("_As_",As).replace("_Bs_",Bs) +
aoqi@0 299 "\n";
aoqi@0 300 hasInnerClass=true;
aoqi@0 301 innerClassname="$1";
aoqi@0 302 break;
aoqi@0 303 case src4: // (repeating)annotations on type parameters, bounds and type arguments on class decl.
aoqi@0 304 /*
aoqi@0 305 * @A @B @D
aoqi@0 306 * class Test2<@A @B @C @D T extends @A @B Object> {
aoqi@0 307 * Map<List<String>, Integer> map =
aoqi@0 308 * new HashMap<List< String>, Integer>();
aoqi@0 309 * Map<List<String>,Integer> map2 = new HashMap<>();
aoqi@0 310 * String test(Test2<T> this) { return null;}
aoqi@0 311 * <T> String genericMethod(T t) { return null; }
aoqi@0 312 * }
aoqi@0 313 */
aoqi@0 314 source = new String( source +
aoqi@0 315 "// " + src.description + "\n" +
aoqi@0 316 "_As_ _Bs_ _Ds_\n" + //8004829: A and B on type parameter below.
aoqi@0 317 "class " + testname + "<_As_ _Bs_ @C _Ds_ T extends _As_ _Bs_ Object> {\n" +
aoqi@0 318 " Map<List<String>, Integer> map =\n" +
aoqi@0 319 " new HashMap<List< String>, Integer>();\n" +
aoqi@0 320 " Map<List<String>,Integer> map2 = new HashMap<>();\n" +
aoqi@0 321 " String test(" + testname + "<T> this) { return null;}\n" +
aoqi@0 322 " <T> String genericMethod(T t) { return null; }\n" +
aoqi@0 323 "}\n\n").concat(sourceBase).replace("_OTHER_", annot2).replace("_As_",As).replace("_Bs_",Bs).replace("_Ds_",Ds) +
aoqi@0 324 "\n";
aoqi@0 325 break;
aoqi@0 326 case src5: // (repeating) annotations on type parameters, bounds and type arguments on method.
aoqi@0 327 /*
aoqi@0 328 * class Test14<T extends Object> {
aoqi@0 329 * Map<List<String>, Integer> map =
aoqi@0 330 * new HashMap<List<String>, Integer>();
aoqi@0 331 * Map<List<String>, Integer> map2 = new HashMap<>();
aoqi@0 332 * String test(@A @B Test14<@D T> this) { return null;}
aoqi@0 333 * <@C @D T> @A @B String genericMethod(@A @B @D T t) { return null; }
aoqi@0 334 * }
aoqi@0 335 */
aoqi@0 336 source = new String( source +
aoqi@0 337 "// " + src.description + "\n" +
aoqi@0 338 "class " + testname + "<T extends Object> {\n" +
aoqi@0 339 " Map<List<String>, Integer> map =\n" +
aoqi@0 340 " new HashMap<List<String>, Integer>();\n" +
aoqi@0 341 " Map<List<String>, Integer> map2 = new HashMap<>();\n" +
aoqi@0 342 " String test(_As_ _Bs_ " + testname + "<_Ds_ T> this) { return null;}\n" +
aoqi@0 343 " <@C _Ds_ T> _As_ _Bs_ String genericMethod(_As_ _Bs_ _Ds_ T t) { return null; }\n" +
aoqi@0 344 "}\n\n").concat(sourceBase).replace("_OTHER_", annot2).replace("_As_",As).replace("_Bs_",Bs).replace("_Ds_",Ds) +
aoqi@0 345 "\n";
aoqi@0 346 break;
aoqi@0 347 case src6: // repeating annotations on type parameters, bounds and type arguments in method.
aoqi@0 348 /*
aoqi@0 349 * class Test7{
aoqi@0 350 * <E extends Comparable> Map<List<E>, E > foo(E e) {
aoqi@0 351 * class maptest <@A @B @D E> {
aoqi@0 352 * Map<List<@A @B @D E>,@A @B @D E> getMap() {
aoqi@0 353 * return new HashMap<List<E>,E>();
aoqi@0 354 * }
aoqi@0 355 * }
aoqi@0 356 * return new maptest<E>().getMap();
aoqi@0 357 * }
aoqi@0 358 * Map<List<String>,String> shm = foo(new String("hello"));
aoqi@0 359 * }
aoqi@0 360 */
aoqi@0 361 source = new String( source +
aoqi@0 362 "// " + src.description + "\n" +
aoqi@0 363 "class "+ testname + "{\n" +
aoqi@0 364 " <E extends Comparable> Map<List<E>, E > foo(E e) {\n" +
aoqi@0 365 " class maptest <_As_ _Bs_ _Ds_ E> {\n" + // inner class $1maptest
aoqi@0 366 " Map<List<_As_ _Bs_ _Ds_ E>,_As_ _Bs_ _Ds_ E> getMap() { \n" +
aoqi@0 367 " return new HashMap<List<E>,E>();\n" +
aoqi@0 368 " }\n" +
aoqi@0 369 " }\n" +
aoqi@0 370 " return new maptest<E>().getMap();\n" +
aoqi@0 371 " }\n" +
aoqi@0 372 " Map<List<String>,String> shm = foo(new String(\"hello\"));\n" +
aoqi@0 373 "}\n\n").concat(sourceBase).replace("_OTHER_", annot2).replace("_As_",As).replace("_Bs_",Bs).replace("_Ds_",Ds) +
aoqi@0 374 "\n";
aoqi@0 375 hasInnerClass=true;
aoqi@0 376 innerClassname="$1maptest";
aoqi@0 377 break;
aoqi@0 378 }
aoqi@0 379 return imports + source;
aoqi@0 380 }
aoqi@0 381 }

mercurial