langtools/test/tools/javac/T6985181.java
changeset 7382 e1ed8c9e12e5
parent 7381 5d924959cd81
parent 7140 4951967a61b4
child 7383 cbd66f8db06b
equal deleted inserted replaced
7381:5d924959cd81 7382:e1ed8c9e12e5
     1 /*
       
     2  * Copyright (c) 2010, 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 
       
    30 import java.io.*;
       
    31 import java.util.*;
       
    32 
       
    33 public 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 = "@interface Simple { }\ninterface Test<@Simple T> { }";
       
    40 
       
    41         File srcFile = writeFile("Test.java", code);
       
    42         File classesDir = new File("classes");
       
    43         classesDir.mkdirs();
       
    44         compile("-d", classesDir.getPath(), srcFile.getPath());
       
    45         String out = javap(new File(classesDir, srcFile.getName().replace(".java", ".class")));
       
    46         if (!out.contains("RuntimeInvisibleTypeAnnotations"))
       
    47             throw new Exception("RuntimeInvisibleTypeAnnotations not found");
       
    48     }
       
    49 
       
    50     void compile(String... args) throws Exception {
       
    51         StringWriter sw = new StringWriter();
       
    52         PrintWriter pw = new PrintWriter(sw);
       
    53         int rc = com.sun.tools.javac.Main.compile(args, pw);
       
    54         pw.close();
       
    55         String out = sw.toString();
       
    56         if (out.length() > 0)
       
    57             System.err.println(out);
       
    58         if (rc != 0)
       
    59             throw new Exception("Compilation failed: rc=" + rc);
       
    60     }
       
    61 
       
    62     String javap(File classFile) throws Exception {
       
    63         StringWriter sw = new StringWriter();
       
    64         PrintWriter pw = new PrintWriter(sw);
       
    65         String[] args = { "-v", classFile.getPath() };
       
    66         int rc = com.sun.tools.javap.Main.run(args, pw);
       
    67         pw.close();
       
    68         String out = sw.toString();
       
    69         if (out.length() > 0)
       
    70             System.err.println(out);
       
    71         if (rc != 0)
       
    72             throw new Exception("javap failed: rc=" + rc);
       
    73         return out;
       
    74     }
       
    75 
       
    76     File writeFile(String path, String body) throws IOException {
       
    77         File f = new File(path);
       
    78         FileWriter out = new FileWriter(f);
       
    79         try {
       
    80             out.write(body);
       
    81         } finally {
       
    82             out.close();
       
    83         }
       
    84         return f;
       
    85     }
       
    86 }