Class Types
- java.lang.Object
-
- org.jboss.forge.roaster.model.util.Types
-
public class Types extends java.lang.ObjectUtil classes for java types.- Author:
- Lincoln Baxter, III, George Gastaldi
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static booleanareEquivalent(java.lang.String left, java.lang.String right)Checks if two type names are equal after removing the generic part.static intgetArrayDimension(java.lang.String name)Returns the dimension of the array.static java.lang.StringgetArraySuffix(java.lang.String type)Get the array suffix.static java.lang.StringgetDefaultValue(java.lang.Class<?> clazz)Returns the default value for a given class according to the Java Language Specification.static java.lang.StringgetDefaultValue(java.lang.String type)Returns the default value for a given type according to the Java Language Specification.static java.lang.StringgetGenerics(java.lang.String type)Returns the generic part of a given type.static java.lang.StringgetGenericsTypeParameter(java.lang.String type)static java.lang.StringgetPackage(java.lang.String typeName)Extracts the package part of a given type name.static booleanisArray(java.lang.String type)Checks if the given type is an array.static booleanisBasicType(java.lang.String type)Checks if the give type is a basic type, so it's either primitive or one of the primitive wrapper classes.static booleanisGeneric(java.lang.String type)Checks if the given type is generic.static booleanisJavaLang(java.lang.String type)Checks if the given type is part of thejava.langpackage.static booleanisPrimitive(java.lang.String type)Checks if the given type is primitive according to the Java Language Specification.static booleanisQualified(java.lang.String typeName)Checks if the given type name is qualified, that means that it contains at least one '.'.static booleanisSimpleName(java.lang.String name)Checks if the given name is a simple name, so not full qualified.static <O extends JavaType<O>>
java.lang.StringrebuildGenericNameWithArrays(java.lang.String resolvedTypeName, Type<O> type)static java.lang.String[]splitGenerics(java.lang.String typeName)Returns the available generics as a String array.static java.lang.StringstripArray(java.lang.String type)Strips the array from a given type.static java.lang.StringstripGenerics(java.lang.String type)Removes the generics part of a given type.static java.lang.String[]tokenizeClassName(java.lang.String className)Splits the given class name into is parts which are separated by a '.'.static java.lang.Class<?>toPrimitive(java.lang.String type)static java.lang.StringtoResolvedType(java.lang.String type, Importer<?> importer)Resolves the given type including generics.static java.lang.StringtoSimpleName(java.lang.String type)Calculates the simple name of the given type including all generics.static booleanvalidateGenerics(java.lang.String type)Checks if the generics are valid of this given type.
-
-
-
Method Detail
-
areEquivalent
public static boolean areEquivalent(java.lang.String left, java.lang.String right)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:
java.lang.NullPointerException- if one of the arguments isnull
- Both types are
-
toSimpleName
public static java.lang.String toSimpleName(java.lang.String type)
Calculates the simple name of the given type including all generics.- Parameters:
type- the type to convert- Returns:
- the simple name
- Throws:
java.lang.NullPointerException- if the type isnulljava.lang.IllegalArgumentException- if the generic part couldn't be parsed
-
toResolvedType
public static java.lang.String toResolvedType(java.lang.String type, Importer<?> importer)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:
java.lang.NullPointerException- if the type and/or the importer arenull
-
tokenizeClassName
public static java.lang.String[] tokenizeClassName(java.lang.String className)
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:
java.lang.NullPointerException- if the class name isnull
-
isQualified
public static boolean isQualified(java.lang.String typeName)
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:
java.lang.NullPointerException- if the type name isnull
-
getPackage
public static java.lang.String getPackage(java.lang.String typeName)
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:
java.lang.NullPointerException- if the type name isnull
-
isSimpleName
public static boolean isSimpleName(java.lang.String name)
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:
java.lang.NullPointerException- if the name isnull
-
isJavaLang
public static boolean isJavaLang(java.lang.String type)
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:
java.lang.NullPointerException- if the type name isnull
-
isBasicType
public static boolean isBasicType(java.lang.String type)
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:
java.lang.NullPointerException- if the type name isnull
-
isGeneric
public static boolean isGeneric(java.lang.String type)
Checks if the given type is generic.- Parameters:
type- the type to check- Returns:
trueif this type is generic,falseotherwise- Throws:
java.lang.NullPointerException- if the given type isnull
-
validateGenerics
public static boolean validateGenerics(java.lang.String type)
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:
java.lang.NullPointerException- if the given type isnull
-
stripGenerics
public static java.lang.String stripGenerics(java.lang.String type)
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:
java.lang.NullPointerException- if the given type isnull
-
getGenerics
public static java.lang.String getGenerics(java.lang.String type)
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:
java.lang.NullPointerException- if the given type isnull
-
getGenericsTypeParameter
public static java.lang.String getGenericsTypeParameter(java.lang.String type)
-
isArray
public static boolean isArray(java.lang.String type)
Checks if the given type is an array.- Parameters:
type- the type to check- Returns:
true, if the type is an array,falseotherwise- Throws:
java.lang.NullPointerException- if the given type isnull
-
stripArray
public static java.lang.String stripArray(java.lang.String type)
Strips the array from a given type.- Parameters:
type- the type to remove the array from- Returns:
- the type without an array
- Throws:
java.lang.NullPointerException- if the given type isnull
-
isPrimitive
public static boolean isPrimitive(java.lang.String type)
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:
java.lang.NullPointerException- if the given type isnull
-
toPrimitive
public static java.lang.Class<?> toPrimitive(java.lang.String type)
-
getArrayDimension
public static int getArrayDimension(java.lang.String name)
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:
java.lang.NullPointerException- if the given name isnull
-
getArraySuffix
public static java.lang.String getArraySuffix(java.lang.String type)
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
public static <O extends JavaType<O>> java.lang.String rebuildGenericNameWithArrays(java.lang.String resolvedTypeName, Type<O> type)
-
getDefaultValue
public static java.lang.String getDefaultValue(java.lang.Class<?> clazz)
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:
java.lang.NullPointerException- if the given class isnull
-
getDefaultValue
public static java.lang.String getDefaultValue(java.lang.String type)
Returns the default value for a given type according to the Java Language Specification.- Parameters:
type- the type- Returns:
- the default value
- Throws:
java.lang.NullPointerException- if the given type isnull
-
splitGenerics
public static java.lang.String[] splitGenerics(java.lang.String typeName)
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:
java.lang.NullPointerException- if the given type isnull
-
-