langtools/test/tools/javap/T4501661.java
author katleman
Thu, 21 Aug 2014 14:16:14 -0700
changeset 25878 6d561031123e
parent 5520 86e4b9a9da40
child 26388 6048554767c0
permissions -rw-r--r--
Added tag jdk9-b27 for changeset 98ce0879ab4c
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 };
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    41
        for (boolean priv: values) {
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    42
            for (boolean prot: values) {
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    43
                for (boolean publ: values) {
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    44
                    test(priv, prot, publ, classFile);
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    45
                }
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    46
            }
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
        if (errors > 0)
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    50
            throw new Exception(errors + " errors found");
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
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    53
    void test(boolean priv, boolean prot, boolean publ, File classFile) {
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    54
        List<String> args = new ArrayList<String>();
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    55
        if (priv)
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    56
            args.add("-private");
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    57
        if (prot)
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    58
            args.add("-protected");
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    59
        if (publ)
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    60
            args.add("-public");
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    61
        boolean expectOK = (args.size() <= 1);
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    62
        args.add(classFile.getPath());
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    63
        String out = javap(args, expectOK);
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    64
        if (out == null)
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    65
            return;
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    66
        if (!priv && !prot && !publ)
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    67
            checkNone(out, "private");
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    68
        if (prot)
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    69
            checkNone(out, "private", "package");
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    70
        if (publ)
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    71
            checkNone(out, "private", "package", "protected");
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    72
    }
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    73
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    74
    File writeTestFile() throws IOException {
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    75
        File f = new File("Test.java");
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    76
        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
    77
        out.println("abstract class Test { ");
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    78
        out.println("  public void public_m() { }");
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    79
        out.println("  protected void protected_m() { }");
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    80
        out.println("  private void private_m() { }");
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    81
        out.println("  void package_m() { }");
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    82
        out.println("}");
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    83
        out.close();
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    84
        return f;
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    85
    }
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    86
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    87
    File compileTestFile(File f) {
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    88
        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
    89
        if (rc != 0)
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    90
            throw new Error("compilation failed. rc=" + rc);
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    91
        String path = f.getPath();
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    92
        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
    93
    }
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    94
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    95
    String javap(List<String> args, boolean expectOK) {
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    96
        StringWriter sw = new StringWriter();
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    97
        PrintWriter pw = new PrintWriter(sw);
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
    98
        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
    99
        System.err.println(args);
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
   100
        System.err.println(sw);
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
   101
        if (expectOK) {
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
   102
            if (rc == 0)
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
   103
                return sw.toString();
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
   104
            else
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
   105
                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
   106
        } else {
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
                error("javap succeeded unexpectedly");
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
   109
        }
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
   110
        return null;
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
   111
    }
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
   112
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
   113
    void checkNone(String log, String... words) {
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
   114
        for (String word: words) {
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
   115
            if (log.indexOf(word) != -1)
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
   116
                error("\"" + word + "\" unexpectedly found in output");
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
    }
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
   119
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
   120
    void error(String msg) {
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
   121
        System.err.println("error: " + msg);
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
   122
        errors++;
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
    int errors;
44f1d401c8f5 4501661: disallow mixing -public, -private, and -protected options at the same time
jjg
parents:
diff changeset
   126
}