Package org.apache.wayang.core.util
Class ReflectionUtils
java.lang.Object
org.apache.wayang.core.util.ReflectionUtils
Utilities for reflection code.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> T
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
(String className, String methodName) Executes a parameterless static method.static <T> Class<T>
generalize
(Class<? extends T> baseClass) Casts the givenClass
to a more general one.static String
getDeclaringJar
(Class<?> cls) Identifies and returns the JAR file declaring the givenClass
if no such file could be determined.static String
getDeclaringJar
(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
(Object obj, String property) Retrieve a property from an object.static URL
getResourceURL
(String resourceName) Provides a resource asURL
.getTypeParameters
(Class<?> subclass, Class<?> superclass) Retrieve theType
s of type parameters from an extended/implemented superclass/interface.static Type
getWrapperClass
(Type type, int index) static <T> T
instantiateDefault
(Class<? extends T> cls) Creates a new instance of the givenClass
via the default constructor.static <T> T
instantiateDefault
(String className) Creates a new instance of aClass
via the default constructor.static <T> T
Tries to instantiate an arbitrary instance of the givenClass
.static <T> T
instantiateSomehow
(Class<T> cls, Tuple<Class<?>, Supplier<?>>... defaultParameters) Tries to instantiate an arbitrary instance of the givenClass
.static InputStream
loadResource
(String resourceName) Provides a resource as anInputStream
.static <T> T
retrieveStaticVariable
(String className, String variableName) Retrieves a static variable.static <T> Class<T>
Casts the givenClass
to a more specific one.static double
Convert the givenObject
to adouble
.
-
Constructor Details
-
ReflectionUtils
public ReflectionUtils()
-
-
Method Details
-
getDeclaringJar
Identifies and returns the JAR file declaring theClass
of the givenobject
ornull
if no such file could be determined. -
getDeclaringJar
Identifies and returns the JAR file declaring the givenClass
if no such file could be determined. -
loadResource
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
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
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
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
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
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
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
Creates a new instance of aClass
via the default constructor.- Parameters:
className
- name of theClass
to be instantiated- Returns:
- the instance
-
instantiateDefault
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(Class<T> cls, Tuple<Class<?>, 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(Class<T> cls, List<Tuple<Class<?>, 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
Retrieve theType
s of type parameters from an extended/implemented superclass/interface. -
getProperty
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
Convert the givenObject
to adouble
.- Parameters:
o
- theObject
- Returns:
- the
double
-
getWrapperClass
-