1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/tools/javap/typeAnnotations/Visibility.java Wed Apr 27 01:34:52 2016 +0800 1.3 @@ -0,0 +1,145 @@ 1.4 +/* 1.5 + * Copyright (c) 2008, 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 Visibility 1.32 + * @bug 6843077 1.33 + * @summary test that type annotations are recorded in the classfile 1.34 + */ 1.35 + 1.36 +public class Visibility { 1.37 + public static void main(String[] args) throws Exception { 1.38 + new Visibility().run(); 1.39 + } 1.40 + 1.41 + public void run() throws Exception { 1.42 + File javaFile = writeTestFile(); 1.43 + File classFile = compileTestFile(javaFile); 1.44 + 1.45 + ClassFile cf = ClassFile.read(classFile); 1.46 + for (Method m: cf.methods) { 1.47 + test(cf, m); 1.48 + } 1.49 + 1.50 + countAnnotations(); 1.51 + 1.52 + if (errors > 0) 1.53 + throw new Exception(errors + " errors found"); 1.54 + System.out.println("PASSED"); 1.55 + } 1.56 + 1.57 + void test(ClassFile cf, Method m) { 1.58 + test(cf, m, Attribute.RuntimeVisibleTypeAnnotations, true); 1.59 + test(cf, m, Attribute.RuntimeInvisibleTypeAnnotations, false); 1.60 + } 1.61 + 1.62 + // test the result of Attributes.getIndex according to expectations 1.63 + // encoded in the method's name 1.64 + void test(ClassFile cf, Method m, String name, boolean visible) { 1.65 + int index = m.attributes.getIndex(cf.constant_pool, name); 1.66 + if (index != -1) { 1.67 + Attribute attr = m.attributes.get(index); 1.68 + assert attr instanceof RuntimeTypeAnnotations_attribute; 1.69 + RuntimeTypeAnnotations_attribute tAttr = (RuntimeTypeAnnotations_attribute)attr; 1.70 + all += tAttr.annotations.length; 1.71 + if (visible) 1.72 + visibles += tAttr.annotations.length; 1.73 + else 1.74 + invisibles += tAttr.annotations.length; 1.75 + } 1.76 + } 1.77 + 1.78 + File writeTestFile() throws IOException { 1.79 + File f = new File("Test.java"); 1.80 + PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(f))); 1.81 + out.println("import java.lang.annotation.ElementType;"); 1.82 + out.println("import java.lang.annotation.Retention;"); 1.83 + out.println("import java.lang.annotation.RetentionPolicy;"); 1.84 + out.println("import java.lang.annotation.Target;"); 1.85 + out.println("abstract class Test { "); 1.86 + // visible annotations: RUNTIME 1.87 + out.println(" @Retention(RetentionPolicy.RUNTIME)"); 1.88 + out.println(" @Target(ElementType.TYPE_USE)"); 1.89 + out.println(" @interface A { }"); 1.90 + out.println(" void visible(@A Test this) { }"); 1.91 + 1.92 + // invisible annotations: CLASS 1.93 + out.println(" @Retention(RetentionPolicy.CLASS)"); 1.94 + out.println(" @Target(ElementType.TYPE_USE)"); 1.95 + out.println(" @interface B { }"); 1.96 + out.println(" void invisible(@B Test this) { }"); 1.97 + 1.98 + // source annotations 1.99 + out.println(" @Retention(RetentionPolicy.SOURCE)"); 1.100 + out.println(" @Target(ElementType.TYPE_USE)"); 1.101 + out.println(" @interface C { }"); 1.102 + out.println(" void source(@C Test this) { }"); 1.103 + 1.104 + // default visibility: CLASS 1.105 + out.println(" @Target(ElementType.TYPE_USE)"); 1.106 + out.println(" @interface D { }"); 1.107 + out.println(" void def(@D Test this) { }"); 1.108 + out.println("}"); 1.109 + out.close(); 1.110 + return f; 1.111 + } 1.112 + 1.113 + File compileTestFile(File f) { 1.114 + int rc = com.sun.tools.javac.Main.compile(new String[] { "-source", "1.8", "-g", f.getPath() }); 1.115 + if (rc != 0) 1.116 + throw new Error("compilation failed. rc=" + rc); 1.117 + String path = f.getPath(); 1.118 + return new File(path.substring(0, path.length() - 5) + ".class"); 1.119 + } 1.120 + 1.121 + void countAnnotations() { 1.122 + int expected_all = 3, expected_visibles = 1, expected_invisibles = 2; 1.123 + 1.124 + if (expected_all != all) { 1.125 + errors++; 1.126 + System.err.println("expected " + expected_all 1.127 + + " annotations but found " + all); 1.128 + } 1.129 + 1.130 + if (expected_visibles != visibles) { 1.131 + errors++; 1.132 + System.err.println("expected " + expected_visibles 1.133 + + " visibles annotations but found " + visibles); 1.134 + } 1.135 + 1.136 + if (expected_invisibles != invisibles) { 1.137 + errors++; 1.138 + System.err.println("expected " + expected_invisibles 1.139 + + " invisibles annotations but found " + invisibles); 1.140 + } 1.141 + 1.142 + } 1.143 + 1.144 + int errors; 1.145 + int all; 1.146 + int visibles; 1.147 + int invisibles; 1.148 +}