1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/tools/javap/typeAnnotations/TypeCasts.java Wed Apr 27 01:34:52 2016 +0800 1.3 @@ -0,0 +1,155 @@ 1.4 +/* 1.5 + * Copyright (c) 2009, 2013, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + */ 1.26 + 1.27 +import java.io.*; 1.28 +import com.sun.tools.classfile.*; 1.29 + 1.30 +/* 1.31 + * @test 1.32 + * @bug 6843077 1.33 + * @summary test that typecasts annotation are emitted if only the cast 1.34 + * expression is optimized away 1.35 + */ 1.36 + 1.37 +public class TypeCasts { 1.38 + public static void main(String[] args) throws Exception { 1.39 + new TypeCasts().run(); 1.40 + } 1.41 + 1.42 + public void run() throws Exception { 1.43 + File javaFile = writeTestFile(); 1.44 + File classFile = compileTestFile(javaFile); 1.45 + 1.46 + ClassFile cf = ClassFile.read(classFile); 1.47 + for (Method m: cf.methods) { 1.48 + test(cf, m); 1.49 + } 1.50 + 1.51 + countAnnotations(); 1.52 + 1.53 + if (errors > 0) 1.54 + throw new Exception(errors + " errors found"); 1.55 + System.out.println("PASSED"); 1.56 + } 1.57 + 1.58 + void test(ClassFile cf, Method m) { 1.59 + test(cf, m, Attribute.RuntimeVisibleTypeAnnotations, true); 1.60 + test(cf, m, Attribute.RuntimeInvisibleTypeAnnotations, false); 1.61 + } 1.62 + 1.63 + 1.64 + // test the result of Attributes.getIndex according to expectations 1.65 + // encoded in the method's name 1.66 + void test(ClassFile cf, Method m, String name, boolean visible) { 1.67 + Attribute attr = null; 1.68 + Code_attribute cAttr = null; 1.69 + 1.70 + int index = m.attributes.getIndex(cf.constant_pool, Attribute.Code); 1.71 + if(index!= -1) { 1.72 + attr = m.attributes.get(index); 1.73 + assert attr instanceof Code_attribute; 1.74 + cAttr = (Code_attribute)attr; 1.75 + index = cAttr.attributes.getIndex(cf.constant_pool, name); 1.76 + if(index!= -1) { 1.77 + attr = cAttr.attributes.get(index); 1.78 + assert attr instanceof RuntimeTypeAnnotations_attribute; 1.79 + RuntimeTypeAnnotations_attribute tAttr = (RuntimeTypeAnnotations_attribute)attr; 1.80 + all += tAttr.annotations.length; 1.81 + if (visible) 1.82 + visibles += tAttr.annotations.length; 1.83 + else 1.84 + invisibles += tAttr.annotations.length; 1.85 + } 1.86 + } 1.87 + } 1.88 + 1.89 + 1.90 + 1.91 + File writeTestFile() throws IOException { 1.92 + File f = new File("Test.java"); 1.93 + PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(f))); 1.94 + out.println("import java.lang.annotation.*;"); 1.95 + out.println("class Test { "); 1.96 + out.println(" @Target(ElementType.TYPE_USE) @interface A { }"); 1.97 + 1.98 + out.println(" void emit() {"); 1.99 + out.println(" Object o = null;"); 1.100 + out.println(" String s = null;"); 1.101 + 1.102 + out.println(" String a0 = (@A String)o;"); 1.103 + out.println(" Object a1 = (@A Object)o;"); 1.104 + 1.105 + out.println(" String b0 = (@A String)s;"); 1.106 + out.println(" Object b1 = (@A Object)s;"); 1.107 + out.println(" }"); 1.108 + 1.109 + out.println(" void alldeadcode() {"); 1.110 + out.println(" Object o = null;"); 1.111 + 1.112 + out.println(" if (false) {"); 1.113 + out.println(" String a0 = (@A String)o;"); 1.114 + out.println(" }"); 1.115 + out.println(" }"); 1.116 + 1.117 + out.println("}"); 1.118 + out.close(); 1.119 + return f; 1.120 + } 1.121 + 1.122 + File compileTestFile(File f) { 1.123 + int rc = com.sun.tools.javac.Main.compile(new String[] { "-source", "1.8", "-g", f.getPath() }); 1.124 + if (rc != 0) 1.125 + throw new Error("compilation failed. rc=" + rc); 1.126 + String path = f.getPath(); 1.127 + return new File(path.substring(0, path.length() - 5) + ".class"); 1.128 + } 1.129 + 1.130 + void countAnnotations() { 1.131 + int expected_visibles = 0, expected_invisibles = 4; 1.132 + int expected_all = expected_visibles + expected_invisibles; 1.133 + 1.134 + if (expected_all != all) { 1.135 + errors++; 1.136 + System.err.println("expected " + expected_all 1.137 + + " annotations but found " + all); 1.138 + } 1.139 + 1.140 + if (expected_visibles != visibles) { 1.141 + errors++; 1.142 + System.err.println("expected " + expected_visibles 1.143 + + " visibles annotations but found " + visibles); 1.144 + } 1.145 + 1.146 + if (expected_invisibles != invisibles) { 1.147 + errors++; 1.148 + System.err.println("expected " + expected_invisibles 1.149 + + " invisibles annotations but found " + invisibles); 1.150 + } 1.151 + 1.152 + } 1.153 + 1.154 + int errors; 1.155 + int all; 1.156 + int visibles; 1.157 + int invisibles; 1.158 +}