0N/A/*
0N/A * Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
0N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
0N/A * or visit www.oracle.com if you need additional information or have any
0N/A * questions.
0N/A */
0N/A
0N/Aimport java.io.*;
0N/Aimport java.util.*;
0N/A
0N/A/*
0N/A * @test
0N/A * @bug 4501661
0N/A * @summary disallow mixing -public, -private, and -protected
0N/A */
0N/Apublic class T4501661 {
0N/A public static void main(String... args) throws Exception {
0N/A new T4501661().run();
0N/A }
0N/A
233N/A void run() throws Exception {
233N/A File javaFile = writeTestFile();
233N/A File classFile = compileTestFile(javaFile);
233N/A boolean[] values = { false, true };
233N/A for (boolean priv: values) {
233N/A for (boolean prot: values) {
233N/A for (boolean publ: values) {
233N/A test(priv, prot, publ, classFile);
233N/A }
233N/A }
233N/A }
233N/A
233N/A if (errors > 0)
233N/A throw new Exception(errors + " errors found");
233N/A }
564N/A
0N/A void test(boolean priv, boolean prot, boolean publ, File classFile) {
0N/A List<String> args = new ArrayList<String>();
0N/A if (priv)
0N/A args.add("-private");
0N/A if (prot)
0N/A args.add("-protected");
0N/A if (publ)
0N/A args.add("-public");
0N/A boolean expectOK = (args.size() <= 1);
0N/A args.add(classFile.getPath());
233N/A String out = javap(args, expectOK);
233N/A if (out == null)
0N/A return;
114N/A if (!priv && !prot && !publ)
114N/A checkNone(out, "private");
114N/A if (prot)
114N/A checkNone(out, "private", "package");
114N/A if (publ)
114N/A checkNone(out, "private", "package", "protected");
0N/A }
0N/A
0N/A File writeTestFile() throws IOException {
0N/A File f = new File("Test.java");
0N/A PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(f)));
0N/A out.println("abstract class Test { ");
0N/A out.println(" public void public_m() { }");
567N/A out.println(" protected void protected_m() { }");
567N/A out.println(" private void private_m() { }");
567N/A out.println(" void package_m() { }");
567N/A out.println("}");
567N/A out.close();
0N/A return f;
422N/A }
422N/A
422N/A File compileTestFile(File f) {
0N/A int rc = com.sun.tools.javac.Main.compile(new String[] { "-g", f.getPath() });
422N/A if (rc != 0)
0N/A throw new Error("compilation failed. rc=" + rc);
422N/A String path = f.getPath();
0N/A return new File(path.substring(0, path.length() - 5) + ".class");
0N/A }
0N/A
0N/A String javap(List<String> args, boolean expectOK) {
0N/A StringWriter sw = new StringWriter();
0N/A PrintWriter pw = new PrintWriter(sw);
0N/A int rc = com.sun.tools.javap.Main.run(args.toArray(new String[args.size()]), pw);
0N/A System.err.println(args);
233N/A System.err.println(sw);
233N/A if (expectOK) {
233N/A if (rc == 0)
233N/A return sw.toString();
0N/A else
0N/A error("javap failed unexpectedly; rc=" + rc + "\n" + sw);
0N/A } else {
0N/A if (rc == 0)
0N/A error("javap succeeded unexpectedly");
0N/A }
110N/A return null;
110N/A }
121N/A
0N/A void checkNone(String log, String... words) {
0N/A for (String word: words) {
0N/A if (log.indexOf(word) != -1)
0N/A error("\"" + word + "\" unexpectedly found in output");
0N/A }
0N/A }
0N/A
0N/A void error(String msg) {
0N/A System.err.println("error: " + msg);
0N/A errors++;
0N/A }
0N/A
0N/A int errors;
0N/A}
121N/A