Fri, 30 Oct 2009 10:55:00 -0700
Added tag jdk7-b75 for changeset 2485f5641ed0
jjg@68 | 1 | /* |
jjg@68 | 2 | * Copyright 2008 Sun Microsystems, Inc. All Rights Reserved. |
jjg@68 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
jjg@68 | 4 | * |
jjg@68 | 5 | * This code is free software; you can redistribute it and/or modify it |
jjg@68 | 6 | * under the terms of the GNU General Public License version 2 only, as |
jjg@68 | 7 | * published by the Free Software Foundation. |
jjg@68 | 8 | * |
jjg@68 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
jjg@68 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
jjg@68 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
jjg@68 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
jjg@68 | 13 | * accompanied this code). |
jjg@68 | 14 | * |
jjg@68 | 15 | * You should have received a copy of the GNU General Public License version |
jjg@68 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
jjg@68 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
jjg@68 | 18 | * |
jjg@68 | 19 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
jjg@68 | 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
jjg@68 | 21 | * have any questions. |
jjg@68 | 22 | */ |
jjg@68 | 23 | |
jjg@68 | 24 | import java.io.*; |
jjg@68 | 25 | import java.util.*; |
jjg@68 | 26 | |
jjg@68 | 27 | /* |
jjg@68 | 28 | * @test |
jjg@68 | 29 | * @bug 4501661 |
jjg@68 | 30 | * @summary disallow mixing -public, -private, and -protected |
jjg@68 | 31 | */ |
jjg@68 | 32 | public class T4501661 { |
jjg@68 | 33 | public static void main(String... args) throws Exception { |
jjg@68 | 34 | new T4501661().run(); |
jjg@68 | 35 | } |
jjg@68 | 36 | |
jjg@68 | 37 | void run() throws Exception { |
jjg@68 | 38 | File javaFile = writeTestFile(); |
jjg@68 | 39 | File classFile = compileTestFile(javaFile); |
jjg@68 | 40 | boolean[] values = { false, true }; |
jjg@68 | 41 | for (boolean priv: values) { |
jjg@68 | 42 | for (boolean prot: values) { |
jjg@68 | 43 | for (boolean publ: values) { |
jjg@68 | 44 | test(priv, prot, publ, classFile); |
jjg@68 | 45 | } |
jjg@68 | 46 | } |
jjg@68 | 47 | } |
jjg@68 | 48 | |
jjg@68 | 49 | if (errors > 0) |
jjg@68 | 50 | throw new Exception(errors + " errors found"); |
jjg@68 | 51 | } |
jjg@68 | 52 | |
jjg@68 | 53 | void test(boolean priv, boolean prot, boolean publ, File classFile) { |
jjg@68 | 54 | List<String> args = new ArrayList<String>(); |
jjg@68 | 55 | if (priv) |
jjg@68 | 56 | args.add("-private"); |
jjg@68 | 57 | if (prot) |
jjg@68 | 58 | args.add("-protected"); |
jjg@68 | 59 | if (publ) |
jjg@68 | 60 | args.add("-public"); |
jjg@68 | 61 | boolean expectOK = (args.size() <= 1); |
jjg@68 | 62 | args.add(classFile.getPath()); |
jjg@68 | 63 | String out = javap(args, expectOK); |
jjg@68 | 64 | if (out == null) |
jjg@68 | 65 | return; |
jjg@68 | 66 | if (!priv && !prot && !publ) |
jjg@68 | 67 | checkNone(out, "private"); |
jjg@68 | 68 | if (prot) |
jjg@68 | 69 | checkNone(out, "private", "package"); |
jjg@68 | 70 | if (publ) |
jjg@68 | 71 | checkNone(out, "private", "package", "protected"); |
jjg@68 | 72 | } |
jjg@68 | 73 | |
jjg@68 | 74 | File writeTestFile() throws IOException { |
jjg@68 | 75 | File f = new File("Test.java"); |
jjg@68 | 76 | PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(f))); |
jjg@68 | 77 | out.println("abstract class Test { "); |
jjg@68 | 78 | out.println(" public void public_m() { }"); |
jjg@68 | 79 | out.println(" protected void protected_m() { }"); |
jjg@68 | 80 | out.println(" private void private_m() { }"); |
jjg@68 | 81 | out.println(" void package_m() { }"); |
jjg@68 | 82 | out.println("}"); |
jjg@68 | 83 | out.close(); |
jjg@68 | 84 | return f; |
jjg@68 | 85 | } |
jjg@68 | 86 | |
jjg@68 | 87 | File compileTestFile(File f) { |
jjg@68 | 88 | int rc = com.sun.tools.javac.Main.compile(new String[] { "-g", f.getPath() }); |
jjg@68 | 89 | if (rc != 0) |
jjg@68 | 90 | throw new Error("compilation failed. rc=" + rc); |
jjg@68 | 91 | String path = f.getPath(); |
jjg@68 | 92 | return new File(path.substring(0, path.length() - 5) + ".class"); |
jjg@68 | 93 | } |
jjg@68 | 94 | |
jjg@68 | 95 | String javap(List<String> args, boolean expectOK) { |
jjg@68 | 96 | StringWriter sw = new StringWriter(); |
jjg@68 | 97 | PrintWriter pw = new PrintWriter(sw); |
jjg@68 | 98 | int rc = com.sun.tools.javap.Main.run(args.toArray(new String[args.size()]), pw); |
jjg@68 | 99 | System.err.println(args); |
jjg@68 | 100 | System.err.println(sw); |
jjg@68 | 101 | if (expectOK) { |
jjg@68 | 102 | if (rc == 0) |
jjg@68 | 103 | return sw.toString(); |
jjg@68 | 104 | else |
jjg@68 | 105 | error("javap failed unexpectedly; rc=" + rc + "\n" + sw); |
jjg@68 | 106 | } else { |
jjg@68 | 107 | if (rc == 0) |
jjg@68 | 108 | error("javap succeeded unexpectedly"); |
jjg@68 | 109 | } |
jjg@68 | 110 | return null; |
jjg@68 | 111 | } |
jjg@68 | 112 | |
jjg@68 | 113 | void checkNone(String log, String... words) { |
jjg@68 | 114 | for (String word: words) { |
jjg@68 | 115 | if (log.indexOf(word) != -1) |
jjg@68 | 116 | error("\"" + word + "\" unexpectedly found in output"); |
jjg@68 | 117 | } |
jjg@68 | 118 | } |
jjg@68 | 119 | |
jjg@68 | 120 | void error(String msg) { |
jjg@68 | 121 | System.err.println("error: " + msg); |
jjg@68 | 122 | errors++; |
jjg@68 | 123 | } |
jjg@68 | 124 | |
jjg@68 | 125 | int errors; |
jjg@68 | 126 | } |