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 java.lang.annotation.ElementType; jjg@1521: jjg@1521: import com.sun.tools.classfile.*; jjg@1521: jjg@1521: /* jjg@1521: * @test Presence jjg@1521: * @bug 6843077 jjg@1521: * @summary test that all type annotations are present in the classfile jjg@1521: */ jjg@1521: jjg@1521: public class Presence { jjg@1521: public static void main(String[] args) throws Exception { jjg@1521: new Presence().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: test(cf); jjg@1521: for (Field f : cf.fields) { jjg@1521: test(cf, f); jjg@1521: } 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) { jjg@1521: test(cf, Attribute.RuntimeVisibleTypeAnnotations, true); jjg@1521: test(cf, Attribute.RuntimeInvisibleTypeAnnotations, false); 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: void test(ClassFile cf, Field 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@1521: void test(ClassFile cf, String name, boolean visible) { jjg@1521: int index = cf.attributes.getIndex(cf.constant_pool, name); jjg@1521: if (index != -1) { jjg@1521: Attribute attr = cf.attributes.get(index); jjg@1521: assert attr instanceof RuntimeTypeAnnotations_attribute; jjg@1521: RuntimeTypeAnnotations_attribute tAttr = (RuntimeTypeAnnotations_attribute)attr; jjg@1521: all += tAttr.annotations.length; jjg@1521: if (visible) jjg@1521: visibles += tAttr.annotations.length; jjg@1521: else jjg@1521: invisibles += tAttr.annotations.length; jjg@1521: } jjg@1521: } jjg@1521: jjg@1521: // test the result of Attributes.getIndex according to expectations jjg@1521: // encoded in the method's name jjg@1521: 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@1755: jjg@1755: // collect annotations attributes on method jjg@1521: int index = m.attributes.getIndex(cf.constant_pool, name); jjg@1521: if (index != -1) { jjg@1755: attr = m.attributes.get(index); jjg@1521: assert attr instanceof RuntimeTypeAnnotations_attribute; jjg@1755: tAttr = (RuntimeTypeAnnotations_attribute)attr; jjg@1521: all += tAttr.annotations.length; jjg@1521: if (visible) jjg@1521: visibles += tAttr.annotations.length; jjg@1521: else jjg@1521: invisibles += tAttr.annotations.length; jjg@1521: } jjg@1755: // collect annotations from method's code attribute jjg@1755: 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@1755: } jjg@1521: } jjg@1521: jjg@1521: // test the result of Attributes.getIndex according to expectations jjg@1521: // encoded in the method's name jjg@1521: void test(ClassFile cf, Field m, String name, boolean visible) { jjg@1521: int index = m.attributes.getIndex(cf.constant_pool, name); jjg@1521: if (index != -1) { jjg@1521: Attribute attr = m.attributes.get(index); jjg@1521: assert attr instanceof RuntimeTypeAnnotations_attribute; jjg@1521: RuntimeTypeAnnotations_attribute tAttr = (RuntimeTypeAnnotations_attribute)attr; jjg@1521: all += tAttr.annotations.length; jjg@1521: if (visible) jjg@1521: visibles += tAttr.annotations.length; jjg@1521: else jjg@1521: invisibles += tAttr.annotations.length; jjg@1521: } jjg@1521: } jjg@1521: jjg@1521: File writeTestFile() throws IOException { jjg@1755: File f = new File("TestPresence.java"); jjg@1521: PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(f))); jjg@1521: out.println("import java.util.*;"); jjg@1521: out.println("import java.lang.annotation.*;"); jjg@1521: jjg@1755: out.println("class TestPresence<@TestPresence.A T extends @TestPresence.A List<@TestPresence.A String>> { "); jjg@1521: out.println(" @Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER})"); jjg@1521: out.println(" @interface A { }"); jjg@1521: jjg@1521: out.println(" Map<@A String, Map<@A String, @A String>> f1;"); jjg@1521: jjg@1521: out.println(" <@A TM extends @A List<@A String>>"); jjg@1521: out.println(" Map<@A String, @A List<@A String>>"); jjg@1755: out.println(" method(@A TestPresence this, List<@A String> @A [] param1, String @A [] @A ... param2)"); jjg@1521: out.println(" throws @A Exception {"); jjg@1521: out.println(" @A String lc1 = null;"); jjg@1521: out.println(" @A List<@A String> lc2 = null;"); jjg@1521: out.println(" @A String @A [] [] @A[] lc3 = null;"); jjg@1521: out.println(" List> lc4 = null;"); jjg@1521: out.println(" Object lc5 = (@A List<@A String>) null;"); jjg@1521: out.println(" boolean lc6 = lc1 instanceof @A String;"); jjg@1521: out.println(" boolean lc7 = lc5 instanceof @A String @A [] @A [];"); jjg@1521: out.println(" new @A ArrayList<@A String>();"); jjg@1521: out.println(" Object lc8 = new @A String @A [4];"); jjg@1521: out.println(" try {"); jjg@1521: out.println(" Object lc10 = int.class;"); jjg@1521: out.println(" } catch (@A Exception e) { e.toString(); }"); jjg@1521: out.println(" return null;"); jjg@1521: out.println(" }"); jjg@1521: out.println(" void vararg1(String @A ... t) { } "); jjg@1521: out.println("}"); 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 = 38; 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: }