Fri, 28 Feb 2014 11:12:31 -0800
Merge
jjg@1521 | 1 | /* |
jjg@1521 | 2 | * Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved. |
jjg@1521 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
jjg@1521 | 4 | * |
jjg@1521 | 5 | * This code is free software; you can redistribute it and/or modify it |
jjg@1521 | 6 | * under the terms of the GNU General Public License version 2 only, as |
jjg@1521 | 7 | * published by the Free Software Foundation. |
jjg@1521 | 8 | * |
jjg@1521 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
jjg@1521 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
jjg@1521 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
jjg@1521 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
jjg@1521 | 13 | * accompanied this code). |
jjg@1521 | 14 | * |
jjg@1521 | 15 | * You should have received a copy of the GNU General Public License version |
jjg@1521 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
jjg@1521 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
jjg@1521 | 18 | * |
jjg@1521 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
jjg@1521 | 20 | * or visit www.oracle.com if you need additional information or have any |
jjg@1521 | 21 | * questions. |
jjg@1521 | 22 | */ |
jjg@1521 | 23 | |
jjg@1521 | 24 | import java.io.*; |
jjg@1521 | 25 | import java.lang.annotation.ElementType; |
jjg@1521 | 26 | |
jjg@1521 | 27 | import com.sun.tools.classfile.*; |
jjg@1521 | 28 | |
jjg@1521 | 29 | /* |
jjg@1521 | 30 | * @test Presence |
jjg@1521 | 31 | * @bug 6843077 |
jjg@1521 | 32 | * @summary test that all type annotations are present in the classfile |
jjg@1521 | 33 | */ |
jjg@1521 | 34 | |
jjg@1521 | 35 | public class Presence { |
jjg@1521 | 36 | public static void main(String[] args) throws Exception { |
jjg@1521 | 37 | new Presence().run(); |
jjg@1521 | 38 | } |
jjg@1521 | 39 | |
jjg@1521 | 40 | public void run() throws Exception { |
jjg@1521 | 41 | File javaFile = writeTestFile(); |
jjg@1521 | 42 | File classFile = compileTestFile(javaFile); |
jjg@1521 | 43 | |
jjg@1521 | 44 | ClassFile cf = ClassFile.read(classFile); |
jjg@1521 | 45 | test(cf); |
jjg@1521 | 46 | for (Field f : cf.fields) { |
jjg@1521 | 47 | test(cf, f); |
jjg@1521 | 48 | } |
jjg@1521 | 49 | for (Method m: cf.methods) { |
jjg@1521 | 50 | test(cf, m); |
jjg@1521 | 51 | } |
jjg@1521 | 52 | |
jjg@1521 | 53 | countAnnotations(); |
jjg@1521 | 54 | |
jjg@1521 | 55 | if (errors > 0) |
jjg@1521 | 56 | throw new Exception(errors + " errors found"); |
jjg@1521 | 57 | System.out.println("PASSED"); |
jjg@1521 | 58 | } |
jjg@1521 | 59 | |
jjg@1521 | 60 | void test(ClassFile cf) { |
jjg@1521 | 61 | test(cf, Attribute.RuntimeVisibleTypeAnnotations, true); |
jjg@1521 | 62 | test(cf, Attribute.RuntimeInvisibleTypeAnnotations, false); |
jjg@1521 | 63 | } |
jjg@1521 | 64 | |
jjg@1521 | 65 | void test(ClassFile cf, Method m) { |
jjg@1521 | 66 | test(cf, m, Attribute.RuntimeVisibleTypeAnnotations, true); |
jjg@1521 | 67 | test(cf, m, Attribute.RuntimeInvisibleTypeAnnotations, false); |
jjg@1521 | 68 | } |
jjg@1521 | 69 | |
jjg@1521 | 70 | void test(ClassFile cf, Field m) { |
jjg@1521 | 71 | test(cf, m, Attribute.RuntimeVisibleTypeAnnotations, true); |
jjg@1521 | 72 | test(cf, m, Attribute.RuntimeInvisibleTypeAnnotations, false); |
jjg@1521 | 73 | } |
jjg@1521 | 74 | |
jjg@1521 | 75 | // test the result of Attributes.getIndex according to expectations |
jjg@1521 | 76 | // encoded in the method's name |
jjg@1521 | 77 | void test(ClassFile cf, String name, boolean visible) { |
jjg@1521 | 78 | int index = cf.attributes.getIndex(cf.constant_pool, name); |
jjg@1521 | 79 | if (index != -1) { |
jjg@1521 | 80 | Attribute attr = cf.attributes.get(index); |
jjg@1521 | 81 | assert attr instanceof RuntimeTypeAnnotations_attribute; |
jjg@1521 | 82 | RuntimeTypeAnnotations_attribute tAttr = (RuntimeTypeAnnotations_attribute)attr; |
jjg@1521 | 83 | all += tAttr.annotations.length; |
jjg@1521 | 84 | if (visible) |
jjg@1521 | 85 | visibles += tAttr.annotations.length; |
jjg@1521 | 86 | else |
jjg@1521 | 87 | invisibles += tAttr.annotations.length; |
jjg@1521 | 88 | } |
jjg@1521 | 89 | } |
jjg@1521 | 90 | |
jjg@1521 | 91 | // test the result of Attributes.getIndex according to expectations |
jjg@1521 | 92 | // encoded in the method's name |
jjg@1521 | 93 | void test(ClassFile cf, Method m, String name, boolean visible) { |
jjg@1755 | 94 | Attribute attr = null; |
jjg@1755 | 95 | Code_attribute cAttr = null; |
jjg@1755 | 96 | RuntimeTypeAnnotations_attribute tAttr = null; |
jjg@1755 | 97 | |
jjg@1755 | 98 | // collect annotations attributes on method |
jjg@1521 | 99 | int index = m.attributes.getIndex(cf.constant_pool, name); |
jjg@1521 | 100 | if (index != -1) { |
jjg@1755 | 101 | attr = m.attributes.get(index); |
jjg@1521 | 102 | assert attr instanceof RuntimeTypeAnnotations_attribute; |
jjg@1755 | 103 | tAttr = (RuntimeTypeAnnotations_attribute)attr; |
jjg@1521 | 104 | all += tAttr.annotations.length; |
jjg@1521 | 105 | if (visible) |
jjg@1521 | 106 | visibles += tAttr.annotations.length; |
jjg@1521 | 107 | else |
jjg@1521 | 108 | invisibles += tAttr.annotations.length; |
jjg@1521 | 109 | } |
jjg@1755 | 110 | // collect annotations from method's code attribute |
jjg@1755 | 111 | index = m.attributes.getIndex(cf.constant_pool, Attribute.Code); |
jjg@1755 | 112 | if(index!= -1) { |
jjg@1755 | 113 | attr = m.attributes.get(index); |
jjg@1755 | 114 | assert attr instanceof Code_attribute; |
jjg@1755 | 115 | cAttr = (Code_attribute)attr; |
jjg@1755 | 116 | index = cAttr.attributes.getIndex(cf.constant_pool, name); |
jjg@1755 | 117 | if(index!= -1) { |
jjg@1755 | 118 | attr = cAttr.attributes.get(index); |
jjg@1755 | 119 | assert attr instanceof RuntimeTypeAnnotations_attribute; |
jjg@1755 | 120 | tAttr = (RuntimeTypeAnnotations_attribute)attr; |
jjg@1755 | 121 | all += tAttr.annotations.length; |
jjg@1755 | 122 | if (visible) |
jjg@1755 | 123 | visibles += tAttr.annotations.length; |
jjg@1755 | 124 | else |
jjg@1755 | 125 | invisibles += tAttr.annotations.length; |
jjg@1755 | 126 | } |
jjg@1755 | 127 | } |
jjg@1521 | 128 | } |
jjg@1521 | 129 | |
jjg@1521 | 130 | // test the result of Attributes.getIndex according to expectations |
jjg@1521 | 131 | // encoded in the method's name |
jjg@1521 | 132 | void test(ClassFile cf, Field m, String name, boolean visible) { |
jjg@1521 | 133 | int index = m.attributes.getIndex(cf.constant_pool, name); |
jjg@1521 | 134 | if (index != -1) { |
jjg@1521 | 135 | Attribute attr = m.attributes.get(index); |
jjg@1521 | 136 | assert attr instanceof RuntimeTypeAnnotations_attribute; |
jjg@1521 | 137 | RuntimeTypeAnnotations_attribute tAttr = (RuntimeTypeAnnotations_attribute)attr; |
jjg@1521 | 138 | all += tAttr.annotations.length; |
jjg@1521 | 139 | if (visible) |
jjg@1521 | 140 | visibles += tAttr.annotations.length; |
jjg@1521 | 141 | else |
jjg@1521 | 142 | invisibles += tAttr.annotations.length; |
jjg@1521 | 143 | } |
jjg@1521 | 144 | } |
jjg@1521 | 145 | |
jjg@1521 | 146 | File writeTestFile() throws IOException { |
jjg@1755 | 147 | File f = new File("TestPresence.java"); |
jjg@1521 | 148 | PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(f))); |
jjg@1521 | 149 | out.println("import java.util.*;"); |
jjg@1521 | 150 | out.println("import java.lang.annotation.*;"); |
jjg@1521 | 151 | |
jjg@1755 | 152 | out.println("class TestPresence<@TestPresence.A T extends @TestPresence.A List<@TestPresence.A String>> { "); |
jjg@1521 | 153 | out.println(" @Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER})"); |
jjg@1521 | 154 | out.println(" @interface A { }"); |
jjg@1521 | 155 | |
jjg@1521 | 156 | out.println(" Map<@A String, Map<@A String, @A String>> f1;"); |
jjg@1521 | 157 | |
jjg@1521 | 158 | out.println(" <@A TM extends @A List<@A String>>"); |
jjg@1521 | 159 | out.println(" Map<@A String, @A List<@A String>>"); |
jjg@1755 | 160 | out.println(" method(@A TestPresence<T> this, List<@A String> @A [] param1, String @A [] @A ... param2)"); |
jjg@1521 | 161 | out.println(" throws @A Exception {"); |
jjg@1521 | 162 | out.println(" @A String lc1 = null;"); |
jjg@1521 | 163 | out.println(" @A List<@A String> lc2 = null;"); |
jjg@1521 | 164 | out.println(" @A String @A [] [] @A[] lc3 = null;"); |
jjg@1521 | 165 | out.println(" List<? extends @A List<@A String>> lc4 = null;"); |
jjg@1521 | 166 | out.println(" Object lc5 = (@A List<@A String>) null;"); |
jjg@1521 | 167 | out.println(" boolean lc6 = lc1 instanceof @A String;"); |
jjg@1521 | 168 | out.println(" boolean lc7 = lc5 instanceof @A String @A [] @A [];"); |
jjg@1521 | 169 | out.println(" new @A ArrayList<@A String>();"); |
jjg@1521 | 170 | out.println(" Object lc8 = new @A String @A [4];"); |
jjg@1521 | 171 | out.println(" try {"); |
jjg@1521 | 172 | out.println(" Object lc10 = int.class;"); |
jjg@1521 | 173 | out.println(" } catch (@A Exception e) { e.toString(); }"); |
jjg@1521 | 174 | out.println(" return null;"); |
jjg@1521 | 175 | out.println(" }"); |
jjg@1521 | 176 | out.println(" void vararg1(String @A ... t) { } "); |
jjg@1521 | 177 | out.println("}"); |
jjg@1521 | 178 | out.close(); |
jjg@1521 | 179 | return f; |
jjg@1521 | 180 | } |
jjg@1521 | 181 | |
jjg@1521 | 182 | File compileTestFile(File f) { |
jjg@1521 | 183 | int rc = com.sun.tools.javac.Main.compile(new String[] { "-source", "1.8", "-g", f.getPath() }); |
jjg@1521 | 184 | if (rc != 0) |
jjg@1521 | 185 | throw new Error("compilation failed. rc=" + rc); |
jjg@1521 | 186 | String path = f.getPath(); |
jjg@1521 | 187 | return new File(path.substring(0, path.length() - 5) + ".class"); |
jjg@1521 | 188 | } |
jjg@1521 | 189 | |
jjg@1521 | 190 | void countAnnotations() { |
jjg@1521 | 191 | int expected_visibles = 0, expected_invisibles = 38; |
jjg@1521 | 192 | int expected_all = expected_visibles + expected_invisibles; |
jjg@1521 | 193 | |
jjg@1521 | 194 | if (expected_all != all) { |
jjg@1521 | 195 | errors++; |
jjg@1521 | 196 | System.err.println("expected " + expected_all |
jjg@1521 | 197 | + " annotations but found " + all); |
jjg@1521 | 198 | } |
jjg@1521 | 199 | |
jjg@1521 | 200 | if (expected_visibles != visibles) { |
jjg@1521 | 201 | errors++; |
jjg@1521 | 202 | System.err.println("expected " + expected_visibles |
jjg@1521 | 203 | + " visibles annotations but found " + visibles); |
jjg@1521 | 204 | } |
jjg@1521 | 205 | |
jjg@1521 | 206 | if (expected_invisibles != invisibles) { |
jjg@1521 | 207 | errors++; |
jjg@1521 | 208 | System.err.println("expected " + expected_invisibles |
jjg@1521 | 209 | + " invisibles annotations but found " + invisibles); |
jjg@1521 | 210 | } |
jjg@1521 | 211 | |
jjg@1521 | 212 | } |
jjg@1521 | 213 | |
jjg@1521 | 214 | int errors; |
jjg@1521 | 215 | int all; |
jjg@1521 | 216 | int visibles; |
jjg@1521 | 217 | int invisibles; |
jjg@1521 | 218 | } |