List of utility methods to do Reflection Field Find
Field | findField(Class clazz, String name) Finds a field of a given class recursively by stepping up the inheritance chain. Class currentClass = clazz; while (currentClass != Object.class) { for (Field field : currentClass.getDeclaredFields()) { if (name.equals(field.getName())) { return field; currentClass = currentClass.getSuperclass(); ... |
Field | findField(Class clazz, String name) find Field return findField(clazz, name, (Class) null);
|
Field | findField(Class clazz, String name) find Field List<Field> fields = getAllDeclaredFields(clazz); for (Field field : fields) { if (name.equals(field.getName())) return field; return null; |
Field | findField(Class cls, String name) Finds the first (from the bottom of the inheritance hierarchy) field with the specified name. if (cls != null) { try { return cls.getDeclaredField(name); } catch (NoSuchFieldException e) { return findField(cls.getSuperclass(), name); } else { throw new NoSuchFieldException(); ... |
Field | findField(Class objectClass, String fieldName) find Field Class cursor = objectClass; while (cursor != null) { try { return cursor.getDeclaredField(fieldName); } catch (NoSuchFieldException ex) { cursor = cursor.getSuperclass(); throw new RuntimeException( String.format("Class %s does not contain a field named '%s'.", objectClass.getName(), fieldName)); |
Field | findField(Class type, String fieldName) Find the field with the given name in the class. while (type != null) { Field[] fields = type.getDeclaredFields(); for (Field field : fields) { if (field.getName().equals(fieldName)) return field; type = type.getSuperclass(); return null; |
Field | findField(Class> c, String name) find Field if (c == null) { return null; String cacheKey = c.getName() + name; Field f = refFieldsCache.get(cacheKey); if (f != null) { return f; try { f = c.getDeclaredField(name); f.setAccessible(true); refFieldsCache.put(cacheKey, f); return f; } catch (Exception err) { Class<?> s = c.getSuperclass(); if (s != null) { f = findField(s, name); if (f != null) { refFieldsCache.put(cacheKey, f); return f; return null; |
Field | findField(Class> c, String property, Class> propertyType) Returns a public field for a given property Field result = null; Field[] fields = c.getFields(); for (Field f : fields) { String fn = f.getName(); if (fn.charAt(0) == '_') { fn = fn.substring(1); if (fn.equals(property) && (propertyType == null || f.getType().isAssignableFrom(propertyType))) { ... |
Field | findField(Class> cl, String fieldName) find Field try { return cl.getDeclaredField(fieldName); } catch (SecurityException sex) { return null; } catch (NoSuchFieldException e) { if (Objects.equals(cl, Object.class)) { throw e; return findField(cl.getSuperclass(), fieldName); |
Field | findField(Class> classToCheck, String propertyName) find Field if (classToCheck == null || Object.class.equals(classToCheck)) { return null; try { return classToCheck.getDeclaredField(propertyName); } catch (NoSuchFieldException nsfe) { return findField(classToCheck.getSuperclass(), propertyName); |