Class ModelUtils


  • public class ModelUtils
    extends java.lang.Object
    Utility methods for the javax.lang.model package.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static javax.lang.model.util.SimpleTypeVisitor8<java.util.Optional<javax.lang.model.type.DeclaredType>,​?> DECLARED_TYPE_VISITOR  
      private static javax.lang.model.util.SimpleElementVisitor8<java.util.Optional<javax.lang.model.element.TypeElement>,​?> TYPE_ELEMENT_VISITOR  
      private static javax.lang.model.util.SimpleTypeVisitor8<java.util.Optional<javax.lang.model.type.TypeVariable>,​?> TYPE_VARIABLE_VISITOR  
    • Constructor Summary

      Constructors 
      Constructor Description
      ModelUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static javax.lang.model.element.TypeElement asElement​(javax.lang.model.type.DeclaredType type)
      Returns the TypeElement corresponding to type.
      static boolean erasesToAnyOf​(javax.lang.model.type.DeclaredType type, java.lang.Class<?>... possibilities)
      Returns true if type erases to any of possibilities.
      static boolean erasesToAnyOf​(javax.lang.model.type.DeclaredType type, QualifiedName... possibilities)
      Returns true if type erases to any of possibilities.
      static java.util.Optional<javax.lang.model.element.AnnotationMirror> findAnnotationMirror​(javax.lang.model.element.Element element, java.lang.Class<? extends java.lang.annotation.Annotation> annotationClass)
      Returns an AnnotationMirror for the annotation of type annotationClass on element, or Optional.empty() if no such annotation exists.
      static java.util.Optional<javax.lang.model.element.AnnotationMirror> findAnnotationMirror​(javax.lang.model.element.Element element, java.lang.String annotationClassName)
      Returns an AnnotationMirror for the annotation of type annotationClassName on element, or Optional.empty() if no such annotation exists.
      static java.util.Optional<javax.lang.model.element.AnnotationMirror> findAnnotationMirror​(javax.lang.model.element.Element element, QualifiedName annotationClass)
      Returns an AnnotationMirror for the annotation of type annotationClass on element, or Optional.empty() if no such annotation exists.
      static java.util.Optional<javax.lang.model.element.AnnotationValue> findProperty​(javax.lang.model.element.AnnotationMirror annotation, java.lang.String propertyName)  
      static javax.lang.model.type.TypeMirror getReturnType​(javax.lang.model.element.TypeElement type, javax.lang.model.element.ExecutableElement method, javax.lang.model.util.Types types)
      Determines the return type of method, if called on an instance of type type.
      static javax.lang.model.type.TypeMirror getReturnType​(javax.lang.model.type.DeclaredType type, javax.lang.model.element.ExecutableElement method, javax.lang.model.util.Types types)
      Determines the return type of method, if called on an instance of type type.
      private static boolean isPlainWildcard​(javax.lang.model.type.TypeMirror type)  
      static java.util.Optional<javax.lang.model.element.TypeElement> maybeAsTypeElement​(javax.lang.model.type.TypeMirror type)
      Returns the TypeElement corresponding to type, if there is one.
      static java.util.Optional<javax.lang.model.type.DeclaredType> maybeDeclared​(javax.lang.model.type.TypeMirror type)
      Returns type as a DeclaredType, if it is one.
      static java.util.Optional<javax.lang.model.element.TypeElement> maybeType​(javax.lang.model.element.Element element)
      Returns element as a TypeElement, if it is one.
      static java.util.Optional<javax.lang.model.type.TypeMirror> maybeUnbox​(javax.lang.model.type.TypeMirror mirror, javax.lang.model.util.Types types)
      Applies unboxing conversion to mirror, if it can be unboxed.
      static java.util.Optional<javax.lang.model.type.TypeVariable> maybeVariable​(javax.lang.model.type.TypeMirror type)  
      static boolean needsSafeVarargs​(javax.lang.model.type.TypeMirror elementType)
      Returns true if a method with a variable number of elementType arguments needs a &#64;SafeVarargs annotation to avoid compiler warnings in Java 7+.
      static java.util.Set<javax.lang.model.element.ExecutableElement> only​(javax.lang.model.element.Modifier modifier, java.util.Set<javax.lang.model.element.ExecutableElement> methods)  
      static java.util.Optional<javax.lang.model.element.ExecutableElement> override​(javax.lang.model.element.TypeElement type, javax.lang.model.util.Types types, java.lang.String methodName, javax.lang.model.type.TypeMirror... params)
      Returns the method on type that overrides method methodName(params).
      static java.util.Optional<javax.lang.model.element.ExecutableElement> override​(javax.lang.model.type.DeclaredType type, javax.lang.model.util.Types types, java.lang.String methodName, javax.lang.model.type.TypeMirror... params)
      Returns the method on type that overrides method methodName(params).
      static boolean overrides​(javax.lang.model.element.TypeElement type, javax.lang.model.util.Types types, java.lang.String methodName, javax.lang.model.type.TypeMirror... params)
      Returns whether type overrides method methodName(params).
      static boolean overrides​(javax.lang.model.type.DeclaredType type, javax.lang.model.util.Types types, java.lang.String methodName, javax.lang.model.type.TypeMirror... params)
      Returns whether type overrides method methodName(params).
      private static boolean signatureMatches​(javax.lang.model.element.ExecutableElement method, javax.lang.model.util.Types types, java.lang.String name, javax.lang.model.type.TypeMirror... params)  
      static javax.lang.model.type.TypeMirror upperBound​(javax.lang.model.util.Elements elements, javax.lang.model.type.TypeMirror type)
      Returns the upper bound of type. T -> T ? -> Object ? extends T -> T ? super T -> Object
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • TYPE_ELEMENT_VISITOR

        private static final javax.lang.model.util.SimpleElementVisitor8<java.util.Optional<javax.lang.model.element.TypeElement>,​?> TYPE_ELEMENT_VISITOR
      • DECLARED_TYPE_VISITOR

        private static final javax.lang.model.util.SimpleTypeVisitor8<java.util.Optional<javax.lang.model.type.DeclaredType>,​?> DECLARED_TYPE_VISITOR
      • TYPE_VARIABLE_VISITOR

        private static final javax.lang.model.util.SimpleTypeVisitor8<java.util.Optional<javax.lang.model.type.TypeVariable>,​?> TYPE_VARIABLE_VISITOR
    • Constructor Detail

      • ModelUtils

        public ModelUtils()
    • Method Detail

      • findAnnotationMirror

        public static java.util.Optional<javax.lang.model.element.AnnotationMirror> findAnnotationMirror​(javax.lang.model.element.Element element,
                                                                                                         java.lang.Class<? extends java.lang.annotation.Annotation> annotationClass)
        Returns an AnnotationMirror for the annotation of type annotationClass on element, or Optional.empty() if no such annotation exists.
      • findAnnotationMirror

        public static java.util.Optional<javax.lang.model.element.AnnotationMirror> findAnnotationMirror​(javax.lang.model.element.Element element,
                                                                                                         QualifiedName annotationClass)
        Returns an AnnotationMirror for the annotation of type annotationClass on element, or Optional.empty() if no such annotation exists.
      • findAnnotationMirror

        public static java.util.Optional<javax.lang.model.element.AnnotationMirror> findAnnotationMirror​(javax.lang.model.element.Element element,
                                                                                                         java.lang.String annotationClassName)
        Returns an AnnotationMirror for the annotation of type annotationClassName on element, or Optional.empty() if no such annotation exists.
      • findProperty

        public static java.util.Optional<javax.lang.model.element.AnnotationValue> findProperty​(javax.lang.model.element.AnnotationMirror annotation,
                                                                                                java.lang.String propertyName)
      • maybeType

        public static java.util.Optional<javax.lang.model.element.TypeElement> maybeType​(javax.lang.model.element.Element element)
        Returns element as a TypeElement, if it is one.
      • maybeDeclared

        public static java.util.Optional<javax.lang.model.type.DeclaredType> maybeDeclared​(javax.lang.model.type.TypeMirror type)
        Returns type as a DeclaredType, if it is one.
      • maybeVariable

        public static java.util.Optional<javax.lang.model.type.TypeVariable> maybeVariable​(javax.lang.model.type.TypeMirror type)
      • maybeAsTypeElement

        public static java.util.Optional<javax.lang.model.element.TypeElement> maybeAsTypeElement​(javax.lang.model.type.TypeMirror type)
        Returns the TypeElement corresponding to type, if there is one.
      • asElement

        public static javax.lang.model.element.TypeElement asElement​(javax.lang.model.type.DeclaredType type)
        Returns the TypeElement corresponding to type.
      • maybeUnbox

        public static java.util.Optional<javax.lang.model.type.TypeMirror> maybeUnbox​(javax.lang.model.type.TypeMirror mirror,
                                                                                      javax.lang.model.util.Types types)
        Applies unboxing conversion to mirror, if it can be unboxed.
      • override

        public static java.util.Optional<javax.lang.model.element.ExecutableElement> override​(javax.lang.model.element.TypeElement type,
                                                                                              javax.lang.model.util.Types types,
                                                                                              java.lang.String methodName,
                                                                                              javax.lang.model.type.TypeMirror... params)
        Returns the method on type that overrides method methodName(params).
      • override

        public static java.util.Optional<javax.lang.model.element.ExecutableElement> override​(javax.lang.model.type.DeclaredType type,
                                                                                              javax.lang.model.util.Types types,
                                                                                              java.lang.String methodName,
                                                                                              javax.lang.model.type.TypeMirror... params)
        Returns the method on type that overrides method methodName(params).
      • overrides

        public static boolean overrides​(javax.lang.model.element.TypeElement type,
                                        javax.lang.model.util.Types types,
                                        java.lang.String methodName,
                                        javax.lang.model.type.TypeMirror... params)
        Returns whether type overrides method methodName(params).
      • overrides

        public static boolean overrides​(javax.lang.model.type.DeclaredType type,
                                        javax.lang.model.util.Types types,
                                        java.lang.String methodName,
                                        javax.lang.model.type.TypeMirror... params)
        Returns whether type overrides method methodName(params).
      • needsSafeVarargs

        public static boolean needsSafeVarargs​(javax.lang.model.type.TypeMirror elementType)
        Returns true if a method with a variable number of elementType arguments needs a &#64;SafeVarargs annotation to avoid compiler warnings in Java 7+.
      • only

        public static java.util.Set<javax.lang.model.element.ExecutableElement> only​(javax.lang.model.element.Modifier modifier,
                                                                                     java.util.Set<javax.lang.model.element.ExecutableElement> methods)
      • isPlainWildcard

        private static boolean isPlainWildcard​(javax.lang.model.type.TypeMirror type)
      • signatureMatches

        private static boolean signatureMatches​(javax.lang.model.element.ExecutableElement method,
                                                javax.lang.model.util.Types types,
                                                java.lang.String name,
                                                javax.lang.model.type.TypeMirror... params)
      • getReturnType

        public static javax.lang.model.type.TypeMirror getReturnType​(javax.lang.model.element.TypeElement type,
                                                                     javax.lang.model.element.ExecutableElement method,
                                                                     javax.lang.model.util.Types types)
        Determines the return type of method, if called on an instance of type type.

        For instance, in this example, myY.getProperty() returns List<T>, not T:

        
            interface X<T> {
              T getProperty();
            }
            @FreeBuilder interface Y<T> extends X<List<T>> { }

        (Unfortunately, a bug in Eclipse prevents us handling these cases correctly at the moment. javac works fine.)

      • getReturnType

        public static javax.lang.model.type.TypeMirror getReturnType​(javax.lang.model.type.DeclaredType type,
                                                                     javax.lang.model.element.ExecutableElement method,
                                                                     javax.lang.model.util.Types types)
        Determines the return type of method, if called on an instance of type type.

        For instance, in this example, myY.getProperty() returns List<T>, not T:

        
            interface X<T> {
              T getProperty();
            }
            @FreeBuilder interface Y<T> extends X<List<T>> { }

        (Unfortunately, a bug in Eclipse prevents us handling these cases correctly at the moment. javac works fine.)

      • upperBound

        public static javax.lang.model.type.TypeMirror upperBound​(javax.lang.model.util.Elements elements,
                                                                  javax.lang.model.type.TypeMirror type)
        Returns the upper bound of type.
        • T -> T
        • ? -> Object
        • ? extends T -> T
        • ? super T -> Object
      • erasesToAnyOf

        public static boolean erasesToAnyOf​(javax.lang.model.type.DeclaredType type,
                                            QualifiedName... possibilities)
        Returns true if type erases to any of possibilities.
      • erasesToAnyOf

        public static boolean erasesToAnyOf​(javax.lang.model.type.DeclaredType type,
                                            java.lang.Class<?>... possibilities)
        Returns true if type erases to any of possibilities.