langtools/test/tools/javap/BoundsTypeVariableTest.java
author mchung
Wed, 27 May 2015 13:25:18 -0700
changeset 30846 2b3f379840f0
parent 30730 d3ce7619db2c
child 36526 3b41f1c69604
permissions -rw-r--r--
8074432: Move jdeps and javap to jdk.jdeps module Reviewed-by: jjg, alanb, erikj
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
20618
313dc106ebca 8003537: javap use internal class name when printing bound of type variable
ksrini
parents:
diff changeset
     1
/*
30730
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 20618
diff changeset
     2
 * Copyright (c) 2013, 2015, Oracle and/or its affiliates. All rights reserved.
20618
313dc106ebca 8003537: javap use internal class name when printing bound of type variable
ksrini
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
313dc106ebca 8003537: javap use internal class name when printing bound of type variable
ksrini
parents:
diff changeset
     4
 *
313dc106ebca 8003537: javap use internal class name when printing bound of type variable
ksrini
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
313dc106ebca 8003537: javap use internal class name when printing bound of type variable
ksrini
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
313dc106ebca 8003537: javap use internal class name when printing bound of type variable
ksrini
parents:
diff changeset
     7
 * published by the Free Software Foundation.
313dc106ebca 8003537: javap use internal class name when printing bound of type variable
ksrini
parents:
diff changeset
     8
 *
313dc106ebca 8003537: javap use internal class name when printing bound of type variable
ksrini
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
313dc106ebca 8003537: javap use internal class name when printing bound of type variable
ksrini
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
313dc106ebca 8003537: javap use internal class name when printing bound of type variable
ksrini
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
313dc106ebca 8003537: javap use internal class name when printing bound of type variable
ksrini
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
313dc106ebca 8003537: javap use internal class name when printing bound of type variable
ksrini
parents:
diff changeset
    13
 * accompanied this code).
313dc106ebca 8003537: javap use internal class name when printing bound of type variable
ksrini
parents:
diff changeset
    14
 *
313dc106ebca 8003537: javap use internal class name when printing bound of type variable
ksrini
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
313dc106ebca 8003537: javap use internal class name when printing bound of type variable
ksrini
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
313dc106ebca 8003537: javap use internal class name when printing bound of type variable
ksrini
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
313dc106ebca 8003537: javap use internal class name when printing bound of type variable
ksrini
parents:
diff changeset
    18
 *
313dc106ebca 8003537: javap use internal class name when printing bound of type variable
ksrini
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
313dc106ebca 8003537: javap use internal class name when printing bound of type variable
ksrini
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
313dc106ebca 8003537: javap use internal class name when printing bound of type variable
ksrini
parents:
diff changeset
    21
 * questions.
313dc106ebca 8003537: javap use internal class name when printing bound of type variable
ksrini
parents:
diff changeset
    22
 */
313dc106ebca 8003537: javap use internal class name when printing bound of type variable
ksrini
parents:
diff changeset
    23
313dc106ebca 8003537: javap use internal class name when printing bound of type variable
ksrini
parents:
diff changeset
    24
/**
313dc106ebca 8003537: javap use internal class name when printing bound of type variable
ksrini
parents:
diff changeset
    25
 * @test
313dc106ebca 8003537: javap use internal class name when printing bound of type variable
ksrini
parents:
diff changeset
    26
 * @bug 8003537
313dc106ebca 8003537: javap use internal class name when printing bound of type variable
ksrini
parents:
diff changeset
    27
 * @summary javap should not use / in Bounds Type Variables
30846
2b3f379840f0 8074432: Move jdeps and javap to jdk.jdeps module
mchung
parents: 30730
diff changeset
    28
 * @modules jdk.jdeps
20618
313dc106ebca 8003537: javap use internal class name when printing bound of type variable
ksrini
parents:
diff changeset
    29
 */
313dc106ebca 8003537: javap use internal class name when printing bound of type variable
ksrini
parents:
diff changeset
    30
313dc106ebca 8003537: javap use internal class name when printing bound of type variable
ksrini
parents:
diff changeset
    31
import java.io.File;
313dc106ebca 8003537: javap use internal class name when printing bound of type variable
ksrini
parents:
diff changeset
    32
import java.io.IOException;
313dc106ebca 8003537: javap use internal class name when printing bound of type variable
ksrini
parents:
diff changeset
    33
import java.io.PrintWriter;
313dc106ebca 8003537: javap use internal class name when printing bound of type variable
ksrini
parents:
diff changeset
    34
import java.io.StringWriter;
313dc106ebca 8003537: javap use internal class name when printing bound of type variable
ksrini
parents:
diff changeset
    35
