InstanceOfTree#getType isn't documented as returning null [1], but the implementation can return null for pattern trees [2].
The docs for getPattern() do mention when null is returned. I'd expect either getType() should be documented as returning null, or the implementation shouldn't return null.
E.g. in the following, calling getType on the InstanceOfTree for 'i instanceof R(String message)' will return null:
class PatternTest {
sealed interface I permits R {}
record R(String message) implements I {}
void test(I i) {
if (i instanceof R(String message)) {
System.out.println(message);
}
}
}
Looking at the implementation, it isn't obvious why getType only returns non-null types for BINDINGPATTERN patterns, and not other patterns. Was this possibly an oversight when other pattern AST nodes were added?
[1] https://docs.oracle.com/en/java/javase/23/docs/api/jdk.compiler/com/sun/source/tree/InstanceOfTree.html#getType()
[2] https://github.com/openjdk/jdk/blob/98a93e115137a305aed6b7dbf1d4a7d5906fe77c/src/jdk.compiler/share/classes/com/sun/tools/javac/tree/JCTree.java#L2288
The docs for getPattern() do mention when null is returned. I'd expect either getType() should be documented as returning null, or the implementation shouldn't return null.
E.g. in the following, calling getType on the InstanceOfTree for 'i instanceof R(String message)' will return null:
class PatternTest {
sealed interface I permits R {}
record R(String message) implements I {}
void test(I i) {
if (i instanceof R(String message)) {
System.out.println(message);
}
}
}
Looking at the implementation, it isn't obvious why getType only returns non-null types for BINDINGPATTERN patterns, and not other patterns. Was this possibly an oversight when other pattern AST nodes were added?
[1] https://docs.oracle.com/en/java/javase/23/docs/api/jdk.compiler/com/sun/source/tree/InstanceOfTree.html#getType()
[2] https://github.com/openjdk/jdk/blob/98a93e115137a305aed6b7dbf1d4a7d5906fe77c/src/jdk.compiler/share/classes/com/sun/tools/javac/tree/JCTree.java#L2288
- csr for
-
JDK-8356857 InstanceOfTree#getType doesn't specify when it returns null
-
- Draft
-
- links to
-
Review(master) openjdk/jdk/25205