Sat, 22 Dec 2018 13:10:38 -0800
Added tag jdk8u202-b25 for changeset 15d8c732039b
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 | for (Method m: cf.methods) { |
jjg@1521 | 45 | test(cf, m); |
jjg@1521 | 46 | } |
jjg@1521 | 47 | |
jjg@1521 | 48 | countAnnotations(); |
jjg@1521 | 49 | |
jjg@1521 | 50 | if (errors > 0) |
jjg@1521 | 51 | throw new Exception(errors + " errors found"); |
jjg@1521 | 52 | System.out.println("PASSED"); |
jjg@1521 | 53 | } |
jjg@1521 | 54 | |
jjg@1521 | 55 | void test(ClassFile cf, Method m) { |
jjg@1521 | 56 | test(cf, m, Attribute.RuntimeVisibleTypeAnnotations, true); |
jjg@1521 | 57 | test(cf, m, Attribute.RuntimeInvisibleTypeAnnotations, false); |
jjg@1521 | 58 | } |
jjg@1521 | 59 | |
jjg@1521 | 60 | |
jjg@1521 | 61 | // test the result of Attributes.getIndex according to expectations |
jjg@1521 | 62 | // encoded in the method's name |
jjg@1521 | 63 | void test(ClassFile cf, Method m, String name, boolean visible) { |
jjg@1755 | 64 | Attribute attr = null; |
jjg@1755 | 65 | Code_attribute cAttr = null; |
jjg@1755 | 66 | |
jjg@1755 | 67 | int index = m.attributes.getIndex(cf.constant_pool, Attribute.Code); |
jjg@1755 | 68 | if(index!= -1) { |
jjg@1755 | 69 | attr = m.attributes.get(index); |
jjg@1755 | 70 | assert attr instanceof Code_attribute; |
jjg@1755 | 71 | cAttr = (Code_attribute)attr; |
jjg@1755 | 72 | index = cAttr.attributes.getIndex(cf.constant_pool, name); |
jjg@1755 | 73 | if(index!= -1) { |
jjg@1755 | 74 | attr = cAttr.attributes.get(index); |
jjg@1755 | 75 | assert attr instanceof RuntimeTypeAnnotations_attribute; |
jjg@1755 | 76 | RuntimeTypeAnnotations_attribute tAttr = (RuntimeTypeAnnotations_attribute)attr; |
jjg@1755 | 77 | all += tAttr.annotations.length; |
jjg@1755 | 78 | if (visible) |
jjg@1755 | 79 | visibles += tAttr.annotations.length; |
jjg@1755 | 80 | else |
jjg@1755 | 81 | invisibles += tAttr.annotations.length; |
jjg@1755 | 82 | } |
jjg@1521 | 83 | } |
jjg@1521 | 84 | } |
jjg@1521 | 85 | |
jjg@1521 | 86 | |
jjg@1521 | 87 | |
jjg@1521 | 88 | File writeTestFile() throws IOException { |
jjg@1521 | 89 | File f = new File("Test.java"); |
jjg@1521 | 90 | PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(f))); |
jjg@1521 | 91 | out.println("import java.lang.annotation.*;"); |
jjg@1521 | 92 | out.println("class Test { "); |
jjg@1755 | 93 | out.println(" @Target(ElementType.TYPE_USE) @interface A { }"); |
jjg@1521 | 94 | |
jjg@1521 | 95 | out.println(" void emit() {"); |
jjg@1521 | 96 | out.println(" Object o = null;"); |
jjg@1521 | 97 | out.println(" String s = null;"); |
jjg@1521 | 98 | |
jjg@1521 | 99 | out.println(" String a0 = (@A String)o;"); |
jjg@1521 | 100 | out.println(" Object a1 = (@A Object)o;"); |
jjg@1521 | 101 | |
jjg@1521 | 102 | out.println(" String b0 = (@A String)s;"); |
jjg@1521 | 103 | out.println(" Object b1 = (@A Object)s;"); |
jjg@1521 | 104 | out.println(" }"); |
jjg@1521 | 105 | |
jjg@1521 | 106 | out.println(" void alldeadcode() {"); |
jjg@1521 | 107 | out.println(" Object o = null;"); |
jjg@1521 | 108 | |
jjg@1521 | 109 | out.println(" if (false) {"); |
jjg@1521 | 110 | out.println(" String a0 = (@A String)o;"); |
jjg@1521 | 111 | out.println(" }"); |
jjg@1521 | 112 | out.println(" }"); |
jjg@1521 | 113 | |
jjg@1521 | 114 | out.println("}"); |
jjg@1521 | 115 | out.close(); |
jjg@1521 | 116 | return f; |
jjg@1521 | 117 | } |
jjg@1521 | 118 | |
jjg@1521 | 119 | File compileTestFile(File f) { |
jjg@1521 | 120 | int rc = com.sun.tools.javac.Main.compile(new String[] { "-source", "1.8", "-g", f.getPath() }); |
jjg@1521 | 121 | if (rc != 0) |
jjg@1521 | 122 | throw new Error("compilation failed. rc=" + rc); |
jjg@1521 | 123 | String path = f.getPath(); |
jjg@1521 | 124 | return new File(path.substring(0, path.length() - 5) + ".class"); |
jjg@1521 | 125 | } |
jjg@1521 | 126 | |
jjg@1521 | 127 | void countAnnotations() { |
jjg@1521 | 128 | int expected_visibles = 0, expected_invisibles = 4; |
jjg@1521 | 129 | int expected_all = expected_visibles + expected_invisibles; |
jjg@1521 | 130 | |
jjg@1521 | 131 | if (expected_all != all) { |
jjg@1521 | 132 | errors++; |
jjg@1521 | 133 | System.err.println("expected " + expected_all |
jjg@1521 | 134 | + " annotations but found " + all); |
jjg@1521 | 135 | } |
jjg@1521 | 136 | |
jjg@1521 | 137 | if (expected_visibles != visibles) { |
jjg@1521 | 138 | errors++; |
jjg@1521 | 139 | System.err.println("expected " + expected_visibles |
jjg@1521 | 140 | + " visibles annotations but found " + visibles); |
jjg@1521 | 141 | } |
jjg@1521 | 142 | |
jjg@1521 | 143 | if (expected_invisibles != invisibles) { |
jjg@1521 | 144 | errors++; |
jjg@1521 | 145 | System.err.println("expected " + expected_invisibles |
jjg@1521 | 146 | + " invisibles annotations but found " + invisibles); |
jjg@1521 | 147 | } |
jjg@1521 | 148 | |
jjg@1521 | 149 | } |
jjg@1521 | 150 | |
jjg@1521 | 151 | int errors; |
jjg@1521 | 152 | int all; |
jjg@1521 | 153 | int visibles; |
jjg@1521 | 154 | int invisibles; |
jjg@1521 | 155 | } |