ExtendedTypeKindVisitor7.java

  1. package net.florianschoppmann.java.type;

  2. import javax.annotation.Nullable;
  3. import javax.annotation.processing.SupportedSourceVersion;
  4. import javax.lang.model.type.TypeMirror;
  5. import javax.lang.model.type.UnknownTypeException;
  6. import javax.lang.model.util.TypeKindVisitor7;
  7. import java.util.Objects;

  8. import static javax.lang.model.SourceVersion.RELEASE_7;

  9. /**
  10.  * A visitor of types that supports {@link IntersectionType}, but otherwise behaves as {@link TypeKindVisitor7}.
  11.  *
  12.  * <p>This visitor provides a new method {@link #visitIntersection(IntersectionType, Object)} in order to visit
  13.  * instances of {@link IntersectionType}. At some time in the future, when Java 7 compatibility is no longer
  14.  * required, {@link IntersectionType} and this class will be removed and replaced by their Java 8 equivalents.
  15.  *
  16.  * @param <R> The return type of this visitor's methods. Use {@link Void} for visitors that do not need to return
  17.  *     results.
  18.  * @param <P> The type of the additional parameter to this visitor's methods. Use {@code Void} for visitors that do
  19.  *     not need an additional parameter.
  20.  */
  21. @SupportedSourceVersion(RELEASE_7)
  22. public class ExtendedTypeKindVisitor7<R, P> extends TypeKindVisitor7<R, P> {
  23.     /**
  24.      * Constructor for concrete subclasses to call; uses {@code null} for the default value.
  25.      */
  26.     protected ExtendedTypeKindVisitor7() {
  27.         super(null);
  28.     }

  29.     /**
  30.      * Constructor for concrete subclasses to call; uses the argument for the default value.
  31.      *
  32.      * @param defaultValue the value to assign to {@link #DEFAULT_VALUE}
  33.      */
  34.     protected ExtendedTypeKindVisitor7(R defaultValue) {
  35.         super(defaultValue);
  36.     }

  37.     /**
  38.      * Visits an unknown kind of type or an {@link IntersectionType}, which did not exist in the {@link TypeMirror}
  39.      * hierarchy of Java 7.
  40.      *
  41.      * <p>This method is final. Use {@link #visitOther(TypeMirror, Object)} to override the default behavior when
  42.      * visiting an unknown type.
  43.      *
  44.      * @param typeMirror the type to visit
  45.      * @param parameter a visitor-specified parameter
  46.      * @return a visitor-specified result
  47.      * @throws UnknownTypeException a visitor implementation may optionally throw this exception
  48.      */
  49.     @Override
  50.     @Nullable
  51.     public final R visitUnknown(TypeMirror typeMirror, @Nullable P parameter) {
  52.         Objects.requireNonNull(typeMirror);
  53.         if (typeMirror instanceof IntersectionType && ((IntersectionType) typeMirror).isIntersectionType()) {
  54.             return visitIntersection((IntersectionType) typeMirror, parameter);
  55.         } else {
  56.             return visitOther(typeMirror, parameter);
  57.         }
  58.     }

  59.     /**
  60.      * Visits an unknown kind of type. This can occur if the language evolves and new kinds of types are added to the
  61.      * {@link TypeMirror} hierarchy.
  62.      *
  63.      * <p>The default implementation of this method in {@code TypeKindVisitor7WithIntersectionType} will always throw
  64.      * {@link UnknownTypeException}. This behavior is not required of a subclass.
  65.      *
  66.      * @param typeMirror the type to visit
  67.      * @param parameter a visitor-specified parameter
  68.      * @return a visitor-specified result
  69.      * @throws UnknownTypeException a visitor implementation may optionally throw this exception
  70.      */
  71.     @Nullable
  72.     public R visitOther(TypeMirror typeMirror, @Nullable P parameter) {
  73.         throw new UnknownTypeException(typeMirror, parameter);
  74.     }

  75.     /**
  76.      * Visits an intersection type by calling {@link #defaultAction(TypeMirror, Object)}.
  77.      *
  78.      * @param intersectionType the intersection type to visit
  79.      * @param parameter a visitor-specified parameter
  80.      * @return the result of {@code defaultAction}
  81.      */
  82.     @Nullable
  83.     public R visitIntersection(IntersectionType intersectionType, @Nullable P parameter) {
  84.         return defaultAction(intersectionType, parameter);
  85.     }
  86. }