Class JavaModelAnalyzerUtil


  • public class JavaModelAnalyzerUtil
    extends java.lang.Object
    • Constructor Summary

      Constructors 
      Constructor Description
      JavaModelAnalyzerUtil​(javax.lang.model.util.Elements elements, javax.lang.model.util.Types types)  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.util.Collection<? extends javax.lang.model.element.Element> findAnnotatedElements​(java.util.Collection<javax.lang.model.element.TypeElement> typeElements, java.lang.Class<?> annotationType)  
      javax.lang.model.element.ExecutableElement findMethod​(javax.lang.model.element.TypeElement typeElement, java.lang.String name, javax.lang.model.type.TypeMirror requiredReturnType, javax.lang.model.type.TypeMirror requiredParamType)
      Searches the given type element for a method with the given name and an actual return type that is compatible with the given return type, and has an actual parameter that is compatible with the given parameter type.
      java.lang.String getClassname​(javax.lang.model.element.TypeElement typeElem)
      Returns the classname (without any package qualifier) of the given type element.
      javax.lang.model.element.TypeElement getCompilationUnit​(javax.lang.model.element.Element elem)
      Returns the top-level Java class that contains the given element.
      java.lang.String getPackage​(javax.lang.model.element.TypeElement typeElem)
      Returns the Java package the given type element (or it's outer type) belongs to.
      java.lang.String getPackage​(javax.lang.model.type.DeclaredType type)
      Returns the Java package the given type (or it's outer type) belongs to.
      javax.lang.model.type.PrimitiveType getPrimitiveBooleanType()  
      java.lang.String getPropertyName​(javax.lang.model.element.ExecutableElement methodEl)
      Returns the name of the property that is accessed by the given [G|S]etter method.
      javax.lang.model.type.TypeMirror getType​(javax.lang.model.type.DeclaredType ownerType, javax.lang.model.element.Element element)
      Returns the effective type of the given element when is is viewed as a member of the given owner type.
      javax.lang.model.type.NoType getVoidType()  
      boolean hasBuildMethod​(javax.lang.model.element.TypeElement typeElement, javax.lang.model.type.TypeMirror requiredReturnType)
      Returns true, if the given type element has a method called "build" with no parameters and which has an actual return type that is compatible with the given return type.
      boolean hasCloneMethodThatDoesNotThrowACloneNotSupportedException​(javax.lang.model.element.TypeElement typeElement)
      Returns true, if the given type element has a method called "clone" with no parameters and which does not throw a CloneNotSupportedException.
      boolean hasGetMethod​(javax.lang.model.element.TypeElement typeElement, javax.lang.model.type.TypeMirror requiredReturnType)
      Returns true, if the given type element has a method called "get" with no parameters and which has an actual return type that is compatible with the given return type.
      boolean hasMethod​(javax.lang.model.element.TypeElement typeElement, java.lang.String name, javax.lang.model.type.TypeMirror requiredReturnType, javax.lang.model.type.TypeMirror requiredParamType)
      Returns true, if the given type element has a method with the given name and has an actual return type that is compatible with the given return type, and has an actual parameter that is compatible with the given parameter type.
      boolean hasPublicNoArgsConstructor​(javax.lang.model.element.TypeElement typeEl)
      Returns true if the given type element defines a public no-args constructor.
      boolean isAccessibleForBuilder​(javax.lang.model.element.Element el, BuilderM builderM)
      Returns true if the given element is accessible for the given builder.
      boolean isDeclaredInObject​(javax.lang.model.element.Element el)
      Returns whether the given element is directly declared in Object.
      boolean isGetterMethod​(javax.lang.model.element.ExecutableElement el)
      Returns true if the given element is a Getter-method.
      boolean isSetterMethod​(javax.lang.model.element.ExecutableElement el)
      Returns true if the given element is a Setter-method.
      boolean isStatic​(javax.lang.model.element.Element el)
      Returns true if the given element is marked with a 'static' modifier.
      boolean isUpperBoundToObject​(javax.lang.model.element.TypeParameterElement typeParamEl)
      Returns true if the given type parameter has an upper bound of type Object.
      boolean isValidJavaIdentifier​(java.lang.String string)
      Returns true if the given string is a valid Java identifier.
      boolean isValidJavaPackageName​(java.lang.String string)
      Returns true if the given string is a valid Java package name.
      boolean matchesUpperBound​(javax.lang.model.element.TypeElement typeElement, javax.lang.model.element.TypeParameterElement typeParamEl)
      Returns true if the given typeElement is a subtype of the given type parameter's upper bound.
      static java.lang.String uncapitalize​(java.lang.String str)  
      • Methods inherited from class java.lang.Object

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

      • JavaModelAnalyzerUtil

        public JavaModelAnalyzerUtil​(javax.lang.model.util.Elements elements,
                                     javax.lang.model.util.Types types)
    • Method Detail

      • getPrimitiveBooleanType

        public javax.lang.model.type.PrimitiveType getPrimitiveBooleanType()
      • getVoidType

        public javax.lang.model.type.NoType getVoidType()
      • getClassname

        public java.lang.String getClassname​(javax.lang.model.element.TypeElement typeElem)
        Returns the classname (without any package qualifier) of the given type element.
        Parameters:
        typeElem - the type element
        Returns:
        the classname of the given type element
      • getPackage

        public java.lang.String getPackage​(javax.lang.model.type.DeclaredType type)
        Returns the Java package the given type (or it's outer type) belongs to.
        Parameters:
        type - the type
        Returns:
        the Java package the given type belongs to
      • getPackage

        public java.lang.String getPackage​(javax.lang.model.element.TypeElement typeElem)
        Returns the Java package the given type element (or it's outer type) belongs to.
        Parameters:
        typeElem - the type element
        Returns:
        the Java package the given type element belongs to
      • getCompilationUnit

        public javax.lang.model.element.TypeElement getCompilationUnit​(javax.lang.model.element.Element elem)
        Returns the top-level Java class that contains the given element.
        Parameters:
        elem - the element
        Returns:
        the top-level Java class that contains the given element
      • isAccessibleForBuilder

        public boolean isAccessibleForBuilder​(javax.lang.model.element.Element el,
                                              BuilderM builderM)
        Returns true if the given element is accessible for the given builder.
        Parameters:
        el - the element
        builderM - the builder
        Returns:
        true if the given element is accessible
      • isStatic

        public boolean isStatic​(javax.lang.model.element.Element el)
        Returns true if the given element is marked with a 'static' modifier.
        Parameters:
        el - the element
        Returns:
        true if the given element is marked with a 'static' modifier
      • isSetterMethod

        public boolean isSetterMethod​(javax.lang.model.element.ExecutableElement el)
        Returns true if the given element is a Setter-method.
        Parameters:
        el - the element
        Returns:
        true if the given element is a Setter-method
      • isGetterMethod

        public boolean isGetterMethod​(javax.lang.model.element.ExecutableElement el)
        Returns true if the given element is a Getter-method.
        Parameters:
        el - the element
        Returns:
        true if the given element is a Getter-method
      • isDeclaredInObject

        public boolean isDeclaredInObject​(javax.lang.model.element.Element el)
        Returns whether the given element is directly declared in Object.
        Parameters:
        el - the element
        Returns:
        true if the element is declared in Object
      • getPropertyName

        public java.lang.String getPropertyName​(javax.lang.model.element.ExecutableElement methodEl)
        Returns the name of the property that is accessed by the given [G|S]etter method.
        Parameters:
        methodEl - the method element
        Returns:
        the name of the property
      • getType

        public javax.lang.model.type.TypeMirror getType​(javax.lang.model.type.DeclaredType ownerType,
                                                        javax.lang.model.element.Element element)
        Returns the effective type of the given element when is is viewed as a member of the given owner type.
        Parameters:
        ownerType - the owner type
        element - the element
        Returns:
        the effective type of the given element
      • hasBuildMethod

        public boolean hasBuildMethod​(javax.lang.model.element.TypeElement typeElement,
                                      javax.lang.model.type.TypeMirror requiredReturnType)
        Returns true, if the given type element has a method called "build" with no parameters and which has an actual return type that is compatible with the given return type.
        Parameters:
        typeElement - the type element
        requiredReturnType - the required return type (maybe NoType)
        Returns:
        true, if the type element has a build method
      • hasCloneMethodThatDoesNotThrowACloneNotSupportedException

        public boolean hasCloneMethodThatDoesNotThrowACloneNotSupportedException​(javax.lang.model.element.TypeElement typeElement)
        Returns true, if the given type element has a method called "clone" with no parameters and which does not throw a CloneNotSupportedException.
        Parameters:
        typeElement - the type element
        Returns:
        true, if the method is found
      • hasGetMethod

        public boolean hasGetMethod​(javax.lang.model.element.TypeElement typeElement,
                                    javax.lang.model.type.TypeMirror requiredReturnType)
        Returns true, if the given type element has a method called "get" with no parameters and which has an actual return type that is compatible with the given return type.
        Parameters:
        typeElement - the type element
        requiredReturnType - the required return type (maybe NoType).
        Returns:
        true, if the type element has the required method
      • hasMethod

        public boolean hasMethod​(javax.lang.model.element.TypeElement typeElement,
                                 java.lang.String name,
                                 javax.lang.model.type.TypeMirror requiredReturnType,
                                 javax.lang.model.type.TypeMirror requiredParamType)
        Returns true, if the given type element has a method with the given name and has an actual return type that is compatible with the given return type, and has an actual parameter that is compatible with the given parameter type.
        Parameters:
        typeElement - the type element
        name - the required name of the method
        requiredReturnType - the required return type (maybe NoType).
        requiredParamType - the type of the required (first) parameter, or null if no parameter is required
        Returns:
        true, if the type element has the required method
      • findMethod

        public javax.lang.model.element.ExecutableElement findMethod​(javax.lang.model.element.TypeElement typeElement,
                                                                     java.lang.String name,
                                                                     javax.lang.model.type.TypeMirror requiredReturnType,
                                                                     javax.lang.model.type.TypeMirror requiredParamType)
        Searches the given type element for a method with the given name and an actual return type that is compatible with the given return type, and has an actual parameter that is compatible with the given parameter type.
        Parameters:
        typeElement - the type element
        name - the required name of the method
        requiredReturnType - the required return type (maybe NoType).
        requiredParamType - the type of the required (first) parameter, or null if no parameter is required
        Returns:
        the ExecutableElement repesenting the found method, or null if none if found
      • hasPublicNoArgsConstructor

        public boolean hasPublicNoArgsConstructor​(javax.lang.model.element.TypeElement typeEl)
        Returns true if the given type element defines a public no-args constructor.
        Parameters:
        typeEl - Type element.
        Returns:
        true if the given type element defines a public no-args constructor
      • matchesUpperBound

        public boolean matchesUpperBound​(javax.lang.model.element.TypeElement typeElement,
                                         javax.lang.model.element.TypeParameterElement typeParamEl)
        Returns true if the given typeElement is a subtype of the given type parameter's upper bound.
        Parameters:
        typeElement - the type element
        typeParamEl - the type parameter element
        Returns:
        true if the given typeElement is a subtype of the given type parameter's upper bound
      • isUpperBoundToObject

        public boolean isUpperBoundToObject​(javax.lang.model.element.TypeParameterElement typeParamEl)
        Returns true if the given type parameter has an upper bound of type Object.
        Parameters:
        typeParamEl - the type parameter
        Returns:
        true if the given type parameter has an upper bound of type Object
      • isValidJavaIdentifier

        public boolean isValidJavaIdentifier​(java.lang.String string)
        Returns true if the given string is a valid Java identifier.
        Parameters:
        string - the string
        Returns:
        true if the given string is a valid Java identifier
      • isValidJavaPackageName

        public boolean isValidJavaPackageName​(java.lang.String string)
        Returns true if the given string is a valid Java package name.

        This does not check if the package exists.

        Parameters:
        string - the string
        Returns:
        true if the given string is a valid Java package name.
      • findAnnotatedElements

        public java.util.Collection<? extends javax.lang.model.element.Element> findAnnotatedElements​(java.util.Collection<javax.lang.model.element.TypeElement> typeElements,
                                                                                                      java.lang.Class<?> annotationType)
      • uncapitalize

        public static java.lang.String uncapitalize​(java.lang.String str)