Class ReflectionUtils


  • public class ReflectionUtils
    extends java.lang.Object
    Utilities for reflection code.
    • Constructor Summary

      Constructors 
      Constructor Description
      ReflectionUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <T> T evaluate​(java.lang.String statement)
      Tries to evaluate the given statement, thereby supporting the following statement types: new <class name>() <class name>.<constant> <class name>.<static method>()
      static <T> T executeStaticArglessMethod​(java.lang.String className, java.lang.String methodName)
      Executes a parameterless static method.
      static <T> java.lang.Class<T> generalize​(java.lang.Class<? extends T> baseClass)
      Casts the given Class to a more general one.
      static java.lang.String getDeclaringJar​(java.lang.Class<?> cls)
      Identifies and returns the JAR file declaring the given Class if no such file could be determined.
      static java.lang.String getDeclaringJar​(java.lang.Object object)
      Identifies and returns the JAR file declaring the Class of the given object or null if no such file could be determined.
      static <T> T getProperty​(java.lang.Object obj, java.lang.String property)
      Retrieve a property from an object.
      static java.net.URL getResourceURL​(java.lang.String resourceName)
      Provides a resource as URL.
      static java.util.Map<java.lang.String,​java.lang.reflect.Type> getTypeParameters​(java.lang.Class<?> subclass, java.lang.Class<?> superclass)
      Retrieve the Types of type parameters from an extended/implemented superclass/interface.
      static java.lang.reflect.Type getWrapperClass​(java.lang.reflect.Type type, int index)  
      static <T> T instantiateDefault​(java.lang.Class<? extends T> cls)
      Creates a new instance of the given Class via the default constructor.
      static <T> T instantiateDefault​(java.lang.String className)
      Creates a new instance of a Class via the default constructor.
      static <T> T instantiateSomehow​(java.lang.Class<T> cls, java.util.List<Tuple<java.lang.Class<?>,​java.util.function.Supplier<?>>> defaultParameters)
      Tries to instantiate an arbitrary instance of the given Class.
      static <T> T instantiateSomehow​(java.lang.Class<T> cls, Tuple<java.lang.Class<?>,​java.util.function.Supplier<?>>... defaultParameters)
      Tries to instantiate an arbitrary instance of the given Class.
      static java.io.InputStream loadResource​(java.lang.String resourceName)
      Provides a resource as an InputStream.
      static <T> T retrieveStaticVariable​(java.lang.String className, java.lang.String variableName)
      Retrieves a static variable.
      static <T> java.lang.Class<T> specify​(java.lang.Class<? super T> baseClass)
      Casts the given Class to a more specific one.
      static double toDouble​(java.lang.Object o)
      Convert the given Object to a double.
      • Methods inherited from class java.lang.Object

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

      • ReflectionUtils

        public ReflectionUtils()
    • Method Detail

      • getDeclaringJar

        public static java.lang.String getDeclaringJar​(java.lang.Object object)
        Identifies and returns the JAR file declaring the Class of the given object or null if no such file could be determined.
      • getDeclaringJar

        public static java.lang.String getDeclaringJar​(java.lang.Class<?> cls)
        Identifies and returns the JAR file declaring the given Class if no such file could be determined.
      • loadResource

        public static java.io.InputStream loadResource​(java.lang.String resourceName)
        Provides a resource as an InputStream.
        Parameters:
        resourceName - the name or path of the resource
        Returns:
        an InputStream with the contents of the resource or null if the resource could not be found
      • getResourceURL

        public static java.net.URL getResourceURL​(java.lang.String resourceName)
        Provides a resource as URL.
        Parameters:
        resourceName - the name or path of the resource
        Returns:
        a URL describing the path to the resource or null if the resource could not be found
      • specify

        public static <T> java.lang.Class<T> specify​(java.lang.Class<? super T> baseClass)
        Casts the given Class to a more specific one.
        Type Parameters:
        T - the specific type parameter for the baseClass
        Parameters:
        baseClass - that should be casted
        Returns:
        the baseClass, casted
      • generalize

        public static <T> java.lang.Class<T> generalize​(java.lang.Class<? extends T> baseClass)
        Casts the given Class to a more general one.
        Type Parameters:
        T - the specific type parameter for the baseClass
        Parameters:
        baseClass - that should be casted
        Returns:
        the baseClass, casted
      • evaluate

        public static <T> T evaluate​(java.lang.String statement)
                              throws java.lang.IllegalArgumentException
        Tries to evaluate the given statement, thereby supporting the following statement types:
        • new <class name>()
        • <class name>.<constant>
        • <class name>.<static method>()
        Type Parameters:
        T - the return type
        Parameters:
        statement - the statement
        Returns:
        the result of the evaluated statement
        Throws:
        java.lang.IllegalArgumentException
      • executeStaticArglessMethod

        public static <T> T executeStaticArglessMethod​(java.lang.String className,
                                                       java.lang.String methodName)
        Executes a parameterless static method.
        Type Parameters:
        T -
        Parameters:
        className - the name of the Class that comprises the method
        methodName - the name of the method
        Returns:
        the return value of the executed method
      • retrieveStaticVariable

        public static <T> T retrieveStaticVariable​(java.lang.String className,
                                                   java.lang.String variableName)
        Retrieves a static variable.
        Type Parameters:
        T -
        Parameters:
        className - the name of the Class that comprises the method
        variableName - the name of the method
        Returns:
        the return value of the executed method
      • instantiateDefault

        public static <T> T instantiateDefault​(java.lang.String className)
        Creates a new instance of a Class via the default constructor.
        Parameters:
        className - name of the Class to be instantiated
        Returns:
        the instance
      • instantiateDefault

        public static <T> T instantiateDefault​(java.lang.Class<? extends T> cls)
        Creates a new instance of the given Class via the default constructor.
        Parameters:
        cls - the Class to be instantiated
        Returns:
        the instance
      • instantiateSomehow

        public static <T> T instantiateSomehow​(java.lang.Class<T> cls,
                                               Tuple<java.lang.Class<?>,​java.util.function.Supplier<?>>... defaultParameters)
        Tries to instantiate an arbitrary instance of the given Class.
        Parameters:
        cls - that should be instantiated
        defaultParameters - designate specific default parameter values for parameter Classes
        Returns:
        the instance
      • instantiateSomehow

        public static <T> T instantiateSomehow​(java.lang.Class<T> cls,
                                               java.util.List<Tuple<java.lang.Class<?>,​java.util.function.Supplier<?>>> defaultParameters)
        Tries to instantiate an arbitrary instance of the given Class.
        Parameters:
        cls - that should be instantiated
        defaultParameters - designate specific default parameter values for parameter Classes
        Returns:
        the instance
      • getTypeParameters

        public static java.util.Map<java.lang.String,​java.lang.reflect.Type> getTypeParameters​(java.lang.Class<?> subclass,
                                                                                                     java.lang.Class<?> superclass)
        Retrieve the Types of type parameters from an extended/implemented superclass/interface.
        Parameters:
        subclass - the Class implementing the superclass/interface with type parameters
        superclass - the superclass/interface defining the type parameters
        Returns:
        a Map mapping the type parameter names to their implemented Type
      • getProperty

        public static <T> T getProperty​(java.lang.Object obj,
                                        java.lang.String property)
        Retrieve a property from an object.
        Type Parameters:
        T - the return type
        Parameters:
        obj - from which the property should be retrieved
        property - the property
        Returns:
        the property
      • toDouble

        public static double toDouble​(java.lang.Object o)
        Convert the given Object to a double.
        Parameters:
        o - the Object
        Returns:
        the double
      • getWrapperClass

        public static java.lang.reflect.Type getWrapperClass​(java.lang.reflect.Type type,
                                                             int index)