test/tools/javap/typeAnnotations/PresenceInner.java

Fri, 26 Jun 2009 19:12:41 -0700

author
jjg
date
Fri, 26 Jun 2009 19:12:41 -0700
changeset 309
664edca41e34
child 554
9d9f26857129
permissions
-rw-r--r--

6855544: add missing files
Reviewed-by: jjg, mcimadamore, darcy
Contributed-by: mernst@cs.washington.edu, mali@csail.mit.edu, mpapi@csail.mit.edu

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

mercurial