1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/tools/javap/typeAnnotations/Presence.java Wed Jan 23 13:27:24 2013 -0800 1.3 @@ -0,0 +1,195 @@ 1.4 +/* 1.5 + * Copyright (c) 2008, 2013, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + */ 1.26 + 1.27 +import java.io.*; 1.28 +import java.lang.annotation.ElementType; 1.29 + 1.30 +import com.sun.tools.classfile.*; 1.31 + 1.32 +/* 1.33 + * @test Presence 1.34 + * @bug 6843077 1.35 + * @summary test that all type annotations are present in the classfile 1.36 + */ 1.37 + 1.38 +public class Presence { 1.39 + public static void main(String[] args) throws Exception { 1.40 + new Presence().run(); 1.41 + } 1.42 + 1.43 + public void run() throws Exception { 1.44 + File javaFile = writeTestFile(); 1.45 + File classFile = compileTestFile(javaFile); 1.46 + 1.47 + ClassFile cf = ClassFile.read(classFile); 1.48 + test(cf); 1.49 + for (Field f : cf.fields) { 1.50 + test(cf, f); 1.51 + } 1.52 + for (Method m: cf.methods) { 1.53 + test(cf, m); 1.54 + } 1.55 + 1.56 + countAnnotations(); 1.57 + 1.58 + if (errors > 0) 1.59 + throw new Exception(errors + " errors found"); 1.60 + System.out.println("PASSED"); 1.61 + } 1.62 + 1.63 + void test(ClassFile cf) { 1.64 + test(cf, Attribute.RuntimeVisibleTypeAnnotations, true); 1.65 + test(cf, Attribute.RuntimeInvisibleTypeAnnotations, false); 1.66 + } 1.67 + 1.68 + void test(ClassFile cf, Method m) { 1.69 + test(cf, m, Attribute.RuntimeVisibleTypeAnnotations, true); 1.70 + test(cf, m, Attribute.RuntimeInvisibleTypeAnnotations, false); 1.71 + } 1.72 + 1.73 + void test(ClassFile cf, Field m) { 1.74 + test(cf, m, Attribute.RuntimeVisibleTypeAnnotations, true); 1.75 + test(cf, m, Attribute.RuntimeInvisibleTypeAnnotations, false); 1.76 + } 1.77 + 1.78 + // test the result of Attributes.getIndex according to expectations 1.79 + // encoded in the method's name 1.80 + void test(ClassFile cf, String name, boolean visible) { 1.81 + int index = cf.attributes.getIndex(cf.constant_pool, name); 1.82 + if (index != -1) { 1.83 + Attribute attr = cf.attributes.get(index); 1.84 + assert attr instanceof RuntimeTypeAnnotations_attribute; 1.85 + RuntimeTypeAnnotations_attribute tAttr = (RuntimeTypeAnnotations_attribute)attr; 1.86 + all += tAttr.annotations.length; 1.87 + if (visible) 1.88 + visibles += tAttr.annotations.length; 1.89 + else 1.90 + invisibles += tAttr.annotations.length; 1.91 + } 1.92 + } 1.93 + 1.94 + // test the result of Attributes.getIndex according to expectations 1.95 + // encoded in the method's name 1.96 + void test(ClassFile cf, Method m, String name, boolean visible) { 1.97 + int index = m.attributes.getIndex(cf.constant_pool, name); 1.98 + if (index != -1) { 1.99 + Attribute attr = m.attributes.get(index); 1.100 + assert attr instanceof RuntimeTypeAnnotations_attribute; 1.101 + RuntimeTypeAnnotations_attribute tAttr = (RuntimeTypeAnnotations_attribute)attr; 1.102 + all += tAttr.annotations.length; 1.103 + if (visible) 1.104 + visibles += tAttr.annotations.length; 1.105 + else 1.106 + invisibles += tAttr.annotations.length; 1.107 + } 1.108 + } 1.109 + 1.110 + // test the result of Attributes.getIndex according to expectations 1.111 + // encoded in the method's name 1.112 + void test(ClassFile cf, Field m, String name, boolean visible) { 1.113 + int index = m.attributes.getIndex(cf.constant_pool, name); 1.114 + if (index != -1) { 1.115 + Attribute attr = m.attributes.get(index); 1.116 + assert attr instanceof RuntimeTypeAnnotations_attribute; 1.117 + RuntimeTypeAnnotations_attribute tAttr = (RuntimeTypeAnnotations_attribute)attr; 1.118 + all += tAttr.annotations.length; 1.119 + if (visible) 1.120 + visibles += tAttr.annotations.length; 1.121 + else 1.122 + invisibles += tAttr.annotations.length; 1.123 + } 1.124 + } 1.125 + 1.126 + File writeTestFile() throws IOException { 1.127 + File f = new File("Test.java"); 1.128 + PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(f))); 1.129 + out.println("import java.util.*;"); 1.130 + out.println("import java.lang.annotation.*;"); 1.131 + 1.132 + out.println("class Test<@Test.A T extends @Test.A List<@Test.A String>> { "); 1.133 + out.println(" @Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER})"); 1.134 + out.println(" @interface A { }"); 1.135 + 1.136 + out.println(" Map<@A String, Map<@A String, @A String>> f1;"); 1.137 + 1.138 + out.println(" <@A TM extends @A List<@A String>>"); 1.139 + out.println(" Map<@A String, @A List<@A String>>"); 1.140 + out.println(" method(@A Test<T> this, List<@A String> @A [] param1, String @A [] @A ... param2)"); 1.141 + out.println(" throws @A Exception {"); 1.142 + out.println(" @A String lc1 = null;"); 1.143 + out.println(" @A List<@A String> lc2 = null;"); 1.144 + out.println(" @A String @A [] [] @A[] lc3 = null;"); 1.145 + out.println(" List<? extends @A List<@A String>> lc4 = null;"); 1.146 + out.println(" Object lc5 = (@A List<@A String>) null;"); 1.147 + out.println(" boolean lc6 = lc1 instanceof @A String;"); 1.148 + out.println(" boolean lc7 = lc5 instanceof @A String @A [] @A [];"); 1.149 + out.println(" new @A ArrayList<@A String>();"); 1.150 + out.println(" Object lc8 = new @A String @A [4];"); 1.151 + out.println(" try {"); 1.152 + out.println(" Object lc10 = int.class;"); 1.153 + out.println(" } catch (@A Exception e) { e.toString(); }"); 1.154 + out.println(" return null;"); 1.155 + out.println(" }"); 1.156 + out.println(" void vararg1(String @A ... t) { } "); 1.157 + out.println("}"); 1.158 + out.close(); 1.159 + return f; 1.160 + } 1.161 + 1.162 + File compileTestFile(File f) { 1.163 + int rc = com.sun.tools.javac.Main.compile(new String[] { "-source", "1.8", "-g", f.getPath() }); 1.164 + if (rc != 0) 1.165 + throw new Error("compilation failed. rc=" + rc); 1.166 + String path = f.getPath(); 1.167 + return new File(path.substring(0, path.length() - 5) + ".class"); 1.168 + } 1.169 + 1.170 + void countAnnotations() { 1.171 + int expected_visibles = 0, expected_invisibles = 38; 1.172 + int expected_all = expected_visibles + expected_invisibles; 1.173 + 1.174 + if (expected_all != all) { 1.175 + errors++; 1.176 + System.err.println("expected " + expected_all 1.177 + + " annotations but found " + all); 1.178 + } 1.179 + 1.180 + if (expected_visibles != visibles) { 1.181 + errors++; 1.182 + System.err.println("expected " + expected_visibles 1.183 + + " visibles annotations but found " + visibles); 1.184 + } 1.185 + 1.186 + if (expected_invisibles != invisibles) { 1.187 + errors++; 1.188 + System.err.println("expected " + expected_invisibles 1.189 + + " invisibles annotations but found " + invisibles); 1.190 + } 1.191 + 1.192 + } 1.193 + 1.194 + int errors; 1.195 + int all; 1.196 + int visibles; 1.197 + int invisibles; 1.198 +}