1/* 2 * Copyright (c) 2009, 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24import java.io.*; 25import com.sun.tools.classfile.*; 26 27/* 28 * @test 29 * @bug 6843077 30 * @summary test that typecasts annotation are emitted if only the cast 31 * expression is optimized away 32 * @modules jdk.jdeps/com.sun.tools.classfile 33 */ 34 35public class TypeCasts { 36 public static void main(String[] args) throws Exception { 37 new TypeCasts().run(); 38 } 39 40 public void run() throws Exception { 41 File javaFile = writeTestFile(); 42 File classFile = compileTestFile(javaFile); 43 44 ClassFile cf = ClassFile.read(classFile); 45 for (Method m: cf.methods) { 46 test(cf, m); 47 } 48 49 countAnnotations(); 50 51 if (errors > 0) 52 throw new Exception(errors + " errors found"); 53 System.out.println("PASSED"); 54 } 55 56 void test(ClassFile cf, Method m) { 57 test(cf, m, Attribute.RuntimeVisibleTypeAnnotations, true); 58 test(cf, m, Attribute.RuntimeInvisibleTypeAnnotations, false); 59 } 60 61 62 // test the result of Attributes.getIndex according to expectations 63 // encoded in the method's name 64 void test(ClassFile cf, Method m, String name, boolean visible) { 65 Attribute attr = null; 66 Code_attribute cAttr = null; 67 68 int index = m.attributes.getIndex(cf.constant_pool, Attribute.Code); 69 if(index!= -1) { 70 attr = m.attributes.get(index); 71 assert attr instanceof Code_attribute; 72 cAttr = (Code_attribute)attr; 73 index = cAttr.attributes.getIndex(cf.constant_pool, name); 74 if(index!= -1) { 75 attr = cAttr.attributes.get(index); 76 assert attr instanceof RuntimeTypeAnnotations_attribute; 77 RuntimeTypeAnnotations_attribute tAttr = (RuntimeTypeAnnotations_attribute)attr; 78 all += tAttr.annotations.length; 79 if (visible) 80 visibles += tAttr.annotations.length; 81 else 82 invisibles += tAttr.annotations.length; 83 } 84 } 85 } 86 87 88 89 File writeTestFile() throws IOException { 90 File f = new File("Test.java"); 91 PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(f))); 92 out.println("import java.lang.annotation.*;"); 93 out.println("class Test { "); 94 out.println(" @Target(ElementType.TYPE_USE) @interface A { }"); 95 96 out.println(" void emit() {"); 97 out.println(" Object o = null;"); 98 out.println(" String s = null;"); 99 100 out.println(" String a0 = (@A String)o;"); 101 out.println(" Object a1 = (@A Object)o;"); 102 103 out.println(" String b0 = (@A String)s;"); 104 out.println(" Object b1 = (@A Object)s;"); 105 out.println(" }"); 106 107 out.println(" void alldeadcode() {"); 108 out.println(" Object o = null;"); 109 110 out.println(" if (false) {"); 111 out.println(" String a0 = (@A String)o;"); 112 out.println(" }"); 113 out.println(" }"); 114 115 out.println("}"); 116 out.close(); 117 return f; 118 } 119 120 File compileTestFile(File f) { 121 int rc = com.sun.tools.javac.Main.compile(new String[] {"-g", f.getPath() }); 122 if (rc != 0) 123 throw new Error("compilation failed. rc=" + rc); 124 String path = f.getPath(); 125 return new File(path.substring(0, path.length() - 5) + ".class"); 126 } 127 128 void countAnnotations() { 129 int expected_visibles = 0, expected_invisibles = 4; 130 int expected_all = expected_visibles + expected_invisibles; 131 132 if (expected_all != all) { 133 errors++; 134 System.err.println("expected " + expected_all 135 + " annotations but found " + all); 136 } 137 138 if (expected_visibles != visibles) { 139 errors++; 140 System.err.println("expected " + expected_visibles 141 + " visibles annotations but found " + visibles); 142 } 143 144 if (expected_invisibles != invisibles) { 145 errors++; 146 System.err.println("expected " + expected_invisibles 147 + " invisibles annotations but found " + invisibles); 148 } 149 150 } 151 152 int errors; 153 int all; 154 int visibles; 155 int invisibles; 156} 157