Package net.solarnetwork.util
Class ClassUtils
java.lang.Object
net.solarnetwork.util.ClassUtils
Utility methods for dealing with classes at runtime.
- Version:
- 2.0
- Author:
- matt
-
Field Summary
FieldsModifier and TypeFieldDescriptionA set of package name prefix values representing built-in Java classes. -
Method Summary
Modifier and TypeMethodDescriptionstatic voidcopyBeanProperties(Object src, Object dest, Set<String> ignore) Copy non-null bean properties from one object to another.static voidcopyBeanProperties(Object src, Object dest, Set<String> ignore, boolean emptyStringToNull) Copy non-null bean properties from one object to another.getAllInterfacesForClassAsSet(Class<?> clazz, Set<String> excluding) Get a set of interfaces implemented by a class and any superclasses or extended interfaces, optionally excluding based on a set of name prefix values.getAllNonJavaInterfacesForClassAsSet(Class<?> clazz) Get all interfaces implemented by a class, excluding those in theJAVA_PACKAGE_PREFIXESpackage set.getBeanProperties(Object o, Set<String> ignore) Get a Map of non-null bean properties for an object.getBeanProperties(Object o, Set<String> ignore, boolean serializeIgnore) Get a Map of non-null bean properties for an object.static StringgetResourceAsString(String resourceName, Class<?> clazz) Load a textual classpath resource into a String.static StringgetResourceAsString(String resourceName, Class<?> clazz, Pattern skip) Load a textual classpath resource into a String.getSimpleBeanProperties(Object o, Set<String> ignore) Get a Map of non-null simple bean properties for an object.static <T> TinstantiateClass(String className, Class<T> type) Instantiate a class of a specific interface type.static <T> Class<? extends T>Load a class of a particular type.static voidsetBeanProperties(Object o, Map<String, ?> values) Set bean property values on an object from a Map.static voidsetBeanProperties(Object o, Map<String, ?> values, boolean ignoreErrors) Set bean property values on an object from a Map.
-
Field Details
-
JAVA_PACKAGE_PREFIXES
A set of package name prefix values representing built-in Java classes.This can be useful as an exclusion set in the
getAllInterfacesForClassAsSet(Class, Set)method.- Since:
- 1.3
-
-
Method Details
-
instantiateClass
Instantiate a class of a specific interface type.- Type Parameters:
T- the desired interface type- Parameters:
className- the class name that implements the interfacetype- the desired interface- Returns:
- new instance of the desired type
-
loadClass
Load a class of a particular type.This uses the
type's ClassLoader to load the class. If that is not available, it will use the current thread's context class loader.- Type Parameters:
T- the desired interface type- Parameters:
className- the class name that implements the interfacetype- the desired interface- Returns:
- the class
-
setBeanProperties
Set bean property values on an object from a Map.- Parameters:
o- the bean to set JavaBean properties onvalues- a Map of JavaBean property names and their corresponding values to set
-
setBeanProperties
Set bean property values on an object from a Map.- Parameters:
o- The bean to set JavaBean properties on.values- A Map of JavaBean property names and their corresponding values to set.ignoreErrors- Flag to ignore unknown and invalid properties.- Since:
- 1.1
-
getBeanProperties
Get a Map of non-null bean properties for an object.- Parameters:
o- the object to inspectignore- a set of property names to ignore (optional)- Returns:
- Map (never null)
-
getSimpleBeanProperties
Get a Map of non-null simple bean properties for an object.- Parameters:
o- the object to inspectignore- a set of property names to ignore (optional)- Returns:
- Map (never null)
- Since:
- 1.1
-
copyBeanProperties
Copy non-null bean properties from one object to another.- Parameters:
src- the object to copy values fromdest- the object to copy values toignore- a set of property names to ignore (optional) where null
-
copyBeanProperties
public static void copyBeanProperties(Object src, Object dest, Set<String> ignore, boolean emptyStringToNull) Copy non-null bean properties from one object to another.- Parameters:
src- the object to copy values fromdest- the object to copy values toignore- a set of property names to ignore (optional)emptyStringToNull- if true then String values that are empty or contain only whitespace will be treated as if they where null
-
getBeanProperties
public static Map<String,Object> getBeanProperties(Object o, Set<String> ignore, boolean serializeIgnore) Get a Map of non-null bean properties for an object.- Parameters:
o- the object to inspectignore- a set of property names to ignore (optional)serializeIgnore- if true test for theSerializeIgnoreannotation for ignoring properties- Returns:
- Map (never null)
-
getResourceAsString
Load a textual classpath resource into a String.- Parameters:
resourceName- the resource to loadclazz- the Class to load the resource from- Returns:
- the String
-
getResourceAsString
Load a textual classpath resource into a String.- Parameters:
resourceName- the resource to loadclazz- the Class to load the resource fromskip- an optional pattern that will be used to match against lines; matches will be left out of the string used to match- Returns:
- the text
- Throws:
RuntimeException- if the resource cannot be loaded- Since:
- 1.3
-
getAllNonJavaInterfacesForClassAsSet
Get all interfaces implemented by a class, excluding those in theJAVA_PACKAGE_PREFIXESpackage set.- Parameters:
clazz- the class to get all implemented interfaces for- Returns:
- the set of interfaces
- Since:
- 1.1
-
getAllInterfacesForClassAsSet
Get a set of interfaces implemented by a class and any superclasses or extended interfaces, optionally excluding based on a set of name prefix values.The iteration order of the returned set will be equal to the order returned by
Class.getInterfaces()method, in reverse class hierarchy order.- Parameters:
clazz- the class to get all implemented interfaces forexcluding- a set of class name prefix values to exclude from the results- Returns:
- the set of interfaces
- Since:
- 1.1
-