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