MemberUtil

public final class MemberUtil

Util class that provides convenient methods connected with the class members.

Fields

BOOLEAN_GETTER_PREFIX

public static final String BOOLEAN_GETTER_PREFIX

GETTER_PREFIX

public static final String GETTER_PREFIX

GET_OR_SET_END_INDEX

public static final int GET_OR_SET_END_INDEX

IS_END_INDEX

public static final int IS_END_INDEX

SETTER_PREFIX

public static final String SETTER_PREFIX

Methods

getCorrectType

public static Class<?> getCorrectType(AnnotatedElement object)

Gets annotated element type. If this element is not a member of the class, it returns null. Otherwise, it will try to resolve the type by checking it directly, or via getter/setter methods. If member is neither a field or getter/setter method, it returns null.

Parameters:
  • object – annotated element to retrieve type from
Returns:

type of the element, or null if not applicable

getCorrectType

public static Class<?> getCorrectType(Member object)

Gets member type. It will try to resolve the type by checking it directly, or via getter/setter methods. If member is neither a field or getter/setter method, it returns null.

Parameters:
  • object – annotated element to retrieve type from
Returns:

type of the element, or null if not applicable

getDeclaringClass

public static Class<?> getDeclaringClass(AccessibleObject ac)

Retrieves a declaring class for the given object. Returns null if the given object is not a member of a class.

Parameters:
  • ac – object to verify
Returns:

A class, to which this object belongs

getDefaultEnumName

public static String getDefaultEnumName(String entityClassName, String fieldName)

Builds an enum class name for given entity class name and field name.

Parameters:
  • entityClassName – the class name of the entity
  • fieldName – the field name
Returns:

enum class name

getFieldAndAccessorsForElement

public static List<AccessibleObject> getFieldAndAccessorsForElement(AccessibleObject ao)

Returns a list of objects, that are either field or getter/setter methods of this field, based on single accessible object of a class. If it fails to find anything, it returns the passed object.

Parameters:
  • ao – an object to find field, getter/setter method for
Returns:

a list of field and getter/setter methods or ao if nothing has been found

getFieldName

public static String getFieldName(AnnotatedElement object)

Gets field name, from the specified annotated element. It will return null, if annotated element is not a class member. Otherwise, it will try to resolve the field name, by either reading it directly from the member, or by determining the name, based on the getter/setter method. It will return null if member is neither a field or getter/setter.

Parameters:
  • object – annotated element to retrieve field name from
Returns:

field name, if possible; null otherwise

getFieldName

public static String getFieldName(Member object)

Gets field name, from the specified member. It will try to resolve the field name, by either reading it directly from the member, or by determining the name, based on the getter/setter method. It will return null if member is neither a field or getter/setter.

Parameters:
  • object – class member to retrieve field name from
Returns:

field name, if possible; null otherwise

getFieldNameFromGetterSetterName

public static String getFieldNameFromGetterSetterName(String getterSetterName)

Attempts to retrieve field name from the getter/setter method name. It will throw java.lang.IllegalArgumentException if provided value is empty or does not match the setter/getter naming convention.

Parameters:
  • getterSetterName – getter/setter method name
Returns:

field name

getGenericType

public static Class<?> getGenericType(AnnotatedElement object)

Retrieves an actual type from the parameterized class member. If annotated element is not a class member, it returns null. It always checks for the first parameter. If you want to specify which parameter to retrieve, use getGenericType(java.lang.reflect.AnnotatedElement,int). This will work on fields and getter/setter methods. It will return null for other class members or if there is no parameterized type on them.

Parameters:
  • object – annotated element to retrieve actual type from
Returns:

Actual type of the parameterized class member

getGenericType

public static Class<?> getGenericType(AnnotatedElement object, int typeNumber)

Retrieves an actual type from the parameterized class member. If annotated element is not a class member, it returns null. It will check the parameter on position typeNumber. This will work on fields and getter/setter methods. It will return null for other class members or if there is no parameterized type on them.

Parameters:
  • object – annotated element to retrieve actual type from
  • typeNumber – position of the parameterized type
Returns:

Actual type of the parameterized class member

getGenericType

public static Class<?> getGenericType(Member object, int typeNumber)

Retrieves an actual type from the parameterized class member. It will check the parameter on position typeNumber This will work on fields and getter/setter methods. It will return null for other class members or if there is no parameterized type on them.

Parameters:
  • object – class member to retrieve actual type from
  • typeNumber – position of the parameterized type
Returns:

Actual type of the parameterized class member

getGetterName

public static String getGetterName(String fieldName, CtClass declaring)

Returns getter method name for the given field name and the class declaration.

Parameters:
  • fieldName – field name
  • declaring – the class declaration that contains the field
Returns:

getter name

getMembers

public static List<Member> getMembers(Class<?> clazz, Predicate memberPredicate)

Gets all members of a class, that match the specified predicate.

Parameters:
  • clazz – class to retrieve members from
  • memberPredicate – predicate that must be fulfilled by class members
Returns:

list of class members

getSetterName

public static String getSetterName(String fieldName)

Returns setter method name for the given field name.

Parameters:
  • fieldName – field name
Returns:

setter name

isGetter

public static boolean isGetter(Member member)

Checks if given class member is a getter method. This includes boolean-specific getters, starting with “is” prefix.

Parameters:
  • member – class member to verify
Returns:

true if given class member is a getter method; false otherwise

isSetter

public static boolean isSetter(Member member)

Checks if given class member is a setter method.

Parameters:
  • member – class member to verify
Returns:

true if given class member is a setter method; false otherwise