### Question:
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);  }### Answer:
@Test void testFieldAccess() throws NoSuchFieldException, SecurityException { TestObject testObject = new TestObject(); Member fieldOrMethod = TestObject.class.getDeclaredField("field"); String result = MemberAccessors.getValue(testObject, fieldOrMethod); assertThat(result, is(testObject.field)); }
@Test void testFieldAccess_NativeType() throws NoSuchFieldException, SecurityException { TestObject testObject = new TestObject(); Member fieldOrMethod = TestObject.class.getDeclaredField("intField"); int result = MemberAccessors.getValue(testObject, fieldOrMethod); assertThat(result, is(testObject.intField)); }
@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())); }
@Test void testGetValue_Void() throws NoSuchMethodException, SecurityException { TestObject testObject = new TestObject(); Member fieldOrMethod = TestObject.class.getDeclaredMethod("getVoid"); Assertions.assertThrows(IllegalArgumentException.class, () -> MemberAccessors.getValue(testObject, fieldOrMethod)); }
@Test void testGetValue_NativeReturnType() throws NoSuchMethodException, SecurityException { TestObject testObject = new TestObject(); Member fieldOrMethod = TestObject.class.getDeclaredMethod("getInt"); int result = MemberAccessors.getValue(testObject, fieldOrMethod); assertThat(result, is(testObject.getInt())); }
@Test void testGetValue_MethodThrowsException() throws NoSuchMethodException, SecurityException { TestObject testObject = new TestObject(); Member fieldOrMethod = TestObject.class.getDeclaredMethod("getThrowException"); assertThrows(RuntimeException.class, () -> MemberAccessors.getValue(testObject, fieldOrMethod)); }
@Test void testGetValue_UnsupportedMemberType() throws NoSuchMethodException, SecurityException { TestObject testObject = new TestObject(); Constructor<? extends TestObject> constructor = testObject.getClass().getDeclaredConstructor(); assertThrows(IllegalArgumentException.class, () -> MemberAccessors.getValue(testObject, constructor)); }