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

Wed, 29 May 2013 10:16:32 -0700

author
katleman
date
Wed, 29 May 2013 10:16:32 -0700
changeset 1768
149890642a0e
parent 1755
ddb4a2bfcd82
child 2525
2eb010b6cb22
permissions
-rw-r--r--

8015525: JDK8 b91 source with GPL header errors
Reviewed-by: dholmes, lancea

jjg@1755 1 /*
katleman@1768 2 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
jjg@1755 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jjg@1755 4 *
jjg@1755 5 * This code is free software; you can redistribute it and/or modify it
jjg@1755 6 * under the terms of the GNU General Public License version 2 only, as
jjg@1755 7 * published by the Free Software Foundation.
jjg@1755 8 *
jjg@1755 9 * This code is distributed in the hope that it will be useful, but WITHOUT
jjg@1755 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jjg@1755 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
jjg@1755 12 * version 2 for more details (a copy is included in the LICENSE file that
jjg@1755 13 * accompanied this code).
jjg@1755 14 *
jjg@1755 15 * You should have received a copy of the GNU General Public License version
jjg@1755 16 * 2 along with this work; if not, write to the Free Software Foundation,
jjg@1755 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jjg@1755 18 *
jjg@1755 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jjg@1755 20 * or visit www.oracle.com if you need additional information or have any
jjg@1755 21 * questions.
jjg@1755 22 */
jjg@1755 23
jjg@1755 24 /*
jjg@1755 25 * @test
jjg@1755 26 * @bug 8005681
jjg@1755 27 * @summary Repeated annotations on new,array,cast.
jjg@1755 28 */
jjg@1755 29 import java.lang.annotation.*;
jjg@1755 30 import java.io.*;
jjg@1755 31 import java.util.List;
jjg@1755 32 import com.sun.tools.classfile.*;
jjg@1755 33
jjg@1755 34 import java.lang.annotation.*;
jjg@1755 35 import static java.lang.annotation.RetentionPolicy.*;
jjg@1755 36 import static java.lang.annotation.ElementType.*;
jjg@1755 37
jjg@1755 38 public class TestNewCastArray {
jjg@1755 39 int errors = 0;
jjg@1755 40 List<String> failedTests = new java.util.LinkedList<>();
jjg@1755 41
jjg@1755 42 // 'b' tests fail with only even numbers of annotations (8005681).
jjg@1755 43 String[] testclasses = {"Test1",
jjg@1755 44 "Test2a", "Test3a", "Test4a", "Test5a",
jjg@1755 45 "Test2b", "Test3b", "Test4b", "Test5b"
jjg@1755 46 };
jjg@1755 47
jjg@1755 48 public static void main(String[] args) throws Exception {
jjg@1755 49 new TestNewCastArray().run();
jjg@1755 50 }
jjg@1755 51
jjg@1755 52 void check(String testcase, int expected, int actual) {
jjg@1755 53 String res = testcase + ": (expected) " + expected + ", " + actual + " (actual): ";
jjg@1755 54 if(expected == actual) {
jjg@1755 55 res = res.concat("PASS");
jjg@1755 56 } else {
jjg@1755 57 errors++;
jjg@1755 58 res = res.concat("FAIL");
jjg@1755 59 failedTests.add(res);
jjg@1755 60 }
jjg@1755 61 System.out.println(res);
jjg@1755 62 }
jjg@1755 63
jjg@1755 64 void report() {
jjg@1755 65 if(errors!=0) {
jjg@1755 66 System.err.println("Failed tests: " + errors +
jjg@1755 67 "\nfailed test cases:\n");
jjg@1755 68 for(String t: failedTests)
jjg@1755 69 System.err.println(" " + t);
jjg@1755 70 throw new RuntimeException("FAIL: There were test failures.");
jjg@1755 71 } else
jjg@1755 72 System.out.println("PASS");
jjg@1755 73 }
jjg@1755 74
jjg@1755 75 void test(String clazz, String ttype, ClassFile cf, Method m, Field f,
jjg@1755 76 String name, boolean codeattr) {
jjg@1755 77 int actual = 0;
jjg@1755 78 int expected = 0, cexpected = 0;
jjg@1755 79 int index = 0;
jjg@1755 80 String memberName = null;
jjg@1755 81 Attribute attr = null;
jjg@1755 82 Code_attribute cAttr = null;
jjg@1755 83 String testcase = "undefined";
jjg@1755 84 try {
jjg@1755 85 switch(ttype) {
jjg@1755 86 case "METHOD":
jjg@1755 87 index = m.attributes.getIndex(cf.constant_pool, name);
jjg@1755 88 memberName = m.getName(cf.constant_pool);
jjg@1755 89 if(index != -1)
jjg@1755 90 attr = m.attributes.get(index);
jjg@1755 91 break;
jjg@1755 92 case "MCODE":
jjg@1755 93 memberName = m.getName(cf.constant_pool);
jjg@1755 94 //fetch index of and code attribute and annotations from code attribute.
jjg@1755 95 index = m.attributes.getIndex(cf.constant_pool, Attribute.Code);
jjg@1755 96 if(index!= -1) {
jjg@1755 97 attr = m.attributes.get(index);
jjg@1755 98 assert attr instanceof Code_attribute;
jjg@1755 99 cAttr = (Code_attribute)attr;
jjg@1755 100 index = cAttr.attributes.getIndex(cf.constant_pool, name);
jjg@1755 101 if(index!= -1)
jjg@1755 102 attr = cAttr.attributes.get(index);
jjg@1755 103 }
jjg@1755 104 break;
jjg@1755 105 case "FIELD":
jjg@1755 106 index = f.attributes.getIndex(cf.constant_pool, name);
jjg@1755 107 memberName = f.getName(cf.constant_pool);
jjg@1755 108 if(index != -1)
jjg@1755 109 attr = f.attributes.get(index);
jjg@1755 110 break;
jjg@1755 111 case "CODE":
jjg@1755 112 memberName = f.getName(cf.constant_pool);
jjg@1755 113 //fetch index of and code attribute and annotations from code attribute.
jjg@1755 114 index = cf.attributes.getIndex(cf.constant_pool, Attribute.Code);
jjg@1755 115 if(index!= -1) {
jjg@1755 116 attr = cf.attributes.get(index);
jjg@1755 117 assert attr instanceof Code_attribute;
jjg@1755 118 cAttr = (Code_attribute)attr;
jjg@1755 119 index = cAttr.attributes.getIndex(cf.constant_pool, name);
jjg@1755 120 if(index!= -1)
jjg@1755 121 attr = cAttr.attributes.get(index);
jjg@1755 122 }
jjg@1755 123 break;
jjg@1755 124 default:
jjg@1755 125 break;
jjg@1755 126 }
jjg@1755 127 } catch(ConstantPoolException cpe) { cpe.printStackTrace(); }
jjg@1755 128 testcase = clazz+" "+ttype + ": " + memberName + ", " + name;
jjg@1755 129 if(index != -1) {
jjg@1755 130 //count RuntimeTypeAnnotations
jjg@1755 131 assert attr instanceof RuntimeTypeAnnotations_attribute;
jjg@1755 132 RuntimeTypeAnnotations_attribute tAttr =
jjg@1755 133 (RuntimeTypeAnnotations_attribute)attr;
jjg@1755 134 actual += tAttr.annotations.length;
jjg@1755 135 }
jjg@1755 136 if(memberName.compareTo("<init>")==0) memberName=clazz+memberName;
jjg@1755 137 switch ( memberName ) {
jjg@1755 138 //METHOD:
jjg@1755 139 case "Test1<init>": expected=0; break;
jjg@1755 140 case "testr22_22": expected=4; break;
jjg@1755 141 case "testr11_11": expected=4; break;
jjg@1755 142 case "testr12_21": expected=4; break;
jjg@1755 143 case "testr20_02": expected=2; break;
jjg@1755 144
jjg@1755 145 case "Test2a<init>": cexpected=0; break;
jjg@1755 146 case "test00_00_11_11": cexpected=4; break;
jjg@1755 147 case "test21_12_21_12": cexpected=8; break;
jjg@1755 148 case "test_new1": cexpected=2; break;
jjg@1755 149 case "test_new2": cexpected=2; break;
jjg@1755 150 case "test_cast1": cexpected=2; break;
jjg@1755 151 case "test_cast2": cexpected=2; break;
jjg@1755 152
jjg@1755 153 case "Test2b<init>": cexpected=0; break;
jjg@1755 154 case "test20_02_20_02": cexpected=4; break;
jjg@1755 155 case "test22_22_22_22": cexpected=8; break;
jjg@1755 156 case "test_new3": cexpected=1; break;
jjg@1755 157 case "test_new4": cexpected=1; break;
jjg@1755 158 case "test_new5": cexpected=2; break;
jjg@1755 159 case "test_cast3": cexpected=1; break;
jjg@1755 160 case "test_cast4": cexpected=2; break;
jjg@1755 161
jjg@1755 162 case "Test3a<init>": cexpected=10; break;
jjg@1755 163 case "SA_21_12c": cexpected = 0; break;
jjg@1755 164 case "SA_01_10c": expected = 0; break;
jjg@1755 165 case "SA_11_11c": expected = 0; break;
jjg@1755 166
jjg@1755 167 case "Test3b<init>": cexpected=6; break;
jjg@1755 168 case "SA_22_22c": cexpected = 0; break;
jjg@1755 169 case "SA_20_02c": cexpected = 0; break;
jjg@1755 170
jjg@1755 171 case "Test3c<init>": cexpected=8; break;
jjg@1755 172 case "SA_10_10": cexpected = 0; break;
jjg@1755 173 case "SA_10_01": cexpected = 0; break;
jjg@1755 174 case "SA_21_12": cexpected = 0; break;
jjg@1755 175
jjg@1755 176 case "Test3d<init>": cexpected=6; break;
jjg@1755 177 case "SA_20_02": cexpected = 0; break;
jjg@1755 178 case "SA_22_22": cexpected = 0; break;
jjg@1755 179
jjg@1755 180 case "Test4a<init>": cexpected=4; break;
jjg@1755 181 case "nS_21": cexpected = 0; break;
jjg@1755 182 case "nS_12": cexpected = 0; break;
jjg@1755 183
jjg@1755 184 case "Test4b<init>": cexpected=4; break;
jjg@1755 185 case "nS20": cexpected = 0; break;
jjg@1755 186 case "nS02": cexpected = 0; break;
jjg@1755 187 case "nS22": cexpected = 0; break;
jjg@1755 188
jjg@1755 189 case "Test5a<init>": cexpected=4; break;
jjg@1755 190 case "ci11": expected = 0; break;
jjg@1755 191 case "ci21": expected = 0; break;
jjg@1755 192
jjg@1755 193 case "Test5b<init>": cexpected=3; break;
jjg@1755 194 case "ci2": expected = 0; break;
jjg@1755 195 case "ci22": expected = 0; break;
jjg@1755 196
jjg@1755 197 default: expected = 0; break;
jjg@1755 198 }
jjg@1755 199 if(codeattr)
jjg@1755 200 check(testcase, cexpected, actual);
jjg@1755 201 else
jjg@1755 202 check(testcase, expected, actual);
jjg@1755 203 }
jjg@1755 204
jjg@1755 205 public void run() {
jjg@1755 206 ClassFile cf = null;
jjg@1755 207 InputStream in = null;
jjg@1755 208 for( String clazz : testclasses) {
jjg@1755 209 String testclazz = "TestNewCastArray$" + clazz + ".class";
jjg@1755 210 System.out.println("Testing " + testclazz);
jjg@1755 211 try {
jjg@1755 212 in = getClass().getResource(testclazz).openStream();
jjg@1755 213 cf = ClassFile.read(in);
jjg@1755 214 in.close();
jjg@1755 215 } catch(Exception e) { e.printStackTrace(); }
jjg@1755 216
jjg@1755 217 if(clazz.startsWith("Test1")) {
jjg@1755 218 for (Field f: cf.fields)
jjg@1755 219 test(clazz, "FIELD", cf, null, f, Attribute.RuntimeVisibleTypeAnnotations, false);
jjg@1755 220 for (Method m: cf.methods)
jjg@1755 221 test(clazz, "METHOD", cf, m, null, Attribute.RuntimeVisibleTypeAnnotations, false);
jjg@1755 222 } else {
jjg@1755 223 for (Field f: cf.fields)
jjg@1755 224 test(clazz, "CODE", cf, null, f, Attribute.RuntimeVisibleTypeAnnotations, true);
jjg@1755 225 for (Method m: cf.methods)
jjg@1755 226 test(clazz, "MCODE", cf, m, null, Attribute.RuntimeVisibleTypeAnnotations, true);
jjg@1755 227 }
jjg@1755 228 }
jjg@1755 229 report();
jjg@1755 230 }
jjg@1755 231
jjg@1755 232 //////// test class //////////////////////////
jjg@1755 233 // "Test1" not in code attribute.
jjg@1755 234 // on arrays on and in method return
jjg@1755 235 static class Test1 {
jjg@1755 236 Test1(){}
jjg@1755 237 // OK expect 5, got 5
jjg@1755 238 String @A @A @B @B[] @A @A @B @B [] testr22_22(Test1 this, String param, String ... vararg) {
jjg@1755 239 String [][] sarray = new String [2][2];
jjg@1755 240 return sarray;
jjg@1755 241 }
jjg@1755 242 // OK expect 5, got 5
jjg@1755 243 String @A @B [] @A @B [] testr11_11(Test1 this, String param, String ... vararg) {
jjg@1755 244 String [][] sarray = new String [2][2];
jjg@1755 245 return sarray;
jjg@1755 246 }
jjg@1755 247 // OK expect 5, got 5
jjg@1755 248 String @A @B @B []@B @B @A[] testr12_21(Test1 this, String param, String ... vararg) {
jjg@1755 249 String [][] sarray = new String [2][2];
jjg@1755 250 return sarray;
jjg@1755 251 }
jjg@1755 252 // OK expect 3, got 3
jjg@1755 253 String @A @A [] @B @B [] testr20_02(Test1 this, String param, String ... vararg) {
jjg@1755 254 String [][] sarray = new String [2][2];
jjg@1755 255 return sarray;
jjg@1755 256 }
jjg@1755 257 }
jjg@1755 258
jjg@1755 259 // Inside method body (in method's code attribute)
jjg@1755 260 static class Test2a {
jjg@1755 261 Test2a(){}
jjg@1755 262 Object o = new Integer(1);
jjg@1755 263 // expect 4
jjg@1755 264 String[][] test00_00_11_11(Test2a this, String param, String ... vararg) {
jjg@1755 265 String [] [] sarray = new String @A @B[2] @A @B [2];
jjg@1755 266 return sarray;
jjg@1755 267 }
jjg@1755 268
jjg@1755 269 // expect 8
jjg@1755 270 String[][] test21_12_21_12(Test2a this, String param, String ... vararg) {
jjg@1755 271 String @A @A @B [] @A @B @B [] sarray = new String @A @A @B[2] @A @B @B [2];
jjg@1755 272 return sarray;
jjg@1755 273 }
jjg@1755 274
jjg@1755 275 void test_new1() { String nS_21 = new @A @A @B String("Hello"); }
jjg@1755 276 void test_new2() { String nS_12 = new @A @B @B String("Hello"); }
jjg@1755 277 void test_cast1() { String tcs11 = (@A @B String)o; }
jjg@1755 278 void test_cast2() { String tcs21 = (@A @A @B String)o; }
jjg@1755 279 }
jjg@1755 280
jjg@1755 281 static class Test2b {
jjg@1755 282 Test2b(){}
jjg@1755 283 Object o = new Integer(1);
jjg@1755 284 // expect 4
jjg@1755 285 String[][] test20_02_20_02(Test2b this, String param, String ... vararg) {
jjg@1755 286 String @A @A [] @B @B [] sarray = new String @A @A[2] @B @B [2];
jjg@1755 287 return sarray;
jjg@1755 288 }
jjg@1755 289
jjg@1755 290 // expect 8
jjg@1755 291 String[][] test22_22_22_22(Test2b this, String param, String ... vararg) {
jjg@1755 292 String @A @A @B @B [] @A @A @B @B [] sarray = new String @A @A @B @B [2] @A @A @B @B [2];
jjg@1755 293 return sarray;
jjg@1755 294 }
jjg@1755 295
jjg@1755 296 void test_new3() { String nS20 = new @A @A String("Hello"); }
jjg@1755 297 void test_new4() { String nS02 = new @B @B String("Hello"); }
jjg@1755 298 void test_new5() { String nS22 = new @A @A @B @B String("Hello"); }
jjg@1755 299 void test_cast3() { String tcs2 = (@A @A String)o; }
jjg@1755 300 void test_cast4() { String tcs22 = (@A @A @B @B String)o;}
jjg@1755 301 }
jjg@1755 302
jjg@1755 303 // array levels
jjg@1755 304 static class Test3a {
jjg@1755 305 Test3a(){}
jjg@1755 306 // expect 4+2+4=10
jjg@1755 307 String [][] SA_21_12c = new String @A @A @B [2] @A @B @B[2];
jjg@1755 308 String [][] SA_01_10c = new String @B [2] @A [2];
jjg@1755 309 String [][] SA_11_11c = new String @A @B [2] @A @B [2];
jjg@1755 310 }
jjg@1755 311
jjg@1755 312 static class Test3b {
jjg@1755 313 Test3b(){}
jjg@1755 314 // expect 4+2=6
jjg@1755 315 String [][] SA_22_22c = new String @A @A @B @B[2] @A @A @B @B[2];
jjg@1755 316 String [][] SA_20_02c = new String @A @A [2] @B @B[2];
jjg@1755 317 }
jjg@1755 318 static class Test3c {
jjg@1755 319 Test3c(){}
jjg@1755 320 // OK expect 4
jjg@1755 321 String @A [] @A[] SA_10_10 = new String [2][2];
jjg@1755 322 String @A [] @B[] SA_10_01 = new String [2][2];
jjg@1755 323 String @A @A @B[] @A @B @B [] SA_21_12 = new String [2][2];
jjg@1755 324 }
jjg@1755 325
jjg@1755 326 static class Test3d {
jjg@1755 327 Test3d(){}
jjg@1755 328 // OK expect 4
jjg@1755 329 String @A @A [] @B @B [] SA_20_02 = new String [2][2];
jjg@1755 330 String @A @A @B @B[] @A @A @B @B [] SA_22_22 = new String [2][2];
jjg@1755 331 }
jjg@1755 332
jjg@1755 333 // on new
jjg@1755 334 static class Test4a {
jjg@1755 335 Test4a(){}
jjg@1755 336 // expect 2+2=4
jjg@1755 337 String nS_21 = new @A @A @B String("Hello");
jjg@1755 338 String nS_12 = new @A @B @B String("Hello");
jjg@1755 339 }
jjg@1755 340
jjg@1755 341 static class Test4b {
jjg@1755 342 Test4b(){}
jjg@1755 343 // expect 1+1+2=4
jjg@1755 344 String nS20 = new @A @A String("Hello");
jjg@1755 345 String nS02 = new @B @B String("Hello");
jjg@1755 346 String nS22 = new @A @A @B @B String("Hello");
jjg@1755 347 }
jjg@1755 348
jjg@1755 349 // Cast expressions
jjg@1755 350 static class Test5a {
jjg@1755 351 Test5a(){}
jjg@1755 352 Object o = new Integer(1);
jjg@1755 353 // expect 2+2=4
jjg@1755 354 Integer ci11 = (@A @B Integer)o; // OK expect 3, got 3
jjg@1755 355 Integer ci21 = (@A @A @B Integer)o; // OK expect 3, got 3
jjg@1755 356 }
jjg@1755 357
jjg@1755 358 static class Test5b {
jjg@1755 359 Test5b(){}
jjg@1755 360 Object o = new Integer(1);
jjg@1755 361 // Cast expressions
jjg@1755 362 // expect 1+2=3
jjg@1755 363 Integer ci2 = (@A @A Integer)o; // FAIL expect 2, got 1
jjg@1755 364 Integer ci22 = (@A @A @B @B Integer)o; // FAIL expect 3, got 1
jjg@1755 365 }
jjg@1755 366
jjg@1755 367 @Retention(RUNTIME) @Target({TYPE_USE}) @Repeatable( AC.class ) @interface A { }
jjg@1755 368 @Retention(RUNTIME) @Target({TYPE_USE}) @Repeatable( BC.class ) @interface B { }
jjg@1755 369 @Retention(RUNTIME) @Target({FIELD}) @Repeatable( FC.class ) @interface F { }
jjg@1755 370 @Retention(RUNTIME) @Target({TYPE_USE}) @interface AC { A[] value(); }
jjg@1755 371 @Retention(RUNTIME) @Target({TYPE_USE}) @interface BC { B[] value(); }
jjg@1755 372 @Retention(RUNTIME) @Target({FIELD}) @interface FC { F[] value(); }
jjg@1755 373
jjg@1755 374 }
jjg@1755 375

mercurial