@Test void testGetValue() throws NoSuchMethodException, SecurityException { TestObject testObject = new TestObject(); Member fieldOrMethod = TestObject.class.getDeclaredMethod("getValue"); String result = MemberAccessors.getValue(testObject, fieldOrMethod); assertThat(result, is(testObject.getValue())); }
@SuppressWarnings("unchecked") public static <T> T getValue(Object object, Member fieldOrMethod) { if (fieldOrMethod instanceof Field) { return (T)getFieldValue(object, fieldOrMethod); } else if (fieldOrMethod instanceof Method) { Function<Object, Object> accessor = ACCESSOR_CACHE .computeIfAbsent(fieldOrMethod, key -> new MemberAccessors((Method)key).createFunction()); return (T)accessor.apply(object); } else { throw new IllegalArgumentException("Only field or method is supported, found " + fieldOrMethod.getClass().getCanonicalName() + " as type of " + getNameOf(fieldOrMethod)); } }
MemberAccessors { @SuppressWarnings("unchecked") public static <T> T getValue(Object object, Member fieldOrMethod) { if (fieldOrMethod instanceof Field) { return (T)getFieldValue(object, fieldOrMethod); } else if (fieldOrMethod instanceof Method) { Function<Object, Object> accessor = ACCESSOR_CACHE .computeIfAbsent(fieldOrMethod, key -> new MemberAccessors((Method)key).createFunction()); return (T)accessor.apply(object); } else { throw new IllegalArgumentException("Only field or method is supported, found " + fieldOrMethod.getClass().getCanonicalName() + " as type of " + getNameOf(fieldOrMethod)); } } }
MemberAccessors { @SuppressWarnings("unchecked") public static <T> T getValue(Object object, Member fieldOrMethod) { if (fieldOrMethod instanceof Field) { return (T)getFieldValue(object, fieldOrMethod); } else if (fieldOrMethod instanceof Method) { Function<Object, Object> accessor = ACCESSOR_CACHE .computeIfAbsent(fieldOrMethod, key -> new MemberAccessors((Method)key).createFunction()); return (T)accessor.apply(object); } else { throw new IllegalArgumentException("Only field or method is supported, found " + fieldOrMethod.getClass().getCanonicalName() + " as type of " + getNameOf(fieldOrMethod)); } } private  MemberAccessors(Method method); }
MemberAccessors { @SuppressWarnings("unchecked") public static <T> T getValue(Object object, Member fieldOrMethod) { if (fieldOrMethod instanceof Field) { return (T)getFieldValue(object, fieldOrMethod); } else if (fieldOrMethod instanceof Method) { Function<Object, Object> accessor = ACCESSOR_CACHE .computeIfAbsent(fieldOrMethod, key -> new MemberAccessors((Method)key).createFunction()); return (T)accessor.apply(object); } else { throw new IllegalArgumentException("Only field or method is supported, found " + fieldOrMethod.getClass().getCanonicalName() + " as type of " + getNameOf(fieldOrMethod)); } } private  MemberAccessors(Method method); @Override String toString(); @SuppressWarnings("unchecked") static T getValue(Object object, Member fieldOrMethod); }
MemberAccessors { @SuppressWarnings("unchecked") public static <T> T getValue(Object object, Member fieldOrMethod) { if (fieldOrMethod instanceof Field) { return (T)getFieldValue(object, fieldOrMethod); } else if (fieldOrMethod instanceof Method) { Function<Object, Object> accessor = ACCESSOR_CACHE .computeIfAbsent(fieldOrMethod, key -> new MemberAccessors((Method)key).createFunction()); return (T)accessor.apply(object); } else { throw new IllegalArgumentException("Only field or method is supported, found " + fieldOrMethod.getClass().getCanonicalName() + " as type of " + getNameOf(fieldOrMethod)); } } private  MemberAccessors(Method method); @Override String toString(); @SuppressWarnings("unchecked") static T getValue(Object object, Member fieldOrMethod);  }