@Test public void testGetRestrictedFieldByReflectionRecursively() throws Exception { DerivedClass derivedClass = new DerivedClass(5); Assert.assertEquals(derivedClass.getEnclosingValue(), 5); ((EnclosedClass) RestrictedFieldAccessingUtils .getRestrictedFieldByReflectionRecursively(derivedClass, "enclose", derivedClass.getClass())).setValue(100); Assert.assertEquals(derivedClass.getEnclosingValue(), 100); }
public static Object getRestrictedFieldByReflectionRecursively(Object containingObj, String fieldName, Class clazz) throws NoSuchFieldException, IllegalAccessException { if (clazz.getCanonicalName().equals("java.lang.Object")) { throw new NoSuchFieldException( String.format("Field %s doesn't exist in specified class and its ancestors", fieldName)); } if (!Arrays.asList(clazz.getDeclaredFields()).stream() .anyMatch(x -> x.getName().equals(fieldName))) { return getRestrictedFieldByReflectionRecursively(containingObj, fieldName, clazz.getSuperclass()); } else { return getRestrictedFieldByReflection(containingObj, fieldName, clazz); } }
RestrictedFieldAccessingUtils { public static Object getRestrictedFieldByReflectionRecursively(Object containingObj, String fieldName, Class clazz) throws NoSuchFieldException, IllegalAccessException { if (clazz.getCanonicalName().equals("java.lang.Object")) { throw new NoSuchFieldException( String.format("Field %s doesn't exist in specified class and its ancestors", fieldName)); } if (!Arrays.asList(clazz.getDeclaredFields()).stream() .anyMatch(x -> x.getName().equals(fieldName))) { return getRestrictedFieldByReflectionRecursively(containingObj, fieldName, clazz.getSuperclass()); } else { return getRestrictedFieldByReflection(containingObj, fieldName, clazz); } } }
RestrictedFieldAccessingUtils { public static Object getRestrictedFieldByReflectionRecursively(Object containingObj, String fieldName, Class clazz) throws NoSuchFieldException, IllegalAccessException { if (clazz.getCanonicalName().equals("java.lang.Object")) { throw new NoSuchFieldException( String.format("Field %s doesn't exist in specified class and its ancestors", fieldName)); } if (!Arrays.asList(clazz.getDeclaredFields()).stream() .anyMatch(x -> x.getName().equals(fieldName))) { return getRestrictedFieldByReflectionRecursively(containingObj, fieldName, clazz.getSuperclass()); } else { return getRestrictedFieldByReflection(containingObj, fieldName, clazz); } } private  RestrictedFieldAccessingUtils(); }
RestrictedFieldAccessingUtils { public static Object getRestrictedFieldByReflectionRecursively(Object containingObj, String fieldName, Class clazz) throws NoSuchFieldException, IllegalAccessException { if (clazz.getCanonicalName().equals("java.lang.Object")) { throw new NoSuchFieldException( String.format("Field %s doesn't exist in specified class and its ancestors", fieldName)); } if (!Arrays.asList(clazz.getDeclaredFields()).stream() .anyMatch(x -> x.getName().equals(fieldName))) { return getRestrictedFieldByReflectionRecursively(containingObj, fieldName, clazz.getSuperclass()); } else { return getRestrictedFieldByReflection(containingObj, fieldName, clazz); } } private  RestrictedFieldAccessingUtils(); static Object getRestrictedFieldByReflection(Object containingObj, String fieldName, Class clazz); static Object getRestrictedFieldByReflectionRecursively(Object containingObj, String fieldName, Class clazz); }
RestrictedFieldAccessingUtils { public static Object getRestrictedFieldByReflectionRecursively(Object containingObj, String fieldName, Class clazz) throws NoSuchFieldException, IllegalAccessException { if (clazz.getCanonicalName().equals("java.lang.Object")) { throw new NoSuchFieldException( String.format("Field %s doesn't exist in specified class and its ancestors", fieldName)); } if (!Arrays.asList(clazz.getDeclaredFields()).stream() .anyMatch(x -> x.getName().equals(fieldName))) { return getRestrictedFieldByReflectionRecursively(containingObj, fieldName, clazz.getSuperclass()); } else { return getRestrictedFieldByReflection(containingObj, fieldName, clazz); } } private  RestrictedFieldAccessingUtils(); static Object getRestrictedFieldByReflection(Object containingObj, String fieldName, Class clazz); static Object getRestrictedFieldByReflectionRecursively(Object containingObj, String fieldName, Class clazz);  }