Path: blob/aarch64-shenandoah-jdk8u272-b10/langtools/test/tools/javap/T4501661.java
32285 views
/*1* Copyright (c) 2008, 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*/31public class T4501661 {32public static void main(String... args) throws Exception {33new T4501661().run();34}3536void run() throws Exception {37File javaFile = writeTestFile();38File classFile = compileTestFile(javaFile);39boolean[] values = { false, true };40for (boolean priv: values) {41for (boolean prot: values) {42for (boolean publ: values) {43test(priv, prot, publ, classFile);44}45}46}4748if (errors > 0)49throw new Exception(errors + " errors found");50}5152void test(boolean priv, boolean prot, boolean publ, File classFile) {53List<String> args = new ArrayList<String>();54if (priv)55args.add("-private");56if (prot)57args.add("-protected");58if (publ)59args.add("-public");60boolean expectOK = (args.size() <= 1);61args.add(classFile.getPath());62String out = javap(args, expectOK);63if (out == null)64return;65if (!priv && !prot && !publ)66checkNone(out, "private");67if (prot)68checkNone(out, "private", "package");69if (publ)70checkNone(out, "private", "package", "protected");71}7273File writeTestFile() throws IOException {74File f = new File("Test.java");75PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(f)));76out.println("abstract class Test { ");77out.println(" public void public_m() { }");78out.println(" protected void protected_m() { }");79out.println(" private void private_m() { }");80out.println(" void package_m() { }");81out.println("}");82out.close();83return f;84}8586File compileTestFile(File f) {87int rc = com.sun.tools.javac.Main.compile(new String[] { "-g", f.getPath() });88if (rc != 0)89throw new Error("compilation failed. rc=" + rc);90String path = f.getPath();91return new File(path.substring(0, path.length() - 5) + ".class");92}9394String javap(List<String> args, boolean expectOK) {95StringWriter sw = new StringWriter();96PrintWriter pw = new PrintWriter(sw);97int rc = com.sun.tools.javap.Main.run(args.toArray(new String[args.size()]), pw);98System.err.println(args);99System.err.println(sw);100if (expectOK) {101if (rc == 0)102return sw.toString();103else104error("javap failed unexpectedly; rc=" + rc + "\n" + sw);105} else {106if (rc == 0)107error("javap succeeded unexpectedly");108}109return null;110}111112void checkNone(String log, String... words) {113for (String word: words) {114if (log.indexOf(word) != -1)115error("\"" + word + "\" unexpectedly found in output");116}117}118119void error(String msg) {120System.err.println("error: " + msg);121errors++;122}123124int errors;125}126127128