jjg@1521: /* jjg@1521: * Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved. jjg@1521: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jjg@1521: * jjg@1521: * This code is free software; you can redistribute it and/or modify it jjg@1521: * under the terms of the GNU General Public License version 2 only, as jjg@1521: * published by the Free Software Foundation. jjg@1521: * jjg@1521: * This code is distributed in the hope that it will be useful, but WITHOUT jjg@1521: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jjg@1521: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jjg@1521: * version 2 for more details (a copy is included in the LICENSE file that jjg@1521: * accompanied this code). jjg@1521: * jjg@1521: * You should have received a copy of the GNU General Public License version jjg@1521: * 2 along with this work; if not, write to the Free Software Foundation, jjg@1521: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jjg@1521: * jjg@1521: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jjg@1521: * or visit www.oracle.com if you need additional information or have any jjg@1521: * questions. jjg@1521: */ jjg@1521: jjg@1521: import java.io.*; jjg@1521: import com.sun.tools.classfile.*; jjg@1521: jjg@1521: /* jjg@1521: * @test NewArray jjg@1521: * @bug 6843077 jjg@1755: * @summary Test type annotations on local array are in method's code attribute. jjg@1521: */ jjg@1521: jjg@1521: public class NewArray { jjg@1755: jjg@1521: public static void main(String[] args) throws Exception { jjg@1521: new NewArray().run(); jjg@1521: } jjg@1521: jjg@1521: public void run() throws Exception { jjg@1521: File javaFile = writeTestFile(); jjg@1521: File classFile = compileTestFile(javaFile); jjg@1521: jjg@1521: ClassFile cf = ClassFile.read(classFile); jjg@1521: for (Method m: cf.methods) { jjg@1521: test(cf, m); jjg@1521: } jjg@1521: jjg@1521: countAnnotations(); jjg@1521: jjg@1521: if (errors > 0) jjg@1521: throw new Exception(errors + " errors found"); jjg@1521: System.out.println("PASSED"); jjg@1521: } jjg@1521: jjg@1521: void test(ClassFile cf, Method m) { jjg@1521: test(cf, m, Attribute.RuntimeVisibleTypeAnnotations, true); jjg@1521: test(cf, m, Attribute.RuntimeInvisibleTypeAnnotations, false); jjg@1521: } jjg@1521: jjg@1521: // test the result of Attributes.getIndex according to expectations jjg@1521: // encoded in the method's name jjg@1755: void test(ClassFile cf, Method m, String name, boolean visible) { jjg@1755: Attribute attr = null; jjg@1755: Code_attribute cAttr = null; jjg@1755: RuntimeTypeAnnotations_attribute tAttr = null; jjg@1521: jjg@1755: int index = m.attributes.getIndex(cf.constant_pool, Attribute.Code); jjg@1755: if(index!= -1) { jjg@1755: attr = m.attributes.get(index); jjg@1755: assert attr instanceof Code_attribute; jjg@1755: cAttr = (Code_attribute)attr; jjg@1755: index = cAttr.attributes.getIndex(cf.constant_pool, name); jjg@1755: if(index!= -1) { jjg@1755: attr = cAttr.attributes.get(index); jjg@1755: assert attr instanceof RuntimeTypeAnnotations_attribute; jjg@1755: tAttr = (RuntimeTypeAnnotations_attribute)attr; jjg@1755: all += tAttr.annotations.length; jjg@1755: if (visible) jjg@1755: visibles += tAttr.annotations.length; jjg@1755: else jjg@1755: invisibles += tAttr.annotations.length; jjg@1755: } jjg@1521: } jjg@1521: } jjg@1521: jjg@1521: File writeTestFile() throws IOException { jjg@1521: File f = new File("Test.java"); jjg@1521: PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(f))); jjg@1521: out.println("import java.lang.annotation.*;"); jjg@1521: out.println("import java.util.*;"); jjg@1521: out.println("class Test { "); jjg@1755: out.println(" @Target(ElementType.TYPE_USE) @interface A { }"); jjg@1755: out.println(" void test() {"); jjg@1755: out.println(" Object a = new @A String @A [5] @A [];"); jjg@1755: out.println(" Object b = new @A String @A [5] @A [3];"); jjg@1755: out.println(" Object c = new @A String @A [] @A [] {};"); jjg@1755: out.println(" }"); jjg@1521: out.println("}"); jjg@1521: jjg@1521: out.close(); jjg@1521: return f; jjg@1521: } jjg@1521: jjg@1521: File compileTestFile(File f) { jjg@1521: int rc = com.sun.tools.javac.Main.compile(new String[] { "-source", "1.8", "-g", f.getPath() }); jjg@1521: if (rc != 0) jjg@1521: throw new Error("compilation failed. rc=" + rc); jjg@1521: String path = f.getPath(); jjg@1521: return new File(path.substring(0, path.length() - 5) + ".class"); jjg@1521: } jjg@1521: jjg@1521: void countAnnotations() { jjg@1521: int expected_visibles = 0, expected_invisibles = 9; jjg@1521: int expected_all = expected_visibles + expected_invisibles; jjg@1521: jjg@1521: if (expected_all != all) { jjg@1521: errors++; jjg@1521: System.err.println("expected " + expected_all jjg@1521: + " annotations but found " + all); jjg@1521: } jjg@1521: jjg@1521: if (expected_visibles != visibles) { jjg@1521: errors++; jjg@1521: System.err.println("expected " + expected_visibles jjg@1521: + " visibles annotations but found " + visibles); jjg@1521: } jjg@1521: jjg@1521: if (expected_invisibles != invisibles) { jjg@1521: errors++; jjg@1521: System.err.println("expected " + expected_invisibles jjg@1521: + " invisibles annotations but found " + invisibles); jjg@1521: } jjg@1521: jjg@1521: } jjg@1521: jjg@1521: int errors; jjg@1521: int all; jjg@1521: int visibles; jjg@1521: int invisibles; jjg@1521: }