Tue, 24 Aug 2010 15:09:21 -0700
6929404: Filer.getResource(SOURCE_PATH, ...) does not work when -sourcepath contains >1 entry
Reviewed-by: darcy
1.1 --- a/src/share/classes/com/sun/tools/javac/processing/JavacFiler.java Tue Aug 24 11:31:00 2010 -0700 1.2 +++ b/src/share/classes/com/sun/tools/javac/processing/JavacFiler.java Tue Aug 24 15:09:21 2010 -0700 1.3 @@ -34,6 +34,7 @@ 1.4 import java.util.*; 1.5 1.6 import java.io.Closeable; 1.7 +import java.io.FileNotFoundException; 1.8 import java.io.InputStream; 1.9 import java.io.OutputStream; 1.10 import java.io.FilterOutputStream; 1.11 @@ -450,10 +451,15 @@ 1.12 // TODO: Only support reading resources in selected output 1.13 // locations? Only allow reading of non-source, non-class 1.14 // files from the supported input locations? 1.15 - FileObject fileObject = fileManager.getFileForOutput(location, 1.16 - pkg.toString(), 1.17 - relativeName.toString(), 1.18 - null); 1.19 + FileObject fileObject = fileManager.getFileForInput(location, 1.20 + pkg.toString(), 1.21 + relativeName.toString()); 1.22 + if (fileObject == null) { 1.23 + String name = (pkg.length() == 0) 1.24 + ? relativeName.toString() : (pkg + "/" + relativeName); 1.25 + throw new FileNotFoundException(name); 1.26 + } 1.27 + 1.28 // If the path was already opened for writing, throw an exception. 1.29 checkFileReopening(fileObject, false); 1.30 return new FilerInputFileObject(fileObject);
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/test/tools/javac/processing/filer/TestGetResource2.java Tue Aug 24 15:09:21 2010 -0700 2.3 @@ -0,0 +1,172 @@ 2.4 +/* 2.5 + * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved. 2.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 2.7 + * 2.8 + * This code is free software; you can redistribute it and/or modify it 2.9 + * under the terms of the GNU General Public License version 2 only, as 2.10 + * published by the Free Software Foundation. 2.11 + * 2.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 2.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 2.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 2.15 + * version 2 for more details (a copy is included in the LICENSE file that 2.16 + * accompanied this code). 2.17 + * 2.18 + * You should have received a copy of the GNU General Public License version 2.19 + * 2 along with this work; if not, write to the Free Software Foundation, 2.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 2.21 + * 2.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2.23 + * or visit www.oracle.com if you need additional information or have any 2.24 + * questions. 2.25 + */ 2.26 + 2.27 +/* @test 2.28 + * @bug 6929404 2.29 + * @summary Filer.getResource(SOURCE_PATH, ...) does not work when -sourcepath contains >1 entry 2.30 + */ 2.31 + 2.32 +import java.io.*; 2.33 +import java.security.*; 2.34 +import java.util.*; 2.35 +import javax.annotation.processing.*; 2.36 +import javax.lang.model.*; 2.37 +import javax.lang.model.element.*; 2.38 +import javax.tools.*; 2.39 +import javax.tools.Diagnostic.Kind; 2.40 +import javax.tools.JavaCompiler.CompilationTask; 2.41 + 2.42 +public class TestGetResource2 { 2.43 + 2.44 + public static void main(String[] args) throws Exception { 2.45 + new TestGetResource2().run(); 2.46 + } 2.47 + 2.48 + void run() throws Exception { 2.49 + JavaCompiler javac = ToolProvider.getSystemJavaCompiler(); 2.50 + CodeSource cs = javac.getClass().getProtectionDomain().getCodeSource(); 2.51 + if (cs == null) { 2.52 + System.err.println("got compiler from " + 2.53 + ClassLoader.getSystemResource(javac.getClass().getName().replace(".", "/")+".class")); 2.54 + } else { 2.55 + System.err.println("got compiler from " + cs.getLocation()); 2.56 + } 2.57 + 2.58 + testSingleSourceDir(javac); 2.59 + testCompositeSourcePath(javac); 2.60 + testMissingResource(javac); 2.61 + } 2.62 + 2.63 + private void testSingleSourceDir(JavaCompiler javac) throws Exception { 2.64 + System.err.println("testSingleSourceDir"); 2.65 + File tmpdir = new File("testSingleSourceDir"); 2.66 + File srcdir = new File(tmpdir, "src"); 2.67 + File destdir = new File(tmpdir, "dest"); 2.68 + write(srcdir, "pkg/X.java", "package pkg; class X {}"); 2.69 + write(srcdir, "resources/file.txt", "hello"); 2.70 + destdir.mkdirs(); 2.71 + 2.72 + CompilationTask task = javac.getTask(null, null, null, 2.73 + Arrays.asList("-sourcepath", srcdir.toString(), "-d", destdir.toString()), 2.74 + Collections.singleton("pkg.X"), null); 2.75 + task.setProcessors(Collections.singleton(new AnnoProc())); 2.76 + boolean result = task.call(); 2.77 + System.err.println("javac result with single source dir: " + result); 2.78 + expect(result, true); 2.79 + } 2.80 + 2.81 + private void testCompositeSourcePath(JavaCompiler javac) throws Exception { 2.82 + System.err.println("testCompositeSearchPath"); 2.83 + File tmpdir = new File("testCompositeSourcePath"); 2.84 + File srcdir = new File(tmpdir, "src"); 2.85 + File rsrcdir = new File(tmpdir, "rsrc"); 2.86 + File destdir = new File(tmpdir, "dest"); 2.87 + write(srcdir, "pkg/X.java", "package pkg; class X {}"); 2.88 + write(rsrcdir, "resources/file.txt", "hello"); 2.89 + destdir.mkdirs(); 2.90 + 2.91 + CompilationTask task = javac.getTask(null, null, null, 2.92 + Arrays.asList("-sourcepath", srcdir + File.pathSeparator + rsrcdir, "-d", destdir.toString()), 2.93 + Collections.singleton("pkg.X"), null); 2.94 + task.setProcessors(Collections.singleton(new AnnoProc())); 2.95 + boolean result = task.call(); 2.96 + System.err.println("javac result with composite source path: " + result); 2.97 + expect(result, true); 2.98 + } 2.99 + 2.100 + private void testMissingResource(JavaCompiler javac) throws Exception { 2.101 + System.err.println("testMissingResource"); 2.102 + File tmpdir = new File("testMissingResource"); 2.103 + File srcdir = new File(tmpdir, "src"); 2.104 + File destdir = new File(tmpdir, "dest"); 2.105 + write(srcdir, "pkg/X.java", "package pkg; class X {}"); 2.106 + destdir.mkdirs(); 2.107 + 2.108 + CompilationTask task = javac.getTask(null, null, null, 2.109 + Arrays.asList("-sourcepath", srcdir.toString(), "-d", destdir.toString()), 2.110 + Collections.singleton("pkg.X"), null); 2.111 + task.setProcessors(Collections.singleton(new AnnoProc())); 2.112 + boolean result = task.call(); 2.113 + System.err.println("javac result when missing resource: " + result); 2.114 + expect(result, false); 2.115 + 2.116 + if (errors > 0) 2.117 + throw new Exception(errors + " errors occurred"); 2.118 + } 2.119 + 2.120 + @SupportedAnnotationTypes("*") 2.121 + static class AnnoProc extends AbstractProcessor { 2.122 + 2.123 + public @Override boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { 2.124 + if (roundEnv.processingOver()) { 2.125 + return false; 2.126 + } 2.127 + 2.128 + try { 2.129 + FileObject resource = processingEnv.getFiler().getResource(StandardLocation.SOURCE_PATH, "resources", "file.txt"); 2.130 + try { 2.131 + resource.openInputStream().close(); 2.132 + processingEnv.getMessager().printMessage(Kind.NOTE, "found: " + resource.toUri()); 2.133 + return true; 2.134 + } catch (IOException x) { 2.135 + processingEnv.getMessager().printMessage(Kind.ERROR, "could not read: " + resource.toUri()); 2.136 + x.printStackTrace(); 2.137 + } 2.138 + } catch (IOException x) { 2.139 + processingEnv.getMessager().printMessage(Kind.ERROR, "did not find resource"); 2.140 + x.printStackTrace(); 2.141 + } 2.142 + 2.143 + return false; 2.144 + } 2.145 + 2.146 + @Override 2.147 + public SourceVersion getSupportedSourceVersion() { 2.148 + return SourceVersion.latest(); 2.149 + } 2.150 + } 2.151 + 2.152 + private File write(File dir, String path, String contents) throws IOException { 2.153 + File f = new File(dir, path); 2.154 + f.getParentFile().mkdirs(); 2.155 + Writer w = new FileWriter(f); 2.156 + try { 2.157 + w.write(contents); 2.158 + } finally { 2.159 + w.close(); 2.160 + } 2.161 + return f; 2.162 + } 2.163 + 2.164 + void expect(boolean val, boolean expect) { 2.165 + if (val != expect) 2.166 + error("Unexpected value: " + val + "; expected: " + expect); 2.167 + } 2.168 + 2.169 + void error(String msg) { 2.170 + System.err.println(msg); 2.171 + errors++; 2.172 + } 2.173 + 2.174 + int errors = 0; 2.175 +}