Package es.bsc.dataclay.util.reflection
Class Reflector
- java.lang.Object
-
- es.bsc.dataclay.util.reflection.Reflector
-
public final class Reflector extends Object
This class contains useful functions when Java Reflection is necessary.
-
-
Field Summary
Fields Modifier and Type Field Description static ClassLoader
BOOTSTRAP_CLASSLOADER
Bootstrap class loader.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static int
countOccurrences(String str, String findStr)
Count the occurrences of some string into anotherstatic String
getCanonicalTypeNameFromSignature(String signature)
Get the name of the type corresponding to the provided signature and also get the [] array representation.static Class<?>
getClassFromSignatureAndArray(String signature, ClassLoader classLoader)
Get the class corresponding to the provided signature.static String
getDescriptorFromTypeName(String typeName)
Get the descriptor from of the type with name providedstatic Object
getField(Object instance, String fieldName)
This function gets the value of the field with name specified in the object instance provided.static String
getImmutableTypeNameFromPrimitiveTypeName(String typeName)
This function returns the primitive type name from immutable type name.static String
getInternalNameFromTypeName(String typeName)
Get internal name from type namestatic String
getPackageName(String fullName)
Get package name from full class namestatic String
getPrimitiveTypeNameFromImmutableTypeName(String typeName)
This function returns the primitive type name from immutable type name.static String
getSignatureFromConstructor(Constructor<?> constructor)
Get the signature from constructor providedstatic String
getSignatureFromInternalName(String internalName)
Get the signature from of the fully qualified type name (internal name) providedstatic String
getSignatureFromMethod(Method method)
Get the signature from method providedstatic String
getSignatureFromTypeName(String typeName)
Get the signature from of the type with name providedstatic Object
getStaticField(Class<?> staticClass, String fieldName)
This function gets the value of the static field with name specified in the object instance provided.static List<String>
getSubtypes(String signature)
Get sub typesstatic String
getTypeNameFromInternalName(String internalName)
Get type name from internal namestatic String
getTypeNameFromSignatureOrDescriptor(String signature)
Get the name of the type corresponding to the provided signature.static boolean
isArrayTypeName(String typeName)
Indicates if type name is an array type.static boolean
isImmutableSignature(String signature)
This function verifies if there is a immutable type with the signature providedstatic boolean
isImmutableTypeName(String typeName)
This function verifies if there is a lang type with the name provided.static boolean
isJavaPrimitiveOrArraySignature(String signature)
Check if the signature provided belongs to a Java type or to a Primitive type.static boolean
isJavaPrimitiveOrArrayTypeName(String typeName)
This function verifies if type name is java, array or primitive.static boolean
isJavaTypeName(String className)
This function verifies if there is a Java type with the name providedstatic boolean
isPrimitiveSignature(String signature)
This function verifies if there is a primitive type with the signature providedstatic boolean
isPrimitiveTypeName(String typeName)
Check if a name/signature is a Primitive type (quite close to Java primitives, but agnostic).static boolean
isStub(String className, ClassLoader theclassLoader)
Check if class with name provided is an stub.static boolean
isStubField(String fieldName)
Check if the field provided is DataClay fieldstatic void
nullifyAllFields(Object object)
Nullify all fields of the object in order to free space.static void
setField(Object instance, String fieldName, Object fieldValue)
Sets the value of the field of instance provided with field name specified
-
-
-
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 valuefieldName
- Name of the field- Returns:
- The value of the field with name specified in the instance provided
- Throws:
NoSuchFieldException
- If field not existsIllegalAccessException
- 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 valuefieldName
- Name of the field- Returns:
- The value of the static field with name specified in the instance provided
- Throws:
NoSuchFieldException
- If field not existsIllegalAccessException
- If field is not accessible
-
setField
public static void setField(Object instance, String fieldName, Object fieldValue) throws NoSuchFieldException, IllegalAccessException
Sets the value of the field of instance provided with field name specified- Parameters:
instance
- Instance to modifyfieldName
- Name of the field to setfieldValue
- Value to set- Throws:
NoSuchFieldException
- If the field does not exist.IllegalAccessException
- If the 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 analyzefindStr
- 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.
-
-