@Test public void testNoSuchFieldException() throws Exception { DerivedClass derivedClass = new DerivedClass(5); try { RestrictedFieldAccessingUtils .getRestrictedFieldByReflectionRecursively(derivedClass, "non", derivedClass.getClass()); } catch (NoSuchFieldException ne) { Assert.assertTrue(true); return; } Assert.assertTrue(false); }
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);  }