test/tools/javap/typeAnnotations/Presence.java

Wed, 23 Jan 2013 13:27:24 -0800

author
jjg
date
Wed, 23 Jan 2013 13:27:24 -0800
changeset 1521
71f35e4b93a5
child 1755
ddb4a2bfcd82
permissions
-rw-r--r--

8006775: JSR 308: Compiler changes in JDK8
Reviewed-by: jjg
Contributed-by: mernst@cs.washington.edu, wmdietl@cs.washington.edu, mpapi@csail.mit.edu, mahmood@notnoop.com

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@1521 94 int index = m.attributes.getIndex(cf.constant_pool, name);
jjg@1521 95 if (index != -1) {
jjg@1521 96 Attribute attr = m.attributes.get(index);
jjg@1521 97 assert attr instanceof RuntimeTypeAnnotations_attribute;
jjg@1521 98 RuntimeTypeAnnotations_attribute tAttr = (RuntimeTypeAnnotations_attribute)attr;
jjg@1521 99 all += tAttr.annotations.length;
jjg@1521 100 if (visible)
jjg@1521 101 visibles += tAttr.annotations.length;
jjg@1521 102 else
jjg@1521 103 invisibles += tAttr.annotations.length;
jjg@1521 104 }
jjg@1521 105 }
jjg@1521 106
jjg@1521 107 // test the result of Attributes.getIndex according to expectations
jjg@1521 108 // encoded in the method's name
jjg@1521 109 void test(ClassFile cf, Field m, String name, boolean visible) {
jjg@1521 110 int index = m.attributes.getIndex(cf.constant_pool, name);
jjg@1521 111 if (index != -1) {
jjg@1521 112 Attribute attr = m.attributes.get(index);
jjg@1521 113 assert attr instanceof RuntimeTypeAnnotations_attribute;
jjg@1521 114 RuntimeTypeAnnotations_attribute tAttr = (RuntimeTypeAnnotations_attribute)attr;
jjg@1521 115 all += tAttr.annotations.length;
jjg@1521 116 if (visible)
jjg@1521 117 visibles += tAttr.annotations.length;
jjg@1521 118 else
jjg@1521 119 invisibles += tAttr.annotations.length;
jjg@1521 120 }
jjg@1521 121 }
jjg@1521 122
jjg@1521 123 File writeTestFile() throws IOException {
jjg@1521 124 File f = new File("Test.java");
jjg@1521 125 PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(f)));
jjg@1521 126 out.println("import java.util.*;");
jjg@1521 127 out.println("import java.lang.annotation.*;");
jjg@1521 128
jjg@1521 129 out.println("class Test<@Test.A T extends @Test.A List<@Test.A String>> { ");
jjg@1521 130 out.println(" @Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER})");
jjg@1521 131 out.println(" @interface A { }");
jjg@1521 132
jjg@1521 133 out.println(" Map<@A String, Map<@A String, @A String>> f1;");
jjg@1521 134
jjg@1521 135 out.println(" <@A TM extends @A List<@A String>>");
jjg@1521 136 out.println(" Map<@A String, @A List<@A String>>");
jjg@1521 137 out.println(" method(@A Test<T> this, List<@A String> @A [] param1, String @A [] @A ... param2)");
jjg@1521 138 out.println(" throws @A Exception {");
jjg@1521 139 out.println(" @A String lc1 = null;");
jjg@1521 140 out.println(" @A List<@A String> lc2 = null;");
jjg@1521 141 out.println(" @A String @A [] [] @A[] lc3 = null;");
jjg@1521 142 out.println(" List<? extends @A List<@A String>> lc4 = null;");
jjg@1521 143 out.println(" Object lc5 = (@A List<@A String>) null;");
jjg@1521 144 out.println(" boolean lc6 = lc1 instanceof @A String;");
jjg@1521 145 out.println(" boolean lc7 = lc5 instanceof @A String @A [] @A [];");
jjg@1521 146 out.println(" new @A ArrayList<@A String>();");
jjg@1521 147 out.println(" Object lc8 = new @A String @A [4];");
jjg@1521 148 out.println(" try {");
jjg@1521 149 out.println(" Object lc10 = int.class;");
jjg@1521 150 out.println(" } catch (@A Exception e) { e.toString(); }");
jjg@1521 151 out.println(" return null;");
jjg@1521 152 out.println(" }");
jjg@1521 153 out.println(" void vararg1(String @A ... t) { } ");
jjg@1521 154 out.println("}");
jjg@1521 155 out.close();
jjg@1521 156 return f;
jjg@1521 157 }
jjg@1521 158
jjg@1521 159 File compileTestFile(File f) {
jjg@1521 160 int rc = com.sun.tools.javac.Main.compile(new String[] { "-source", "1.8", "-g", f.getPath() });
jjg@1521 161 if (rc != 0)
jjg@1521 162 throw new Error("compilation failed. rc=" + rc);
jjg@1521 163 String path = f.getPath();
jjg@1521 164 return new File(path.substring(0, path.length() - 5) + ".class");
jjg@1521 165 }
jjg@1521 166
jjg@1521 167 void countAnnotations() {
jjg@1521 168 int expected_visibles = 0, expected_invisibles = 38;
jjg@1521 169 int expected_all = expected_visibles + expected_invisibles;
jjg@1521 170
jjg@1521 171 if (expected_all != all) {
jjg@1521 172 errors++;
jjg@1521 173 System.err.println("expected " + expected_all
jjg@1521 174 + " annotations but found " + all);
jjg@1521 175 }
jjg@1521 176
jjg@1521 177 if (expected_visibles != visibles) {
jjg@1521 178 errors++;
jjg@1521 179 System.err.println("expected " + expected_visibles
jjg@1521 180 + " visibles annotations but found " + visibles);
jjg@1521 181 }
jjg@1521 182
jjg@1521 183 if (expected_invisibles != invisibles) {
jjg@1521 184 errors++;
jjg@1521 185 System.err.println("expected " + expected_invisibles
jjg@1521 186 + " invisibles annotations but found " + invisibles);
jjg@1521 187 }
jjg@1521 188
jjg@1521 189 }
jjg@1521 190
jjg@1521 191 int errors;
jjg@1521 192 int all;
jjg@1521 193 int visibles;
jjg@1521 194 int invisibles;
jjg@1521 195 }

mercurial