Package com.gengoai.reflection
Class RExecutable<T extends Executable,V extends RExecutable>
- java.lang.Object
-
- com.gengoai.reflection.RExecutable<T,V>
-
- Type Parameters:
T
- the Executable type parameterV
- this type parameter
- All Implemented Interfaces:
Serializable
- Direct Known Subclasses:
RConstructor
,RMethod
public abstract class RExecutable<T extends Executable,V extends RExecutable> extends Object
Base class for Executable objects (Methods and Constructors)- Author:
- David B. Bracewell
- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
RExecutable(Reflect owner)
Instantiates a new R executable.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description V
allowPrivilegedAccess()
Allow privileged access to the objectprotected Object[]
convertParameters(Object... args)
Converts the given set of arguments to values acceptable by this executable<A extends Annotation>
AgetAnnotation(@NonNull Class<A> annotationClass, boolean onlyDirect)
Gets the annotation of the given class on this object.<A extends Annotation>
AgetAnnotation(Class<A> annotationClass)
Gets the annotation of the given class present on this object.Annotation[]
getAnnotations()
Gets all annotations present on this object.Annotation[]
getAnnotations(boolean onlyDirect)
Gets all annotations on this object.<A extends Annotation>
A[]getAnnotations(@NonNull Class<A> annotationClass)
Gets all associated annotations of given type on this object.<A extends Annotation>
A[]getAnnotations(@NonNull Class<A> annotationClass, boolean onlyDirect)
Gets all annotations on this object.Reflect
getDeclaringClass()
Gets the class that declares this executable as aReflect
objectabstract T
getElement()
Gets the AnnotatedElementint
getModifiers()
Gets the modifiers on this field.String
getName()
Gets the name of the elementReflect
getOwner()
Gets theReflect
object from which this executable was created.RParameter
getParameter(int i)
Gets the ith parameter of the executableint
getParameterCount()
Gets the number of parameters on the executableList<RParameter>
getParameters()
Gets all parameters of this executable.abstract Type
getType()
Gets the type of the elementboolean
isAnnotationDeclared(@NonNull Class<? extends Annotation>... annotationClasses)
Determines if any of the given annotations are declared on this objectboolean
isAnnotationPresent(@NonNull Class<? extends Annotation>... annotationClasses)
Determines if any of the given annotations are present on this objectboolean
isPrivileged()
is privileged access allowed on this object?boolean
isVarArgs()
Checks if this executable has a varargs parameterboolean
parameterTypesCompatible(@NonNull Type... types)
Determines if the given types are compatible with the this executable's parameters<O> O
process(@NonNull CheckedFunction<T,O> function)
Applies the givenCheckedFunction
to the object.<A extends Annotation,O>
OprocessAnnotation(@NonNull Class<A> annotationClass, @NonNull CheckedFunction<? super A,? extends O> function)
Applies the givenCheckedBiFunction
to this object with the given annotation if it is present on the object.<A extends Annotation,O>
List<O>processAnnotations(@NonNull Class<A> annotationClass, @NonNull CheckedFunction<? super A,? extends O> function)
Applies the givenCheckedBiFunction
to this object and all instances of the given annotation present on the object.V
setIsPrivileged(boolean allowPrivilegedAccess)
Sets whether or not privileged access is allowed on this objectString
toString()
void
with(@NonNull CheckedConsumer<T> consumer)
Applies the givenCheckedConsumer
to the object.<A extends Annotation>
VwithAnnotation(@NonNull Class<A> annotationClass, @NonNull CheckedConsumer<? super A> consumer)
Applies the givenCheckedBiConsumer
to this object with the given annotation if it is present on the object.<A extends Annotation>
VwithAnnotations(@NonNull Class<A> annotationClass, @NonNull CheckedConsumer<? super A> consumer)
Applies the givenCheckedBiConsumer
to this object and all instances of the given annotation present on this object.
-
-
-
Constructor Detail
-
RExecutable
protected RExecutable(Reflect owner)
Instantiates a new R executable.- Parameters:
owner
- the owning Reflect
-
-
Method Detail
-
convertParameters
protected final Object[] convertParameters(Object... args) throws TypeConversionException
Converts the given set of arguments to values acceptable by this executable- Parameters:
args
- the arguments to convert- Returns:
- the converted arguments
- Throws:
TypeConversionException
- Something went wrong converting the arguments.
-
getDeclaringClass
public Reflect getDeclaringClass()
Gets the class that declares this executable as aReflect
object- Returns:
- the declaring class as a
Reflect
object
-
getModifiers
public int getModifiers()
Gets the modifiers on this field.- Returns:
- the modifiers
-
getName
public final String getName()
Gets the name of the element- Returns:
- the name of the element
-
getParameter
public RParameter getParameter(int i)
Gets the ith parameter of the executable- Parameters:
i
- the index of the parameter in the executable's parameter list- Returns:
- the parameter
-
getParameters
public List<RParameter> getParameters()
Gets all parameters of this executable.- Returns:
- the parameters
-
isVarArgs
public boolean isVarArgs()
Checks if this executable has a varargs parameter- Returns:
- True if the executable takes a vararg parameter, False othrwise
-
getParameterCount
public int getParameterCount()
Gets the number of parameters on the executable- Returns:
- the number of parameters on the executable
-
parameterTypesCompatible
public boolean parameterTypesCompatible(@NonNull @NonNull Type... types)
Determines if the given types are compatible with the this executable's parameters- Parameters:
types
- the types to check- Returns:
- True if the given types are compatible
-
allowPrivilegedAccess
public final V allowPrivilegedAccess()
Allow privileged access to the object- Returns:
- this Object
-
getOwner
public final Reflect getOwner()
Gets theReflect
object from which this executable was created.- Returns:
- the
Reflect
object from which this executable was created.
-
isPrivileged
public final boolean isPrivileged()
is privileged access allowed on this object?- Returns:
- True - privileged access is allowed, False - no privileged access is allowed
-
process
public final <O> O process(@NonNull @NonNull CheckedFunction<T,O> function) throws ReflectionException
Applies the givenCheckedFunction
to the object. This method takes care of accessibility concerns.- Type Parameters:
O
- the return type parameter- Parameters:
function
- the function to apply- Returns:
- the return value of the function
- Throws:
ReflectionException
- Something went wrong during reflection
-
setIsPrivileged
public final V setIsPrivileged(boolean allowPrivilegedAccess)
Sets whether or not privileged access is allowed on this object- Parameters:
allowPrivilegedAccess
- True - privileged access is allowed, False - no privileged access is allowed- Returns:
- this object
-
with
public final void with(@NonNull @NonNull CheckedConsumer<T> consumer) throws ReflectionException
Applies the givenCheckedConsumer
to the object. This method takes care of accessibility concerns.- Parameters:
consumer
- the consumer to apply- Throws:
ReflectionException
- Something went wrong during reflection
-
getAnnotation
public final <A extends Annotation> A getAnnotation(Class<A> annotationClass)
Gets the annotation of the given class present on this object.- Type Parameters:
A
- the annotation type parameter- Parameters:
annotationClass
- the annotation class- Returns:
- the annotation (null if does not exist)
-
getAnnotation
public final <A extends Annotation> A getAnnotation(@NonNull @NonNull Class<A> annotationClass, boolean onlyDirect)
Gets the annotation of the given class on this object. WhenonlyDirect
is true it will only return annotations directly present viagetDeclaredAnnotation
otherwise will return any instance present viagetAnnotation
.- Type Parameters:
A
- the annotation type parameter- Parameters:
annotationClass
- the annotation classonlyDirect
- True - only declared annotations, False present annotations- Returns:
- the annotation (null if does not exist)
-
getAnnotations
public final Annotation[] getAnnotations(boolean onlyDirect)
Gets all annotations on this object. WhenonlyDirect
is true it will only return annotations directly present viagetDeclaredAnnotations
otherwise will return any instance present viagetAnnotations
.- Parameters:
onlyDirect
- True - only declared annotations, False present annotations- Returns:
- the array of Annotation
-
getAnnotations
public final Annotation[] getAnnotations()
Gets all annotations present on this object.- Returns:
- the array of Annotation
-
getAnnotations
public final <A extends Annotation> A[] getAnnotations(@NonNull @NonNull Class<A> annotationClass, boolean onlyDirect)
Gets all annotations on this object. WhenonlyDirect
is true it will only return annotations directly present viagetDeclaredAnnotationsByType
otherwise will return any instance present viagetAnnotationsByType
.- Type Parameters:
A
- the annotation type parameter- Parameters:
annotationClass
- the annotation classonlyDirect
- True - only declared annotations, False present annotations- Returns:
- the array of Annotation
-
getAnnotations
public final <A extends Annotation> A[] getAnnotations(@NonNull @NonNull Class<A> annotationClass)
Gets all associated annotations of given type on this object.- Type Parameters:
A
- the annotation type parameter- Parameters:
annotationClass
- the annotation class- Returns:
- the array of Annotation
-
getElement
public abstract T getElement()
Gets the AnnotatedElement- Returns:
- the AnnotatedElement object
-
getType
public abstract Type getType()
Gets the type of the element- Returns:
- the type of the element
-
isAnnotationDeclared
@SafeVarargs public final boolean isAnnotationDeclared(@NonNull @NonNull Class<? extends Annotation>... annotationClasses)
Determines if any of the given annotations are declared on this object- Parameters:
annotationClasses
- the annotation classes- Returns:
- True - if any of the given annotations are declared on this object
-
isAnnotationPresent
@SafeVarargs public final boolean isAnnotationPresent(@NonNull @NonNull Class<? extends Annotation>... annotationClasses)
Determines if any of the given annotations are present on this object- Parameters:
annotationClasses
- the annotation classes- Returns:
- True - if any of the given annotations are present on this object
-
processAnnotation
public final <A extends Annotation,O> O processAnnotation(@NonNull @NonNull Class<A> annotationClass, @NonNull @NonNull CheckedFunction<? super A,? extends O> function) throws ReflectionException
Applies the givenCheckedBiFunction
to this object with the given annotation if it is present on the object.- Type Parameters:
A
- the annotation type parameterO
- the return type parameter- Parameters:
annotationClass
- the annotation classfunction
- the function to apply- Returns:
- the return value of the function or null if no annotation was present
- Throws:
ReflectionException
- Something went wrong during reflection
-
processAnnotations
public final <A extends Annotation,O> List<O> processAnnotations(@NonNull @NonNull Class<A> annotationClass, @NonNull @NonNull CheckedFunction<? super A,? extends O> function) throws ReflectionException
Applies the givenCheckedBiFunction
to this object and all instances of the given annotation present on the object.- Type Parameters:
A
- the annotation type parameterO
- the return type parameter- Parameters:
annotationClass
- the annotation classfunction
- the function to apply- Returns:
- A list of the return values of the function or null if no annotation was present
- Throws:
ReflectionException
- Something went wrong during reflection
-
withAnnotation
public final <A extends Annotation> V withAnnotation(@NonNull @NonNull Class<A> annotationClass, @NonNull @NonNull CheckedConsumer<? super A> consumer) throws ReflectionException
Applies the givenCheckedBiConsumer
to this object with the given annotation if it is present on the object.- Type Parameters:
A
- the annotation type parameter- Parameters:
annotationClass
- the annotation classconsumer
- the consumer to apply- Returns:
- This object
- Throws:
ReflectionException
- Something went wrong during reflection
-
withAnnotations
public final <A extends Annotation> V withAnnotations(@NonNull @NonNull Class<A> annotationClass, @NonNull @NonNull CheckedConsumer<? super A> consumer) throws ReflectionException
Applies the givenCheckedBiConsumer
to this object and all instances of the given annotation present on this object.- Type Parameters:
A
- the annotation type parameter- Parameters:
annotationClass
- the annotation classconsumer
- the consumer to apply- Returns:
- This object
- Throws:
ReflectionException
- Something went wrong during reflection
-
-