-
Bug
-
Resolution: Not an Issue
-
P3
-
6u1
-
generic
-
generic
Issue | Fix Version | Assignee | Priority | Status | Resolution | Resolved In Build |
---|---|---|---|---|---|---|
JDK-2188498 | 6 | Joe Darcy | P3 | Closed | Won't Fix |
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.
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.
- backported by
-
JDK-2188498 Filer.getResource doesn't throw IOException when getting nonexistent file
-
- Closed
-
- relates to
-
JDK-6929404 Filer.getResource(SOURCE_PATH, ...) does not work when -sourcepath contains >1 entry
-
- Closed
-