Class ReflectionUtils


  • public final class ReflectionUtils
    extends java.lang.Object
    General utility methods for working with reflection.
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      private static class  ReflectionUtils.NegativeCacheSentinel
      A unique class which is used as a sentinel value in the caching for getClassByName.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static java.util.Map<java.lang.ClassLoader,​java.util.Map<java.lang.String,​java.lang.ref.WeakReference<java.lang.Class<?>>>> CACHE_CLASSES  
      private static java.lang.ClassLoader CLASSLOADER  
      private static java.lang.Class<?> NEGATIVE_CACHE_SENTINEL
      Sentinel value to store negative cache results in CACHE_CLASSES.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.Class<?> getClassByName​(java.lang.String name)
      Loads a class by name.
      private static java.lang.Class<?> getClassByNameOrNull​(java.lang.String name)
      Loads a class by name, returning null rather than throwing an exception if it couldn't be loaded.
      static <T> T newInstance​(java.lang.Class<T> klass, java.lang.Object... args)
      Uses the constructor represented by this Constructor object to create and initialize a new instance of the constructor's declaring class, with the specified initialization parameters.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • CACHE_CLASSES

        private static final java.util.Map<java.lang.ClassLoader,​java.util.Map<java.lang.String,​java.lang.ref.WeakReference<java.lang.Class<?>>>> CACHE_CLASSES
      • CLASSLOADER

        private static final java.lang.ClassLoader CLASSLOADER
      • NEGATIVE_CACHE_SENTINEL

        private static final java.lang.Class<?> NEGATIVE_CACHE_SENTINEL
        Sentinel value to store negative cache results in CACHE_CLASSES.
    • Constructor Detail

      • ReflectionUtils

        private ReflectionUtils()
        The private constructor of ReflectionUtils.
    • Method Detail

      • getClassByName

        public static java.lang.Class<?> getClassByName​(java.lang.String name)
                                                 throws java.lang.ClassNotFoundException
        Loads a class by name.
        Parameters:
        name - the class name.
        Returns:
        the class object.
        Throws:
        java.lang.ClassNotFoundException - if the class is not found.
      • getClassByNameOrNull

        private static java.lang.Class<?> getClassByNameOrNull​(java.lang.String name)
        Loads a class by name, returning null rather than throwing an exception if it couldn't be loaded. This is to avoid the overhead of creating an exception.
        Parameters:
        name - the class name.
        Returns:
        the class object, or null if it could not be found.
      • newInstance

        public static <T> T newInstance​(java.lang.Class<T> klass,
                                        java.lang.Object... args)
        Uses the constructor represented by this Constructor object to create and initialize a new instance of the constructor's declaring class, with the specified initialization parameters.
        Type Parameters:
        T - type for the new instance
        Parameters:
        klass - the Class object.
        args - array of objects to be passed as arguments to the constructor call.
        Returns:
        a new object created by calling the constructor this object represents.