Path: blob/aarch64-shenandoah-jdk8u272-b10/langtools/test/tools/javap/AccessModifiers.java
32285 views
/*1* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 802753026* @summary test -public, -protected, -package, -private options27*/2829import java.io.*;30import java.util.*;31import java.lang.StringBuilder;3233public class AccessModifiers {34public int errorCount;35protected String protectedField;36String packageField;37private String privateField;3839public static void main(String[] args) throws Exception {40new AccessModifiers().run();41}4243private void run() throws Exception {44List<String> pubMembers = new ArrayList<String>();45pubMembers.add("public int errorCount");46pubMembers.add("public AccessModifiers");47pubMembers.add("public static void main");4849List<String> proMembers = new ArrayList<String>();50proMembers.add("protected java.lang.String protectedField");51proMembers.add("protected java.lang.String runJavap");5253List<String> pkgMembers = new ArrayList<String>();54pkgMembers.add("java.lang.String packageField");55pkgMembers.add("boolean verify");56pkgMembers.add("void error");5758List<String> priMembers = new ArrayList<String>();59priMembers.add("private java.lang.String privateField");60priMembers.add("private void run() throws java.lang.Exception");61priMembers.add("private void test");6263List<String> expectedList = new ArrayList<String>();6465expectedList.addAll(pubMembers);66test("-public", expectedList);6768expectedList.addAll(proMembers);69test("-protected", expectedList);7071expectedList.addAll(pkgMembers);72test("-package", expectedList);7374expectedList.addAll(priMembers);75test("-private", expectedList);7677if (errorCount > 0)78throw new Exception(errorCount + " errors received");79}8081private void test(String option, List<String> expectedStrs) throws Exception {82String output = runJavap(0, option);83if (verify(output, expectedStrs))84System.out.println(option + " test passed");85}8687protected String runJavap(int expect, String... options) {88// convert the varargs to a list in order to add class name89List<String> optlist = new ArrayList<String>();90optlist.addAll(Arrays.asList(options));91optlist.add("AccessModifiers");92String[] newoptions = optlist.toArray(new String[optlist.size()]);93StringWriter sw = new StringWriter();94PrintWriter pw = new PrintWriter(sw);95System.out.printf("\nRun javap " + optlist + "\n\n");96int rc = com.sun.tools.javap.Main.run(newoptions, pw);97pw.close();98System.out.println(sw);99if (rc != expect)100throw new Error("Expect to return " + expect + ", but return " + rc);101return sw.toString();102}103104boolean verify(String output, List<String> expects) {105boolean pass = true;106for (String expect: expects) {107if (!output.contains(expect)) {108error(expect + " not found");109pass = false;110}111}112return pass;113}114115void error(String msg) {116System.err.println(msg);117errorCount++;118}119}120121122