@Test void testGetReadMethod() throws NoSuchFieldException { assertThat(ReflectionUtils.getReadMethod(PrimitiveFieldsWithDefaultValuesBean.class.getDeclaredField("b"))).isEmpty(); final Optional<Method> readMethod = ReflectionUtils.getReadMethod(Foo.class.getDeclaredField("bar")); assertThat(readMethod).isNotNull(); assertThat(readMethod.get().getName()).isEqualTo("getBar"); }
public static Optional<Method> getReadMethod(Field field) { String fieldName = field.getName(); Class<?> fieldClass = field.getDeclaringClass(); String capitalizedFieldName = fieldName.substring(0, 1).toUpperCase(ENGLISH) + fieldName.substring(1); Optional<Method> getter = getPublicMethod("get" + capitalizedFieldName, fieldClass); if (getter.isPresent()) { return getter; } return getPublicMethod("is" + capitalizedFieldName, fieldClass); }
ReflectionUtils { public static Optional<Method> getReadMethod(Field field) { String fieldName = field.getName(); Class<?> fieldClass = field.getDeclaringClass(); String capitalizedFieldName = fieldName.substring(0, 1).toUpperCase(ENGLISH) + fieldName.substring(1); Optional<Method> getter = getPublicMethod("get" + capitalizedFieldName, fieldClass); if (getter.isPresent()) { return getter; } return getPublicMethod("is" + capitalizedFieldName, fieldClass); } }
ReflectionUtils { public static Optional<Method> getReadMethod(Field field) { String fieldName = field.getName(); Class<?> fieldClass = field.getDeclaringClass(); String capitalizedFieldName = fieldName.substring(0, 1).toUpperCase(ENGLISH) + fieldName.substring(1); Optional<Method> getter = getPublicMethod("get" + capitalizedFieldName, fieldClass); if (getter.isPresent()) { return getter; } return getPublicMethod("is" + capitalizedFieldName, fieldClass); } private  ReflectionUtils(); }
ReflectionUtils { public static Optional<Method> getReadMethod(Field field) { String fieldName = field.getName(); Class<?> fieldClass = field.getDeclaringClass(); String capitalizedFieldName = fieldName.substring(0, 1).toUpperCase(ENGLISH) + fieldName.substring(1); Optional<Method> getter = getPublicMethod("get" + capitalizedFieldName, fieldClass); if (getter.isPresent()) { return getter; } return getPublicMethod("is" + capitalizedFieldName, fieldClass); } private  ReflectionUtils(); @SuppressWarnings("unchecked") static Randomizer<T> asRandomizer(final Supplier<T> supplier); static List<Field> getDeclaredFields(T type); static List<Field> getInheritedFields(Class<?> type); static void setProperty(final Object object, final Field field, final Object value); static void setFieldValue(final Object object, final Field field, final Object value); static Object getFieldValue(final Object object, final Field field); static Class<?> getWrapperType(Class<?> primitiveType); static boolean isPrimitiveFieldWithDefaultValue(final Object object, final Field field); static boolean isStatic(final Field field); static boolean isInterface(final Class<?> type); static boolean isAbstract(final Class<T> type); static boolean isPublic(final Class<T> type); static boolean isArrayType(final Class<?> type); static boolean isEnumType(final Class<?> type); static boolean isCollectionType(final Class<?> type); static boolean isCollectionType(final Type type); static boolean isPopulatable(final Type type); static boolean isIntrospectable(final Class<?> type); static boolean isMapType(final Class<?> type); static boolean isJdkBuiltIn(final Class<?> type); static boolean isParameterizedType(final Type type); static boolean isWildcardType(final Type type); static boolean isTypeVariable(final Type type); static List<Class<?>> getPublicConcreteSubTypesOf(final Class<T> type); static List<Class<?>> filterSameParameterizedTypes(final List<Class<?>> types, final Type type); static T getAnnotation(Field field, Class<T> annotationType); static boolean isAnnotationPresent(Field field, Class<? extends Annotation> annotationType); static Collection<?> getEmptyImplementationForCollectionInterface(final Class<?> collectionInterface); static Collection<?> createEmptyCollectionForType(Class<?> fieldType, int initialSize); static Map<?, ?> getEmptyImplementationForMapInterface(final Class<?> mapInterface); static Optional<Method> getReadMethod(Field field); @SuppressWarnings("unchecked") static Randomizer<T> newInstance(final Class<T> type, final RandomizerArgument[] randomizerArguments); }
ReflectionUtils { public static Optional<Method> getReadMethod(Field field) { String fieldName = field.getName(); Class<?> fieldClass = field.getDeclaringClass(); String capitalizedFieldName = fieldName.substring(0, 1).toUpperCase(ENGLISH) + fieldName.substring(1); Optional<Method> getter = getPublicMethod("get" + capitalizedFieldName, fieldClass); if (getter.isPresent()) { return getter; } return getPublicMethod("is" + capitalizedFieldName, fieldClass); } private  ReflectionUtils(); @SuppressWarnings("unchecked") static Randomizer<T> asRandomizer(final Supplier<T> supplier); static List<Field> getDeclaredFields(T type); static List<Field> getInheritedFields(Class<?> type); static void setProperty(final Object object, final Field field, final Object value); static void setFieldValue(final Object object, final Field field, final Object value); static Object getFieldValue(final Object object, final Field field); static Class<?> getWrapperType(Class<?> primitiveType); static boolean isPrimitiveFieldWithDefaultValue(final Object object, final Field field); static boolean isStatic(final Field field); static boolean isInterface(final Class<?> type); static boolean isAbstract(final Class<T> type); static boolean isPublic(final Class<T> type); static boolean isArrayType(final Class<?> type); static boolean isEnumType(final Class<?> type); static boolean isCollectionType(final Class<?> type); static boolean isCollectionType(final Type type); static boolean isPopulatable(final Type type); static boolean isIntrospectable(final Class<?> type); static boolean isMapType(final Class<?> type); static boolean isJdkBuiltIn(final Class<?> type); static boolean isParameterizedType(final Type type); static boolean isWildcardType(final Type type); static boolean isTypeVariable(final Type type); static List<Class<?>> getPublicConcreteSubTypesOf(final Class<T> type); static List<Class<?>> filterSameParameterizedTypes(final List<Class<?>> types, final Type type); static T getAnnotation(Field field, Class<T> annotationType); static boolean isAnnotationPresent(Field field, Class<? extends Annotation> annotationType); static Collection<?> getEmptyImplementationForCollectionInterface(final Class<?> collectionInterface); static Collection<?> createEmptyCollectionForType(Class<?> fieldType, int initialSize); static Map<?, ?> getEmptyImplementationForMapInterface(final Class<?> mapInterface); static Optional<Method> getReadMethod(Field field); @SuppressWarnings("unchecked") static Randomizer<T> newInstance(final Class<T> type, final RandomizerArgument[] randomizerArguments);  }