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