Path: blob/master/test/langtools/tools/javap/AccessModifiers.java
41145 views
/*1* Copyright (c) 2013, 2016, 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* @modules jdk.jdeps/com.sun.tools.javap28*/2930import java.io.*;31import java.util.*;32import java.lang.StringBuilder;3334public class AccessModifiers {35public int errorCount;36protected String protectedField;37String packageField;38private String privateField;3940public static void main(String[] args) throws Exception {41new AccessModifiers().run();42}4344private void run() throws Exception {45List<String> pubMembers = new ArrayList<String>();46pubMembers.add("public int errorCount");47pubMembers.add("public AccessModifiers");48pubMembers.add("public static void main");4950List<String> proMembers = new ArrayList<String>();51proMembers.add("protected java.lang.String protectedField");52proMembers.add("protected java.lang.String runJavap");5354List<String> pkgMembers = new ArrayList<String>();55pkgMembers.add("java.lang.String packageField");56pkgMembers.add("boolean verify");57pkgMembers.add("void error");5859List<String> priMembers = new ArrayList<String>();60priMembers.add("private java.lang.String privateField");61priMembers.add("private void run() throws java.lang.Exception");62priMembers.add("private void test");6364List<String> expectedList = new ArrayList<String>();6566expectedList.addAll(pubMembers);67test("-public", expectedList);6869expectedList.addAll(proMembers);70test("-protected", expectedList);7172expectedList.addAll(pkgMembers);73test("-package", expectedList);7475expectedList.addAll(priMembers);76test("-private", expectedList);7778if (errorCount > 0)79throw new Exception(errorCount + " errors received");80}8182private void test(String option, List<String> expectedStrs) throws Exception {83String output = runJavap(0, option);84if (verify(output, expectedStrs))85System.out.println(option + " test passed");86}8788protected String runJavap(int expect, String... options) {89// convert the varargs to a list in order to add class name90List<String> optlist = new ArrayList<String>();91optlist.addAll(Arrays.asList(options));92optlist.add("AccessModifiers");93String[] newoptions = optlist.toArray(new String[optlist.size()]);94StringWriter sw = new StringWriter();95PrintWriter pw = new PrintWriter(sw);96System.out.printf("\nRun javap " + optlist + "\n\n");97int rc = com.sun.tools.javap.Main.run(newoptions, pw);98pw.close();99System.out.println(sw);100if (rc != expect)101throw new Error("Expect to return " + expect + ", but return " + rc);102return sw.toString();103}104105boolean verify(String output, List<String> expects) {106boolean pass = true;107for (String expect: expects) {108if (!output.contains(expect)) {109error(expect + " not found");110pass = false;111}112}113return pass;114}115116void error(String msg) {117System.err.println(msg);118errorCount++;119}120}121122123