1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/tools/javac/typeAnnotations/classfile/DeadCode.java Tue Jan 26 11:23:54 2010 -0800 1.3 @@ -0,0 +1,181 @@ 1.4 +/* 1.5 + * Copyright 2009-2010 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 java.net.URL; 1.29 +import java.util.List; 1.30 + 1.31 +import com.sun.tools.classfile.*; 1.32 + 1.33 +/* 1.34 + * @test 1.35 + * @bug 6917130 1.36 + * @summary test that optimized away annotations are not emited to classfile 1.37 + */ 1.38 + 1.39 +public class DeadCode { 1.40 + public static void main(String[] args) throws Exception { 1.41 + new DeadCode().run(); 1.42 + } 1.43 + 1.44 + public void run() throws Exception { 1.45 + ClassFile cf = getClassFile("DeadCode$Test.class"); 1.46 + test(cf); 1.47 + for (Field f : cf.fields) { 1.48 + test(cf, f); 1.49 + } 1.50 + for (Method m: cf.methods) { 1.51 + test(cf, m); 1.52 + } 1.53 + 1.54 + countAnnotations(); 1.55 + 1.56 + if (errors > 0) 1.57 + throw new Exception(errors + " errors found"); 1.58 + System.out.println("PASSED"); 1.59 + } 1.60 + 1.61 + ClassFile getClassFile(String name) throws IOException, ConstantPoolException { 1.62 + URL url = getClass().getResource(name); 1.63 + InputStream in = url.openStream(); 1.64 + try { 1.65 + return ClassFile.read(in); 1.66 + } finally { 1.67 + in.close(); 1.68 + } 1.69 + } 1.70 + 1.71 + /************ Helper annotations counting methods ******************/ 1.72 + void test(ClassFile cf) { 1.73 + test(cf, Attribute.RuntimeVisibleTypeAnnotations, true); 1.74 + test(cf, Attribute.RuntimeInvisibleTypeAnnotations, false); 1.75 + } 1.76 + 1.77 + void test(ClassFile cf, Method m) { 1.78 + test(cf, m, Attribute.RuntimeVisibleTypeAnnotations, true); 1.79 + test(cf, m, Attribute.RuntimeInvisibleTypeAnnotations, false); 1.80 + } 1.81 + 1.82 + void test(ClassFile cf, Field m) { 1.83 + test(cf, m, Attribute.RuntimeVisibleTypeAnnotations, true); 1.84 + test(cf, m, Attribute.RuntimeInvisibleTypeAnnotations, false); 1.85 + } 1.86 + 1.87 + // test the result of Attributes.getIndex according to expectations 1.88 + // encoded in the method's name 1.89 + void test(ClassFile cf, String name, boolean visible) { 1.90 + int index = cf.attributes.getIndex(cf.constant_pool, name); 1.91 + if (index != -1) { 1.92 + Attribute attr = cf.attributes.get(index); 1.93 + assert attr instanceof RuntimeTypeAnnotations_attribute; 1.94 + RuntimeTypeAnnotations_attribute tAttr = (RuntimeTypeAnnotations_attribute)attr; 1.95 + all += tAttr.annotations.length; 1.96 + if (visible) 1.97 + visibles += tAttr.annotations.length; 1.98 + else 1.99 + invisibles += tAttr.annotations.length; 1.100 + } 1.101 + } 1.102 + 1.103 + // test the result of Attributes.getIndex according to expectations 1.104 + // encoded in the method's name 1.105 + void test(ClassFile cf, Method m, String name, boolean visible) { 1.106 + int index = m.attributes.getIndex(cf.constant_pool, name); 1.107 + if (index != -1) { 1.108 + Attribute attr = m.attributes.get(index); 1.109 + assert attr instanceof RuntimeTypeAnnotations_attribute; 1.110 + RuntimeTypeAnnotations_attribute tAttr = (RuntimeTypeAnnotations_attribute)attr; 1.111 + all += tAttr.annotations.length; 1.112 + if (visible) 1.113 + visibles += tAttr.annotations.length; 1.114 + else 1.115 + invisibles += tAttr.annotations.length; 1.116 + } 1.117 + } 1.118 + 1.119 + // test the result of Attributes.getIndex according to expectations 1.120 + // encoded in the method's name 1.121 + void test(ClassFile cf, Field m, String name, boolean visible) { 1.122 + int index = m.attributes.getIndex(cf.constant_pool, name); 1.123 + if (index != -1) { 1.124 + Attribute attr = m.attributes.get(index); 1.125 + assert attr instanceof RuntimeTypeAnnotations_attribute; 1.126 + RuntimeTypeAnnotations_attribute tAttr = (RuntimeTypeAnnotations_attribute)attr; 1.127 + all += tAttr.annotations.length; 1.128 + if (visible) 1.129 + visibles += tAttr.annotations.length; 1.130 + else 1.131 + invisibles += tAttr.annotations.length; 1.132 + } 1.133 + } 1.134 + 1.135 + void countAnnotations() { 1.136 + int expected_all = expected_visibles + expected_invisibles; 1.137 + 1.138 + if (expected_all != all) { 1.139 + errors++; 1.140 + System.err.println("expected " + expected_all 1.141 + + " annotations but found " + all); 1.142 + } 1.143 + 1.144 + if (expected_visibles != visibles) { 1.145 + errors++; 1.146 + System.err.println("expected " + expected_visibles 1.147 + + " visibles annotations but found " + visibles); 1.148 + } 1.149 + 1.150 + if (expected_invisibles != invisibles) { 1.151 + errors++; 1.152 + System.err.println("expected " + expected_invisibles 1.153 + + " invisibles annotations but found " + invisibles); 1.154 + } 1.155 + 1.156 + } 1.157 + 1.158 + int errors; 1.159 + int all; 1.160 + int visibles; 1.161 + int invisibles; 1.162 + 1.163 + /*********************** Test class *************************/ 1.164 + static int expected_invisibles = 1; 1.165 + static int expected_visibles = 0; 1.166 + static class Test { 1.167 + @interface A {} 1.168 + 1.169 + void test() { 1.170 + List<? extends @A Object> o = null; 1.171 + o.toString(); 1.172 + 1.173 + @A String m; 1.174 + if (false) { 1.175 + @A String a; 1.176 + @A String b = "m"; 1.177 + b.toString(); 1.178 + List<? extends @A Object> c = null; 1.179 + c.toString(); 1.180 + } 1.181 + } 1.182 + } 1.183 + 1.184 +}