Package io.cui.tools.reflect
Class MoreReflection
- java.lang.Object
-
- io.cui.tools.reflect.MoreReflection
-
public final class MoreReflection extends Object
Provides a number of methods simplifying the usage of Reflection-based access.Caution
Use reflection only if there is no other way. Even if some of the problems are minimized by using this type. It should be used either in test-code, what is we actually do, and not production code. An other reason could be framework code. as for that you should exactly know what you do.
- Author:
- Oliver Wolff
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static Optional<Field>accessField(Class<?> type, String fieldName)Tries to access a field on a given type.static booleancheckWhetherParameterIsAssignable(Class<?> assignableSource, Class<?> queryType)static StringcomputePropertyNameFromMethodName(String methodName)Helper method that extract the property-name from a given accessor-method name.static <A extends Annotation>
List<A>extractAllAnnotations(Class<?> annotatedType, Class<A> annotation)Helper class for extracting all annotations of a given class including from their ancestors.static <A extends Annotation>
Optional<A>extractAnnotation(Class<?> annotatedType, Class<A> annotation)Helper class for extracting an annotation of a given class including from their ancestors.static <T> Class<T>extractFirstGenericTypeArgument(Class<?> typeToBeExtractedFrom)Extracts the first generic type argument for the given type.static Optional<Class<?>>extractGenericTypeCovariantly(Type type)static Optional<ParameterizedType>extractParameterizedType(Class<?> typeToBeExtractedFrom)Extracts aParameterizedTypeview for the given typestatic Optional<String>findCaller(Collection<String> markerClasses)Try to detect class from call stack which was the previous, before the marker class namestatic Optional<StackTraceElement>findCallerElement(Throwable throwable, Collection<String> markerClasses)Tries to detect class from call stack which was the previous, before the marker class namestatic <T> TnewProxy(Class<T> interfaceType, InvocationHandler handler)Returns a proxy instance that implementsinterfaceTypeby dispatching method invocations tohandler.static Optional<Method>retrieveAccessMethod(Class<?> clazz, String propertyName)Retrieves the access-method for a given property Name.static List<Method>retrieveAccessMethods(Class<?> clazz)Determines the access methods of a given class.static List<Method>retrieveAccessMethods(Class<?> clazz, Collection<String> ignoreProperties)Determines the access methods of a given class.static List<Method>retrievePublicObjectMethods(Class<?> clazz)Determines the public not static methods of a givenClass.static Optional<Method>retrieveWriteMethod(Class<?> clazz, String propertyName, Class<?> parameterType)Determines the modifier methods of a given class.static Collection<Method>retrieveWriteMethodCandidates(Class<?> clazz, String propertyName)Determines the modifier methods of a given class for a property.
-
-
-
Method Detail
-
accessField
public static Optional<Field> accessField(Class<?> type, String fieldName)
Tries to access a field on a given type. If none can be found it recursively calls itself with the corresponding parent untilObject. Caution:The field elements are shared between requests (cached), therefore you must ensure that changes to the instance, like
Field.setAccessible(boolean)are reseted by the client. This can be simplified by usingFieldWrapper
-
retrievePublicObjectMethods
public static List<Method> retrievePublicObjectMethods(Class<?> clazz)
Determines the public not static methods of a givenClass.Object.getClass()will implicitly ignore- Parameters:
clazz- to be checked- Returns:
- the found public-methods.
-
retrieveAccessMethods
public static List<Method> retrieveAccessMethods(Class<?> clazz)
Determines the access methods of a given class. An access method is defined as being a public not static zero-argument method that is prefixed with either "get" or "is". The Method "getClass" is explicitly filtered- Parameters:
clazz- to be checked- Returns:
- the found access-methods.
-
retrieveAccessMethods
public static List<Method> retrieveAccessMethods(Class<?> clazz, Collection<String> ignoreProperties)
Determines the access methods of a given class. An access method is defined as being a public not static zero-argument method that is prefixed with either "get" or "is". The Method "getClass" is explicitly filtered- Parameters:
clazz- to be checkedignoreProperties- identifies the property by name that must be filtered from the result- Returns:
- the found access-methods.
-
retrieveWriteMethod
public static Optional<Method> retrieveWriteMethod(Class<?> clazz, String propertyName, Class<?> parameterType)
Determines the modifier methods of a given class. A modifier method is defined as being a public not static single-argument method that is prefixed with either "set" or consists of the propertyName only.- Parameters:
clazz- to be checkedpropertyName- to be checked, must not be nullparameterType- identifying the parameter to be passed to the given method, must not be null- Returns:
- the found modifier-method or
Optional.empty()if none could be found
-
checkWhetherParameterIsAssignable
public static boolean checkWhetherParameterIsAssignable(Class<?> assignableSource, Class<?> queryType)
- Parameters:
assignableSource- the type to be checkedqueryType- to be checked for- Returns:
- boolean indicating whether the given parameter, identified by their class attributes match
-
retrieveWriteMethodCandidates
public static Collection<Method> retrieveWriteMethodCandidates(Class<?> clazz, String propertyName)
Determines the modifier methods of a given class for a property. A modifier method is defined as being a public not static single-argument method that is prefixed with either "set" or consists of the ropertyName only. This will implicitly return all possible setter or builder methods, e.g.setPropertyName(String name),propertyName(String name)andsetPropertyName(Collection<String> name)will all be part of the result.- Parameters:
clazz- to be checkedpropertyName- to be checked, must not be null- Returns:
- the found modifier-methods or an empty
Collectionif none could be found
-
retrieveAccessMethod
public static Optional<Method> retrieveAccessMethod(Class<?> clazz, String propertyName)
Retrieves the access-method for a given property Name. SeeretrieveAccessMethods(Class)for the definition of an access-method- Parameters:
clazz- must not be nullpropertyName- to be accessed- Returns:
Optional.empty()in case no method could be found, anOptionalwith the found method otherwise.
-
computePropertyNameFromMethodName
public static String computePropertyNameFromMethodName(String methodName)
Helper method that extract the property-name from a given accessor-method name.- Parameters:
methodName- must not be null nor empty- Returns:
- the possible attribute name of a given method-name, e.g. it return 'name' for getName/setName/isName. If none of the prefixes 'get', 'set', 'is' is found it returns the passed String.
-
extractAllAnnotations
public static <A extends Annotation> List<A> extractAllAnnotations(Class<?> annotatedType, Class<A> annotation)
Helper class for extracting all annotations of a given class including from their ancestors.- Type Parameters:
A- the concrete annotation type- Parameters:
annotatedType- the (possibly) annotated type. If it is null orObject.getClass()it will return an empty listannotation- the annotation to be extracted, must not be null- Returns:
- an immutable List with all annotations found at the given object or one of its ancestors. May be empty but never null
-
extractAnnotation
public static <A extends Annotation> Optional<A> extractAnnotation(Class<?> annotatedType, Class<A> annotation)
Helper class for extracting an annotation of a given class including from their ancestors.- Type Parameters:
A- the concrete annotation type- Parameters:
annotatedType- the (possibly) annotated type. If it is null orObject.getClass()Optional.empty()annotation- the annotation to be extracted, must not be null- Returns:
- an
Optionalon the annotated Object if the annotation can be found. In case the annotation is found multiple times the first element will be returned.
-
extractFirstGenericTypeArgument
public static <T> Class<T> extractFirstGenericTypeArgument(Class<?> typeToBeExtractedFrom)
Extracts the first generic type argument for the given type.- Type Parameters:
T- identifying the type to be looked for- Parameters:
typeToBeExtractedFrom- must not be null- Returns:
- an
Optionalof the KeyStoreType-Argument of the given class. - Throws:
IllegalArgumentException- in case the given type does not represent a generic.
-
extractGenericTypeCovariantly
public static Optional<Class<?>> extractGenericTypeCovariantly(Type type)
- Parameters:
type- to be extracted from- Returns:
- if applicable the actual type argument for the given type. If the type represents
already a
Classit will be returned directly. Otherwise, the super-type will be checked by calling the superclass
-
extractParameterizedType
public static Optional<ParameterizedType> extractParameterizedType(Class<?> typeToBeExtractedFrom)
Extracts aParameterizedTypeview for the given type- Parameters:
typeToBeExtractedFrom- must not be null- Returns:
- an
Optionalof theParameterizedTypeview of the given class.
-
newProxy
public static <T> T newProxy(Class<T> interfaceType, InvocationHandler handler)
Returns a proxy instance that implementsinterfaceTypeby dispatching method invocations tohandler. The class loader ofinterfaceTypewill be used to define the proxy class. To implement multiple interfaces or specify a class loader, use Proxy#newProxyInstance(Class, Constructor, InvocationHandler).- Type Parameters:
T- the target type of the proxy- Parameters:
interfaceType- must not be nullhandler- the invocation handler- Returns:
- the created Proxy-instance
- Throws:
IllegalArgumentException- ifinterfaceTypedoes not specify the type of Java interface
-
findCaller
public static Optional<String> findCaller(Collection<String> markerClasses)
Try to detect class from call stack which was the previous, before the marker class name- Parameters:
markerClasses- class names which could be used as marker before the real caller name. Collection must not benull.- Returns:
- option of detected caller class name
-
findCallerElement
public static Optional<StackTraceElement> findCallerElement(Throwable throwable, Collection<String> markerClasses)
Tries to detect class from call stack which was the previous, before the marker class name- Parameters:
throwable- is an optional parameter, will be used to access the stackmarkerClasses- class names which could be used as marker before the real caller name. Collection must not benull.- Returns:
- option of detected caller class name
-
-