Path: blob/master/test/langtools/tools/javap/T4501661.java
41144 views
/*1* Copyright (c) 2008, 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*/2223import java.io.*;24import java.util.*;2526/*27* @test28* @bug 450166129* @summary disallow mixing -public, -private, and -protected30* @modules jdk.jdeps/com.sun.tools.javap31*/32public class T4501661 {33public static void main(String... args) throws Exception {34new T4501661().run();35}3637void run() throws Exception {38File javaFile = writeTestFile();39File classFile = compileTestFile(javaFile);40boolean[] values = { false, true };41for (boolean pack : values) {42for (boolean priv : values) {43for (boolean prot : values) {44for (boolean publ : values) {45test(pack, priv, prot, publ, classFile);46}47}48}49}5051if (errors > 0)52throw new Exception(errors + " errors found");53}5455void test(boolean pack, boolean priv, boolean prot, boolean publ, File classFile) {56List<String> args = new ArrayList<String>();57if (pack)58args.add("-package");59if (priv)60args.add("-private");61if (prot)62args.add("-protected");63if (publ)64args.add("-public");65boolean expectOK = (args.size() <= 1);66args.add(classFile.getPath());67String out = javap(args, expectOK);68if (out == null)69return;70if (!priv && !prot && !publ)71checkNone(out, "private");72if (prot)73checkNone(out, "private", "package");74if (publ)75checkNone(out, "private", "package", "protected");76}7778File writeTestFile() throws IOException {79File f = new File("Test.java");80PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(f)));81out.println("abstract class Test { ");82out.println(" public void public_m() { }");83out.println(" protected void protected_m() { }");84out.println(" private void private_m() { }");85out.println(" void package_m() { }");86out.println("}");87out.close();88return f;89}9091File compileTestFile(File f) {92int rc = com.sun.tools.javac.Main.compile(new String[] { "-g", f.getPath() });93if (rc != 0)94throw new Error("compilation failed. rc=" + rc);95String path = f.getPath();96return new File(path.substring(0, path.length() - 5) + ".class");97}9899String javap(List<String> args, boolean expectOK) {100StringWriter sw = new StringWriter();101PrintWriter pw = new PrintWriter(sw);102int rc = com.sun.tools.javap.Main.run(args.toArray(new String[args.size()]), pw);103System.err.println(args);104System.err.println(sw);105if (expectOK) {106if (rc == 0)107return sw.toString();108else109error("javap failed unexpectedly; rc=" + rc + "\n" + sw);110} else {111if (rc == 0)112error("javap succeeded unexpectedly");113}114return null;115}116117void checkNone(String log, String... words) {118for (String word: words) {119if (log.indexOf(word) != -1)120error("\"" + word + "\" unexpectedly found in output");121}122}123124void error(String msg) {125System.err.println("error: " + msg);126errors++;127}128129int errors;130}131132133