Package org.apache.wayang.core.util
Class ReflectionUtils
- java.lang.Object
-
- org.apache.wayang.core.util.ReflectionUtils
-
public class ReflectionUtils extends java.lang.ObjectUtilities for reflection code.
-
-
Constructor Summary
Constructors Constructor Description ReflectionUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T> Tevaluate(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> TexecuteStaticArglessMethod(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 givenClassto a more general one.static java.lang.StringgetDeclaringJar(java.lang.Class<?> cls)Identifies and returns the JAR file declaring the givenClassif no such file could be determined.static java.lang.StringgetDeclaringJar(java.lang.Object object)Identifies and returns the JAR file declaring theClassof the givenobjectornullif no such file could be determined.static <T> TgetProperty(java.lang.Object obj, java.lang.String property)Retrieve a property from an object.static java.net.URLgetResourceURL(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 theTypes of type parameters from an extended/implemented superclass/interface.static java.lang.reflect.TypegetWrapperClass(java.lang.reflect.Type type, int index)static <T> TinstantiateDefault(java.lang.Class<? extends T> cls)Creates a new instance of the givenClassvia the default constructor.static <T> TinstantiateDefault(java.lang.String className)Creates a new instance of aClassvia the default constructor.static <T> TinstantiateSomehow(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> TinstantiateSomehow(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.InputStreamloadResource(java.lang.String resourceName)Provides a resource as anInputStream.static <T> TretrieveStaticVariable(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 givenClassto a more specific one.static doubletoDouble(java.lang.Object o)Convert the givenObjectto adouble.
-
-
-
Method Detail
-
getDeclaringJar
public static java.lang.String getDeclaringJar(java.lang.Object object)
Identifies and returns the JAR file declaring theClassof the givenobjectornullif 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 givenClassif 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
InputStreamwith the contents of the resource ornullif 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
URLdescribing the path to the resource ornullif the resource could not be found
-
specify
public static <T> java.lang.Class<T> specify(java.lang.Class<? super T> baseClass)
Casts the givenClassto 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 givenClassto 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.IllegalArgumentExceptionTries 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 theClassthat 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 theClassthat 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 aClassvia the default constructor.- Parameters:
className- name of theClassto be instantiated- Returns:
- the instance
-
instantiateDefault
public static <T> T instantiateDefault(java.lang.Class<? extends T> cls)
Creates a new instance of the givenClassvia the default constructor.- Parameters:
cls- theClassto 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 parameterClasses- 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 parameterClasses- 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 theTypes of type parameters from an extended/implemented superclass/interface.- Parameters:
subclass- theClassimplementing the superclass/interface with type parameterssuperclass- the superclass/interface defining the type parameters- Returns:
- a
Mapmapping 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 givenObjectto adouble.- Parameters:
o- theObject- Returns:
- the
double
-
getWrapperClass
public static java.lang.reflect.Type getWrapperClass(java.lang.reflect.Type type, int index)
-
-