1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/tools/javap/typeAnnotations/JSR175Annotations.java Fri Jun 26 19:12:41 2009 -0700 1.3 @@ -0,0 +1,152 @@ 1.4 +/* 1.5 + * Copyright 2008 Sun Microsystems, Inc. All Rights Reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 1.23 + * CA 95054 USA or visit www.sun.com if you need additional information or 1.24 + * have any questions. 1.25 + */ 1.26 + 1.27 +import java.io.*; 1.28 +import com.sun.tools.classfile.*; 1.29 + 1.30 +/* 1.31 + * @test JSR175Annotations 1.32 + * @bug 6843077 1.33 + * @summary test that only type annotations are recorded as such in classfile 1.34 + */ 1.35 + 1.36 +public class JSR175Annotations { 1.37 + public static void main(String[] args) throws Exception { 1.38 + new JSR175Annotations().run(); 1.39 + } 1.40 + 1.41 + public void run() throws Exception { 1.42 + File javaFile = writeTestFile(); 1.43 + File classFile = compileTestFile(javaFile); 1.44 + 1.45 + ClassFile cf = ClassFile.read(classFile); 1.46 + for (Field f : cf.fields) { 1.47 + test(cf, f); 1.48 + } 1.49 + for (Method m: cf.methods) { 1.50 + test(cf, m); 1.51 + } 1.52 + 1.53 + countAnnotations(); 1.54 + 1.55 + if (errors > 0) 1.56 + throw new Exception(errors + " errors found"); 1.57 + System.out.println("PASSED"); 1.58 + } 1.59 + 1.60 + void test(ClassFile cf, Method m) { 1.61 + test(cf, m, Attribute.RuntimeVisibleTypeAnnotations, true); 1.62 + test(cf, m, Attribute.RuntimeInvisibleTypeAnnotations, false); 1.63 + } 1.64 + 1.65 + void test(ClassFile cf, Field m) { 1.66 + test(cf, m, Attribute.RuntimeVisibleTypeAnnotations, true); 1.67 + test(cf, m, Attribute.RuntimeInvisibleTypeAnnotations, false); 1.68 + } 1.69 + 1.70 + // test the result of Attributes.getIndex according to expectations 1.71 + // encoded in the method's name 1.72 + void test(ClassFile cf, Method m, String name, boolean visible) { 1.73 + int index = m.attributes.getIndex(cf.constant_pool, name); 1.74 + if (index != -1) { 1.75 + Attribute attr = m.attributes.get(index); 1.76 + assert attr instanceof RuntimeTypeAnnotations_attribute; 1.77 + RuntimeTypeAnnotations_attribute tAttr = (RuntimeTypeAnnotations_attribute)attr; 1.78 + all += tAttr.annotations.length; 1.79 + if (visible) 1.80 + visibles += tAttr.annotations.length; 1.81 + else 1.82 + invisibles += tAttr.annotations.length; 1.83 + } 1.84 + } 1.85 + 1.86 + // test the result of Attributes.getIndex according to expectations 1.87 + // encoded in the method's name 1.88 + void test(ClassFile cf, Field m, String name, boolean visible) { 1.89 + int index = m.attributes.getIndex(cf.constant_pool, name); 1.90 + if (index != -1) { 1.91 + Attribute attr = m.attributes.get(index); 1.92 + assert attr instanceof RuntimeTypeAnnotations_attribute; 1.93 + RuntimeTypeAnnotations_attribute tAttr = (RuntimeTypeAnnotations_attribute)attr; 1.94 + all += tAttr.annotations.length; 1.95 + if (visible) 1.96 + visibles += tAttr.annotations.length; 1.97 + else 1.98 + invisibles += tAttr.annotations.length; 1.99 + } 1.100 + } 1.101 + 1.102 + File writeTestFile() throws IOException { 1.103 + File f = new File("Test.java"); 1.104 + PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(f))); 1.105 + out.println("import java.lang.annotation.Retention;"); 1.106 + out.println("import java.lang.annotation.RetentionPolicy;"); 1.107 + out.println("abstract class Test { "); 1.108 + out.println(" @Retention(RetentionPolicy.RUNTIME)"); 1.109 + out.println(" @interface A { }"); 1.110 + out.println(" @A String m;"); 1.111 + out.println(" @A String method(@A String a) {"); 1.112 + out.println(" return a;"); 1.113 + out.println(" }"); 1.114 + out.println("}"); 1.115 + out.close(); 1.116 + return f; 1.117 + } 1.118 + 1.119 + File compileTestFile(File f) { 1.120 + int rc = com.sun.tools.javac.Main.compile(new String[] { "-source", "1.7", "-g", f.getPath() }); 1.121 + if (rc != 0) 1.122 + throw new Error("compilation failed. rc=" + rc); 1.123 + String path = f.getPath(); 1.124 + return new File(path.substring(0, path.length() - 5) + ".class"); 1.125 + } 1.126 + 1.127 + void countAnnotations() { 1.128 + int expected_visibles = 0, expected_invisibles = 0; 1.129 + int expected_all = expected_visibles + expected_invisibles; 1.130 + 1.131 + if (expected_all != all) { 1.132 + errors++; 1.133 + System.err.println("expected " + expected_all 1.134 + + " annotations but found " + all); 1.135 + } 1.136 + 1.137 + if (expected_visibles != visibles) { 1.138 + errors++; 1.139 + System.err.println("expected " + expected_visibles 1.140 + + " visibles annotations but found " + visibles); 1.141 + } 1.142 + 1.143 + if (expected_invisibles != invisibles) { 1.144 + errors++; 1.145 + System.err.println("expected " + expected_invisibles 1.146 + + " invisibles annotations but found " + invisibles); 1.147 + } 1.148 + 1.149 + } 1.150 + 1.151 + int errors; 1.152 + int all; 1.153 + int visibles; 1.154 + int invisibles; 1.155 +}