Package net.karneim.pojobuilder.analysis
Class JavaModelAnalyzerUtil
- java.lang.Object
-
- net.karneim.pojobuilder.analysis.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.ExecutableElementfindMethod(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.StringgetClassname(javax.lang.model.element.TypeElement typeElem)Returns the classname (without any package qualifier) of the given type element.javax.lang.model.element.TypeElementgetCompilationUnit(javax.lang.model.element.Element elem)Returns the top-level Java class that contains the given element.java.lang.StringgetPackage(javax.lang.model.element.TypeElement typeElem)Returns the Java package the given type element (or it's outer type) belongs to.java.lang.StringgetPackage(javax.lang.model.type.DeclaredType type)Returns the Java package the given type (or it's outer type) belongs to.javax.lang.model.type.PrimitiveTypegetPrimitiveBooleanType()java.lang.StringgetPropertyName(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.TypeMirrorgetType(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.NoTypegetVoidType()booleanhasBuildMethod(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.booleanhasCloneMethodThatDoesNotThrowACloneNotSupportedException(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 aCloneNotSupportedException.booleanhasGetMethod(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.booleanhasMethod(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.booleanhasPublicNoArgsConstructor(javax.lang.model.element.TypeElement typeEl)Returns true if the given type element defines a public no-args constructor.booleanisAccessibleForBuilder(javax.lang.model.element.Element el, BuilderM builderM)Returns true if the given element is accessible for the given builder.booleanisDeclaredInObject(javax.lang.model.element.Element el)Returns whether the given element is directly declared inObject.booleanisGetterMethod(javax.lang.model.element.ExecutableElement el)Returns true if the given element is a Getter-method.booleanisSetterMethod(javax.lang.model.element.ExecutableElement el)Returns true if the given element is a Setter-method.booleanisStatic(javax.lang.model.element.Element el)Returns true if the given element is marked with a 'static' modifier.booleanisUpperBoundToObject(javax.lang.model.element.TypeParameterElement typeParamEl)Returns true if the given type parameter has an upper bound of typeObject.booleanisValidJavaIdentifier(java.lang.String string)Returnstrueif the given string is a valid Java identifier.booleanisValidJavaPackageName(java.lang.String string)Returnstrueif the given string is a valid Java package name.booleanmatchesUpperBound(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.Stringuncapitalize(java.lang.String str)
-
-
-
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 elementbuilderM- 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 inObject.- 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 typeelement- 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 elementrequiredReturnType- the required return type (maybeNoType)- 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 aCloneNotSupportedException.- 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 elementrequiredReturnType- the required return type (maybeNoType).- 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 elementname- the required name of the methodrequiredReturnType- the required return type (maybeNoType).requiredParamType- the type of the required (first) parameter, ornullif 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 elementname- the required name of the methodrequiredReturnType- the required return type (maybeNoType).requiredParamType- the type of the required (first) parameter, ornullif no parameter is required- Returns:
- the ExecutableElement repesenting the found method, or
nullif 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 elementtypeParamEl- 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 typeObject.- 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)
Returnstrueif the given string is a valid Java identifier.- Parameters:
string- the string- Returns:
trueif the given string is a valid Java identifier
-
isValidJavaPackageName
public boolean isValidJavaPackageName(java.lang.String string)
Returnstrueif the given string is a valid Java package name.This does not check if the package exists.
- Parameters:
string- the string- Returns:
trueif 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)
-
-