langtools/test/tools/javap/T4501661.java
author kizune
Wed, 03 Sep 2014 19:22:16 +0400
changeset 26388 6048554767c0
parent 5520 86e4b9a9da40
child 30730 d3ce7619db2c
permissions -rw-r--r--
8044597: Request to update tools/javap/T4501661.java to add test for package option Reviewed-by: jjg
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
862
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
     1
/*
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 862
diff changeset
     2
 * Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.
862
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
     4
 *
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
     7
 * published by the Free Software Foundation.
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
     8
 *
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    13
 * accompanied this code).
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    14
 *
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    18
 *
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 862
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 862
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 862
diff changeset
    21
 * questions.
862
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    22
 */
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    23
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    24
import java.io.*;
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    25
import java.util.*;
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    26
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    27
/*
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    28
 * @test
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    29
 * @bug 4501661
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    30
 * @summary disallow mixing -public, -private, and -protected
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    31
 */
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    32
public class T4501661 {
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    33
    public static void main(String... args) throws Exception {
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    34
        new T4501661().run();
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    35
    }
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    36
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    37
    void run() throws Exception {
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    38
        File javaFile = writeTestFile();
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    39
        File classFile = compileTestFile(javaFile);
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    40
        boolean[] values = { false, true };
26388
6048554767c0 8044597: Request to update tools/javap/T4501661.java to add test for package option
kizune
parents: 5520
diff changeset
    41
        for (boolean pack : values) {
6048554767c0 8044597: Request to update tools/javap/T4501661.java to add test for package option
kizune
parents: 5520
diff changeset
    42
            for (boolean priv : values) {
6048554767c0 8044597: Request to update tools/javap/T4501661.java to add test for package option
kizune
parents: 5520
diff changeset
    43
                for (boolean prot : values) {
6048554767c0 8044597: Request to update tools/javap/T4501661.java to add test for package option
kizune
parents: 5520
diff changeset
    44
                    for (boolean publ : values) {
6048554767c0 8044597: Request to update tools/javap/T4501661.java to add test for package option
kizune
parents: 5520
diff changeset
    45
                        test(pack, priv, prot, publ, classFile);
6048554767c0 8044597: Request to update tools/javap/T4501661.java to add test for package option
kizune
parents: 5520
diff changeset
    46
                    }
862
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    47
                }
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    48
            }
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    49
        }
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    50
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    51
        if (errors > 0)
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    52
            throw new Exception(errors + " errors found");
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    53
    }
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    54
26388
6048554767c0 8044597: Request to update tools/javap/T4501661.java to add test for package option
kizune
parents: 5520
diff changeset
    55
    void test(boolean pack, boolean priv, boolean prot, boolean publ, File classFile) {
862
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    56
        List<String> args = new ArrayList<String>();
26388
6048554767c0 8044597: Request to update tools/javap/T4501661.java to add test for package option
kizune
parents: 5520
diff changeset
    57
        if (pack)
6048554767c0 8044597: Request to update tools/javap/T4501661.java to add test for package option
kizune
parents: 5520
diff changeset
    58
            args.add("-package");
862
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    59
        if (priv)
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    60
            args.add("-private");
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    61
        if (prot)
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    62
            args.add("-protected");
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    63
        if (publ)
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    64
            args.add("-public");
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    65
        boolean expectOK = (args.size() <= 1);
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    66
        args.add(classFile.getPath());
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    67
        String out = javap(args, expectOK);
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    68
        if (out == null)
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    69
            return;
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    70
        if (!priv && !prot && !publ)
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    71
            checkNone(out, "private");
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    72
        if (prot)
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    73
            checkNone(out, "private", "package");
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    74
        if (publ)
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    75
            checkNone(out, "private", "package", "protected");
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    76
    }
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    77
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    78
    File writeTestFile() throws IOException {
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    79
        File f = new File("Test.java");
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    80
        PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(f)));
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    81
        out.println("abstract class Test { ");
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    82
        out.println("  public void public_m() { }");
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    83
        out.println("  protected void protected_m() { }");
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    84
        out.println("  private void private_m() { }");
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    85
        out.println("  void package_m() { }");
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    86
        out.println("}");
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    87
        out.close();
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    88
        return f;
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    89
    }
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    90
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    91
    File compileTestFile(File f) {
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    92
        int rc = com.sun.tools.javac.Main.compile(new String[] { "-g", f.getPath() });
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    93
        if (rc != 0)
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    94
            throw new Error("compilation failed. rc=" + rc);
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    95
        String path = f.getPath();
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    96
        return new File(path.substring(0, path.length() - 5) + ".class");
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    97
    }
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    98
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    99
    String javap(List<String> args, boolean expectOK) {
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
   100
        StringWriter sw = new StringWriter();
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
   101
        PrintWriter pw = new PrintWriter(sw);
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
   102
        int rc = com.sun.tools.javap.Main.run(args.toArray(new String[args.size()]), pw);
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
   103
        System.err.println(args);
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
   104
        System.err.println(sw);
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
   105
        if (expectOK) {
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
   106
            if (rc == 0)
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
   107
                return sw.toString();
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
   108
            else
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
   109
                error("javap failed unexpectedly; rc=" + rc + "\n" + sw);
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
   110
        } else {
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
   111
            if (rc == 0)
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
   112
                error("javap succeeded unexpectedly");
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
   113
        }
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
   114
        return null;
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
   115
    }
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
   116
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
   117
    void checkNone(String log, String... words) {
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
   118
        for (String word: words) {
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
   119
            if (log.indexOf(word) != -1)
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
   120
                error("\"" + word + "\" unexpectedly found in output");
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
   121
        }
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
   122
    }
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
   123
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
   124
    void error(String msg) {
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
   125
        System.err.println("error: " + msg);
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
   126
        errors++;
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
   127
    }
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
   128
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
   129
    int errors;
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
   130
}