Sat, 22 Dec 2018 13:10:38 -0800
Added tag jdk8u202-b25 for changeset 15d8c732039b
jjg@1521 | 1 | /* |
jjg@1521 | 2 | * Copyright (c) 2008, 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 NewArray |
jjg@1521 | 29 | * @bug 6843077 |
jjg@1755 | 30 | * @summary Test type annotations on local array are in method's code attribute. |
jjg@1521 | 31 | */ |
jjg@1521 | 32 | |
jjg@1521 | 33 | public class NewArray { |
jjg@1755 | 34 | |
jjg@1521 | 35 | public static void main(String[] args) throws Exception { |
jjg@1521 | 36 | new NewArray().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 | // test the result of Attributes.getIndex according to expectations |
jjg@1521 | 61 | // encoded in the method's name |
jjg@1755 | 62 | void test(ClassFile cf, Method m, String name, boolean visible) { |
jjg@1755 | 63 | Attribute attr = null; |
jjg@1755 | 64 | Code_attribute cAttr = null; |
jjg@1755 | 65 | RuntimeTypeAnnotations_attribute tAttr = null; |
jjg@1521 | 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 | 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 | File writeTestFile() throws IOException { |
jjg@1521 | 87 | File f = new File("Test.java"); |
jjg@1521 | 88 | PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(f))); |
jjg@1521 | 89 | out.println("import java.lang.annotation.*;"); |
jjg@1521 | 90 | out.println("import java.util.*;"); |
jjg@1521 | 91 | out.println("class Test { "); |
jjg@1755 | 92 | out.println(" @Target(ElementType.TYPE_USE) @interface A { }"); |
jjg@1755 | 93 | out.println(" void test() {"); |
jjg@1755 | 94 | out.println(" Object a = new @A String @A [5] @A [];"); |
jjg@1755 | 95 | out.println(" Object b = new @A String @A [5] @A [3];"); |
jjg@1755 | 96 | out.println(" Object c = new @A String @A [] @A [] {};"); |
jjg@1755 | 97 | out.println(" }"); |
jjg@1521 | 98 | out.println("}"); |
jjg@1521 | 99 | |
jjg@1521 | 100 | out.close(); |
jjg@1521 | 101 | return f; |
jjg@1521 | 102 | } |
jjg@1521 | 103 | |
jjg@1521 | 104 | File compileTestFile(File f) { |
jjg@1521 | 105 | int rc = com.sun.tools.javac.Main.compile(new String[] { "-source", "1.8", "-g", f.getPath() }); |
jjg@1521 | 106 | if (rc != 0) |
jjg@1521 | 107 | throw new Error("compilation failed. rc=" + rc); |
jjg@1521 | 108 | String path = f.getPath(); |
jjg@1521 | 109 | return new File(path.substring(0, path.length() - 5) + ".class"); |
jjg@1521 | 110 | } |
jjg@1521 | 111 | |
jjg@1521 | 112 | void countAnnotations() { |
jjg@1521 | 113 | int expected_visibles = 0, expected_invisibles = 9; |
jjg@1521 | 114 | int expected_all = expected_visibles + expected_invisibles; |
jjg@1521 | 115 | |
jjg@1521 | 116 | if (expected_all != all) { |
jjg@1521 | 117 | errors++; |
jjg@1521 | 118 | System.err.println("expected " + expected_all |
jjg@1521 | 119 | + " annotations but found " + all); |
jjg@1521 | 120 | } |
jjg@1521 | 121 | |
jjg@1521 | 122 | if (expected_visibles != visibles) { |
jjg@1521 | 123 | errors++; |
jjg@1521 | 124 | System.err.println("expected " + expected_visibles |
jjg@1521 | 125 | + " visibles annotations but found " + visibles); |
jjg@1521 | 126 | } |
jjg@1521 | 127 | |
jjg@1521 | 128 | if (expected_invisibles != invisibles) { |
jjg@1521 | 129 | errors++; |
jjg@1521 | 130 | System.err.println("expected " + expected_invisibles |
jjg@1521 | 131 | + " invisibles annotations but found " + invisibles); |
jjg@1521 | 132 | } |
jjg@1521 | 133 | |
jjg@1521 | 134 | } |
jjg@1521 | 135 | |
jjg@1521 | 136 | int errors; |
jjg@1521 | 137 | int all; |
jjg@1521 | 138 | int visibles; |
jjg@1521 | 139 | int invisibles; |
jjg@1521 | 140 | } |