T6985181.java revision 3294:9adfb22ff08f
1/*
2 * Copyright (c) 2010, 2016, 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 * @modules jdk.compiler
29 *          jdk.jdeps/com.sun.tools.javap
30 */
31
32import java.io.*;
33import java.util.*;
34
35public class T6985181 {
36    public static void main(String... args) throws Exception{
37        new T6985181().run();
38    }
39
40    public void run() throws Exception {
41        String code = "@java.lang.annotation.Target(java.lang.annotation.ElementType.TYPE_PARAMETER)\n" +
42                "@interface Simple { }\n" +
43                "interface Test<@Simple T> { }";
44
45        File srcFile = writeFile("Test.java", code);
46        File classesDir = new File("classes");
47        classesDir.mkdirs();
48        compile("-d", classesDir.getPath(), srcFile.getPath());
49        String out = javap(new File(classesDir, srcFile.getName().replace(".java", ".class")));
50        if (!out.contains("RuntimeInvisibleTypeAnnotations"))
51            throw new Exception("RuntimeInvisibleTypeAnnotations not found");
52    }
53
54    void compile(String... args) throws Exception {
55        StringWriter sw = new StringWriter();
56        PrintWriter pw = new PrintWriter(sw);
57        int rc = com.sun.tools.javac.Main.compile(args, pw);
58        pw.close();
59        String out = sw.toString();
60        if (out.length() > 0)
61            System.err.println(out);
62        if (rc != 0)
63            throw new Exception("Compilation failed: rc=" + rc);
64    }
65
66    String javap(File classFile) throws Exception {
67        StringWriter sw = new StringWriter();
68        PrintWriter pw = new PrintWriter(sw);
69        String[] args = { "-v", classFile.getPath() };
70        int rc = com.sun.tools.javap.Main.run(args, pw);
71        pw.close();
72        String out = sw.toString();
73        if (out.length() > 0)
74            System.err.println(out);
75        if (rc != 0)
76            throw new Exception("javap failed: rc=" + rc);
77        return out;
78    }
79
80    File writeFile(String path, String body) throws IOException {
81        File f = new File(path);
82        FileWriter out = new FileWriter(f);
83        try {
84            out.write(body);
85        } finally {
86            out.close();
87        }
88        return f;
89    }
90}
91