@Test public void shouldRetrieveSettersAndField() throws NoSuchMethodException, NoSuchFieldException { AccessibleObject field = TestClass.class.getDeclaredField("boolField"); AccessibleObject getter = TestClass.class.getMethod("isBoolField"); AccessibleObject setter = TestClass.class.getMethod("setBoolField", boolean.class); List<AccessibleObject> expected = Arrays.asList(field, getter, setter); assertEquals(expected, MemberUtil.getFieldAndAccessorsForElement(field)); assertEquals(expected, MemberUtil.getFieldAndAccessorsForElement(getter)); assertEquals(expected, MemberUtil.getFieldAndAccessorsForElement(setter)); }
public static List<AccessibleObject> getFieldAndAccessorsForElement(AccessibleObject ao) { String fieldName = getFieldName(ao); if (fieldName == null) { return Arrays.asList(ao); } Class declaringClass = ((Member) ao).getDeclaringClass(); try { PropertyDescriptor descriptor = new PropertyDescriptor(fieldName, declaringClass); Field field = ReflectionUtils.findField(declaringClass, fieldName); Method getter = descriptor.getReadMethod(); Method setter = descriptor.getWriteMethod(); List<AccessibleObject> result = new ArrayList<>(); if (field != null) { result.add(field); } if (getter != null) { result.add(getter); } if (setter != null) { result.add(setter); } if (result.isEmpty()) { result.add(ao); } return result; } catch (IntrospectionException e) { return Arrays.asList(ao); } }
MemberUtil { public static List<AccessibleObject> getFieldAndAccessorsForElement(AccessibleObject ao) { String fieldName = getFieldName(ao); if (fieldName == null) { return Arrays.asList(ao); } Class declaringClass = ((Member) ao).getDeclaringClass(); try { PropertyDescriptor descriptor = new PropertyDescriptor(fieldName, declaringClass); Field field = ReflectionUtils.findField(declaringClass, fieldName); Method getter = descriptor.getReadMethod(); Method setter = descriptor.getWriteMethod(); List<AccessibleObject> result = new ArrayList<>(); if (field != null) { result.add(field); } if (getter != null) { result.add(getter); } if (setter != null) { result.add(setter); } if (result.isEmpty()) { result.add(ao); } return result; } catch (IntrospectionException e) { return Arrays.asList(ao); } } }
MemberUtil { public static List<AccessibleObject> getFieldAndAccessorsForElement(AccessibleObject ao) { String fieldName = getFieldName(ao); if (fieldName == null) { return Arrays.asList(ao); } Class declaringClass = ((Member) ao).getDeclaringClass(); try { PropertyDescriptor descriptor = new PropertyDescriptor(fieldName, declaringClass); Field field = ReflectionUtils.findField(declaringClass, fieldName); Method getter = descriptor.getReadMethod(); Method setter = descriptor.getWriteMethod(); List<AccessibleObject> result = new ArrayList<>(); if (field != null) { result.add(field); } if (getter != null) { result.add(getter); } if (setter != null) { result.add(setter); } if (result.isEmpty()) { result.add(ao); } return result; } catch (IntrospectionException e) { return Arrays.asList(ao); } } private  MemberUtil(); }
MemberUtil { public static List<AccessibleObject> getFieldAndAccessorsForElement(AccessibleObject ao) { String fieldName = getFieldName(ao); if (fieldName == null) { return Arrays.asList(ao); } Class declaringClass = ((Member) ao).getDeclaringClass(); try { PropertyDescriptor descriptor = new PropertyDescriptor(fieldName, declaringClass); Field field = ReflectionUtils.findField(declaringClass, fieldName); Method getter = descriptor.getReadMethod(); Method setter = descriptor.getWriteMethod(); List<AccessibleObject> result = new ArrayList<>(); if (field != null) { result.add(field); } if (getter != null) { result.add(getter); } if (setter != null) { result.add(setter); } if (result.isEmpty()) { result.add(ao); } return result; } catch (IntrospectionException e) { return Arrays.asList(ao); } } private  MemberUtil(); static List<Member> getMembers(Class<?> clazz, Predicate memberPredicate); static String getFieldName(AnnotatedElement object); static String getFieldName(Member object); static String getDefaultEnumName(String entityClassName, String fieldName); static Class<?> getCorrectType(AnnotatedElement object); static Class<?> getCorrectType(Member object); static Class<?> getGenericType(AnnotatedElement object); static Class<?> getGenericType(AnnotatedElement object, int typeNumber); static Class<?> getGenericType(Member object, int typeNumber); static String getGetterName(String fieldName, CtClass declaring); static String getSetterName(String fieldName); static boolean isGetter(Member member); static boolean isSetter(Member member); static String getFieldNameFromGetterSetterName(String getterSetterName); static Class<?> getDeclaringClass(AccessibleObject ac); static List<AccessibleObject> getFieldAndAccessorsForElement(AccessibleObject ao); }
MemberUtil { public static List<AccessibleObject> getFieldAndAccessorsForElement(AccessibleObject ao) { String fieldName = getFieldName(ao); if (fieldName == null) { return Arrays.asList(ao); } Class declaringClass = ((Member) ao).getDeclaringClass(); try { PropertyDescriptor descriptor = new PropertyDescriptor(fieldName, declaringClass); Field field = ReflectionUtils.findField(declaringClass, fieldName); Method getter = descriptor.getReadMethod(); Method setter = descriptor.getWriteMethod(); List<AccessibleObject> result = new ArrayList<>(); if (field != null) { result.add(field); } if (getter != null) { result.add(getter); } if (setter != null) { result.add(setter); } if (result.isEmpty()) { result.add(ao); } return result; } catch (IntrospectionException e) { return Arrays.asList(ao); } } private  MemberUtil(); static List<Member> getMembers(Class<?> clazz, Predicate memberPredicate); static String getFieldName(AnnotatedElement object); static String getFieldName(Member object); static String getDefaultEnumName(String entityClassName, String fieldName); static Class<?> getCorrectType(AnnotatedElement object); static Class<?> getCorrectType(Member object); static Class<?> getGenericType(AnnotatedElement object); static Class<?> getGenericType(AnnotatedElement object, int typeNumber); static Class<?> getGenericType(Member object, int typeNumber); static String getGetterName(String fieldName, CtClass declaring); static String getSetterName(String fieldName); static boolean isGetter(Member member); static boolean isSetter(Member member); static String getFieldNameFromGetterSetterName(String getterSetterName); static Class<?> getDeclaringClass(AccessibleObject ac); static List<AccessibleObject> getFieldAndAccessorsForElement(AccessibleObject ao); static final String GETTER_PREFIX; static final String BOOLEAN_GETTER_PREFIX; static final String SETTER_PREFIX; static final int GET_OR_SET_END_INDEX; static final int IS_END_INDEX; }