Class Types

    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static boolean areEquivalent​(java.lang.String left, java.lang.String right)
      Checks if two type names are equal after removing the generic part.
      static int getArrayDimension​(java.lang.String name)
      Returns the dimension of the array.
      static java.lang.String getArraySuffix​(java.lang.String type)
      Get the array suffix.
      static java.lang.String getDefaultValue​(java.lang.Class<?> clazz)
      Returns the default value for a given class according to the Java Language Specification.
      static java.lang.String getDefaultValue​(java.lang.String type)
      Returns the default value for a given type according to the Java Language Specification.
      static java.lang.String getGenerics​(java.lang.String type)
      Returns the generic part of a given type.
      static java.lang.String getGenericsTypeParameter​(java.lang.String type)  
      static java.lang.String getPackage​(java.lang.String typeName)
      Extracts the package part of a given type name.
      static boolean isArray​(java.lang.String type)
      Checks if the given type is an array.
      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.
      static boolean isGeneric​(java.lang.String type)
      Checks if the given type is generic.
      static boolean isJavaLang​(java.lang.String type)
      Checks if the given type is part of the java.lang package.
      static boolean isPrimitive​(java.lang.String type)
      Checks if the given type is primitive according to the Java Language Specification.
      static boolean isQualified​(java.lang.String typeName)
      Checks if the given type name is qualified, that means that it contains at least one '.'.
      static boolean isSimpleName​(java.lang.String name)
      Checks if the given name is a simple name, so not full qualified.
      static <O extends JavaType<O>>
      java.lang.String
      rebuildGenericNameWithArrays​(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.String stripArray​(java.lang.String type)
      Strips the array from a given type.
      static java.lang.String stripGenerics​(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.String toResolvedType​(java.lang.String type, Importer<?> importer)
      Resolves the given type including generics.
      static java.lang.String toSimpleName​(java.lang.String type)
      Calculates the simple name of the given type including all generics.
      static boolean validateGenerics​(java.lang.String type)
      Checks if the generics are valid of this given type.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • 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) 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:
        java.lang.NullPointerException - if one of the arguments isnull
      • 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 is null
        java.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 resolve
        importer - the importer to use to check for existing imports
        Returns:
        the resolved type
        Throws:
        java.lang.NullPointerException - if the type and/or the importer are null
      • 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 is null
      • 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, false otherwise
        Throws:
        java.lang.NullPointerException - if the type name is null
      • 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 is null
      • 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, 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:
        java.lang.NullPointerException - if the name is null
      • isJavaLang

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

        public static boolean isGeneric​(java.lang.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:
        java.lang.NullPointerException - if the given type is null
      • 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, false otherwise
        Throws:
        java.lang.NullPointerException - if the given type is null
      • 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 is null
      • getGenerics

        public static java.lang.String getGenerics​(java.lang.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:
        java.lang.NullPointerException - if the given type is null
      • 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, false otherwise
        Throws:
        java.lang.NullPointerException - if the given type is null
      • 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 is null
      • 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:
        true if this type is primitive, false otherwise
        Throws:
        java.lang.NullPointerException - if the given type is null
      • 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 is null
      • getArraySuffix

        public static java.lang.String getArraySuffix​(java.lang.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>> 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 is null
      • 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 is null
      • 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 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:
        java.lang.NullPointerException - if the given type is null