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