jjg@480: /* jjg@480: * Copyright 2009-2010 Sun Microsystems, Inc. All Rights Reserved. jjg@480: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jjg@480: * jjg@480: * This code is free software; you can redistribute it and/or modify it jjg@480: * under the terms of the GNU General Public License version 2 only, as jjg@480: * published by the Free Software Foundation. jjg@480: * jjg@480: * This code is distributed in the hope that it will be useful, but WITHOUT jjg@480: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jjg@480: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jjg@480: * version 2 for more details (a copy is included in the LICENSE file that jjg@480: * accompanied this code). jjg@480: * jjg@480: * You should have received a copy of the GNU General Public License version jjg@480: * 2 along with this work; if not, write to the Free Software Foundation, jjg@480: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jjg@480: * jjg@480: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, jjg@480: * CA 95054 USA or visit www.sun.com if you need additional information or jjg@480: * have any questions. jjg@480: */ jjg@480: jjg@480: import java.io.*; jjg@480: import java.net.URL; jjg@480: import java.util.List; jjg@480: jjg@480: import com.sun.tools.classfile.*; jjg@480: jjg@480: /* jjg@480: * @test jjg@480: * @bug 6917130 jjg@480: * @summary test that optimized away annotations are not emited to classfile jjg@480: */ jjg@480: jjg@480: public class DeadCode { jjg@480: public static void main(String[] args) throws Exception { jjg@480: new DeadCode().run(); jjg@480: } jjg@480: jjg@480: public void run() throws Exception { jjg@480: ClassFile cf = getClassFile("DeadCode$Test.class"); jjg@480: test(cf); jjg@480: for (Field f : cf.fields) { jjg@480: test(cf, f); jjg@480: } jjg@480: for (Method m: cf.methods) { jjg@480: test(cf, m); jjg@480: } jjg@480: jjg@480: countAnnotations(); jjg@480: jjg@480: if (errors > 0) jjg@480: throw new Exception(errors + " errors found"); jjg@480: System.out.println("PASSED"); jjg@480: } jjg@480: jjg@480: ClassFile getClassFile(String name) throws IOException, ConstantPoolException { jjg@480: URL url = getClass().getResource(name); jjg@480: InputStream in = url.openStream(); jjg@480: try { jjg@480: return ClassFile.read(in); jjg@480: } finally { jjg@480: in.close(); jjg@480: } jjg@480: } jjg@480: jjg@480: /************ Helper annotations counting methods ******************/ jjg@480: void test(ClassFile cf) { jjg@480: test(cf, Attribute.RuntimeVisibleTypeAnnotations, true); jjg@480: test(cf, Attribute.RuntimeInvisibleTypeAnnotations, false); jjg@480: } jjg@480: jjg@480: void test(ClassFile cf, Method m) { jjg@480: test(cf, m, Attribute.RuntimeVisibleTypeAnnotations, true); jjg@480: test(cf, m, Attribute.RuntimeInvisibleTypeAnnotations, false); jjg@480: } jjg@480: jjg@480: void test(ClassFile cf, Field m) { jjg@480: test(cf, m, Attribute.RuntimeVisibleTypeAnnotations, true); jjg@480: test(cf, m, Attribute.RuntimeInvisibleTypeAnnotations, false); jjg@480: } jjg@480: jjg@480: // test the result of Attributes.getIndex according to expectations jjg@480: // encoded in the method's name jjg@480: void test(ClassFile cf, String name, boolean visible) { jjg@480: int index = cf.attributes.getIndex(cf.constant_pool, name); jjg@480: if (index != -1) { jjg@480: Attribute attr = cf.attributes.get(index); jjg@480: assert attr instanceof RuntimeTypeAnnotations_attribute; jjg@480: RuntimeTypeAnnotations_attribute tAttr = (RuntimeTypeAnnotations_attribute)attr; jjg@480: all += tAttr.annotations.length; jjg@480: if (visible) jjg@480: visibles += tAttr.annotations.length; jjg@480: else jjg@480: invisibles += tAttr.annotations.length; jjg@480: } jjg@480: } jjg@480: jjg@480: // test the result of Attributes.getIndex according to expectations jjg@480: // encoded in the method's name jjg@480: void test(ClassFile cf, Method m, String name, boolean visible) { jjg@480: int index = m.attributes.getIndex(cf.constant_pool, name); jjg@480: if (index != -1) { jjg@480: Attribute attr = m.attributes.get(index); jjg@480: assert attr instanceof RuntimeTypeAnnotations_attribute; jjg@480: RuntimeTypeAnnotations_attribute tAttr = (RuntimeTypeAnnotations_attribute)attr; jjg@480: all += tAttr.annotations.length; jjg@480: if (visible) jjg@480: visibles += tAttr.annotations.length; jjg@480: else jjg@480: invisibles += tAttr.annotations.length; jjg@480: } jjg@480: } jjg@480: jjg@480: // test the result of Attributes.getIndex according to expectations jjg@480: // encoded in the method's name jjg@480: void test(ClassFile cf, Field m, String name, boolean visible) { jjg@480: int index = m.attributes.getIndex(cf.constant_pool, name); jjg@480: if (index != -1) { jjg@480: Attribute attr = m.attributes.get(index); jjg@480: assert attr instanceof RuntimeTypeAnnotations_attribute; jjg@480: RuntimeTypeAnnotations_attribute tAttr = (RuntimeTypeAnnotations_attribute)attr; jjg@480: all += tAttr.annotations.length; jjg@480: if (visible) jjg@480: visibles += tAttr.annotations.length; jjg@480: else jjg@480: invisibles += tAttr.annotations.length; jjg@480: } jjg@480: } jjg@480: jjg@480: void countAnnotations() { jjg@480: int expected_all = expected_visibles + expected_invisibles; jjg@480: jjg@480: if (expected_all != all) { jjg@480: errors++; jjg@480: System.err.println("expected " + expected_all jjg@480: + " annotations but found " + all); jjg@480: } jjg@480: jjg@480: if (expected_visibles != visibles) { jjg@480: errors++; jjg@480: System.err.println("expected " + expected_visibles jjg@480: + " visibles annotations but found " + visibles); jjg@480: } jjg@480: jjg@480: if (expected_invisibles != invisibles) { jjg@480: errors++; jjg@480: System.err.println("expected " + expected_invisibles jjg@480: + " invisibles annotations but found " + invisibles); jjg@480: } jjg@480: jjg@480: } jjg@480: jjg@480: int errors; jjg@480: int all; jjg@480: int visibles; jjg@480: int invisibles; jjg@480: jjg@480: /*********************** Test class *************************/ jjg@480: static int expected_invisibles = 1; jjg@480: static int expected_visibles = 0; jjg@480: static class Test { jjg@480: @interface A {} jjg@480: jjg@480: void test() { jjg@480: List o = null; jjg@480: o.toString(); jjg@480: jjg@480: @A String m; jjg@480: if (false) { jjg@480: @A String a; jjg@480: @A String b = "m"; jjg@480: b.toString(); jjg@480: List c = null; jjg@480: c.toString(); jjg@480: } jjg@480: } jjg@480: } jjg@480: jjg@480: }