Package com.helixframework.reflection
Class ClassUtil
java.lang.Object
com.helixframework.reflection.ClassUtil
Class reflection utilities.
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> TcreateInstance(Class<T> clazz) Creates a new instance of the specified class.static <T> TCreates a new instance of the specified class using the provided parameter types and parameters.static <T> TcreateInstance(String className) Creates a new instance of the specified class.static <T> TCreates a new instance of the specified class using the provided parameter types and parameters.getFieldsUpTo(Class<?> startClass, Class<?> exclusiveParent) Retrieves all fields of a class and its superclasses up to an exclusive parent class.static ObjectgetFieldValue(Object target, String fieldName) Retrieves the value of a field from the target object.static List<PropertyDescriptor>getReadableProperties(Class<?> instanceType) Retrieves a list of readable property descriptors for the given class.static Map<String,PropertyDescriptor> getReadablePropertiesAsMap(Class<?> instanceType) Retrieves readable property descriptors for the given class as a map.static PropertyDescriptorgetReadableProperty(String name, Class<?> instanceType) Retrieves a readable property descriptor for the given class and property name.static booleanisArraysArrayList(Class<?> clazz) Determines if the given class is an instance of Arrays$ArrayList.static booleanisAssignable(Class<?> type, Class<?>... classes) Determines if the given type is assignable to all the specified classes.static booleanisPrimitive(Class<?> clazz) Determines if the given class is a primitive type.static booleanDetermines if the given class is a record.static booleanDetermines if the given object is a record.static <T> Class<T>load(T instance) Loads the class of the given instance.static voidsetFieldValue(Object target, String fieldName, Object value) Sets the value of a field on the target object.
-
Method Details
-
load
Loads the class of the given instance.- Type Parameters:
T- the type of the instance- Parameters:
instance- the instance whose class should be loaded- Returns:
- the class of the instance
- Throws:
IllegalArgumentException- if the instance is null
-
createInstance
Creates a new instance of the specified class.- Type Parameters:
T- the type of the class- Parameters:
clazz- the class to create an instance of- Returns:
- a new instance of the specified class
- Throws:
IllegalArgumentException- if the class is nullReflectionRuntimeException- if an error occurs while creating the instance
-
createInstance
public static <T> T createInstance(Class<T> clazz, List<Class<?>> parameterTypes, List<Object> parameters) Creates a new instance of the specified class using the provided parameter types and parameters.- Type Parameters:
T- the type of the class- Parameters:
clazz- the class to create an instance ofparameterTypes- a list of parameter types for the constructorparameters- a list of parameter values to be passed to the constructor- Returns:
- a new instance of the specified class
- Throws:
IllegalArgumentException- if the class, parameter types, or parameters are null, or if the number of parameter types does not match the number of supplied parametersReflectionRuntimeException- if an error occurs while creating the instance
-
createInstance
Creates a new instance of the specified class.- Parameters:
className- the fully qualified name of the class to create an instance of- Returns:
- a new instance of the specified class
- Throws:
IllegalArgumentException- if the className is null or blankReflectionRuntimeException- if an error occurs while creating the instance
-
createInstance
public static <T> T createInstance(String className, List<Class<?>> parameterTypes, List<Object> parameters) Creates a new instance of the specified class using the provided parameter types and parameters.- Type Parameters:
T- the type of the class- Parameters:
className- the fully qualified name of the class to create an instance ofparameterTypes- a list of parameter types for the constructorparameters- a list of parameter values to be passed to the constructor- Returns:
- a new instance of the specified class
- Throws:
IllegalArgumentException- if the class, parameter types, or parameters are null, or if the number of parameter types does not match the number of supplied parametersReflectionRuntimeException- if an error occurs while creating the instance
-
isAssignable
Determines if the given type is assignable to all the specified classes.- Parameters:
type- the type to checkclasses- the classes to check against- Returns:
- true if the type is assignable to all the classes, false otherwise
- Throws:
IllegalArgumentException- if the type or classes are null
-
isPrimitive
Determines if the given class is a primitive type.- Parameters:
clazz- the class to check- Returns:
- true if the given class is a primitive type, false otherwise
-
isRecord
Determines if the given object is a record.- Parameters:
instance- the object to check- Returns:
- true if the object is a record, false otherwise
-
isRecord
Determines if the given class is a record.- Parameters:
clazz- the class to check- Returns:
- true if the class is a record, false otherwise
-
isArraysArrayList
Determines if the given class is an instance of Arrays$ArrayList.java.util.Arrays$ArrayList is a nested class inside the Arrays class that implements the List interface. This implementation has a fixed length and is backed by an array. The method Arrays.asList returns an instance of this class.
- Parameters:
clazz- the class to check- Returns:
- true if the given class is an instance of Arrays$ArrayList, false otherwise
-
getReadableProperties
Retrieves a list of readable property descriptors for the given class.- Parameters:
instanceType- the class for which to retrieve the readable property descriptors- Returns:
- a list of readable property descriptors for the given class
-
getReadablePropertiesAsMap
Retrieves readable property descriptors for the given class as a map.- Parameters:
instanceType- the class for which to retrieve the readable property descriptors- Returns:
- a map of property names to property descriptors for the given class
-
getReadableProperty
Retrieves a readable property descriptor for the given class and property name.- Parameters:
name- the name of the propertyinstanceType- the class for which to retrieve the property descriptor- Returns:
- the readable property descriptor for the given class and property name
-
getFieldsUpTo
Retrieves all fields of a class and its superclasses up to an exclusive parent class.- Parameters:
startClass- the class to start retrieving fields fromexclusiveParent- the exclusive parent class, fields from this class will not be included- Returns:
- an iterable containing all the fields
-
getFieldValue
Retrieves the value of a field from the target object.- Parameters:
target- the object from which to retrieve the field valuefieldName- the name of the field to retrieve- Returns:
- the value of the field from the target object
- Throws:
IllegalArgumentException- if the target is null or the field name is null or blankReflectionRuntimeException- if an error occurs while retrieving the field value
-
setFieldValue
Sets the value of a field on the target object.- Parameters:
target- the object on which to set the field valuefieldName- the name of the field to setvalue- the value to set- Throws:
IllegalArgumentException- if the target is null or the field name is null or blankReflectionRuntimeException- if an error occurs while setting the field value
-