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