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