Class ClassUtils

java.lang.Object
net.solarnetwork.util.ClassUtils

public final class ClassUtils extends Object
Utility methods for dealing with classes at runtime.
Version:
2.0
Author:
matt
  • Field Details

    • JAVA_PACKAGE_PREFIXES

      public static final Set<String> 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

      public static <T> T instantiateClass(String className, Class<T> type)
      Instantiate a class of a specific interface type.
      Type Parameters:
      T - the desired interface type
      Parameters:
      className - the class name that implements the interface
      type - the desired interface
      Returns:
      new instance of the desired type
    • loadClass

      public static <T> Class<? extends T> loadClass(String className, Class<T> type)
      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 interface
      type - the desired interface
      Returns:
      the class
    • setBeanProperties

      public static void setBeanProperties(Object o, Map<String,?> values)
      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
    • setBeanProperties

      public static void setBeanProperties(Object o, Map<String,?> values, boolean ignoreErrors)
      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

      public static Map<String,Object> getBeanProperties(Object o, Set<String> ignore)
      Get a Map of non-null bean properties for an object.
      Parameters:
      o - the object to inspect
      ignore - a set of property names to ignore (optional)
      Returns:
      Map (never null)
    • getSimpleBeanProperties

      public static Map<String,Object> getSimpleBeanProperties(Object o, Set<String> ignore)
      Get a Map of non-null simple bean properties for an object.
      Parameters:
      o - the object to inspect
      ignore - a set of property names to ignore (optional)
      Returns:
      Map (never null)
      Since:
      1.1
    • copyBeanProperties

      public static void copyBeanProperties(Object src, Object dest, Set<String> ignore)
      Copy non-null bean properties from one object to another.
      Parameters:
      src - the object to copy values from
      dest - the object to copy values to
      ignore - 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 from
      dest - the object to copy values to
      ignore - 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 inspect
      ignore - a set of property names to ignore (optional)
      serializeIgnore - if true test for the SerializeIgnore annotation for ignoring properties
      Returns:
      Map (never null)
    • getResourceAsString

      public static String getResourceAsString(String resourceName, Class<?> clazz)
      Load a textual classpath resource into a String.
      Parameters:
      resourceName - the resource to load
      clazz - the Class to load the resource from
      Returns:
      the String
    • getResourceAsString

      public static String getResourceAsString(String resourceName, Class<?> clazz, Pattern skip)
      Load a textual classpath resource into a String.
      Parameters:
      resourceName - the resource to load
      clazz - the Class to load the resource from
      skip - 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

      public static Set<Class<?>> getAllNonJavaInterfacesForClassAsSet(Class<?> clazz)
      Get all interfaces implemented by a class, excluding those in the JAVA_PACKAGE_PREFIXES package set.
      Parameters:
      clazz - the class to get all implemented interfaces for
      Returns:
      the set of interfaces
      Since:
      1.1
    • getAllInterfacesForClassAsSet

      public static Set<Class<?>> 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.

      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 for
      excluding - a set of class name prefix values to exclude from the results
      Returns:
      the set of interfaces
      Since:
      1.1