javaannotationsannotation-processing

What does TypeKind.UNION represent in Java?


I'm implementing an annotation processor. I've noticed that there exists a javax.lang.model.type.TypeKind called TypeKind.UNION. What exactly does this represent, and how would one encounter a TypeKind.Union while processing annotations? Because as far as I'm aware, there are no union types in Java.

The JavaDoc is unhelpful, as it just states the following:

javax.lang.model.type.TypeKind.UNION

A union type.

Since:

  • 1.7

This doesn't really provide any info on the UNION type itself. There are wildcard types and intersecting types in java, which are covered by TypeKind.WILDCARD and TypeKind.INTERSECTION respectively, so TypeKind.UNION doesn't represent one of those.


Solution

  • Union types can appear in a Java catch clause; e.g.

    catch (Exception | Error e) {
      ...
    }
    

    The type of e in the above is a union type.

    AFAIK, this is (currently) the only usage for union types in Java.