Thu, 13 Jan 2011 11:48:10 -0800
6430241: Hard to disable symbol file feature through API
Reviewed-by: mcimadamore
1.1 --- a/src/share/classes/com/sun/tools/javac/file/JavacFileManager.java Tue Jan 11 08:05:28 2011 -0800 1.2 +++ b/src/share/classes/com/sun/tools/javac/file/JavacFileManager.java Thu Jan 13 11:48:10 2011 -0800 1.3 @@ -1,5 +1,5 @@ 1.4 /* 1.5 - * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 1.6 + * Copyright (c) 2005, 2011, Oracle and/or its affiliates. All rights reserved. 1.7 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.8 * 1.9 * This code is free software; you can redistribute it and/or modify it 1.10 @@ -472,7 +472,7 @@ 1.11 */ 1.12 protected Archive openArchive(File zipFileName) throws IOException { 1.13 File origZipFileName = zipFileName; 1.14 - if (!ignoreSymbolFile && paths.isBootClassPathRtJar(zipFileName)) { 1.15 + if (!ignoreSymbolFile && paths.isDefaultBootClassPathRtJar(zipFileName)) { 1.16 File file = zipFileName.getParentFile().getParentFile(); // ${java.home} 1.17 if (new File(file.getName()).equals(new File("jre"))) 1.18 file = file.getParentFile();
2.1 --- a/src/share/classes/com/sun/tools/javac/file/Paths.java Tue Jan 11 08:05:28 2011 -0800 2.2 +++ b/src/share/classes/com/sun/tools/javac/file/Paths.java Thu Jan 13 11:48:10 2011 -0800 2.3 @@ -1,5 +1,5 @@ 2.4 /* 2.5 - * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved. 2.6 + * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved. 2.7 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 2.8 * 2.9 * This code is free software; you can redistribute it and/or modify it 2.10 @@ -112,7 +112,7 @@ 2.11 * rt.jar as found on the default bootclass path. If the user specified a 2.12 * bootclasspath, null is used. 2.13 */ 2.14 - private File bootClassPathRtJar = null; 2.15 + private File defaultBootClassPathRtJar = null; 2.16 2.17 /** 2.18 * Is bootclasspath the default? 2.19 @@ -143,8 +143,10 @@ 2.20 // no defaults for other paths 2.21 p = null; 2.22 } else { 2.23 - if (location == PLATFORM_CLASS_PATH) 2.24 + if (location == PLATFORM_CLASS_PATH) { 2.25 + defaultBootClassPathRtJar = null; 2.26 isDefaultBootClassPath = false; 2.27 + } 2.28 p = new Path(); 2.29 for (File f: path) 2.30 p.addFile(f, warn); // TODO: is use of warn appropriate? 2.31 @@ -185,8 +187,8 @@ 2.32 : Collections.unmodifiableCollection(p); 2.33 } 2.34 2.35 - boolean isBootClassPathRtJar(File file) { 2.36 - return file.equals(bootClassPathRtJar); 2.37 + boolean isDefaultBootClassPathRtJar(File file) { 2.38 + return file.equals(defaultBootClassPathRtJar); 2.39 } 2.40 2.41 /** 2.42 @@ -355,7 +357,7 @@ 2.43 } 2.44 2.45 private Path computeBootClassPath() { 2.46 - bootClassPathRtJar = null; 2.47 + defaultBootClassPathRtJar = null; 2.48 Path path = new Path(); 2.49 2.50 String bootclasspathOpt = options.get(BOOTCLASSPATH); 2.51 @@ -380,7 +382,7 @@ 2.52 File rt_jar = new File("rt.jar"); 2.53 for (File file : getPathEntries(files)) { 2.54 if (new File(file.getName()).equals(rt_jar)) 2.55 - bootClassPathRtJar = file; 2.56 + defaultBootClassPathRtJar = file; 2.57 } 2.58 } 2.59
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 3.2 +++ b/test/tools/javac/api/T6430241.java Thu Jan 13 11:48:10 2011 -0800 3.3 @@ -0,0 +1,237 @@ 3.4 + /* 3.5 + * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved. 3.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 3.7 + * 3.8 + * This code is free software; you can redistribute it and/or modify it 3.9 + * under the terms of the GNU General Public License version 2 only, as 3.10 + * published by the Free Software Foundation. 3.11 + * 3.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 3.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 3.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 3.15 + * version 2 for more details (a copy is included in the LICENSE file that 3.16 + * accompanied this code). 3.17 + * 3.18 + * You should have received a copy of the GNU General Public License version 3.19 + * 2 along with this work; if not, write to the Free Software Foundation, 3.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 3.21 + * 3.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 3.23 + * or visit www.oracle.com if you need additional information or have any 3.24 + * questions. 3.25 + */ 3.26 + 3.27 + /* 3.28 + * @test 3.29 + * @bug 6430241 3.30 + * @summary Hard to disable symbol file feature through API 3.31 + */ 3.32 + 3.33 + import java.io.*; 3.34 + import java.util.*; 3.35 + import javax.tools.*; 3.36 + 3.37 + import com.sun.source.util.JavacTask; 3.38 + import com.sun.tools.javac.api.JavacTool; 3.39 + 3.40 + public class T6430241 { 3.41 + public static void main(String... args) throws Exception { 3.42 + new T6430241().run(); 3.43 + } 3.44 + 3.45 + void run() throws Exception { 3.46 + setup(); 3.47 + testCommandLine(); 3.48 + testSimpleAPI(); 3.49 + testTaskAPI(); 3.50 + 3.51 + if (errors > 0) 3.52 + throw new Exception(errors + " errors found"); 3.53 + } 3.54 + 3.55 + void setup() throws Exception { 3.56 + classesDir = new File("classes"); 3.57 + classesDir.mkdirs(); 3.58 + 3.59 + emptyDir = new File("empty"); 3.60 + emptyDir.mkdirs(); 3.61 + 3.62 + bootClassPath = System.getProperty("sun.boot.class.path"); 3.63 + 3.64 + File srcDir = new File("src"); 3.65 + String test = "import sun.misc.Unsafe; class Test { }"; 3.66 + testFile = writeFile(srcDir, "Test.java", test); 3.67 + } 3.68 + 3.69 + //----- tests for command line invocation 3.70 + 3.71 + void testCommandLine() throws Exception { 3.72 + testCommandLine(true); 3.73 + testCommandLine(true, "-Xbootclasspath/p:" + emptyDir); 3.74 + testCommandLine(false, "-Xbootclasspath:" + bootClassPath); 3.75 + testCommandLine(true, "-Xbootclasspath/a:" + emptyDir); 3.76 + testCommandLine(false, "-XDignore.symbol.file"); 3.77 + System.err.println(); 3.78 + } 3.79 + 3.80 + void testCommandLine(boolean expectWarnings, String... opts) throws Exception { 3.81 + System.err.println("test command line: " + Arrays.asList(opts)); 3.82 + 3.83 + String[] args = initArgs(opts); 3.84 + 3.85 + StringWriter sw = new StringWriter(); 3.86 + PrintWriter pw = new PrintWriter(sw); 3.87 + int rc = com.sun.tools.javac.Main.compile(args, pw); 3.88 + String out = showOutput(sw.toString()); 3.89 + 3.90 + checkCompilationOK(rc); 3.91 + checkOutput(out, expectWarnings); 3.92 + } 3.93 + 3.94 + //----- tests for simple API invocation 3.95 + 3.96 + void testSimpleAPI() { 3.97 + testSimpleAPI(true); 3.98 + testSimpleAPI(true, "-Xbootclasspath/p:" + emptyDir); 3.99 + testSimpleAPI(false, "-Xbootclasspath:" + bootClassPath); 3.100 + testSimpleAPI(true, "-Xbootclasspath/a:" + emptyDir); 3.101 + testSimpleAPI(false, "-XDignore.symbol.file"); 3.102 + System.err.println(); 3.103 + } 3.104 + 3.105 + void testSimpleAPI(boolean expectWarnings, String... opts) { 3.106 + System.err.println("test simple API: " + Arrays.asList(opts)); 3.107 + 3.108 + String[] args = initArgs(opts); 3.109 + 3.110 + ByteArrayOutputStream baos = new ByteArrayOutputStream(); 3.111 + PrintStream ps = new PrintStream(baos); 3.112 + 3.113 + JavacTool tool = JavacTool.create(); 3.114 + int rc = tool.run(null, null, ps, args); 3.115 + 3.116 + String out = showOutput(baos.toString()); 3.117 + 3.118 + checkCompilationOK(rc); 3.119 + checkOutput(out, expectWarnings); 3.120 + } 3.121 + 3.122 + //----- tests for CompilationTask API invocation 3.123 + 3.124 + void testTaskAPI() throws Exception { 3.125 + List<File> bcp = new ArrayList<File>(); 3.126 + for (String f: bootClassPath.split(File.pathSeparator)) { 3.127 + if (!f.isEmpty()) 3.128 + bcp.add(new File(f)); 3.129 + } 3.130 + 3.131 + testTaskAPI(true, null); 3.132 + testTaskAPI(false, bcp); 3.133 + System.err.println(); 3.134 + } 3.135 + 3.136 + void testTaskAPI(boolean expectWarnings, Iterable<? extends File> pcp) throws Exception { 3.137 + System.err.println("test task API: " + pcp); 3.138 + 3.139 + JavacTool tool = JavacTool.create(); 3.140 + StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null); 3.141 + 3.142 + if (pcp != null) 3.143 + fm.setLocation(StandardLocation.PLATFORM_CLASS_PATH, pcp); 3.144 + 3.145 + Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(testFile); 3.146 + 3.147 + StringWriter sw = new StringWriter(); 3.148 + PrintWriter pw = new PrintWriter(sw); 3.149 + JavacTask task = tool.getTask(pw, fm, null, null, null, files); 3.150 + boolean ok = task.call(); 3.151 + String out = showOutput(sw.toString()); 3.152 + 3.153 + checkCompilationOK(ok); 3.154 + checkOutput(out, expectWarnings); 3.155 + } 3.156 + 3.157 + //----- utility methods 3.158 + 3.159 + /** 3.160 + * Create a file with given content. 3.161 + */ 3.162 + File writeFile(File dir, String path, String content) throws IOException { 3.163 + File f = new File(dir, path); 3.164 + f.getParentFile().mkdirs(); 3.165 + FileWriter out = new FileWriter(f); 3.166 + try { 3.167 + out.write(content); 3.168 + } finally { 3.169 + out.close(); 3.170 + } 3.171 + return f; 3.172 + } 3.173 + 3.174 + /** 3.175 + * Initialize args for compilation with given opts. 3.176 + * @return opts -d classesDir testFile 3.177 + */ 3.178 + String[] initArgs(String[] opts) { 3.179 + List<String> args = new ArrayList<String>(); 3.180 + args.addAll(Arrays.asList(opts)); 3.181 + args.add("-d"); 3.182 + args.add(classesDir.getPath()); 3.183 + args.add(testFile.getPath()); 3.184 + return args.toArray(new String[args.size()]); 3.185 + } 3.186 + 3.187 + /** 3.188 + * Show output from compilation if non empty. 3.189 + */ 3.190 + String showOutput(String out) { 3.191 + if (!out.isEmpty()) 3.192 + System.err.println(out); 3.193 + return out; 3.194 + } 3.195 + 3.196 + /** 3.197 + * Verify compilation succeeeded. 3.198 + */ 3.199 + void checkCompilationOK(boolean ok) { 3.200 + if (!ok) 3.201 + error("compilation failed"); 3.202 + } 3.203 + 3.204 + /** 3.205 + * Verify compilation succeeeded. 3.206 + */ 3.207 + void checkCompilationOK(int rc) { 3.208 + if (rc != 0) 3.209 + error("compilation failed, rc: " + rc); 3.210 + } 3.211 + 3.212 + /** 3.213 + * Check whether output contains warnings if and only if warnings 3.214 + * are expected. 3.215 + */ 3.216 + void checkOutput(String out, boolean expectWarnings) { 3.217 + boolean foundWarnings = out.contains("warning"); 3.218 + if (foundWarnings) { 3.219 + if (!expectWarnings) 3.220 + error("unexpected warnings found"); 3.221 + } else { 3.222 + if (expectWarnings) 3.223 + error("expected warnings not found"); 3.224 + } 3.225 + } 3.226 + 3.227 + /** 3.228 + * Report an error. 3.229 + */ 3.230 + void error(String msg) { 3.231 + System.err.println("error: " + msg); 3.232 + errors++; 3.233 + } 3.234 + 3.235 + String bootClassPath; 3.236 + File classesDir; 3.237 + File emptyDir; 3.238 + File testFile; 3.239 + int errors; 3.240 + }