Class MoreReflection

java.lang.Object
de.cuioss.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
  • Constructor Details

  • Method Details

    • 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 until Object. 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 using FieldWrapper

      Parameters:
      type - to be checked, must not be null
      fieldName - to be checked, must not be null
      Returns:
      an Optional Field if it can be found
    • retrievePublicObjectMethods

      public static List<Method> retrievePublicObjectMethods(Class<?> clazz)
      Determines the public not static methods of a given Class. 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 checked
      ignoreProperties - 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 checked
      propertyName - to be checked, must not be null
      parameterType - 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 checked
      queryType - 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) and setPropertyName(Collection<String> name) will all be part of the result.
      Parameters:
      clazz - to be checked
      propertyName - to be checked, must not be null
      Returns:
      the found modifier-methods or an empty Collection if none could be found
    • retrieveAccessMethod

      public static Optional<Method> retrieveAccessMethod(Class<?> clazz, String propertyName)
      Retrieves the access-method for a given property Name. See retrieveAccessMethods(Class) for the definition of an access-method
      Parameters:
      clazz - must not be null
      propertyName - to be accessed
      Returns:
      Optional.empty() in case no method could be found, an Optional with 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 or Object.getClass() it will return an empty list
      annotation - 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 or Object.getClass() Optional.empty()
      annotation - the annotation to be extracted, must not be null
      Returns:
      an Optional on 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 Optional of the KeyStoreType-Argument of the given class.
      Throws:
      IllegalArgumentException - in case the given type does not represent a generic.
    • extractGenericTypeCovariantly

      Parameters:
      type - to be extracted from
      Returns:
      if applicable the actual type argument for the given type. If the type represents already a Class it will be returned directly. Otherwise, the super-type will be checked by calling the superclass
    • extractParameterizedType

      public static Optional<ParameterizedType> extractParameterizedType(Class<?> typeToBeExtractedFrom)
      Extracts a ParameterizedType view for the given type
      Parameters:
      typeToBeExtractedFrom - must not be null
      Returns:
      an Optional of the ParameterizedType view of the given class.
    • newProxy

      public static <T> T newProxy(Class<T> interfaceType, InvocationHandler handler)
      Returns a proxy instance that implements interfaceType by dispatching method invocations to handler. The class loader of interfaceType will 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 null
      handler - the invocation handler
      Returns:
      the created Proxy-instance
      Throws:
      IllegalArgumentException - if interfaceType does 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 be null.
      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 stack
      markerClasses - class names which could be used as marker before the real caller name. Collection must not be null.
      Returns:
      option of detected caller class name