Class Reflector


  • public final class Reflector
    extends Object
    This class contains useful functions when Java Reflection is necessary.
    • Field Detail

      • BOOTSTRAP_CLASSLOADER

        public static final ClassLoader BOOTSTRAP_CLASSLOADER
        Bootstrap class loader.
    • Method Detail

      • getField

        public static Object getField​(Object instance,
                                      String fieldName)
                               throws NoSuchFieldException,
                                      IllegalAccessException
        This function gets the value of the field with name specified in the object instance provided. The field must exists in the class of the instance and must be accessible.
        Parameters:
        instance - Instance from which to get the field value
        fieldName - Name of the field
        Returns:
        The value of the field with name specified in the instance provided
        Throws:
        NoSuchFieldException - If field not exists
        IllegalAccessException - If field is not accessible
      • getStaticField

        public static Object getStaticField​(Class<?> staticClass,
                                            String fieldName)
                                     throws NoSuchFieldException,
                                            IllegalAccessException
        This function gets the value of the static field with name specified in the object instance provided. The field must exists in the class and must be static and accessible.
        Parameters:
        staticClass - Class from which to get the field value
        fieldName - Name of the field
        Returns:
        The value of the static field with name specified in the instance provided
        Throws:
        NoSuchFieldException - If field not exists
        IllegalAccessException - If field is not accessible
      • countOccurrences

        public static int countOccurrences​(String str,
                                           String findStr)
        Count the occurrences of some string into another
        Parameters:
        str - String to analyze
        findStr - String to count
        Returns:
        Number of occurrences of 'findStr' into 'str'.
      • getSubtypes

        public static List<String> getSubtypes​(String signature)
        Get sub types
        Parameters:
        signature - Signature from which to get subtypes
        Returns:
        List of sub types found
      • isPrimitiveTypeName

        public static boolean isPrimitiveTypeName​(String typeName)
        Check if a name/signature is a Primitive type (quite close to Java primitives, but agnostic).
        Parameters:
        typeName - type name to check.
        Returns:
        TRUE if the signature provided belongs to a Primitive type. FALSE otherwise.
      • isArrayTypeName

        public static boolean isArrayTypeName​(String typeName)
        Indicates if type name is an array type.
        Parameters:
        typeName - Type name
        Returns:
        TRUE if type name is array type.
      • isJavaTypeName

        public static boolean isJavaTypeName​(String className)
        This function verifies if there is a Java type with the name provided
        Parameters:
        className - name of the class
        Returns:
        TRUE if there is a java type with name provided. FALSE otherwise.
      • isJavaPrimitiveOrArrayTypeName

        public static boolean isJavaPrimitiveOrArrayTypeName​(String typeName)
        This function verifies if type name is java, array or primitive.
        Parameters:
        typeName - Type name.
        Returns:
        TRUE if type name is java, array or primitive.
      • isImmutableTypeName

        public static boolean isImmutableTypeName​(String typeName)
        This function verifies if there is a lang type with the name provided.
        Parameters:
        typeName - name of the type
        Returns:
        TRUE if there is a lang type with name provided. FALSE otherwise.
      • getPrimitiveTypeNameFromImmutableTypeName

        public static String getPrimitiveTypeNameFromImmutableTypeName​(String typeName)
        This function returns the primitive type name from immutable type name. WARNING: this function return "string" for immutable "java.lang.String". Used in ByteCode generation.
        Parameters:
        typeName - name of the type
        Returns:
        the primitive type name from immutable type name or the same type name if not immutable.
      • getImmutableTypeNameFromPrimitiveTypeName

        public static String getImmutableTypeNameFromPrimitiveTypeName​(String typeName)
        This function returns the primitive type name from immutable type name. WARNING: this function return "string" for immutable "java.lang.String". Used in ByteCode generation.
        Parameters:
        typeName - name of the type
        Returns:
        the primitive type name from immutable type name or the same type name if not immutable.
      • isJavaPrimitiveOrArraySignature

        public static boolean isJavaPrimitiveOrArraySignature​(String signature)
        Check if the signature provided belongs to a Java type or to a Primitive type.
        Parameters:
        signature - Signature to check.
        Returns:
        TRUE if the signature provided belongs to a Java type or to a Primitive type. FALSE otherwise.
      • isPrimitiveSignature

        public static boolean isPrimitiveSignature​(String signature)
        This function verifies if there is a primitive type with the signature provided
        Parameters:
        signature - signature of the type
        Returns:
        TRUE if there is a primitive type with signature provided. FALSE otherwise.
      • isImmutableSignature

        public static boolean isImmutableSignature​(String signature)
        This function verifies if there is a immutable type with the signature provided
        Parameters:
        signature - signature of the type
        Returns:
        TRUE if there is a immutable type with signature provided. FALSE otherwise.
      • getDescriptorFromTypeName

        public static String getDescriptorFromTypeName​(String typeName)
        Get the descriptor from of the type with name provided
        Parameters:
        typeName - Name of the type
        Returns:
        The descriptor of the type with name provided
      • getSignatureFromTypeName

        public static String getSignatureFromTypeName​(String typeName)
        Get the signature from of the type with name provided
        Parameters:
        typeName - Name of the type
        Returns:
        The signature of the type with name provided
      • getSignatureFromInternalName

        public static String getSignatureFromInternalName​(String internalName)
        Get the signature from of the fully qualified type name (internal name) provided
        Parameters:
        internalName - Internal Name of the type
        Returns:
        The signature from of the fully qualified type name (internal name) provided
      • getInternalNameFromTypeName

        public static String getInternalNameFromTypeName​(String typeName)
        Get internal name from type name
        Parameters:
        typeName - Type name
        Returns:
        Internal name.
      • getTypeNameFromInternalName

        public static String getTypeNameFromInternalName​(String internalName)
        Get type name from internal name
        Parameters:
        internalName - Internal name
        Returns:
        Type name
      • getTypeNameFromSignatureOrDescriptor

        public static String getTypeNameFromSignatureOrDescriptor​(String signature)
        Get the name of the type corresponding to the provided signature.
        Parameters:
        signature - Signature to analyze
        Returns:
        The name of the type corresponding to the provided signature.
      • getCanonicalTypeNameFromSignature

        public static String getCanonicalTypeNameFromSignature​(String signature)
        Get the name of the type corresponding to the provided signature and also get the [] array representation.
        Parameters:
        signature - Signature to analyze
        Returns:
        The name of the type corresponding to the provided signature.
      • isStubField

        public static boolean isStubField​(String fieldName)
        Check if the field provided is DataClay field
        Parameters:
        fieldName - Name of the field
        Returns:
        TRUE if the class provided is some of the types in a stub. FALSE otherwise.
      • getSignatureFromMethod

        public static String getSignatureFromMethod​(Method method)
        Get the signature from method provided
        Parameters:
        method - Method
        Returns:
        The signature of method provided
      • getSignatureFromConstructor

        public static String getSignatureFromConstructor​(Constructor<?> constructor)
        Get the signature from constructor provided
        Parameters:
        constructor - Method
        Returns:
        The signature of constructor provided
      • getClassFromSignatureAndArray

        public static Class<?> getClassFromSignatureAndArray​(String signature,
                                                             ClassLoader classLoader)
        Get the class corresponding to the provided signature. If the signature belongs to an array get its component type.
        Parameters:
        signature - Signature to check.
        classLoader - Class loader to use
        Returns:
        The class corresponding to the provided signature. If the signature belongs to an array get its component type.
      • isStub

        public static boolean isStub​(String className,
                                     ClassLoader theclassLoader)
        Check if class with name provided is an stub.
        Parameters:
        className - Name of the class to analyze.
        theclassLoader - Class loader being used
        Returns:
        TRUE if it is an stub for the class loader used for this class. FALSE otherwise.
      • nullifyAllFields

        public static void nullifyAllFields​(Object object)
        Nullify all fields of the object in order to free space.
        Parameters:
        object - Object to nullify.
      • getPackageName

        public static String getPackageName​(String fullName)
        Get package name from full class name
        Parameters:
        fullName - Full package name
        Returns:
        Package name