Class Types

java.lang.Object
org.jboss.forge.roaster.model.util.Types

public class Types extends Object
Util classes for java types.
Author:
Lincoln Baxter, III, George Gastaldi
  • Method Details

    • areEquivalent

      public static boolean areEquivalent(String left, 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) returns true
      • 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) returns true
      Parameters:
      left - the first type (maybe null)
      right - the second type (maybe null)
      Returns:
      true if the above conditions are fulfilled, false otherwise
      Throws:
      NullPointerException - if one of the arguments isnull
    • toSimpleName

      public static String toSimpleName(String type)
      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 is null
      IllegalArgumentException - if the generic part couldn't be parsed
    • toResolvedType

      public static String toResolvedType(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 resolve
      importer - the importer to use to check for existing imports
      Returns:
      the resolved type
      Throws:
      NullPointerException - if the type and/or the importer are null
    • tokenizeClassName

      public static String[] tokenizeClassName(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:
      NullPointerException - if the class name is null
    • isQualified

      public static boolean isQualified(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, false otherwise
      Throws:
      NullPointerException - if the type name is null
    • getPackage

      public static String getPackage(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:
      NullPointerException - if the type name is null
    • isSimpleName

      public static boolean isSimpleName(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, false is returned.
      Parameters:
      name - the name to check
      Returns:
      true, if the name is simple, false otherwise or if the name is no valid java identifier
      Throws:
      NullPointerException - if the name is null
    • isJavaLang

      public static boolean isJavaLang(String type)
      Checks if the given type is part of the java.lang package. For this, the type is first transformed to a simple type.

      The consequence is that the following will return true: isJavaLang("test.String"), because String is part of java.lang.

      Parameters:
      type - the type to check
      Returns:
      true, if the simple name of the type is part of java.lang, false otherwise
      Throws:
      NullPointerException - if the type name is null
    • isBasicType

      public static boolean isBasicType(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:
      true if this type is basic, false otherwise
      Throws:
      NullPointerException - if the type name is null
    • isGeneric

      public static boolean isGeneric(String type)
      Checks if the given type is generic.
      Parameters:
      type - the type to check
      Returns:
      true if this type is generic, false otherwise
      Throws:
      NullPointerException - if the given type is null
    • validateGenerics

      public static boolean validateGenerics(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, false otherwise
      Throws:
      NullPointerException - if the given type is null
    • stripGenerics

      public static String stripGenerics(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:
      NullPointerException - if the given type is null
    • getGenerics

      public static String getGenerics(String type)
      Returns the generic part of a given type. For example getGenerics("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 is null
    • getGenericsTypeParameter

      public static String getGenericsTypeParameter(String type)
    • isArray

      public static boolean isArray(String type)
      Checks if the given type is an array.
      Parameters:
      type - the type to check
      Returns:
      true, if the type is an array, false otherwise
      Throws:
      NullPointerException - if the given type is null
    • stripArray

      public static String stripArray(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:
      NullPointerException - if the given type is null
    • isPrimitive

      public static boolean isPrimitive(String type)
      Checks if the given type is primitive according to the Java Language Specification.
      Parameters:
      type - the type to check
      Returns:
      true if this type is primitive, false otherwise
      Throws:
      NullPointerException - if the given type is null
    • toPrimitive

      public static Class<?> toPrimitive(String type)
    • getArrayDimension

      public static int getArrayDimension(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:
      NullPointerException - if the given name is null
    • getArraySuffix

      public static String getArraySuffix(String type)
      Get the array suffix. For example getArraySuffix("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>> String rebuildGenericNameWithArrays(String resolvedTypeName, Type<O> type)
    • getDefaultValue

      public static String getDefaultValue(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:
      NullPointerException - if the given class is null
    • getDefaultValue

      public static String getDefaultValue(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:
      NullPointerException - if the given type is null
    • splitGenerics

      public static String[] splitGenerics(String typeName)
      Returns the available generics as a String array. Only the first level is split. For example splitGenerics("Foo<Bar<A>, Bar<B>>") returns an array with Bar<A> and Bar<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 is null