Class ReflectionUtils

java.lang.Object
org.apache.wayang.core.util.ReflectionUtils

public class ReflectionUtils extends Object
Utilities for reflection code.
  • Constructor Details

    • ReflectionUtils

      public ReflectionUtils()
  • Method Details

    • getDeclaringJar

      public static String getDeclaringJar(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 String getDeclaringJar(Class<?> cls)
      Identifies and returns the JAR file declaring the given Class if no such file could be determined.
    • loadResource

      public static InputStream loadResource(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 URL getResourceURL(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> Class<T> specify(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> Class<T> generalize(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(String statement) throws 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:
      IllegalArgumentException
    • executeStaticArglessMethod

      public static <T> T executeStaticArglessMethod(String className, 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(String className, 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(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(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(Class<T> cls, Tuple<Class<?>,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(Class<T> cls, List<Tuple<Class<?>,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 Map<String,Type> getTypeParameters(Class<?> subclass, 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(Object obj, 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(Object o)
      Convert the given Object to a double.
      Parameters:
      o - the Object
      Returns:
      the double
    • getWrapperClass

      public static Type getWrapperClass(Type type, int index)