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

Filer.getResource doesn't throw IOException when getting nonexistent file

        The spec for javax.annotation.processing.Filer.getResource() states that
        IOException is thrown if the file cannot be opened.

        So when we are trying to open nonexistent resource we should expect IOException thrown.

        However getting nonexistent resource doesn't cause to IOException. Instead IllegalStateExceptionis thrown.

        You may reproduce the behavior using following processor class:

        package tmp;

        import java.io.InputStream;
        import java.io.Reader;
        import javax.annotation.processing.AbstractProcessor;
        import javax.annotation.processing.RoundEnvironment;
        import javax.annotation.processing.Filer;
        import javax.lang.model.element.TypeElement;
        import javax.lang.model.SourceVersion;
        import java.util.Set;
        import java.util.HashSet;
        import java.io.IOException;
        import javax.tools.StandardLocation;

        public class UnexistingResourceProcessor extends AbstractProcessor {
            StandardLocation[] locations = {
                 StandardLocation.CLASS_OUTPUT, StandardLocation.SOURCE_OUTPUT
            };

            public Set<String> getSupportedAnnotationTypes() {
                return new HashSet<String>() {{add("*");}};
            }

            public SourceVersion getSupportedSourceVersion() {
                return SourceVersion.RELEASE_6;
            }

            public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
                boolean passed = true;
                if(roundEnv.processingOver()) {
                    Filer filer = processingEnv.getFiler();
                    for (StandardLocation location: locations) {
                        try {
                            Reader reader = filer.createResource(
                                    location, "", "foo/" + location + ".unexisting").openReader(true);
                            reader.close();
                            System.out.println("location: " + location);
                        } catch (IOException expected) {
                        } catch (Exception e) {
                            passed = false;
                            System.out.println("location: " + location + ", thrown: " + e);
                        }
                    }
                }
                return true;
            }
        }

        # javac -cp . -processor tmp.UnexistingResourceProcessor java.lang.Object
        location: CLASS_OUTPUT, thrown: java.lang.IllegalStateException: FileObject was not opened for reading.
        location: SOURCE_OUTPUT, thrown: java.lang.IllegalStateException: FileObject was not opened for reading.

        As a result of running these tests IOException is not thrown. In fact IllegalStateException is thrown.

        I suppose it is unexpected behavior.

              darcy Joe Darcy
              ydanilev Yury Danilevich (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: