List of utility methods to do Reflection Field Find
Field | findField(Object object, String name, Class find Field Field f = object.getClass().getDeclaredField(name); f.setAccessible(true); if (f.getType() == klass) { return f; } else { return null; |
Field | findField(Object propertyId, Class classOfItem) find Field Field declaredField; try { declaredField = classOfItem.getDeclaredField(String.valueOf(propertyId)); } catch (NoSuchFieldException e) { declaredField = null; if (declaredField == null && !Object.class.equals(classOfItem)) return findField(propertyId, classOfItem.getSuperclass()); ... |
T | findField(Object target, Class find Field List<T> matches = new ArrayList<T>(); for (Class<?> cl = target.getClass(); cl != Object.class; cl = cl.getSuperclass()) { for (Field field : cl.getDeclaredFields()) { if (type.isAssignableFrom(field.getType())) { field.setAccessible(true); try { matches.add(type.cast(field.get(target))); } catch (IllegalAccessException e) { ... |
Field | findField(Object target, String fieldName) Finds a field with the given name. Class<?> targetClass = target.getClass(); while (targetClass != Object.class) { try { Field field = targetClass.getDeclaredField(fieldName); return field; } catch (NoSuchFieldException e) { targetClass = targetClass.getSuperclass(); throw new NoSuchFieldException(); |
Field | findField(String fieldName, ArrayList find Field for (Field field : fields) { if (field.getName().equals(fieldName)) { return field; return null; |
Field | findField(String fieldName, Class cl) Find a field on a class. Class scannedClass = cl; do { try { return scannedClass.getDeclaredField(fieldName); } catch (NoSuchFieldException | SecurityException e) { scannedClass = scannedClass.getSuperclass(); } while (scannedClass != Object.class); ... |
Field | findField(String fieldName, Class> type) find Field Field field = null; while (field == null && !type.equals(Object.class)) { try { field = type.getDeclaredField(fieldName); } catch (Throwable ignore) { } finally { type = type.getSuperclass(); return field; |
Field | findField(String name, Class c) find Field Field[] fields = c.getDeclaredFields(); Field field = null; for (Field f : fields) { if (f.getName().equals(name)) { field = f; break; if (field == null) { if (c.getSuperclass() != null) { return findField(name, c.getSuperclass()); } else { return null; } else { field.setAccessible(true); return field; |
Field | findField(String name, Object o) find Field Field foundField = null; Field[] fields = o.getClass().getDeclaredFields(); for (Field field : fields) { if (field.getName().equals(name)) { field.setAccessible(true); foundField = field; break; return foundField; |
Field | findField(String string, Class extends Object> clazz) find Field Field[] declaredFields = clazz.getDeclaredFields(); for (Field field : declaredFields) { if (field.getName().equals(string)) { field.setAccessible(true); return field; return null; ... |