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

InstanceOfTree#getType doesn't specify when it returns null

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Unresolved
    • Icon: P4 P4
    • 25
    • None
    • tools
    • None

      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

            jlahoda Jan Lahoda
            cushon Liam Miller-Cushon
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: