T6985181.java revision 1520:71f35e4b93a5
1/* 2 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24/* 25 * @test 26 * @bug 6985181 27 * @summary Annotations lost from classfile 28 */ 29 30import java.io.*; 31import java.util.*; 32 33public class T6985181 { 34 public static void main(String... args) throws Exception{ 35 new T6985181().run(); 36 } 37 38 public void run() throws Exception { 39 String code = "@java.lang.annotation.Target(java.lang.annotation.ElementType.TYPE_PARAMETER)\n" + 40 "@interface Simple { }\n" + 41 "interface Test<@Simple T> { }"; 42 43 File srcFile = writeFile("Test.java", code); 44 File classesDir = new File("classes"); 45 classesDir.mkdirs(); 46 compile("-d", classesDir.getPath(), srcFile.getPath()); 47 String out = javap(new File(classesDir, srcFile.getName().replace(".java", ".class"))); 48 if (!out.contains("RuntimeInvisibleTypeAnnotations")) 49 throw new Exception("RuntimeInvisibleTypeAnnotations not found"); 50 } 51 52 void compile(String... args) throws Exception { 53 StringWriter sw = new StringWriter(); 54 PrintWriter pw = new PrintWriter(sw); 55 int rc = com.sun.tools.javac.Main.compile(args, pw); 56 pw.close(); 57 String out = sw.toString(); 58 if (out.length() > 0) 59 System.err.println(out); 60 if (rc != 0) 61 throw new Exception("Compilation failed: rc=" + rc); 62 } 63 64 String javap(File classFile) throws Exception { 65 StringWriter sw = new StringWriter(); 66 PrintWriter pw = new PrintWriter(sw); 67 String[] args = { "-v", classFile.getPath() }; 68 int rc = com.sun.tools.javap.Main.run(args, pw); 69 pw.close(); 70 String out = sw.toString(); 71 if (out.length() > 0) 72 System.err.println(out); 73 if (rc != 0) 74 throw new Exception("javap failed: rc=" + rc); 75 return out; 76 } 77 78 File writeFile(String path, String body) throws IOException { 79 File f = new File(path); 80 FileWriter out = new FileWriter(f); 81 try { 82 out.write(body); 83 } finally { 84 out.close(); 85 } 86 return f; 87 } 88} 89