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 Visibility jjg@309: * @bug 6843077 jjg@309: * @summary test that type annotations are recorded in the classfile jjg@309: */ jjg@309: jjg@309: public class Visibility { jjg@309: public static void main(String[] args) throws Exception { jjg@309: new Visibility().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: 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, Method 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, 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: File writeTestFile() throws IOException { jjg@309: File f = new File("Test.java"); jjg@309: PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(f))); jjg@309: out.println("import java.lang.annotation.Retention;"); jjg@309: out.println("import java.lang.annotation.RetentionPolicy;"); jjg@309: out.println("abstract class Test { "); jjg@309: // visible annotations: RUNTIME jjg@309: out.println(" @Retention(RetentionPolicy.RUNTIME)"); jjg@309: out.println(" @interface A { }"); jjg@309: out.println(" void visible() @A { }"); jjg@309: jjg@309: // invisible annotations: CLASS jjg@309: out.println(" @Retention(RetentionPolicy.CLASS)"); jjg@309: out.println(" @interface B { }"); jjg@309: out.println(" void invisible() @B { }"); jjg@309: jjg@309: // source annotations jjg@309: out.println(" @Retention(RetentionPolicy.SOURCE)"); jjg@309: out.println(" @interface C { }"); jjg@309: out.println(" void source() @C { }"); jjg@309: jjg@309: // default visibility: CLASS jjg@309: out.println(" @interface D { }"); jjg@309: out.println(" void def() @D { }"); jjg@309: out.println("}"); 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_all = 3, expected_visibles = 1, expected_invisibles = 2; 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: }