1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/tools/javap/T4501661.java Wed Apr 27 01:34:52 2016 +0800 1.3 @@ -0,0 +1,126 @@ 1.4 +/* 1.5 + * Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + */ 1.26 + 1.27 +import java.io.*; 1.28 +import java.util.*; 1.29 + 1.30 +/* 1.31 + * @test 1.32 + * @bug 4501661 1.33 + * @summary disallow mixing -public, -private, and -protected 1.34 + */ 1.35 +public class T4501661 { 1.36 + public static void main(String... args) throws Exception { 1.37 + new T4501661().run(); 1.38 + } 1.39 + 1.40 + void run() throws Exception { 1.41 + File javaFile = writeTestFile(); 1.42 + File classFile = compileTestFile(javaFile); 1.43 + boolean[] values = { false, true }; 1.44 + for (boolean priv: values) { 1.45 + for (boolean prot: values) { 1.46 + for (boolean publ: values) { 1.47 + test(priv, prot, publ, classFile); 1.48 + } 1.49 + } 1.50 + } 1.51 + 1.52 + if (errors > 0) 1.53 + throw new Exception(errors + " errors found"); 1.54 + } 1.55 + 1.56 + void test(boolean priv, boolean prot, boolean publ, File classFile) { 1.57 + List<String> args = new ArrayList<String>(); 1.58 + if (priv) 1.59 + args.add("-private"); 1.60 + if (prot) 1.61 + args.add("-protected"); 1.62 + if (publ) 1.63 + args.add("-public"); 1.64 + boolean expectOK = (args.size() <= 1); 1.65 + args.add(classFile.getPath()); 1.66 + String out = javap(args, expectOK); 1.67 + if (out == null) 1.68 + return; 1.69 + if (!priv && !prot && !publ) 1.70 + checkNone(out, "private"); 1.71 + if (prot) 1.72 + checkNone(out, "private", "package"); 1.73 + if (publ) 1.74 + checkNone(out, "private", "package", "protected"); 1.75 + } 1.76 + 1.77 + File writeTestFile() throws IOException { 1.78 + File f = new File("Test.java"); 1.79 + PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(f))); 1.80 + out.println("abstract class Test { "); 1.81 + out.println(" public void public_m() { }"); 1.82 + out.println(" protected void protected_m() { }"); 1.83 + out.println(" private void private_m() { }"); 1.84 + out.println(" void package_m() { }"); 1.85 + out.println("}"); 1.86 + out.close(); 1.87 + return f; 1.88 + } 1.89 + 1.90 + File compileTestFile(File f) { 1.91 + int rc = com.sun.tools.javac.Main.compile(new String[] { "-g", f.getPath() }); 1.92 + if (rc != 0) 1.93 + throw new Error("compilation failed. rc=" + rc); 1.94 + String path = f.getPath(); 1.95 + return new File(path.substring(0, path.length() - 5) + ".class"); 1.96 + } 1.97 + 1.98 + String javap(List<String> args, boolean expectOK) { 1.99 + StringWriter sw = new StringWriter(); 1.100 + PrintWriter pw = new PrintWriter(sw); 1.101 + int rc = com.sun.tools.javap.Main.run(args.toArray(new String[args.size()]), pw); 1.102 + System.err.println(args); 1.103 + System.err.println(sw); 1.104 + if (expectOK) { 1.105 + if (rc == 0) 1.106 + return sw.toString(); 1.107 + else 1.108 + error("javap failed unexpectedly; rc=" + rc + "\n" + sw); 1.109 + } else { 1.110 + if (rc == 0) 1.111 + error("javap succeeded unexpectedly"); 1.112 + } 1.113 + return null; 1.114 + } 1.115 + 1.116 + void checkNone(String log, String... words) { 1.117 + for (String word: words) { 1.118 + if (log.indexOf(word) != -1) 1.119 + error("\"" + word + "\" unexpectedly found in output"); 1.120 + } 1.121 + } 1.122 + 1.123 + void error(String msg) { 1.124 + System.err.println("error: " + msg); 1.125 + errors++; 1.126 + } 1.127 + 1.128 + int errors; 1.129 +}