Class Types
java.lang.Object
org.jboss.forge.roaster.model.util.Types
Util classes for java types.
- Author:
- Lincoln Baxter, III, George Gastaldi
-
Method Summary
Modifier and TypeMethodDescriptionstatic booleanareEquivalent(String left, String right) Checks if two type names are equal after removing the generic part.static intgetArrayDimension(String name) Returns the dimension of the array.static StringgetArraySuffix(String type) Get the array suffix.static StringgetDefaultValue(Class<?> clazz) Returns the default value for a given class according to the Java Language Specification.static StringgetDefaultValue(String type) Returns the default value for a given type according to the Java Language Specification.static StringgetGenerics(String type) Returns the generic part of a given type.static Stringstatic StringgetPackage(String typeName) Extracts the package part of a given type name.static booleanChecks if the given type is an array.static booleanisBasicType(String type) Checks if the give type is a basic type, so it's either primitive or one of the primitive wrapper classes.static booleanChecks if the given type is generic.static booleanisJavaLang(String type) Checks if the given type is part of thejava.langpackage.static booleanisPrimitive(String type) Checks if the given type is primitive according to the Java Language Specification.static booleanisQualified(String typeName) Checks if the given type name is qualified, that means that it contains at least one '.'.static booleanisSimpleName(String name) Checks if the given name is a simple name, so not full qualified.rebuildGenericNameWithArrays(String resolvedTypeName, Type<O> type) static String[]splitGenerics(String typeName) Returns the available generics as a String array.static StringstripArray(String type) Strips the array from a given type.static StringstripGenerics(String type) Removes the generics part of a given type.static String[]tokenizeClassName(String className) Splits the given class name into is parts which are separated by a '.'.static Class<?>toPrimitive(String type) static StringtoResolvedType(String type, Importer<?> importer) Resolves the given type including generics.static StringtoSimpleName(String type) Calculates the simple name of the given type including all generics.static booleanvalidateGenerics(String type) Checks if the generics are valid of this given type.
-
Method Details
-
areEquivalent
Checks if two type names are equal after removing the generic part. The method returns true one of the following conditions is matched:- Both types are
null - Both types are equal in their String representation, so that
String.equals(Object)returnstrue - Both types have the same simple name and one the following is matched:
- Both types are a simple names
- Only one type is a full qualified name
- Both types are full qualified and their package part is equal, so that
String.equals(Object)returnstrue
- Parameters:
left- the first type (maybenull)right- the second type (maybenull)- Returns:
trueif the above conditions are fulfilled,falseotherwise- Throws:
NullPointerException- if one of the arguments isnull
- Both types are
-
toSimpleName
Calculates the simple name of the given type including all generics.- Parameters:
type- the type to convert- Returns:
- the simple name
- Throws:
NullPointerException- if the type isnullIllegalArgumentException- if the generic part couldn't be parsed
-
toResolvedType
Resolves the given type including generics. For each type it's checked if a import exists. If yes, the simple name is used, else the provided name.NOTE: This method won't import anything.
- Parameters:
type- the type to resolveimporter- the importer to use to check for existing imports- Returns:
- the resolved type
- Throws:
NullPointerException- if the type and/or the importer arenull
-
tokenizeClassName
Splits the given class name into is parts which are separated by a '.'.- Parameters:
className- the class name to tokenize- Returns:
- a string array with the parts
- Throws:
NullPointerException- if the class name isnull
-
isQualified
Checks if the given type name is qualified, that means that it contains at least one '.'.- Parameters:
typeName- the type name to check- Returns:
true, if the type is qualified,falseotherwise- Throws:
NullPointerException- if the type name isnull
-
getPackage
Extracts the package part of a given type name. The package is the part which is the substring before the last occurrence of '.'.- Parameters:
typeName- the type name to extract the package from- Returns:
- the package part or an empty string, if no package coudn't be extracted
- Throws:
NullPointerException- if the type name isnull
-
isSimpleName
Checks if the given name is a simple name, so not full qualified. In the case the name is no valid java identifier,falseis returned.- Parameters:
name- the name to check- Returns:
true, if the name is simple,falseotherwise or if the name is no valid java identifier- Throws:
NullPointerException- if the name isnull
-
isJavaLang
Checks if the given type is part of thejava.langpackage. For this, the type is first transformed to a simple type.The consequence is that the following will return
true:isJavaLang("test.String"), becauseStringis part ofjava.lang.- Parameters:
type- the type to check- Returns:
true, if the simple name of the type is part ofjava.lang,falseotherwise- Throws:
NullPointerException- if the type name isnull
-
isBasicType
Checks if the give type is a basic type, so it's either primitive or one of the primitive wrapper classes.- Parameters:
type- the type to check- Returns:
trueif this type is basic,falseotherwise- Throws:
NullPointerException- if the type name isnull
-
isGeneric
Checks if the given type is generic.- Parameters:
type- the type to check- Returns:
trueif this type is generic,falseotherwise- Throws:
NullPointerException- if the given type isnull
-
validateGenerics
Checks if the generics are valid of this given type.- Parameters:
type- the type to check- Returns:
true, if the generics are valid,falseotherwise- Throws:
NullPointerException- if the given type isnull
-
stripGenerics
Removes the generics part of a given type. More specific, the content between the first occurrence of '<' and the last occurrence if '>' is removed.- Parameters:
type- the type where the generics should be removed- Returns:
- the type without generics
- Throws:
NullPointerException- if the given type isnull
-
getGenerics
Returns the generic part of a given type. For examplegetGenerics("A<B>")returns"<B>".- Parameters:
type- the type to get the generics from- Returns:
- the generics from the given type, or an empty string, if the type has not generics
- Throws:
NullPointerException- if the given type isnull
-
getGenericsTypeParameter
-
isArray
Checks if the given type is an array.- Parameters:
type- the type to check- Returns:
true, if the type is an array,falseotherwise- Throws:
NullPointerException- if the given type isnull
-
stripArray
Strips the array from a given type.- Parameters:
type- the type to remove the array from- Returns:
- the type without an array
- Throws:
NullPointerException- if the given type isnull
-
isPrimitive
Checks if the given type is primitive according to the Java Language Specification.- Parameters:
type- the type to check- Returns:
trueif this type is primitive,falseotherwise- Throws:
NullPointerException- if the given type isnull
-
toPrimitive
-
getArrayDimension
Returns the dimension of the array. It simply counts the "[" from the string.- Parameters:
name- an array type, e.g.: byte[] or [Ljava.lang.Boolean;- Returns:
- the array dimension. 0 if the type is not a valid array
- Throws:
NullPointerException- if the given name isnull
-
getArraySuffix
Get the array suffix. For examplegetArraySuffix("String[][]")will return[][].- Parameters:
type- the type to get the array suffix from- Returns:
- the array suffix or an empty string if the type is not generic
-
rebuildGenericNameWithArrays
-
getDefaultValue
Returns the default value for a given class according to the Java Language Specification.- Parameters:
clazz- the class of the type- Returns:
- the default value
- Throws:
NullPointerException- if the given class isnull
-
getDefaultValue
Returns the default value for a given type according to the Java Language Specification.- Parameters:
type- the type- Returns:
- the default value
- Throws:
NullPointerException- if the given type isnull
-
splitGenerics
Returns the available generics as a String array. Only the first level is split. For examplesplitGenerics("Foo<Bar<A>, Bar<B>>")returns an array withBar<A>andBar<B>.- Parameters:
typeName- the generic type to split- Returns:
- an array with the generic parts (maybe empty but never
null) - Throws:
NullPointerException- if the given type isnull
-