test/tools/javap/typeAnnotations/TypeCasts.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) 2009, 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 com.sun.tools.classfile.*;
jjg@1521 26
jjg@1521 27 /*
jjg@1521 28 * @test
jjg@1521 29 * @bug 6843077
jjg@1521 30 * @summary test that typecasts annotation are emitted if only the cast
jjg@1521 31 * expression is optimized away
jjg@1521 32 */
jjg@1521 33
jjg@1521 34 public class TypeCasts {
jjg@1521 35 public static void main(String[] args) throws Exception {
jjg@1521 36 new TypeCasts().run();
jjg@1521 37 }
jjg@1521 38
jjg@1521 39 public void run() throws Exception {
jjg@1521 40 File javaFile = writeTestFile();
jjg@1521 41 File classFile = compileTestFile(javaFile);
jjg@1521 42
jjg@1521 43 ClassFile cf = ClassFile.read(classFile);
jjg@1521 44 test(cf);
jjg@1521 45 for (Field f : cf.fields) {
jjg@1521 46 test(cf, f);
jjg@1521 47 }
jjg@1521 48 for (Method m: cf.methods) {
jjg@1521 49 test(cf, m);
jjg@1521 50 }
jjg@1521 51
jjg@1521 52 countAnnotations();
jjg@1521 53
jjg@1521 54 if (errors > 0)
jjg@1521 55 throw new Exception(errors + " errors found");
jjg@1521 56 System.out.println("PASSED");
jjg@1521 57 }
jjg@1521 58
jjg@1521 59 void test(ClassFile cf) {
jjg@1521 60 test(cf, Attribute.RuntimeVisibleTypeAnnotations, true);
jjg@1521 61 test(cf, Attribute.RuntimeInvisibleTypeAnnotations, false);
jjg@1521 62 }
jjg@1521 63
jjg@1521 64 void test(ClassFile cf, Method m) {
jjg@1521 65 test(cf, m, Attribute.RuntimeVisibleTypeAnnotations, true);
jjg@1521 66 test(cf, m, Attribute.RuntimeInvisibleTypeAnnotations, false);
jjg@1521 67 }
jjg@1521 68
jjg@1521 69 void test(ClassFile cf, Field m) {
jjg@1521 70 test(cf, m, Attribute.RuntimeVisibleTypeAnnotations, true);
jjg@1521 71 test(cf, m, Attribute.RuntimeInvisibleTypeAnnotations, false);
jjg@1521 72 }
jjg@1521 73
jjg@1521 74 // test the result of Attributes.getIndex according to expectations
jjg@1521 75 // encoded in the method's name
jjg@1521 76 void test(ClassFile cf, String name, boolean visible) {
jjg@1521 77 int index = cf.attributes.getIndex(cf.constant_pool, name);
jjg@1521 78 if (index != -1) {
jjg@1521 79 Attribute attr = cf.attributes.get(index);
jjg@1521 80 assert attr instanceof RuntimeTypeAnnotations_attribute;
jjg@1521 81 RuntimeTypeAnnotations_attribute tAttr = (RuntimeTypeAnnotations_attribute)attr;
jjg@1521 82 all += tAttr.annotations.length;
jjg@1521 83 if (visible)
jjg@1521 84 visibles += tAttr.annotations.length;
jjg@1521 85 else
jjg@1521 86 invisibles += tAttr.annotations.length;
jjg@1521 87 }
jjg@1521 88 }
jjg@1521 89
jjg@1521 90 // test the result of Attributes.getIndex according to expectations
jjg@1521 91 // encoded in the method's name
jjg@1521 92 void test(ClassFile cf, Method m, String name, boolean visible) {
jjg@1521 93 int index = m.attributes.getIndex(cf.constant_pool, name);
jjg@1521 94 if (index != -1) {
jjg@1521 95 Attribute attr = m.attributes.get(index);
jjg@1521 96 assert attr instanceof RuntimeTypeAnnotations_attribute;
jjg@1521 97 RuntimeTypeAnnotations_attribute tAttr = (RuntimeTypeAnnotations_attribute)attr;
jjg@1521 98 all += tAttr.annotations.length;
jjg@1521 99 if (visible)
jjg@1521 100 visibles += tAttr.annotations.length;
jjg@1521 101 else
jjg@1521 102 invisibles += tAttr.annotations.length;
jjg@1521 103 }
jjg@1521 104 }
jjg@1521 105
jjg@1521 106 // test the result of Attributes.getIndex according to expectations
jjg@1521 107 // encoded in the method's name
jjg@1521 108 void test(ClassFile cf, Field m, String name, boolean visible) {
jjg@1521 109 int index = m.attributes.getIndex(cf.constant_pool, name);
jjg@1521 110 if (index != -1) {
jjg@1521 111 Attribute attr = m.attributes.get(index);
jjg@1521 112 assert attr instanceof RuntimeTypeAnnotations_attribute;
jjg@1521 113 RuntimeTypeAnnotations_attribute tAttr = (RuntimeTypeAnnotations_attribute)attr;
jjg@1521 114 all += tAttr.annotations.length;
jjg@1521 115 if (visible)
jjg@1521 116 visibles += tAttr.annotations.length;
jjg@1521 117 else
jjg@1521 118 invisibles += tAttr.annotations.length;
jjg@1521 119 }
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.lang.annotation.*;");
jjg@1521 127 out.println("class Test { ");
jjg@1521 128 out.println(" @Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER})");
jjg@1521 129 out.println(" @interface A { }");
jjg@1521 130
jjg@1521 131 out.println(" void emit() {");
jjg@1521 132 out.println(" Object o = null;");
jjg@1521 133 out.println(" String s = null;");
jjg@1521 134
jjg@1521 135 out.println(" String a0 = (@A String)o;");
jjg@1521 136 out.println(" Object a1 = (@A Object)o;");
jjg@1521 137
jjg@1521 138 out.println(" String b0 = (@A String)s;");
jjg@1521 139 out.println(" Object b1 = (@A Object)s;");
jjg@1521 140 out.println(" }");
jjg@1521 141
jjg@1521 142 out.println(" void alldeadcode() {");
jjg@1521 143 out.println(" Object o = null;");
jjg@1521 144
jjg@1521 145 out.println(" if (false) {");
jjg@1521 146 out.println(" String a0 = (@A String)o;");
jjg@1521 147 out.println(" }");
jjg@1521 148 out.println(" }");
jjg@1521 149
jjg@1521 150 out.println("}");
jjg@1521 151 out.close();
jjg@1521 152 return f;
jjg@1521 153 }
jjg@1521 154
jjg@1521 155 File compileTestFile(File f) {
jjg@1521 156 int rc = com.sun.tools.javac.Main.compile(new String[] { "-source", "1.8", "-g", f.getPath() });
jjg@1521 157 if (rc != 0)
jjg@1521 158 throw new Error("compilation failed. rc=" + rc);
jjg@1521 159 String path = f.getPath();
jjg@1521 160 return new File(path.substring(0, path.length() - 5) + ".class");
jjg@1521 161 }
jjg@1521 162
jjg@1521 163 void countAnnotations() {
jjg@1521 164 int expected_visibles = 0, expected_invisibles = 4;
jjg@1521 165 int expected_all = expected_visibles + expected_invisibles;
jjg@1521 166
jjg@1521 167 if (expected_all != all) {
jjg@1521 168 errors++;
jjg@1521 169 System.err.println("expected " + expected_all
jjg@1521 170 + " annotations but found " + all);
jjg@1521 171 }
jjg@1521 172
jjg@1521 173 if (expected_visibles != visibles) {
jjg@1521 174 errors++;
jjg@1521 175 System.err.println("expected " + expected_visibles
jjg@1521 176 + " visibles annotations but found " + visibles);
jjg@1521 177 }
jjg@1521 178
jjg@1521 179 if (expected_invisibles != invisibles) {
jjg@1521 180 errors++;
jjg@1521 181 System.err.println("expected " + expected_invisibles
jjg@1521 182 + " invisibles annotations but found " + invisibles);
jjg@1521 183 }
jjg@1521 184
jjg@1521 185 }
jjg@1521 186
jjg@1521 187 int errors;
jjg@1521 188 int all;
jjg@1521 189 int visibles;
jjg@1521 190 int invisibles;
jjg@1521 191 }

mercurial