Class ReflectionUtils

java.lang.Object
org.axonframework.common.ReflectionUtils

public final class ReflectionUtils extends Object
Utility class for working with Java Reflection API.
Since:
0.7
Author:
Allard Buijze
  • Field Details

    • RECURSIVE

      public static final boolean RECURSIVE
      Specifying a reflection operation should be performed recursive.
      See Also:
    • NOT_RECURSIVE

      public static final boolean NOT_RECURSIVE
      Specifying a reflection operation should not be performed recursive.
      See Also:
  • Method Details

    • getFieldValue

      public static <R> R getFieldValue(@Nonnull Field field, @Nonnull Object object)
      Returns the value of the given field in the given object.

      If necessary, the field is made accessible, assuming the security manager allows it.

      Type Parameters:
      R - The type of the value to return.
      Parameters:
      field - The field containing the value.
      object - The object to retrieve the field's value from.
      Returns:
      The value of the field in the object.
      Throws:
      IllegalStateException - If the field is not accessible and the security manager doesn't allow it to be made accessible.
    • setFieldValue

      public static <T> void setFieldValue(@Nonnull Field field, @Nonnull Object object, @Nonnull T value)
      Set the field of object to a certain value.

      If necessary, the field is made accessible, assuming the security manager allows it.

      Type Parameters:
      T - The type of the value.
      Parameters:
      field - The field to set value on.
      object - The object to set the value on field.
      value - The value to set on field.
    • declaringClass

      @Nullable public static Class<?> declaringClass(@Nonnull Class<?> instanceClass, @Nonnull String methodName, Class<?>... parameterTypes)
      Returns the class on which the method with given methodName and parameters of type parameterTypes is declared.

      The given instanceClass is the instance on which the method can be called. If the method is not available on the given instanceClass, null is returned.

      Parameters:
      instanceClass - The class on which to look for the method.
      methodName - The name of the method.
      parameterTypes - The parameter types of the method.
      Returns:
      The class on which the method is declared, or null if not found.
    • hasEqualsMethod

      public static boolean hasEqualsMethod(@Nonnull Class<?> type)
      Indicates whether the given class implements a customized equals method.

      This method returns true if the declaring type of the equals method is not Object.

      Parameters:
      type - The type to inspect.
      Returns:
      true if the given type overrides the equals method, otherwise false.
    • explicitlyUnequal

      public static boolean explicitlyUnequal(@Nullable Object value, @Nullable Object otherValue)
      Indicates whether the two given objects are not the same, override an equals method that indicates they are not equal, or implements Comparable which indicates the two are not equal.

      If this method cannot safely indicate two objects are not equal, it returns false.

      Parameters:
      value - One of the values to compare.
      otherValue - other value to compare.
      Returns:
      true if these objects explicitly indicate they are not equal, false otherwise.
    • ensureAccessible

      @Nonnull public static <T extends AccessibleObject> T ensureAccessible(@Nonnull T member)
      Makes the given member accessible via reflection if it is not the case already.
      Type Parameters:
      T - The type of member to make accessible.
      Parameters:
      member - The member (field, method, constructor, e.g.) to make accessible.
      Returns:
      The given member, for easier method chaining.
      Throws:
      IllegalStateException - If the member is not accessible and the security manager doesn't allow it to be made accessible.
    • isAccessible

      public static boolean isAccessible(AccessibleObject member)
      Indicates whether the given member is accessible.

      It does so by checking whether the member is non-final and public, or made accessible via reflection.

      Parameters:
      member - The member (field, method, constructor, e.g.) to check for accessibility
      Returns:
      true if the member is accessible, otherwise false.
    • isNonFinalPublicMember

      public static boolean isNonFinalPublicMember(@Nonnull Member member)
      Checks whether the given member is public and non-final.

      These members do not need to be set accessible using reflection.

      Parameters:
      member - The member to check.
      Returns:
      true if the member is public and non-final, otherwise false..
      See Also:
    • fieldsOf

      @Nonnull public static Iterable<Field> fieldsOf(@Nonnull Class<?> clazz)
      Returns an Iterable of all the fields declared on the given class and its super classes.

      The iterator will always return fields declared in a subtype before returning fields declared in a super type.

      Parameters:
      clazz - The class to return fields for.
      Returns:
      An Iterable providing access to all declared fields in the class hierarchy.
    • fieldsOf

      @Nonnull public static Iterable<Field> fieldsOf(@Nonnull Class<?> clazz, boolean recursive)
      Returns an Iterable of all the fields declared on the given class.

      Will include the given clazz' super classes if recursive has been set. The iterator will always return fields declared in a subtype before returning fields declared in a super type.

      Parameters:
      clazz - The class to return fields for.
      recursive - Defining whether fields should be found recursively on super classes as well.
      Returns:
      An Iterable providing access to all declared fields in the class, including the hierarchy if recursive was set.
    • methodOf

      @Nonnull public static Method methodOf(@Nonnull Class<?> clazz, @Nonnull String methodName, Class<?>... parameterTypes) throws NoSuchMethodException
      Utility function which returns a Method matching the given methodName and parameterTypes in the clazz.
      Parameters:
      clazz - The Class to return a method for.
      methodName - A String for the simple name of the method to return.
      parameterTypes - An array of type Class for all the parameters which are part of the Method being searched for.
      Returns:
      A Method object from the given clazz matching the specified methodName.
      Throws:
      NoSuchMethodException - If no Method can be found matching the methodName in clazz.
    • methodsOf

      @Nonnull public static Iterable<Method> methodsOf(@Nonnull Class<?> clazz)
      Returns an Iterable of all the methods declared on the given class and its super classes.

      The iterator will always return methods declared in a subtype before returning methods declared in a super type.

      Parameters:
      clazz - The class to return methods for.
      Returns:
      An Iterable providing access to all declared methods in the class hierarchy.
    • methodsOf

      @Nonnull public static Iterable<Method> methodsOf(@Nonnull Class<?> clazz, boolean recursive)
      Returns an Iterable of all the methods declared on the given class.

      Will include the given clazz' super classes if recursive has been set. The iterator will always return fields declared in a subtype before returning fields declared in a super type.

      Parameters:
      clazz - The class to return methods for.
      recursive - Defining whether methods should be found recursively on super classes as well.
      Returns:
      An Iterable providing access to all declared methods in the class, including the hierarchy if recursive was set.
    • resolvePrimitiveWrapperType

      @Nonnull public static Class<?> resolvePrimitiveWrapperType(@Nonnull Class<?> primitiveType)
      Returns the boxed wrapper type for the given primitiveType.
      Parameters:
      primitiveType - The primitive type to return boxed wrapper type for.
      Returns:
      The boxed wrapper type for the given primitiveType.
      Throws:
      IllegalArgumentException - will be thrown instead of returning null if no wrapper class was found.
    • resolvePrimitiveWrapperTypeIfPrimitive

      @Nonnull public static Type resolvePrimitiveWrapperTypeIfPrimitive(@Nonnull Type type)
      Returns the boxed wrapper type for the given type if it is primitive.
      Parameters:
      type - A Type to return boxed wrapper type for.
      Returns:
      The boxed wrapper type for the give type, or type if no wrapper class was found.
    • unwrapIfType

      @Nonnull public static Type unwrapIfType(@Nonnull Type type, Class<?>... wrapperTypes)
      Unwrap the given type if is wrapped by any of the given wrapperTypes.

      This method assumes that the wrapperTypes have a single generic argument, which identifies the type they wrap.

      For example, if invoked with Future.class and Optional.class as wrapperTypes:

      • Future<String> resolves to String
      • Optional<String> resolves to String
      • Optional<Future<List<String>>> resolves to List<String>
      Parameters:
      type - The type to unwrap, if wrapped.
      wrapperTypes - The wrapper types to unwrap.
      Returns:
      The unwrapped Type, or the original if it wasn't wrapped in any of the given wrapper types.
    • isTransient

      public static boolean isTransient(@Nonnull Field field)
      Indicates whether the given field has the "transient" modifier.
      Parameters:
      field - The field to inspect.
      Returns:
      true if the field is marked transient, otherwise false.
    • resolveGenericType

      @Nonnull public static Optional<Class<?>> resolveGenericType(@Nonnull Field field, int genericTypeIndex)
      Resolve a generic type parameter from a field declaration.
      Parameters:
      field - The field to find generic parameters for.
      genericTypeIndex - The index of the type.
      Returns:
      An optional that contains the resolved type, if found.
    • resolveMemberGenericType

      @Nonnull public static Optional<Class<?>> resolveMemberGenericType(@Nonnull Member member, int genericTypeIndex)
      Resolve a generic type parameter from a member declaration.
      Parameters:
      member - The member to find generic parameters for.
      genericTypeIndex - The index of the type.
      Returns:
      An optional that contains the resolved type, if found.
    • invokeAndGetMethodValue

      public static <R> R invokeAndGetMethodValue(@Nonnull Method method, @Nonnull Object object)
      Invokes and returns the return value of the given method in the given object. If necessary, the method is made accessible, assuming the security manager allows it.
      Type Parameters:
      R - The type of return object from invoking the given method.
      Parameters:
      method - The method to invoke.
      object - The target object the given method is invoked on.
      Returns:
      The resulting value of invocation of the method in the object
      Throws:
      IllegalStateException - If the method is not accessible and the security manager doesn't allow it to be made accessible.
    • getMemberValue

      public static <R> R getMemberValue(@Nonnull Member member, @Nonnull Object target)
      Returns the value of the given member in the given object, either by returning Field value or invoking the method. If necessary, the member is made accessible, assuming the security manager allows it. Supported members are Field and non-void Method without parameters.
      Type Parameters:
      R - The type of return object from retrieving the member's value.
      Parameters:
      member - The member containing or returning the value.
      target - The object to retrieve the member's value from.
      Returns:
      The value of the member in the object.
      Throws:
      IllegalStateException - if the member is not supported.
    • getMemberValueType

      @Nonnull public static Class<?> getMemberValueType(@Nonnull Member member)
      Returns the type of value of the given member, either by returning the type of Field or type of the return value of a Method.
      Parameters:
      member - The member to get the value type from.
      Returns:
      The type of value of the member.
      Throws:
      IllegalStateException - If the member is not supported.
    • getMemberGenericType

      @Nonnull public static Type getMemberGenericType(@Nonnull Member member)
      Returns the generic type of value of the given member, either by returning the generic type of Field or generic return type of Method.
      Parameters:
      member - The member to get generic type of.
      Returns:
      The generic type of value of the member.
      Throws:
      IllegalStateException - If the member is not supported.
    • getMemberGenericString

      @Nonnull public static String getMemberGenericString(@Nonnull Member member)
      Returns the generic string of the given member.
      Parameters:
      member - the member to get the generic string for.
      Returns:
      The generic string of the member.
      Throws:
      IllegalStateException - If the member is not supported.
    • toDiscernibleSignature

      @Nonnull public static String toDiscernibleSignature(@Nonnull Executable executable)
      Returns a discernible signature without including the classname.

      This will contain the method name and the parameter types, such as: thisIfMyMethod(java.lang.String myString, com.acme.MyGreatObject).

      Parameters:
      executable - The executable to make a signature of.
      Returns:
      The discernible signature.
    • fieldNameFromMember

      @Nonnull public static String fieldNameFromMember(@Nonnull Member member)
      Returns the name of the field represented by the given member.

      If the member is a method, it will return the name of the field it represents. For example, if the member is a getter method for a field named "getFoo", "isFoo", or "foo", it will return "foo". If the member is a field, it will return the name of the field.

      Parameters:
      member - The member to get the field name for.
      Returns:
      The name of the field represented by the given member.