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

Thu, 31 Jan 2013 12:16:03 -0800

author
darcy
date
Thu, 31 Jan 2013 12:16:03 -0800
changeset 1531
8e4c22acebeb
parent 1521
71f35e4b93a5
child 1534
bec996065c45
permissions
-rw-r--r--

8007313: Remove use of {ContainerFor/ContainedBy} from langtools
Reviewed-by: jjg

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

mercurial