langtools/test/tools/javap/AccessModifiers.java
author ssides
Wed, 09 Jul 2014 15:14:06 +0400
changeset 25760 7955db32d6b0
parent 21707 d528488849cb
child 30730 d3ce7619db2c
permissions -rw-r--r--
8046597: fix doclint issues in swing classes, part 4 of 4 Reviewed-by: pchelko
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
21707
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
     1
/*
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
     2
 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
     4
 *
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
     7
 * published by the Free Software Foundation.
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
     8
 *
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
    13
 * accompanied this code).
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
    14
 *
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
    18
 *
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
    21
 * questions.
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
    22
 */
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
    23
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
    24
/*
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
    25
 * @test
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
    26
 * @bug 8027530
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
    27
 * @summary test -public, -protected, -package, -private options
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
    28
 */
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
    29
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
    30
import java.io.*;
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
    31
import java.util.*;
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
    32
import java.lang.StringBuilder;
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
    33
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
    34
public class AccessModifiers {
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
    35
    public int errorCount;
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
    36
    protected String protectedField;
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
    37
    String packageField;
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
    38
    private String privateField;
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
    39
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
    40
    public static void main(String[] args) throws Exception {
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
    41
        new AccessModifiers().run();
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
    42
    }
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
    43
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
    44
    private void run() throws Exception {
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
    45
        List<String> pubMembers = new ArrayList<String>();
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
    46
        pubMembers.add("public int errorCount");
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
    47
        pubMembers.add("public AccessModifiers");
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
    48
        pubMembers.add("public static void main");
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
    49
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
    50
        List<String> proMembers = new ArrayList<String>();
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
    51
        proMembers.add("protected java.lang.String protectedField");
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
    52
        proMembers.add("protected java.lang.String runJavap");
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
    53
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
    54
        List<String> pkgMembers = new ArrayList<String>();
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
    55
        pkgMembers.add("java.lang.String packageField");
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
    56
        pkgMembers.add("boolean verify");
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
    57
        pkgMembers.add("void error");
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
    58
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
    59
        List<String> priMembers = new ArrayList<String>();
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
    60
        priMembers.add("private java.lang.String privateField");
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
    61
        priMembers.add("private void run() throws java.lang.Exception");
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
    62
        priMembers.add("private void test");
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
    63
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
    64
        List<String> expectedList = new ArrayList<String>();
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
    65
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
    66
        expectedList.addAll(pubMembers);
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
    67
        test("-public", expectedList);
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
    68
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
    69
        expectedList.addAll(proMembers);
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
    70
        test("-protected", expectedList);
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
    71
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
    72
        expectedList.addAll(pkgMembers);
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
    73
        test("-package", expectedList);
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
    74
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
    75
        expectedList.addAll(priMembers);
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
    76
        test("-private", expectedList);
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
    77
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
    78
        if (errorCount > 0)
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
    79
            throw new Exception(errorCount + " errors received");
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
    80
    }
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
    81
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
    82
    private void test(String option, List<String> expectedStrs) throws Exception {
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
    83
        String output = runJavap(0, option);
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
    84
        if (verify(output, expectedStrs))
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
    85
            System.out.println(option + " test passed");
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
    86
    }
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
    87
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
    88
    protected String runJavap(int expect, String... options) {
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
    89
        // convert the varargs to a list in order to add class name
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
    90
        List<String> optlist = new ArrayList<String>();
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
    91
        optlist.addAll(Arrays.asList(options));
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
    92
        optlist.add("AccessModifiers");
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
    93
        String[] newoptions = optlist.toArray(new String[optlist.size()]);
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
    94
        StringWriter sw = new StringWriter();
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
    95
        PrintWriter pw = new PrintWriter(sw);
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
    96
        System.out.printf("\nRun javap " + optlist + "\n\n");
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
    97
        int rc = com.sun.tools.javap.Main.run(newoptions, pw);
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
    98
        pw.close();
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
    99
        System.out.println(sw);
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
   100
        if (rc != expect)
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
   101
           throw new Error("Expect to return " + expect + ", but return " + rc);
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
   102
        return sw.toString();
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
   103
    }
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
   104
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
   105
    boolean verify(String output, List<String> expects) {
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
   106
        boolean pass = true;
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
   107
        for (String expect: expects) {
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
   108
            if (!output.contains(expect)) {
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
   109
                error(expect + " not found");
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
   110
                pass = false;
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
   111
            }
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
   112
        }
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
   113
        return pass;
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
   114
    }
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
   115
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
   116
    void error(String msg) {
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
   117
        System.err.println(msg);
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
   118
        errorCount++;
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
   119
    }
d528488849cb 8027530: javap tonga tests cleanup: test -public, -protected, -package, -private options
cl
parents:
diff changeset
   120
}