1 /* 2 * Copyright (c) 2008, 2015, 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 import java.io.*; 25 import com.sun.tools.classfile.*; 26 27 /* 28 * @test NewArray 29 * @bug 6843077 30 * @summary Test type annotations on local array are in method's code attribute. 31 * @modules jdk.compiler/com.sun.tools.classfile 32 */ 33 34 public class NewArray { 35 36 public static void main(String[] args) throws Exception { 37 new NewArray().run(); 38 } 39 40 public void run() throws Exception { 41 File javaFile = writeTestFile(); 42 File classFile = compileTestFile(javaFile); 43 44 ClassFile cf = ClassFile.read(classFile); 45 for (Method m: cf.methods) { 46 test(cf, m); 47 } 48 49 countAnnotations(); 50 51 if (errors > 0) 52 throw new Exception(errors + " errors found"); 53 System.out.println("PASSED"); 54 } 55 56 void test(ClassFile cf, Method m) { 57 test(cf, m, Attribute.RuntimeVisibleTypeAnnotations, true); 58 test(cf, m, Attribute.RuntimeInvisibleTypeAnnotations, false); 59 } 60 61 // test the result of Attributes.getIndex according to expectations 62 // encoded in the method's name 63 void test(ClassFile cf, Method m, String name, boolean visible) { 64 Attribute attr = null; 65 Code_attribute cAttr = null; 66 RuntimeTypeAnnotations_attribute tAttr = null; 67 68 int index = m.attributes.getIndex(cf.constant_pool, Attribute.Code); 69 if(index!= -1) { 70 attr = m.attributes.get(index); 71 assert attr instanceof Code_attribute; 72 cAttr = (Code_attribute)attr; 73 index = cAttr.attributes.getIndex(cf.constant_pool, name); 74 if(index!= -1) { 75 attr = cAttr.attributes.get(index); 76 assert attr instanceof RuntimeTypeAnnotations_attribute; 77 tAttr = (RuntimeTypeAnnotations_attribute)attr; 78 all += tAttr.annotations.length; 79 if (visible) 80 visibles += tAttr.annotations.length; 81 else 82 invisibles += tAttr.annotations.length; 83 } 84 } 85 } 86 87 File writeTestFile() throws IOException { 88 File f = new File("Test.java"); 89 PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(f))); 90 out.println("import java.lang.annotation.*;"); 91 out.println("import java.util.*;"); 92 out.println("class Test { "); 93 out.println(" @Target(ElementType.TYPE_USE) @interface A { }"); 94 out.println(" void test() {"); 95 out.println(" Object a = new @A String @A [5] @A [];"); 96 out.println(" Object b = new @A String @A [5] @A [3];"); 97 out.println(" Object c = new @A String @A [] @A [] {};"); 98 out.println(" }"); 99 out.println("}"); 100 101 out.close(); 102 return f; 103 } 104 105 File compileTestFile(File f) { 106 int rc = com.sun.tools.javac.Main.compile(new String[] {"-g", f.getPath() }); 107 if (rc != 0) 108 throw new Error("compilation failed. rc=" + rc); 109 String path = f.getPath(); 110 return new File(path.substring(0, path.length() - 5) + ".class"); 111 } 112 113 void countAnnotations() { 114 int expected_visibles = 0, expected_invisibles = 9; 115 int expected_all = expected_visibles + expected_invisibles; 116 117 if (expected_all != all) { 118 errors++; 119 System.err.println("expected " + expected_all 120 + " annotations but found " + all); 121 } 122 123 if (expected_visibles != visibles) { 124 errors++; 125 System.err.println("expected " + expected_visibles 126 + " visibles annotations but found " + visibles); 127 } 128 129 if (expected_invisibles != invisibles) { 130 errors++; 131 System.err.println("expected " + expected_invisibles 132 + " invisibles annotations but found " + invisibles); 133 } 134 135 } 136 137 int errors; 138 int all; 139 int visibles; 140 int invisibles; 141 }