Tue, 26 Jan 2010 11:23:54 -0800
6917130: should test that annotations that have been optimized away are not emitted to classfile
Reviewed-by: jjg, darcy
Contributed-by: mali@csail.mit.edu, mernst@cs.washington.edu
jjg@480 | 1 | /* |
jjg@480 | 2 | * Copyright 2009-2010 Sun Microsystems, Inc. All Rights Reserved. |
jjg@480 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
jjg@480 | 4 | * |
jjg@480 | 5 | * This code is free software; you can redistribute it and/or modify it |
jjg@480 | 6 | * under the terms of the GNU General Public License version 2 only, as |
jjg@480 | 7 | * published by the Free Software Foundation. |
jjg@480 | 8 | * |
jjg@480 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
jjg@480 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
jjg@480 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
jjg@480 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
jjg@480 | 13 | * accompanied this code). |
jjg@480 | 14 | * |
jjg@480 | 15 | * You should have received a copy of the GNU General Public License version |
jjg@480 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
jjg@480 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
jjg@480 | 18 | * |
jjg@480 | 19 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
jjg@480 | 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
jjg@480 | 21 | * have any questions. |
jjg@480 | 22 | */ |
jjg@480 | 23 | |
jjg@480 | 24 | import java.io.*; |
jjg@480 | 25 | import java.net.URL; |
jjg@480 | 26 | import java.util.List; |
jjg@480 | 27 | |
jjg@480 | 28 | import com.sun.tools.classfile.*; |
jjg@480 | 29 | |
jjg@480 | 30 | /* |
jjg@480 | 31 | * @test |
jjg@480 | 32 | * @bug 6917130 |
jjg@480 | 33 | * @summary test that optimized away annotations are not emited to classfile |
jjg@480 | 34 | */ |
jjg@480 | 35 | |
jjg@480 | 36 | public class DeadCode { |
jjg@480 | 37 | public static void main(String[] args) throws Exception { |
jjg@480 | 38 | new DeadCode().run(); |
jjg@480 | 39 | } |
jjg@480 | 40 | |
jjg@480 | 41 | public void run() throws Exception { |
jjg@480 | 42 | ClassFile cf = getClassFile("DeadCode$Test.class"); |
jjg@480 | 43 | test(cf); |
jjg@480 | 44 | for (Field f : cf.fields) { |
jjg@480 | 45 | test(cf, f); |
jjg@480 | 46 | } |
jjg@480 | 47 | for (Method m: cf.methods) { |
jjg@480 | 48 | test(cf, m); |
jjg@480 | 49 | } |
jjg@480 | 50 | |
jjg@480 | 51 | countAnnotations(); |
jjg@480 | 52 | |
jjg@480 | 53 | if (errors > 0) |
jjg@480 | 54 | throw new Exception(errors + " errors found"); |
jjg@480 | 55 | System.out.println("PASSED"); |
jjg@480 | 56 | } |
jjg@480 | 57 | |
jjg@480 | 58 | ClassFile getClassFile(String name) throws IOException, ConstantPoolException { |
jjg@480 | 59 | URL url = getClass().getResource(name); |
jjg@480 | 60 | InputStream in = url.openStream(); |
jjg@480 | 61 | try { |
jjg@480 | 62 | return ClassFile.read(in); |
jjg@480 | 63 | } finally { |
jjg@480 | 64 | in.close(); |
jjg@480 | 65 | } |
jjg@480 | 66 | } |
jjg@480 | 67 | |
jjg@480 | 68 | /************ Helper annotations counting methods ******************/ |
jjg@480 | 69 | void test(ClassFile cf) { |
jjg@480 | 70 | test(cf, Attribute.RuntimeVisibleTypeAnnotations, true); |
jjg@480 | 71 | test(cf, Attribute.RuntimeInvisibleTypeAnnotations, false); |
jjg@480 | 72 | } |
jjg@480 | 73 | |
jjg@480 | 74 | void test(ClassFile cf, Method m) { |
jjg@480 | 75 | test(cf, m, Attribute.RuntimeVisibleTypeAnnotations, true); |
jjg@480 | 76 | test(cf, m, Attribute.RuntimeInvisibleTypeAnnotations, false); |
jjg@480 | 77 | } |
jjg@480 | 78 | |
jjg@480 | 79 | void test(ClassFile cf, Field m) { |
jjg@480 | 80 | test(cf, m, Attribute.RuntimeVisibleTypeAnnotations, true); |
jjg@480 | 81 | test(cf, m, Attribute.RuntimeInvisibleTypeAnnotations, false); |
jjg@480 | 82 | } |
jjg@480 | 83 | |
jjg@480 | 84 | // test the result of Attributes.getIndex according to expectations |
jjg@480 | 85 | // encoded in the method's name |
jjg@480 | 86 | void test(ClassFile cf, String name, boolean visible) { |
jjg@480 | 87 | int index = cf.attributes.getIndex(cf.constant_pool, name); |
jjg@480 | 88 | if (index != -1) { |
jjg@480 | 89 | Attribute attr = cf.attributes.get(index); |
jjg@480 | 90 | assert attr instanceof RuntimeTypeAnnotations_attribute; |
jjg@480 | 91 | RuntimeTypeAnnotations_attribute tAttr = (RuntimeTypeAnnotations_attribute)attr; |
jjg@480 | 92 | all += tAttr.annotations.length; |
jjg@480 | 93 | if (visible) |
jjg@480 | 94 | visibles += tAttr.annotations.length; |
jjg@480 | 95 | else |
jjg@480 | 96 | invisibles += tAttr.annotations.length; |
jjg@480 | 97 | } |
jjg@480 | 98 | } |
jjg@480 | 99 | |
jjg@480 | 100 | // test the result of Attributes.getIndex according to expectations |
jjg@480 | 101 | // encoded in the method's name |
jjg@480 | 102 | void test(ClassFile cf, Method m, String name, boolean visible) { |
jjg@480 | 103 | int index = m.attributes.getIndex(cf.constant_pool, name); |
jjg@480 | 104 | if (index != -1) { |
jjg@480 | 105 | Attribute attr = m.attributes.get(index); |
jjg@480 | 106 | assert attr instanceof RuntimeTypeAnnotations_attribute; |
jjg@480 | 107 | RuntimeTypeAnnotations_attribute tAttr = (RuntimeTypeAnnotations_attribute)attr; |
jjg@480 | 108 | all += tAttr.annotations.length; |
jjg@480 | 109 | if (visible) |
jjg@480 | 110 | visibles += tAttr.annotations.length; |
jjg@480 | 111 | else |
jjg@480 | 112 | invisibles += tAttr.annotations.length; |
jjg@480 | 113 | } |
jjg@480 | 114 | } |
jjg@480 | 115 | |
jjg@480 | 116 | // test the result of Attributes.getIndex according to expectations |
jjg@480 | 117 | // encoded in the method's name |
jjg@480 | 118 | void test(ClassFile cf, Field m, String name, boolean visible) { |
jjg@480 | 119 | int index = m.attributes.getIndex(cf.constant_pool, name); |
jjg@480 | 120 | if (index != -1) { |
jjg@480 | 121 | Attribute attr = m.attributes.get(index); |
jjg@480 | 122 | assert attr instanceof RuntimeTypeAnnotations_attribute; |
jjg@480 | 123 | RuntimeTypeAnnotations_attribute tAttr = (RuntimeTypeAnnotations_attribute)attr; |
jjg@480 | 124 | all += tAttr.annotations.length; |
jjg@480 | 125 | if (visible) |
jjg@480 | 126 | visibles += tAttr.annotations.length; |
jjg@480 | 127 | else |
jjg@480 | 128 | invisibles += tAttr.annotations.length; |
jjg@480 | 129 | } |
jjg@480 | 130 | } |
jjg@480 | 131 | |
jjg@480 | 132 | void countAnnotations() { |
jjg@480 | 133 | int expected_all = expected_visibles + expected_invisibles; |
jjg@480 | 134 | |
jjg@480 | 135 | if (expected_all != all) { |
jjg@480 | 136 | errors++; |
jjg@480 | 137 | System.err.println("expected " + expected_all |
jjg@480 | 138 | + " annotations but found " + all); |
jjg@480 | 139 | } |
jjg@480 | 140 | |
jjg@480 | 141 | if (expected_visibles != visibles) { |
jjg@480 | 142 | errors++; |
jjg@480 | 143 | System.err.println("expected " + expected_visibles |
jjg@480 | 144 | + " visibles annotations but found " + visibles); |
jjg@480 | 145 | } |
jjg@480 | 146 | |
jjg@480 | 147 | if (expected_invisibles != invisibles) { |
jjg@480 | 148 | errors++; |
jjg@480 | 149 | System.err.println("expected " + expected_invisibles |
jjg@480 | 150 | + " invisibles annotations but found " + invisibles); |
jjg@480 | 151 | } |
jjg@480 | 152 | |
jjg@480 | 153 | } |
jjg@480 | 154 | |
jjg@480 | 155 | int errors; |
jjg@480 | 156 | int all; |
jjg@480 | 157 | int visibles; |
jjg@480 | 158 | int invisibles; |
jjg@480 | 159 | |
jjg@480 | 160 | /*********************** Test class *************************/ |
jjg@480 | 161 | static int expected_invisibles = 1; |
jjg@480 | 162 | static int expected_visibles = 0; |
jjg@480 | 163 | static class Test { |
jjg@480 | 164 | @interface A {} |
jjg@480 | 165 | |
jjg@480 | 166 | void test() { |
jjg@480 | 167 | List<? extends @A Object> o = null; |
jjg@480 | 168 | o.toString(); |
jjg@480 | 169 | |
jjg@480 | 170 | @A String m; |
jjg@480 | 171 | if (false) { |
jjg@480 | 172 | @A String a; |
jjg@480 | 173 | @A String b = "m"; |
jjg@480 | 174 | b.toString(); |
jjg@480 | 175 | List<? extends @A Object> c = null; |
jjg@480 | 176 | c.toString(); |
jjg@480 | 177 | } |
jjg@480 | 178 | } |
jjg@480 | 179 | } |
jjg@480 | 180 | |
jjg@480 | 181 | } |