aoqi@0: /* aoqi@0: * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: /* aoqi@0: * @test aoqi@0: * @bug 8027530 aoqi@0: * @summary test -public, -protected, -package, -private options aoqi@0: */ aoqi@0: aoqi@0: import java.io.*; aoqi@0: import java.util.*; aoqi@0: import java.lang.StringBuilder; aoqi@0: aoqi@0: public class AccessModifiers { aoqi@0: public int errorCount; aoqi@0: protected String protectedField; aoqi@0: String packageField; aoqi@0: private String privateField; aoqi@0: aoqi@0: public static void main(String[] args) throws Exception { aoqi@0: new AccessModifiers().run(); aoqi@0: } aoqi@0: aoqi@0: private void run() throws Exception { aoqi@0: List pubMembers = new ArrayList(); aoqi@0: pubMembers.add("public int errorCount"); aoqi@0: pubMembers.add("public AccessModifiers"); aoqi@0: pubMembers.add("public static void main"); aoqi@0: aoqi@0: List proMembers = new ArrayList(); aoqi@0: proMembers.add("protected java.lang.String protectedField"); aoqi@0: proMembers.add("protected java.lang.String runJavap"); aoqi@0: aoqi@0: List pkgMembers = new ArrayList(); aoqi@0: pkgMembers.add("java.lang.String packageField"); aoqi@0: pkgMembers.add("boolean verify"); aoqi@0: pkgMembers.add("void error"); aoqi@0: aoqi@0: List priMembers = new ArrayList(); aoqi@0: priMembers.add("private java.lang.String privateField"); aoqi@0: priMembers.add("private void run() throws java.lang.Exception"); aoqi@0: priMembers.add("private void test"); aoqi@0: aoqi@0: List expectedList = new ArrayList(); aoqi@0: aoqi@0: expectedList.addAll(pubMembers); aoqi@0: test("-public", expectedList); aoqi@0: aoqi@0: expectedList.addAll(proMembers); aoqi@0: test("-protected", expectedList); aoqi@0: aoqi@0: expectedList.addAll(pkgMembers); aoqi@0: test("-package", expectedList); aoqi@0: aoqi@0: expectedList.addAll(priMembers); aoqi@0: test("-private", expectedList); aoqi@0: aoqi@0: if (errorCount > 0) aoqi@0: throw new Exception(errorCount + " errors received"); aoqi@0: } aoqi@0: aoqi@0: private void test(String option, List expectedStrs) throws Exception { aoqi@0: String output = runJavap(0, option); aoqi@0: if (verify(output, expectedStrs)) aoqi@0: System.out.println(option + " test passed"); aoqi@0: } aoqi@0: aoqi@0: protected String runJavap(int expect, String... options) { aoqi@0: // convert the varargs to a list in order to add class name aoqi@0: List optlist = new ArrayList(); aoqi@0: optlist.addAll(Arrays.asList(options)); aoqi@0: optlist.add("AccessModifiers"); aoqi@0: String[] newoptions = optlist.toArray(new String[optlist.size()]); aoqi@0: StringWriter sw = new StringWriter(); aoqi@0: PrintWriter pw = new PrintWriter(sw); aoqi@0: System.out.printf("\nRun javap " + optlist + "\n\n"); aoqi@0: int rc = com.sun.tools.javap.Main.run(newoptions, pw); aoqi@0: pw.close(); aoqi@0: System.out.println(sw); aoqi@0: if (rc != expect) aoqi@0: throw new Error("Expect to return " + expect + ", but return " + rc); aoqi@0: return sw.toString(); aoqi@0: } aoqi@0: aoqi@0: boolean verify(String output, List expects) { aoqi@0: boolean pass = true; aoqi@0: for (String expect: expects) { aoqi@0: if (!output.contains(expect)) { aoqi@0: error(expect + " not found"); aoqi@0: pass = false; aoqi@0: } aoqi@0: } aoqi@0: return pass; aoqi@0: } aoqi@0: aoqi@0: void error(String msg) { aoqi@0: System.err.println(msg); aoqi@0: errorCount++; aoqi@0: } aoqi@0: }