@Test public void testGetFieldAndMakeAccessible() throws Exception { Field f = reflectionUtils.getFieldAndMakeAccessible(ReflectTester.class, "foo"); assertTrue(f.isAccessible()); }
public static Field getFieldAndMakeAccessible(Class<?> clazz, String fieldName) { Class<?> searchType = clazz; while (!Object.class.equals(searchType) && searchType != null) { Field[] fields = searchType.getDeclaredFields(); for (Field field : fields) { if (fieldName == null || fieldName.equals(field.getName())) { field.setAccessible(true); return field; } } searchType = searchType.getSuperclass(); } return null; }
ReflectionUtils { public static Field getFieldAndMakeAccessible(Class<?> clazz, String fieldName) { Class<?> searchType = clazz; while (!Object.class.equals(searchType) && searchType != null) { Field[] fields = searchType.getDeclaredFields(); for (Field field : fields) { if (fieldName == null || fieldName.equals(field.getName())) { field.setAccessible(true); return field; } } searchType = searchType.getSuperclass(); } return null; } }
ReflectionUtils { public static Field getFieldAndMakeAccessible(Class<?> clazz, String fieldName) { Class<?> searchType = clazz; while (!Object.class.equals(searchType) && searchType != null) { Field[] fields = searchType.getDeclaredFields(); for (Field field : fields) { if (fieldName == null || fieldName.equals(field.getName())) { field.setAccessible(true); return field; } } searchType = searchType.getSuperclass(); } return null; }  }
ReflectionUtils { public static Field getFieldAndMakeAccessible(Class<?> clazz, String fieldName) { Class<?> searchType = clazz; while (!Object.class.equals(searchType) && searchType != null) { Field[] fields = searchType.getDeclaredFields(); for (Field field : fields) { if (fieldName == null || fieldName.equals(field.getName())) { field.setAccessible(true); return field; } } searchType = searchType.getSuperclass(); } return null; }  static Method getMethodAndMakeAccessible(Class<?> clazz, String name, Class<?>... arguments); static Field getFieldAndMakeAccessible(Class<?> clazz, String fieldName); }
ReflectionUtils { public static Field getFieldAndMakeAccessible(Class<?> clazz, String fieldName) { Class<?> searchType = clazz; while (!Object.class.equals(searchType) && searchType != null) { Field[] fields = searchType.getDeclaredFields(); for (Field field : fields) { if (fieldName == null || fieldName.equals(field.getName())) { field.setAccessible(true); return field; } } searchType = searchType.getSuperclass(); } return null; }  static Method getMethodAndMakeAccessible(Class<?> clazz, String name, Class<?>... arguments); static Field getFieldAndMakeAccessible(Class<?> clazz, String fieldName);  }