Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6929404

Filer.getResource(SOURCE_PATH, ...) does not work when -sourcepath contains >1 entry

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P2 P2
    • 7
    • 6
    • tools
    • b112
    • x86
    • linux
    • Verified

      Run this test case using JDK 6u18 or JDK 7 b77:

      ---%<---
      import java.io.File;
      import java.io.FileWriter;
      import java.io.IOException;
      import java.io.Writer;
      import java.util.Arrays;
      import java.util.Collections;
      import java.util.Set;
      import javax.annotation.processing.AbstractProcessor;
      import javax.annotation.processing.RoundEnvironment;
      import javax.annotation.processing.SupportedAnnotationTypes;
      import javax.annotation.processing.SupportedSourceVersion;
      import javax.lang.model.SourceVersion;
      import javax.lang.model.element.TypeElement;
      import javax.tools.Diagnostic.Kind;
      import javax.tools.FileObject;
      import javax.tools.JavaCompiler;
      import javax.tools.JavaCompiler.CompilationTask;
      import javax.tools.StandardLocation;
      import javax.tools.ToolProvider;
      @SupportedAnnotationTypes("*")
      @SupportedSourceVersion(SourceVersion.RELEASE_6)
      public class Demo extends AbstractProcessor {
          public static void main(String[] args) throws Exception {
              File srcdir = mkdir("src");
              File destdir = mkdir("dest");
              write(srcdir, "pkg/X.java", "package pkg; class X {}");
              write(srcdir, "resources/file.txt", "hello");
              JavaCompiler javac = ToolProvider.getSystemJavaCompiler();
              System.err.println("got compiler from " + javac.getClass().getProtectionDomain().getCodeSource().getLocation());
              CompilationTask task = javac.getTask(null, null, null,
                      Arrays.asList("-sourcepath", srcdir.toString(), "-d", destdir.toString()),
                      Collections.singleton("pkg.X"), null);
              task.setProcessors(Collections.singleton(new Demo()));
              System.err.println("javac result with single source dir: " + task.call());
              srcdir = mkdir("src");
              File rsrcdir = mkdir("rsrc");
              write(srcdir, "pkg/X.java", "package pkg; class X {}");
              write(rsrcdir, "resources/file.txt", "hello");
              task = javac.getTask(null, null, null,
                      Arrays.asList("-sourcepath", srcdir + File.pathSeparator + rsrcdir, "-d", destdir.toString()),
                      Collections.singleton("pkg.X"), null);
              task.setProcessors(Collections.singleton(new Demo()));
              System.err.println("javac result with composite source path: " + task.call());
              srcdir = mkdir("src");
              write(srcdir, "pkg/X.java", "package pkg; class X {}");
              task = javac.getTask(null, null, null,
                      Arrays.asList("-sourcepath", srcdir.toString(), "-d", destdir.toString()),
                      Collections.singleton("pkg.X"), null);
              task.setProcessors(Collections.singleton(new Demo()));
              System.err.println("javac result when missing resource: " + task.call());
          }
          public @Override boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
              if (roundEnv.processingOver()) {
                  return false;
              }
              try {
                  FileObject resource = processingEnv.getFiler().getResource(StandardLocation.SOURCE_PATH, "resources", "file.txt");
                  try {
                      resource.openInputStream().close();
                      processingEnv.getMessager().printMessage(Kind.NOTE, "found: " + resource.toUri());
                      return true;
                  } catch (IOException x) {
                      processingEnv.getMessager().printMessage(Kind.ERROR, "did not find: " + resource.toUri());
                      x.printStackTrace();
                  }
              } catch (IOException x) {
                  processingEnv.getMessager().printMessage(Kind.ERROR, "did not find resource");
                  x.printStackTrace();
              }
              return false;
          }
          private static File mkdir(String name) throws IOException {
              File dir = File.createTempFile(name, "");
              if (!dir.delete()) {
                  throw new IOException(dir.toString());
              }
              if (!dir.mkdir()) {
                  throw new IOException(dir.toString());
              }
              dir.deleteOnExit();
              return dir;
          }
          private static File write(File dir, String path, String contents) throws IOException {
              int i = path.indexOf('/');
              if (i == -1) {
                  File f = new File(dir, path);
                  f.deleteOnExit();
                  Writer w = new FileWriter(f);
                  try {
                      w.write(contents);
                  } finally {
                      w.close();
                  }
                  return f;
              } else {
                  File subdir = new File(dir, path.substring(0, i));
                  subdir.deleteOnExit();
                  if (!subdir.mkdir()) {
                      throw new IOException(subdir.toString());
                  }
                  return write(subdir, path.substring(i + 1), contents);
              }
          }
      }
      ---%<---

      On Ubuntu, I get:

      ---%<---
      got compiler from file:..../jdk1.6.0_18/lib/tools.jar
      Note: found: /tmp/src2920143692070020157/resources/file.txt
      javac result with single source dir: true
      error: did not find: /tmp/src453264489814761295/resources/file.txt
      java.io.FileNotFoundException: /tmp/src453264489814761295/resources/file.txt (No such file or directory)
              at java.io.FileInputStream.open(Native Method)
              at java.io.FileInputStream.<init>(FileInputStream.java:106)
              at com.sun.tools.javac.util.DefaultFileManager$RegularFileObject.openInputStream(DefaultFileManager.java:1277)
              at javax.tools.ForwardingFileObject.openInputStream(ForwardingFileObject.java:56)
              at Demo.process(Demo.java:59)
              at com.sun.tools.javac.processing.JavacProcessingEnvironment.callProcessor(JavacProcessingEnvironment.java:624)
              at com.sun.tools.javac.processing.JavacProcessingEnvironment.discoverAndRunProcs(JavacProcessingEnvironment.java:553)
              at com.sun.tools.javac.processing.JavacProcessingEnvironment.doProcessing(JavacProcessingEnvironment.java:698)
              at com.sun.tools.javac.main.JavaCompiler.processAnnotations(JavaCompiler.java:981)
              at com.sun.tools.javac.main.JavaCompiler.compile(JavaCompiler.java:727)
              at com.sun.tools.javac.main.Main.compile(Main.java:353)
              at com.sun.tools.javac.api.JavacTaskImpl.call(JavacTaskImpl.java:115)
              at Demo.main(Demo.java:43)
      1 error
      javac result with composite source path: false
      error: did not find: /tmp/src756707934637178516/resources/file.txt
      java.io.FileNotFoundException: /tmp/src756707934637178516/resources/file.txt (No such file or directory)
              at java.io.FileInputStream.open(Native Method)
              at java.io.FileInputStream.<init>(FileInputStream.java:106)
              at com.sun.tools.javac.util.DefaultFileManager$RegularFileObject.openInputStream(DefaultFileManager.java:1277)
              at javax.tools.ForwardingFileObject.openInputStream(ForwardingFileObject.java:56)
              at Demo.process(Demo.java:59)
              at com.sun.tools.javac.processing.JavacProcessingEnvironment.callProcessor(JavacProcessingEnvironment.java:624)
              at com.sun.tools.javac.processing.JavacProcessingEnvironment.discoverAndRunProcs(JavacProcessingEnvironment.java:553)
              at com.sun.tools.javac.processing.JavacProcessingEnvironment.doProcessing(JavacProcessingEnvironment.java:698)
              at com.sun.tools.javac.main.JavaCompiler.processAnnotations(JavaCompiler.java:981)
              at com.sun.tools.javac.main.JavaCompiler.compile(JavaCompiler.java:727)
              at com.sun.tools.javac.main.Main.compile(Main.java:353)
              at com.sun.tools.javac.api.JavacTaskImpl.call(JavacTaskImpl.java:115)
              at Demo.main(Demo.java:50)
      1 error
      javac result when missing resource: false
      ---%<---

      The second stack trace, from the third javac run, is expected (though the timing is odd: getResource succeeds). But the first error, from the second run, is not: resources/file.txt is present in the sourcepath passed to the compiler and ought to have been found.

            jjg Jonathan Gibbons
            jglick Jesse Glick (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: