Tue, 08 Jul 2008 18:06:19 -0700
4501661: disallow mixing -public, -private, and -protected options at the same time
Reviewed-by: ksrini
1.1 --- a/src/share/classes/com/sun/tools/javap/JavapTask.java Tue Jul 08 17:53:03 2008 -0700 1.2 +++ b/src/share/classes/com/sun/tools/javap/JavapTask.java Tue Jul 08 18:06:19 2008 -0700 1.3 @@ -140,24 +140,31 @@ 1.4 1.5 new Option(false, "-public") { 1.6 void process(JavapTask task, String opt, String arg) { 1.7 + task.options.accessOptions.add(opt); 1.8 task.options.showAccess = AccessFlags.ACC_PUBLIC; 1.9 } 1.10 }, 1.11 1.12 new Option(false, "-protected") { 1.13 void process(JavapTask task, String opt, String arg) { 1.14 + task.options.accessOptions.add(opt); 1.15 task.options.showAccess = AccessFlags.ACC_PROTECTED; 1.16 } 1.17 }, 1.18 1.19 new Option(false, "-package") { 1.20 void process(JavapTask task, String opt, String arg) { 1.21 + task.options.accessOptions.add(opt); 1.22 task.options.showAccess = 0; 1.23 } 1.24 }, 1.25 1.26 new Option(false, "-p", "-private") { 1.27 void process(JavapTask task, String opt, String arg) { 1.28 + if (!task.options.accessOptions.contains("-p") && 1.29 + !task.options.accessOptions.contains("-private")) { 1.30 + task.options.accessOptions.add(opt); 1.31 + } 1.32 task.options.showAccess = AccessFlags.ACC_PRIVATE; 1.33 } 1.34 }, 1.35 @@ -386,6 +393,16 @@ 1.36 throw new BadArgs("err.unknown.option", arg).showUsage(true); 1.37 } 1.38 1.39 + if (!options.compat && options.accessOptions.size() > 1) { 1.40 + StringBuilder sb = new StringBuilder(); 1.41 + for (String opt: options.accessOptions) { 1.42 + if (sb.length() > 0) 1.43 + sb.append(" "); 1.44 + sb.append(opt); 1.45 + } 1.46 + throw new BadArgs("err.incompatible.options", sb); 1.47 + } 1.48 + 1.49 if (options.ignoreSymbolFile && fileManager instanceof JavapFileManager) 1.50 ((JavapFileManager) fileManager).setIgnoreSymbolFile(true); 1.51
2.1 --- a/src/share/classes/com/sun/tools/javap/Options.java Tue Jul 08 17:53:03 2008 -0700 2.2 +++ b/src/share/classes/com/sun/tools/javap/Options.java Tue Jul 08 18:06:19 2008 -0700 2.3 @@ -25,6 +25,8 @@ 2.4 2.5 package com.sun.tools.javap; 2.6 2.7 +import java.util.HashSet; 2.8 +import java.util.Set; 2.9 import com.sun.tools.classfile.AccessFlags; 2.10 2.11 /* 2.12 @@ -74,6 +76,7 @@ 2.13 public boolean showFlags; 2.14 public boolean showLineAndLocalVariableTables; 2.15 public int showAccess; 2.16 + public Set<String> accessOptions = new HashSet<String>(); 2.17 public boolean showDisassembled; 2.18 public boolean showInternalSignatures; 2.19 public boolean showAllAttrs;
3.1 --- a/src/share/classes/com/sun/tools/javap/resources/javap.properties Tue Jul 08 17:53:03 2008 -0700 3.2 +++ b/src/share/classes/com/sun/tools/javap/resources/javap.properties Tue Jul 08 18:06:19 2008 -0700 3.3 @@ -7,6 +7,7 @@ 3.4 err.end.of.file=unexpected end of file while reading {0} 3.5 err.file.not.found=file not found: {0} 3.6 err.h.not.supported=-h is no longer available - use the 'javah' program 3.7 +err.incompatible.options=bad combination of options: {0} 3.8 err.internal.error=internal error: {0} {1} {2} 3.9 err.ioerror=IO error reading {0}: {1} 3.10 err.missing.arg=no value given for {0}
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 4.2 +++ b/test/tools/javap/T4501661.java Tue Jul 08 18:06:19 2008 -0700 4.3 @@ -0,0 +1,126 @@ 4.4 +/* 4.5 + * Copyright 2008 Sun Microsystems, Inc. All Rights Reserved. 4.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4.7 + * 4.8 + * This code is free software; you can redistribute it and/or modify it 4.9 + * under the terms of the GNU General Public License version 2 only, as 4.10 + * published by the Free Software Foundation. 4.11 + * 4.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 4.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 4.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 4.15 + * version 2 for more details (a copy is included in the LICENSE file that 4.16 + * accompanied this code). 4.17 + * 4.18 + * You should have received a copy of the GNU General Public License version 4.19 + * 2 along with this work; if not, write to the Free Software Foundation, 4.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 4.21 + * 4.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 4.23 + * CA 95054 USA or visit www.sun.com if you need additional information or 4.24 + * have any questions. 4.25 + */ 4.26 + 4.27 +import java.io.*; 4.28 +import java.util.*; 4.29 + 4.30 +/* 4.31 + * @test 4.32 + * @bug 4501661 4.33 + * @summary disallow mixing -public, -private, and -protected 4.34 + */ 4.35 +public class T4501661 { 4.36 + public static void main(String... args) throws Exception { 4.37 + new T4501661().run(); 4.38 + } 4.39 + 4.40 + void run() throws Exception { 4.41 + File javaFile = writeTestFile(); 4.42 + File classFile = compileTestFile(javaFile); 4.43 + boolean[] values = { false, true }; 4.44 + for (boolean priv: values) { 4.45 + for (boolean prot: values) { 4.46 + for (boolean publ: values) { 4.47 + test(priv, prot, publ, classFile); 4.48 + } 4.49 + } 4.50 + } 4.51 + 4.52 + if (errors > 0) 4.53 + throw new Exception(errors + " errors found"); 4.54 + } 4.55 + 4.56 + void test(boolean priv, boolean prot, boolean publ, File classFile) { 4.57 + List<String> args = new ArrayList<String>(); 4.58 + if (priv) 4.59 + args.add("-private"); 4.60 + if (prot) 4.61 + args.add("-protected"); 4.62 + if (publ) 4.63 + args.add("-public"); 4.64 + boolean expectOK = (args.size() <= 1); 4.65 + args.add(classFile.getPath()); 4.66 + String out = javap(args, expectOK); 4.67 + if (out == null) 4.68 + return; 4.69 + if (!priv && !prot && !publ) 4.70 + checkNone(out, "private"); 4.71 + if (prot) 4.72 + checkNone(out, "private", "package"); 4.73 + if (publ) 4.74 + checkNone(out, "private", "package", "protected"); 4.75 + } 4.76 + 4.77 + File writeTestFile() throws IOException { 4.78 + File f = new File("Test.java"); 4.79 + PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(f))); 4.80 + out.println("abstract class Test { "); 4.81 + out.println(" public void public_m() { }"); 4.82 + out.println(" protected void protected_m() { }"); 4.83 + out.println(" private void private_m() { }"); 4.84 + out.println(" void package_m() { }"); 4.85 + out.println("}"); 4.86 + out.close(); 4.87 + return f; 4.88 + } 4.89 + 4.90 + File compileTestFile(File f) { 4.91 + int rc = com.sun.tools.javac.Main.compile(new String[] { "-g", f.getPath() }); 4.92 + if (rc != 0) 4.93 + throw new Error("compilation failed. rc=" + rc); 4.94 + String path = f.getPath(); 4.95 + return new File(path.substring(0, path.length() - 5) + ".class"); 4.96 + } 4.97 + 4.98 + String javap(List<String> args, boolean expectOK) { 4.99 + StringWriter sw = new StringWriter(); 4.100 + PrintWriter pw = new PrintWriter(sw); 4.101 + int rc = com.sun.tools.javap.Main.run(args.toArray(new String[args.size()]), pw); 4.102 + System.err.println(args); 4.103 + System.err.println(sw); 4.104 + if (expectOK) { 4.105 + if (rc == 0) 4.106 + return sw.toString(); 4.107 + else 4.108 + error("javap failed unexpectedly; rc=" + rc + "\n" + sw); 4.109 + } else { 4.110 + if (rc == 0) 4.111 + error("javap succeeded unexpectedly"); 4.112 + } 4.113 + return null; 4.114 + } 4.115 + 4.116 + void checkNone(String log, String... words) { 4.117 + for (String word: words) { 4.118 + if (log.indexOf(word) != -1) 4.119 + error("\"" + word + "\" unexpectedly found in output"); 4.120 + } 4.121 + } 4.122 + 4.123 + void error(String msg) { 4.124 + System.err.println("error: " + msg); 4.125 + errors++; 4.126 + } 4.127 + 4.128 + int errors; 4.129 +}