langtools/test/tools/javap/T4501661.java
author serb
Thu, 14 Jan 2016 23:14:01 +0300
changeset 35687 78e8e8fea09b
parent 30846 2b3f379840f0
child 36526 3b41f1c69604
permissions -rw-r--r--
8147407: Provide support of WaveExtensible sound format Reviewed-by: prr, amenkov
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
/*
30730
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 26388
diff changeset
     2
 * Copyright (c) 2008, 2015, 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
30846
2b3f379840f0 8074432: Move jdeps and javap to jdk.jdeps module
mchung
parents: 30730
diff changeset
    31
 * @modules jdk.jdeps
862
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    32
 */
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    33
public class T4501661 {
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    34
    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
    35
        new T4501661().run();
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
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    38
    void run() throws Exception {
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    39
        File javaFile = writeTestFile();
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    40
        File classFile = compileTestFile(javaFile);
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    41
        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
    42
        for (boolean pack : values) {
6048554767c0 8044597: Request to update tools/javap/T4501661.java to add test for package option
kizune
parents: 5520
diff changeset
    43
            for (boolean priv : values) {
6048554767c0 8044597: Request to update tools/javap/T4501661.java to add test for package option
kizune
parents: 5520
diff changeset
    44
                for (boolean prot : values) {
6048554767c0 8044597: Request to update tools/javap/T4501661.java to add test for package option
kizune
parents: 5520
diff changeset
    45
                    for (boolean publ : values) {
6048554767c0 8044597: Request to update tools/javap/T4501661.java to add test for package option
kizune
parents: 5520
diff changeset
    46
                        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
    47
                    }
862
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
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    52
        if (errors > 0)
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    53
            throw new Exception(errors + " errors found");
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    54
    }
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    55
26388
6048554767c0 8044597: Request to update tools/javap/T4501661.java to add test for package option
kizune
parents: 5520
diff changeset
    56
    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
    57
        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
    58
        if (pack)
6048554767c0 8044597: Request to update tools/javap/T4501661.java to add test for package option
kizune
parents: 5520
diff changeset
    59
            args.add("-package");
862
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    60
        if (priv)
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    61
            args.add("-private");
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    62
        if (prot)
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    63
            args.add("-protected");
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    64
        if (publ)
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    65
            args.add("-public");
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    66
        boolean expectOK = (args.size() <= 1);
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    67
        args.add(classFile.getPath());
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    68
        String out = javap(args, expectOK);
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    69
        if (out == null)
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    70
            return;
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    71
        if (!priv && !prot && !publ)
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    72
            checkNone(out, "private");
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    73
        if (prot)
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    74
            checkNone(out, "private", "package");
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    75
        if (publ)
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    76
            checkNone(out, "private", "package", "protected");
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
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    79
    File writeTestFile() throws IOException {
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    80
        File f = new File("Test.java");
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    81
        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
    82
        out.println("abstract class Test { ");
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    83
        out.println("  public void public_m() { }");
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    84
        out.println("  protected void protected_m() { }");
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    85
        out.println("  private void private_m() { }");
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    86
        out.println("  void package_m() { }");
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    87
        out.println("}");
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    88
        out.close();
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    89
        return f;
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
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    92
    File compileTestFile(File f) {
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    93
        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
    94
        if (rc != 0)
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    95
            throw new Error("compilation failed. rc=" + rc);
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    96
        String path = f.getPath();
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    97
        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
    98
    }
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    99
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
   100
    String javap(List<String> args, boolean expectOK) {
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
   101
        StringWriter sw = new StringWriter();
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
   102
        PrintWriter pw = new PrintWriter(sw);
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
   103
        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
   104
        System.err.println(args);
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
   105
        System.err.println(sw);
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
   106
        if (expectOK) {
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
   107
            if (rc == 0)
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
   108
                return sw.toString();
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
   109
            else
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
   110
                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
   111
        } else {
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
   112
            if (rc == 0)
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
   113
                error("javap succeeded unexpectedly");
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
   114
        }
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
   115
        return null;
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
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
   118
    void checkNone(String log, String... words) {
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
   119
        for (String word: words) {
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
   120
            if (log.indexOf(word) != -1)
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
   121
                error("\"" + word + "\" unexpectedly found in output");
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
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
   125
    void error(String msg) {
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
   126
        System.err.println("error: " + msg);
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
   127
        errors++;
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
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
   130
    int errors;
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
   131
}