1.1 --- a/test/tools/javap/typeAnnotations/Visibility.java Thu Oct 28 13:31:55 2010 -0700 1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 1.3 @@ -1,139 +0,0 @@ 1.4 -/* 1.5 - * Copyright (c) 2008, 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.Retention;"); 1.82 - out.println("import java.lang.annotation.RetentionPolicy;"); 1.83 - out.println("abstract class Test { "); 1.84 - // visible annotations: RUNTIME 1.85 - out.println(" @Retention(RetentionPolicy.RUNTIME)"); 1.86 - out.println(" @interface A { }"); 1.87 - out.println(" void visible() @A { }"); 1.88 - 1.89 - // invisible annotations: CLASS 1.90 - out.println(" @Retention(RetentionPolicy.CLASS)"); 1.91 - out.println(" @interface B { }"); 1.92 - out.println(" void invisible() @B { }"); 1.93 - 1.94 - // source annotations 1.95 - out.println(" @Retention(RetentionPolicy.SOURCE)"); 1.96 - out.println(" @interface C { }"); 1.97 - out.println(" void source() @C { }"); 1.98 - 1.99 - // default visibility: CLASS 1.100 - out.println(" @interface D { }"); 1.101 - out.println(" void def() @D { }"); 1.102 - out.println("}"); 1.103 - out.close(); 1.104 - return f; 1.105 - } 1.106 - 1.107 - File compileTestFile(File f) { 1.108 - int rc = com.sun.tools.javac.Main.compile(new String[] { "-source", "1.7", "-g", f.getPath() }); 1.109 - if (rc != 0) 1.110 - throw new Error("compilation failed. rc=" + rc); 1.111 - String path = f.getPath(); 1.112 - return new File(path.substring(0, path.length() - 5) + ".class"); 1.113 - } 1.114 - 1.115 - void countAnnotations() { 1.116 - int expected_all = 3, expected_visibles = 1, expected_invisibles = 2; 1.117 - 1.118 - if (expected_all != all) { 1.119 - errors++; 1.120 - System.err.println("expected " + expected_all 1.121 - + " annotations but found " + all); 1.122 - } 1.123 - 1.124 - if (expected_visibles != visibles) { 1.125 - errors++; 1.126 - System.err.println("expected " + expected_visibles 1.127 - + " visibles annotations but found " + visibles); 1.128 - } 1.129 - 1.130 - if (expected_invisibles != invisibles) { 1.131 - errors++; 1.132 - System.err.println("expected " + expected_invisibles 1.133 - + " invisibles annotations but found " + invisibles); 1.134 - } 1.135 - 1.136 - } 1.137 - 1.138 - int errors; 1.139 - int all; 1.140 - int visibles; 1.141 - int invisibles; 1.142 -}