Package com.jk.core.util
Class JKObjectUtil
java.lang.Object
com.jk.core.util.JKObjectUtil
This class provides utility methods for working with Java objects and
reflection.
- Version:
- 1.0
- Author:
- Dr. Jalal H. Kiswani
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic void
addItemToList
(Object target, String fieldName, Object value) This method adds an item to a list property with the specified field name on the given target object.static Object
callMethod
(Object obj, String methodName, boolean includePrivateMehtods, Object... args) This method calls a method on an object.static Object
callMethod
(Object obj, String methodName, Object... args) This method calls a method on an object.static Object
callStaticMethod
(Class clas, String methodName, Object... params) This method calls a static method of the specified class with the given method name and parameters.static <T> T
This method creates a deep copy of the provided bean using the BeanUtils library.static Object
This method creates a deep copy of the provided object using serialization and deserialization.static <T> T
createInstanceForGenericClass
(Object parent) This method creates an instance of a generic class based on the generic parameter provided by its parent class.static com.fasterxml.jackson.databind.ObjectMapper
This method creates and configures aJackson ObjectMapper
for JSON serialization and deserialization.static com.fasterxml.jackson.databind.ObjectMapper
createMapper
(com.fasterxml.jackson.core.JsonFactory factory) This method creates and configures aJackson ObjectMapper
for JSON serialization and deserialization.static boolean
This method compares two objects for equality based on their string representations.static Object
This method evaluates a Spring Expression Language (SpEL) expression against the provided root object.static String
fixPropertyName
(String name) This method fixes a property name by capitalizing every character after an underscore and removing underscores.getAllClassesInPackage
(String packageName) This method retrieves the names of all classes within the specified package.static <T> String[]
getAllFieldNames
(Class<T> modelClass) This method retrieves the names of all fields declared in the specified class and its super-classes.getAllFields
(Class<?> clas) This method retrieves all fields (including inherited fields) of a given class.static Class<?>
This method retrieves the Class object corresponding to the provided fully qualified class name.static Field
getFieldByName
(Class<?> classType, String name) This method retrieves a Field object by name from the given class or its super-classes.static String
getFieldNameByType
(Class<?> classType, Class<?> fieldType) This method finds the name of a field in the given class that has the specified field type.static Object
getFieldValue
(Object instance, String fieldName) This method retrieves the value of a field from the given object instance.static Class
getGenericClassFromParent
(Object parent) This method retrieves the generic parameter type from the parent class.static <T> Class<? extends T>
getGenericParamter
(String handler) This method retrieves the class type of the generic parameter for a given handler.static <T> String
getInstanceVariables
(Class<T> clas) This method retrieves the names of instance variables (fields) declared in the provided class and its super-classes.static Object
getPrivateValue
(Class<?> clas, Object instance, String fieldName) This method retrieves the value of a private field from an instance.static Object
getPrivateValue
(Object instance, String fieldName) This method retrieves the value of a private field from an instance.getProperties
(Class<T> clas) This method retrieves the names of properties (getters) declared in the provided class and its super-classes.static <T> T
getPropertyValue
(Object instance, String fieldName) This method retrieves the value of a specified property from the given instance.static Class<?>
getWrapperForPrimitive
(Class<?> clas) This method retrieves the wrapper class for a given primitive class.static int
This method calculates the hash code for a given string.static boolean
This method checks if the given class represents a boolean type.static boolean
isClassAvilableInClassPath
(String string) This method checks if a class is available in the class path.static boolean
This method checks if the given class represents a date type.static boolean
isFieldExists
(Class<?> clas, String fieldName) This method checks if a field with the given name exists within the specified class or its super-classes.static boolean
This method checks if the given field is declared as 'final'.static boolean
This method checks if the provided class represents an integer type (primitive or wrapper).static boolean
isMethodDirectlyExists
(Object object, String methodName, Class<?>... params) This method checks if a method with the given name and parameter types exists directly in the class of the object.static boolean
isPrimitive
(Class<?> type) This method checks if a given class is a primitive type or a wrapper class.static boolean
This method checks if the given field is declared as 'static'.static boolean
This method checks if the given class represents a time type.static boolean
isTimeStamp
(Class type) This method checks if the given class represents a time stamp type.static boolean
isTransient
(Field field) This method checks if the given field is declared as 'transient'.static <T> T
jsonToObject
(String json, Class<T> clas) This method converts a JSON string to an object of the specified class using theJackson
library.static <T> List<T>
jsonToObjectList
(String json, Class<T> clas) This method converts a JSON string to a list of objects of the specified class using theJackson
library.static <T> T
newInstance
(Class<T> clas) This method creates a new instance of the provided class using its default constructor.static <T> T
newInstance
(Class<T> clas, Object... params) This method creates a new instance of the provided class using the constructor that matches the given parameters.static <T> T
newInstance
(String className) This method creates a new instance of the class with the provided class name using its default constructor.static void
This method populates the properties of an instance with values from a map.static void
setPeopertyValue
(Object target, String fieldName, Object value) This method sets the value of a property with the specified field name on the given target object.static void
setPropertiesByOrder
(Object model, String[] names, String[] values) This method sets properties of an object in the specified order using the provided names and values.static Class<?>
This method converts the given fully qualified class name to a Class object.static Class<?>[]
toClassesFromObjects
(Object[] params) This method converts an array of objects to an array of their corresponding Class objects.static String
This method converts an object into its JSON representation usingJackson ObjectMapper
.static Object
This method converts an XML representation back to an object usingXMLDecoder
.static String
This method converts an object to its string representation.static String
This method converts an object to its string representation with options for customization.static String
This method converts the provided object to an XML representation using XMLEncoder.static String
This method converts an object to YAML format using theJackson
library.static <T> T
yamlToObject
(String yaml, Class<T> clas) This method converts a YAML string to an object of the specified class using theJackson
library.
-
Constructor Details
-
JKObjectUtil
public JKObjectUtil()
-
-
Method Details
-
cloneBean
This method creates a deep copy of the provided bean using the BeanUtils library.- Type Parameters:
T
- Specifies the type of the object being cloned.- Parameters:
bean
- Specifies the object to be cloned.- Returns:
- the deep copy of the provided bean.
-
getClass
This method retrieves the Class object corresponding to the provided fully qualified class name.- Parameters:
beanClassName
- Specifies the fully qualified name of the class.- Returns:
- the Class object corresponding to the provided class name.
-
getPropertyValue
This method retrieves the value of a specified property from the given instance.- Type Parameters:
T
- Specifies the expected type of the property value.- Parameters:
instance
- Specifies the object instance from which to retrieve the property value.fieldName
- Specifies the name of the property to retrieve.- Returns:
- the value of the specified property.
-
isBoolean
This method checks if the given class represents a boolean type.- Parameters:
type
- Specifies the class to check.- Returns:
- true, if the class represents a boolean type, false otherwise.
-
isDate
This method checks if the given class represents a date type.- Parameters:
type
- Specifies the class to check- Returns:
- true, if the class represents a date type, false otherwise.
-
isFinal
This method checks if the given field is declared as 'final'.- Parameters:
field
- Specifies the field to check.- Returns:
- true, if the field is declared as 'final', false otherwise.
-
isStatic
This method checks if the given field is declared as 'static'.- Parameters:
field
- Specifies the field to check.- Returns:
- true, if the field is declared as 'static', false otherwise.
-
isTime
This method checks if the given class represents a time type.- Parameters:
type
- Specifies the class to check- Returns:
- true, if the class represents a time type, false otherwise.
-
isTimeStamp
This method checks if the given class represents a time stamp type.- Parameters:
type
- Specifies the class to check- Returns:
- true, if the class represents a time stamp type, false otherwise.
-
isTransient
This method checks if the given field is declared as 'transient'.- Parameters:
field
- Specifies the field to check.- Returns:
- true, if the field is declared as 'transient', false otherwise
-
newInstance
This method creates a new instance of the provided class using its default constructor.- Type Parameters:
T
- Specifies the type of the class.- Parameters:
clas
- Specifies the class for which an instance needs to be created.- Returns:
- the new instance of the provided class.
-
newInstance
This method creates a new instance of the provided class using the constructor that matches the given parameters.- Type Parameters:
T
- Specifies the type of the class.- Parameters:
clas
- Specifies the class for which an instance needs to be created.params
- Specifies the parameters to be passed to the constructor.- Returns:
- the new instance of the provided class.
-
newInstance
This method creates a new instance of the class with the provided class name using its default constructor.- Type Parameters:
T
- Specifies the type of the class.- Parameters:
className
- Specifies the fully qualified name of the class to instantiate.- Returns:
- the new instance of the specified class.
-
setPeopertyValue
This method sets the value of a property with the specified field name on the given target object.- Parameters:
target
- Specifies the object on which the property value will be set.fieldName
- Specifies the name of the field (property) to set the value for.value
- Specifies the value to set for the property.
-
addItemToList
This method adds an item to a list property with the specified field name on the given target object.- Parameters:
target
- Specifies the object on which the list property will be modified.fieldName
- Specifies the name of the list field property to add the item to.value
- Specifies the value to be added to the list property.
-
toClass
This method converts the given fully qualified class name to a Class object.- Parameters:
name
- Specifies the fully qualified name of the class.- Returns:
- the object representing the specified class name.
-
toClassesFromObjects
This method converts an array of objects to an array of their corresponding Class objects.- Parameters:
params
- Specifies the array of objects.- Returns:
- the array of Class objects representing the types of the input objects.
-
toString
This method converts an object to its string representation with options for customization.- Parameters:
object
- Specifies the object to convert.useCurrentTostringIfAvailable
- Indicates whether to uses the object's existing toString method if available or not.- Returns:
- the string representation of the object.
-
toString
This method converts an object to its string representation.- Parameters:
object
- Specifies the object to convert.- Returns:
- the string representation of the object.
-
isMethodDirectlyExists
This method checks if a method with the given name and parameter types exists directly in the class of the object.- Parameters:
object
- Specifies the object whose class will be checked for the method.methodName
- Specifies the name of the method to check for.params
- Specifies the parameter types of the method.- Returns:
- true, if the method exists, false otherwise.
-
equals
This method compares two objects for equality based on their string representations.- Parameters:
source
- Specifies the first object to compare.target
- Specifies the second object to compare.- Returns:
- true, if the string representations of the objects are equalm, false otherwise.
-
getFieldValue
This method retrieves the value of a field from the given object instance.- Parameters:
instance
- Specifies the object instance from which to retrieve the field value.fieldName
- Specifies the name of the field to retrieve the value from.- Returns:
- the value of the specified field in the object instance.
-
callStaticMethod
This method calls a static method of the specified class with the given method name and parameters.- Parameters:
clas
- Specifies the class containing the static method.methodName
- Specifies the name of the static method to call.params
- Specifies the parameters to pass to the static method.- Returns:
- the result of the static method call.
-
copy
This method creates a deep copy of the provided object using serialization and deserialization.- Parameters:
source
- Specifies the object to be copied.- Returns:
- the deep copy of the provided object.
-
toXml
This method converts the provided object to an XML representation using XMLEncoder.- Parameters:
obj
- Specifies the object to be converted to XML.- Returns:
- the XML representation of the provided object.
-
toObject
This method converts an XML representation back to an object usingXMLDecoder
.- Parameters:
xml
- Specifies the XML representation to be converted to an object.- Returns:
- the object reconstructed from the XML representation.
-
hash
This method calculates the hash code for a given string.- Parameters:
name
- Specifies the string for which the hash code needs to be calculated.- Returns:
- the hash code value calculated for the input string.
-
getGenericParamter
This method retrieves the class type of the generic parameter for a given handler.- Type Parameters:
T
- Specifies the type of the generic parameter.- Parameters:
handler
- Specifies the handler for which to retrieve the generic parameter's class type.- Returns:
- the class type of the generic parameter for the given handler.
-
fixPropertyName
This method fixes a property name by capitalizing every character after an underscore and removing underscores.- Parameters:
name
- Specifies the property name to be fixed.- Returns:
- the fixed property name.
-
populate
This method populates the properties of an instance with values from a map.- Parameters:
instance
- Specifies the instance whose properties will be populated.values
- Specifeis the map containing property names as keys and corresponding values.
-
isClassAvilableInClassPath
This method checks if a class is available in the class path.- Parameters:
string
- Specifies the name of the class to check.- Returns:
- true, if the class is found in the class path, false otherwise.
-
callMethod
public static Object callMethod(Object obj, String methodName, boolean includePrivateMehtods, Object... args) throws InvocationTargetException This method calls a method on an object.- Parameters:
obj
- Specifies the object on which the method is to be invoked.methodName
- Specifies the name of the method to invoke.includePrivateMehtods
- Indicates whether to includes private methods in the search for the method or not.args
- Specifies the arguments to pass to the method.- Returns:
- the result of invoking the method.
- Throws:
InvocationTargetException
- if the method invocation throws an exception.
-
callMethod
This method calls a method on an object.- Parameters:
obj
- Specifies the object on which the method is to be invoked.methodName
- Specifies the name of the method to invoke.args
- Specifies the arguments to pass to the method.- Returns:
- the result of invoking the method.
-
getInstanceVariables
This method retrieves the names of instance variables (fields) declared in the provided class and its super-classes.- Type Parameters:
T
- Specifies the type of the class.- Parameters:
clas
- Specifies the class for which to retrieve the instance variable names.- Returns:
- a comma-separated string containing the names of instance variables.
-
getProperties
This method retrieves the names of properties (getters) declared in the provided class and its super-classes.- Type Parameters:
T
- Specifies the type of the class.- Parameters:
clas
- Specifies the class for which to retrieve the property names.- Returns:
- a list of strings containing the names of properties.
-
toJson
This method converts an object into its JSON representation usingJackson ObjectMapper
.- Parameters:
obj
- Specifies the object to be converted to JSON.- Returns:
- the JSON string representing the object.
-
createMapper
public static com.fasterxml.jackson.databind.ObjectMapper createMapper()This method creates and configures aJackson ObjectMapper
for JSON serialization and deserialization.- Returns:
- the instance of ObjectMapper configured for JSON processing.
-
createMapper
public static com.fasterxml.jackson.databind.ObjectMapper createMapper(com.fasterxml.jackson.core.JsonFactory factory) This method creates and configures aJackson ObjectMapper
for JSON serialization and deserialization.- Parameters:
factory
- Specifies the JsonFactory to use for creating JSON parsers and generators, or null to use the default.- Returns:
- the instance of ObjectMapper configured for JSON processing.
-
toYaml
This method converts an object to YAML format using theJackson
library.- Parameters:
obj
- Specifies the object to be converted to YAML.- Returns:
- the YAML representation of the object.
-
jsonToObject
This method converts a JSON string to an object of the specified class using theJackson
library.- Type Parameters:
T
- Specifies the type of the object.- Parameters:
json
- Specifies the JSON string to be converted to an object.clas
- Specifies the class of the object to be created.- Returns:
- the instance of the specified class populated with data from the JSON string.
-
jsonToObjectList
This method converts a JSON string to a list of objects of the specified class using theJackson
library.- Type Parameters:
T
- Specifies the type of the objects in the list.- Parameters:
json
- Specifies the JSON string to be converted to a list of objects.clas
- Specifies the class of the objects in the list.- Returns:
- a list of instances of the specified class populated with data from the JSON string.
-
yamlToObject
This method converts a YAML string to an object of the specified class using theJackson
library.- Type Parameters:
T
- Specifies the type of the object to be created.- Parameters:
yaml
- Specifies the YAML string to be converted to an object.clas
- Specifies the class of the object to be created from the YAML string.- Returns:
- the instance of the specified class populated with data from the YAML string.
-
getWrapperForPrimitive
This method retrieves the wrapper class for a given primitive class.- Parameters:
clas
- Specifies the primitive class for which to find the wrapper class.- Returns:
- the wrapper class corresponding to the given primitive class, or null if not found.
-
isPrimitive
This method checks if a given class is a primitive type or a wrapper class.- Parameters:
type
- Specifies the class to check.- Returns:
- true, if the class is a primitive type or a wrapper class, false otherwise.
-
getFieldNameByType
This method finds the name of a field in the given class that has the specified field type.- Parameters:
classType
- Specifies the class to search for the field.fieldType
- Specifies the type of the field to search for.- Returns:
- the name of the field with the specified type, or null if not found.
-
getFieldByName
This method retrieves a Field object by name from the given class or its super-classes.- Parameters:
classType
- Specifies the class to search for the field.name
- Specifies the name of the field to retrieve.- Returns:
- the Field object corresponding to the specified name, or null if not found.
-
isInteger
This method checks if the provided class represents an integer type (primitive or wrapper).- Parameters:
type
- Specifies the class to check.- Returns:
- true, if the class represents an integer type, false otherwise.
-
isFieldExists
This method checks if a field with the given name exists within the specified class or its super-classes.- Parameters:
clas
- Specifies the class to check for the field.fieldName
- Specifies the name of the field to check.- Returns:
- true, if the field exists, false otherwise.
-
getAllClassesInPackage
This method retrieves the names of all classes within the specified package.- Parameters:
packageName
- Specifies the name of the package to search for classes.- Returns:
- a list of class names within the package.
-
getAllFields
This method retrieves all fields (including inherited fields) of a given class.- Parameters:
clas
- Specifies the class for which to retrieve fields.- Returns:
- the list of Field objects representing the fields of the class.
-
createInstanceForGenericClass
This method creates an instance of a generic class based on the generic parameter provided by its parent class.- Type Parameters:
T
- Specifies the type of the generic parameter.- Parameters:
parent
- Specifies the parent object from which to determine the generic parameter type.- Returns:
- an instance of the generic class.
-
getGenericClassFromParent
This method retrieves the generic parameter type from the parent class.- Parameters:
parent
- Specifies the parent object from which to determine the generic parameter type.- Returns:
- the Class representing the generic parameter type, or null if not found.
-
getPrivateValue
This method retrieves the value of a private field from an instance.- Parameters:
instance
- Specifies the instance from which to retrieve the value.fieldName
- Specifies the name of the private field.- Returns:
- the value of the private field, or null if not found.
-
getPrivateValue
This method retrieves the value of a private field from an instance.- Parameters:
clas
- Specifies the class to search for the field.instance
- Specifies the instance from which to retrieve the value.fieldName
- Specifies the name of the private field.- Returns:
- the value of the private field, or null if not found.
-
evaluate
This method evaluates a Spring Expression Language (SpEL) expression against the provided root object.- Parameters:
rootObject
- Specifies the root object against which the expression will be evaluated.valueExpression
- Specifies the SpEL expression to evaluate.- Returns:
- the result of the expression evaluation.
-
setPropertiesByOrder
This method sets properties of an object in the specified order using the provided names and values.- Parameters:
model
- Specifies the object for which properties will be set.names
- Specifies an array of property names in the order they should be set.values
- Specifies an array of corresponding values to set for each property.
-
getAllFieldNames
This method retrieves the names of all fields declared in the specified class and its super-classes.- Type Parameters:
T
- Specifies the type of the class.- Parameters:
modelClass
- Specifies the class for which field names will be retrieved.- Returns:
- an array of field names.
-