Package org.apache.wayang.core.util
Class ReflectionUtils
- java.lang.Object
-
- org.apache.wayang.core.util.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 givenClass
to a more general one.static java.lang.String
getDeclaringJar(java.lang.Class<?> cls)
Identifies and returns the JAR file declaring the givenClass
if no such file could be determined.static java.lang.String
getDeclaringJar(java.lang.Object object)
Identifies and returns the JAR file declaring theClass
of the givenobject
ornull
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 asURL
.static java.util.Map<java.lang.String,java.lang.reflect.Type>
getTypeParameters(java.lang.Class<?> subclass, java.lang.Class<?> superclass)
Retrieve theType
s 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 givenClass
via the default constructor.static <T> T
instantiateDefault(java.lang.String className)
Creates a new instance of aClass
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 givenClass
.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 givenClass
.static java.io.InputStream
loadResource(java.lang.String resourceName)
Provides a resource as anInputStream
.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 givenClass
to a more specific one.static double
toDouble(java.lang.Object o)
Convert the givenObject
to adouble
.
-
-
-
Method Detail
-
getDeclaringJar
public static java.lang.String getDeclaringJar(java.lang.Object object)
Identifies and returns the JAR file declaring theClass
of the givenobject
ornull
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 givenClass
if no such file could be determined.
-
loadResource
public static java.io.InputStream loadResource(java.lang.String resourceName)
Provides a resource as anInputStream
.- Parameters:
resourceName
- the name or path of the resource- Returns:
- an
InputStream
with the contents of the resource ornull
if the resource could not be found
-
getResourceURL
public static java.net.URL getResourceURL(java.lang.String resourceName)
Provides a resource asURL
.- Parameters:
resourceName
- the name or path of the resource- Returns:
- a
URL
describing the path to the resource ornull
if the resource could not be found
-
specify
public static <T> java.lang.Class<T> specify(java.lang.Class<? super T> baseClass)
Casts the givenClass
to a more specific one.- Type Parameters:
T
- the specific type parameter for thebaseClass
- 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 givenClass
to a more general one.- Type Parameters:
T
- the specific type parameter for thebaseClass
- 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 theClass
that comprises the methodmethodName
- 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 theClass
that comprises the methodvariableName
- 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 aClass
via the default constructor.- Parameters:
className
- name of theClass
to be instantiated- Returns:
- the instance
-
instantiateDefault
public static <T> T instantiateDefault(java.lang.Class<? extends T> cls)
Creates a new instance of the givenClass
via the default constructor.- Parameters:
cls
- theClass
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 givenClass
.- Parameters:
cls
- that should be instantiateddefaultParameters
- designate specific default parameter values for parameterClass
es- 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 givenClass
.- Parameters:
cls
- that should be instantiateddefaultParameters
- designate specific default parameter values for parameterClass
es- 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 theType
s of type parameters from an extended/implemented superclass/interface.- Parameters:
subclass
- theClass
implementing the superclass/interface with type parameterssuperclass
- the superclass/interface defining the type parameters- Returns:
- a
Map
mapping the type parameter names to their implementedType
-
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 retrievedproperty
- the property- Returns:
- the property
-
toDouble
public static double toDouble(java.lang.Object o)
Convert the givenObject
to adouble
.- Parameters:
o
- theObject
- Returns:
- the
double
-
getWrapperClass
public static java.lang.reflect.Type getWrapperClass(java.lang.reflect.Type type, int index)
-
-