import java.nio.charset.Charset;
313dc106ebca 8003537: javap use internal class name when printing bound of type variable
ksrini
parents:
diff changeset
    36
import java.nio.file.Files;
313dc106ebca 8003537: javap use internal class name when printing bound of type variable
ksrini
parents:
diff changeset
    37
import java.util.ArrayList;
313dc106ebca 8003537: javap use internal class name when printing bound of type variable
ksrini
parents:
diff changeset
    38
import java.util.List;
313dc106ebca 8003537: javap use internal class name when printing bound of type variable
ksrini
parents:
diff changeset
    39
import java.util.regex.Pattern;
313dc106ebca 8003537: javap use internal class name when printing bound of type variable
ksrini
parents:
diff changeset
    40
import static java.nio.file.StandardOpenOption.*;
313dc106ebca 8003537: javap use internal class name when printing bound of type variable
ksrini
parents:
diff changeset
    41
313dc106ebca 8003537: javap use internal class name when printing bound of type variable
ksrini
parents:
diff changeset
    42
public class BoundsTypeVariableTest {
313dc106ebca 8003537: javap use internal class name when printing bound of type variable
ksrini
parents:
diff changeset
    43
    public static void main(String... args) throws Exception {
313dc106ebca 8003537: javap use internal class name when printing bound of type variable
ksrini
parents:
diff changeset
    44
        new BoundsTypeVariableTest().run();
313dc106ebca 8003537: javap use internal class name when printing bound of type variable
ksrini
parents:
diff changeset
    45
    }
313dc106ebca 8003537: javap use internal class name when printing bound of type variable
ksrini
parents:
diff changeset
    46
313dc106ebca 8003537: javap use internal class name when printing bound of type variable
ksrini
parents:
diff changeset
    47
    void run() throws Exception {
313dc106ebca 8003537: javap use internal class name when printing bound of type variable
ksrini
parents:
diff changeset
    48
        File srcDir = new File("src");
313dc106ebca 8003537: javap use internal class name when printing bound of type variable
ksrini
parents:
diff changeset
    49
        srcDir.mkdirs();
313dc106ebca 8003537: javap use internal class name when printing bound of type variable
ksrini
parents:
diff changeset
    50
        File classesDir = new File("classes");
313dc106ebca 8003537: javap use internal class name when printing bound of type variable
ksrini
parents:
diff changeset
    51
        classesDir.mkdirs();
313dc106ebca 8003537: javap use internal class name when printing bound of type variable
ksrini
parents:
diff changeset
    52
        final String expect = "public abstract <U extends java.lang.Object> U doit();";
313dc106ebca 8003537: javap use internal class name when printing bound of type variable
ksrini
parents:
diff changeset
    53
        List<String> contents = new ArrayList<>();
313dc106ebca 8003537: javap use internal class name when printing bound of type variable
ksrini
parents:
diff changeset
    54
        contents.add("abstract class X {");
313dc106ebca 8003537: javap use internal class name when printing bound of type variable
ksrini
parents:
diff changeset
    55
        contents.add(expect);
313dc106ebca 8003537: javap use internal class name when printing bound of type variable
ksrini
parents:
diff changeset
    56
        contents.add("}");
313dc106ebca 8003537: javap use internal class name when printing bound of type variable
ksrini
parents:
diff changeset
    57
313dc106ebca 8003537: javap use internal class name when printing bound of type variable
ksrini
parents:
diff changeset
    58
        File f = writeFile(new File(srcDir, "X.java"), contents);
313dc106ebca 8003537: javap use internal class name when printing bound of type variable
ksrini
parents:
diff changeset
    59
        javac("-d", classesDir.getPath(), f.getPath());
313dc106ebca 8003537: javap use internal class name when printing bound of type variable
ksrini
parents:
diff changeset
    60
        String out = javap("-p", "-v", new File(classesDir, "X.class").getPath());
313dc106ebca 8003537: javap use internal class name when printing bound of type variable
ksrini
parents:
diff changeset
    61
        if (!out.contains(expect)) {
313dc106ebca 8003537: javap use internal class name when printing bound of type variable
ksrini
parents:
diff changeset
    62
            throw new Exception("expected pattern not found: " + expect);
313dc106ebca 8003537: javap use internal class name when printing bound of type variable
ksrini
parents:
diff changeset
    63
        }
313dc106ebca 8003537: javap use internal class name when printing bound of type variable
ksrini
parents:
diff changeset
    64
    }
313dc106ebca 8003537: javap use internal class name when printing bound of type variable
ksrini
parents:
diff changeset
    65
313dc106ebca 8003537: javap use internal class name when printing bound of type variable
ksrini
parents:
diff changeset
    66
    File writeFile(File f, List<String> body) throws IOException {
313dc106ebca 8003537: javap use internal class name when printing bound of type variable
ksrini
parents:
diff changeset
    67
        Files.write(f.toPath(), body, Charset.defaultCharset(),
313dc106ebca 8003537: javap use internal class name when printing bound of type variable
ksrini
parents:
diff changeset
    68
                CREATE, TRUNCATE_EXISTING);
313dc106ebca 8003537: javap use internal class name when printing bound of type variable
ksrini
parents:
diff changeset
    69
        return f;
313dc106ebca 8003537: javap use internal class name when printing bound of type variable
ksrini
parents:
diff changeset
    70
    }
313dc106ebca 8003537: javap use internal class name when printing bound of type variable
ksrini
parents:
diff changeset
    71
313dc106ebca 8003537: javap use internal class name when printing bound of type variable
ksrini
parents:
diff changeset
    72
    void javac(String... args) throws Exception {
313dc106ebca 8003537: javap use internal class name when printing bound of type variable
ksrini
parents:
diff changeset
    73
        StringWriter sw = new StringWriter();
313dc106ebca 8003537: javap use internal class name when printing bound of type variable
ksrini
parents:
diff changeset
    74
        PrintWriter pw = new PrintWriter(sw);
313dc106ebca 8003537: javap use internal class name when printing bound of type variable
ksrini
parents:
diff changeset
    75
        int rc = com.sun.tools.javac.Main.compile(args, pw);
313dc106ebca 8003537: javap use internal class name when printing bound of type variable
ksrini
parents:
diff changeset
    76
        pw.flush();
313dc106ebca 8003537: javap use internal class name when printing bound of type variable
ksrini
parents:
diff changeset
    77
        String out = sw.toString();
313dc106ebca 8003537: javap use internal class name when printing bound of type variable
ksrini
parents:
diff changeset
    78
        if (!out.isEmpty())
313dc106ebca 8003537: javap use internal class name when printing bound of type variable
ksrini
parents:
diff changeset
    79
            System.err.println(out);
313dc106ebca 8003537: javap use internal class name when printing bound of type variable
ksrini
parents:
diff changeset
    80
        if (rc != 0)
313dc106ebca 8003537: javap use internal class name when printing bound of type variable
ksrini
parents:
diff changeset
    81
            throw new Exception("compilation failed");
313dc106ebca 8003537: javap use internal class name when printing bound of type variable
ksrini
parents:
diff changeset
    82
    }
313dc106ebca 8003537: javap use internal class name when printing bound of type variable
ksrini
parents:
diff changeset
    83
313dc106ebca 8003537: javap use internal class name when printing bound of type variable
ksrini
parents:
diff changeset
    84
    String javap(String... args) throws Exception {
313dc106ebca 8003537: javap use internal class name when printing bound of type variable
ksrini
parents:
diff changeset
    85
        StringWriter sw = new StringWriter();
313dc106ebca 8003537: javap use internal class name when printing bound of type variable
ksrini
parents:
diff changeset
    86
        PrintWriter pw = new PrintWriter(sw);
313dc106ebca 8003537: javap use internal class name when printing bound of type variable
ksrini
parents:
diff changeset
    87
        int rc = com.sun.tools.javap.Main.run(args, pw);
313dc106ebca 8003537: javap use internal class name when printing bound of type variable
ksrini
parents:
diff changeset
    88
        pw.flush();
313dc106ebca 8003537: javap use internal class name when printing bound of type variable
ksrini
parents:
diff changeset
    89
        String out = sw.toString();
313dc106ebca 8003537: javap use internal class name when printing bound of type variable
ksrini
parents:
diff changeset
    90
        if (!out.isEmpty())
313dc106ebca 8003537: javap use internal class name when printing bound of type variable
ksrini
parents:
diff changeset
    91
            System.err.println(out);
313dc106ebca 8003537: javap use internal class name when printing bound of type variable
ksrini
parents:
diff changeset
    92
        if (rc != 0)
313dc106ebca 8003537: javap use internal class name when printing bound of type variable
ksrini
parents:
diff changeset
    93
            throw new Exception("javap failed");
313dc106ebca 8003537: javap use internal class name when printing bound of type variable
ksrini
parents:
diff changeset
    94
        return out;
313dc106ebca 8003537: javap use internal class name when printing bound of type variable
ksrini
parents:
diff changeset
    95
    }
313dc106ebca 8003537: javap use internal class name when printing bound of type variable
ksrini
parents:
diff changeset
    96
}