@Test public void testFieldFromGetter() { class Foo { String foo = "boo"; @Bar public String getFoo() { return foo; } @SuppressWarnings("unused") public void setFoo(String foo) { this.foo = foo; } } Field foo = ReflectionUtils.getReferencedField(Foo.class, Bar.class); assertNotNull(foo); assertEquals("foo", foo.getName()); }
public static Field getReferencedField( Class<?> clazz, Class<? extends Annotation> annotationClass) { Field field = getFirstAnnotatedField(clazz, annotationClass); if (field == null) { Method method = getFirstAnnotatedMethod(clazz, annotationClass); if (method != null && (isGetter(method) || isSetter(method))) { String fieldName = toFieldName(method); try { field = (fieldName != null) ? clazz.getDeclaredField(fieldName) : null; } catch (Exception e) { } if (field == null) { try { field = (fieldName != null) ? clazz.getField(fieldName) : null; } catch (Exception e) { } } } } return field; }
ReflectionUtils { public static Field getReferencedField( Class<?> clazz, Class<? extends Annotation> annotationClass) { Field field = getFirstAnnotatedField(clazz, annotationClass); if (field == null) { Method method = getFirstAnnotatedMethod(clazz, annotationClass); if (method != null && (isGetter(method) || isSetter(method))) { String fieldName = toFieldName(method); try { field = (fieldName != null) ? clazz.getDeclaredField(fieldName) : null; } catch (Exception e) { } if (field == null) { try { field = (fieldName != null) ? clazz.getField(fieldName) : null; } catch (Exception e) { } } } } return field; } }
ReflectionUtils { public static Field getReferencedField( Class<?> clazz, Class<? extends Annotation> annotationClass) { Field field = getFirstAnnotatedField(clazz, annotationClass); if (field == null) { Method method = getFirstAnnotatedMethod(clazz, annotationClass); if (method != null && (isGetter(method) || isSetter(method))) { String fieldName = toFieldName(method); try { field = (fieldName != null) ? clazz.getDeclaredField(fieldName) : null; } catch (Exception e) { } if (field == null) { try { field = (fieldName != null) ? clazz.getField(fieldName) : null; } catch (Exception e) { } } } } return field; }  }
ReflectionUtils { public static Field getReferencedField( Class<?> clazz, Class<? extends Annotation> annotationClass) { Field field = getFirstAnnotatedField(clazz, annotationClass); if (field == null) { Method method = getFirstAnnotatedMethod(clazz, annotationClass); if (method != null && (isGetter(method) || isSetter(method))) { String fieldName = toFieldName(method); try { field = (fieldName != null) ? clazz.getDeclaredField(fieldName) : null; } catch (Exception e) { } if (field == null) { try { field = (fieldName != null) ? clazz.getField(fieldName) : null; } catch (Exception e) { } } } } return field; }  static Field getFirstAnnotatedField(
			Class<?> clazz,
			Class<? extends Annotation> annotationClass); static List<Field> getAllAnnotatedFields(
			Class<?> clazz,
			Class<? extends Annotation> annotationClass); static Method getFirstAnnotatedMethod(
			Class<?> clazz,
			Class<? extends Annotation> annotationClass); static Class<?> getFirstAnnotatedClass(
			Class<?> clazz,
			Class<? extends Annotation> annotationClass); static T getFirstClassAnnotation(
			Class<?> clazz,
			Class<T> annotationClass); static boolean isAnnotationPresent(
			Class<?> clazz,
			Class<? extends Annotation> annotationClass); static boolean isGetter(Method method); static boolean isSetter(Method method); static String toFieldName(Method getterOrSetter); static Field getReferencedField(
			Class<?> clazz,
			Class<? extends Annotation> annotationClass); static Field getField(final Class<?> clazz, final String fieldName); }
ReflectionUtils { public static Field getReferencedField( Class<?> clazz, Class<? extends Annotation> annotationClass) { Field field = getFirstAnnotatedField(clazz, annotationClass); if (field == null) { Method method = getFirstAnnotatedMethod(clazz, annotationClass); if (method != null && (isGetter(method) || isSetter(method))) { String fieldName = toFieldName(method); try { field = (fieldName != null) ? clazz.getDeclaredField(fieldName) : null; } catch (Exception e) { } if (field == null) { try { field = (fieldName != null) ? clazz.getField(fieldName) : null; } catch (Exception e) { } } } } return field; }  static Field getFirstAnnotatedField(
			Class<?> clazz,
			Class<? extends Annotation> annotationClass); static List<Field> getAllAnnotatedFields(
			Class<?> clazz,
			Class<? extends Annotation> annotationClass); static Method getFirstAnnotatedMethod(
			Class<?> clazz,
			Class<? extends Annotation> annotationClass); static Class<?> getFirstAnnotatedClass(
			Class<?> clazz,
			Class<? extends Annotation> annotationClass); static T getFirstClassAnnotation(
			Class<?> clazz,
			Class<T> annotationClass); static boolean isAnnotationPresent(
			Class<?> clazz,
			Class<? extends Annotation> annotationClass); static boolean isGetter(Method method); static boolean isSetter(Method method); static String toFieldName(Method getterOrSetter); static Field getReferencedField(
			Class<?> clazz,
			Class<? extends Annotation> annotationClass); static Field getField(final Class<?> clazz, final String fieldName);  }