List of utility methods to do Reflection Field Find
Field | findField(Class> type, Class find Field String classKey = "$Utility$findField".concat(type.getName()).concat(annotationClass.getName()); if (!neverExpiredFieldObject.containsKey(classKey)) { neverExpiredFieldObject.put(classKey, findFieldEx(type, annotationClass)); return neverExpiredFieldObject.get(classKey); |
Field | findField(Class> type, String fieldName) find Field if (type.equals(Object.class)) { return type.getDeclaredField(fieldName); Field field = null; try { field = type.getDeclaredField(fieldName); } catch (NoSuchFieldException e) { field = findField(type.getSuperclass(), fieldName); ... |
Object | findField(final Class> aClass, final String fieldName) Get the field related to a name try { Class<?> clazz = aClass; while (true) { for (final Field f : Arrays.asList(clazz.getDeclaredFields())) { if (f.getName().equals(fieldName)) { f.setAccessible(true); return f; if (!"Object".equals(clazz.getName())) { clazz = clazz.getSuperclass(); } else { break; } catch (SecurityException e) { System.out.println(e); return null; |
Field | findField(final Class> clazz, final String name) find Field for (final Field f : clazz.getDeclaredFields()) { if (f.getName().equals(name)) { return f; if (clazz.getSuperclass() == null) { throw new NoSuchFieldException("Field " + name + " could not be found"); return findField(clazz.getSuperclass(), name); |
Field | findField(final Class> clazz, final String name) find Field Class<?> theClazz = clazz; while (theClazz != null) { try { return theClazz.getDeclaredField(name); } catch (NoSuchFieldException e) { theClazz = theClazz.getSuperclass(); throw new NoSuchFieldException(name); |
Field | findField(final Class> clazz, final String name, final Class> type) Attempt to find a Field field on the supplied Class with the supplied name and/or Class type .
if (clazz == null) { throw new IllegalArgumentException("Class must not be null"); if (name == null && type == null) { throw new IllegalArgumentException("Either name or type of the field must be specified."); Class<?> searchType = clazz; while (!Object.class.equals(searchType) && searchType != null) { ... |
Field | findField(final Class> cls, final String fieldName) This method will find a java Field for with a particular name. Class<?> currentClass = cls; while (currentClass != null) { Field[] fields = currentClass.getDeclaredFields(); for (Field field : fields) { if (field.getName().equals(fieldName)) { return field; currentClass = currentClass.getSuperclass(); return null; |
Field | findField(final Class> currentClass, final String fieldName) find Field if (null == currentClass) { return null; for (final Field cf : currentClass.getDeclaredFields()) { if (fieldName.equals(cf.getName())) { return cf; return findField(currentClass.getSuperclass(), fieldName); |
Field | findField(final Class> declaringClass, final String fieldName) find Field try { return declaringClass.getDeclaredField(fieldName); } catch (final NoSuchFieldException nsfe) { final Class<?> superclass = declaringClass.getSuperclass(); if (superclass == null) { throw nsfe; return findField(superclass, fieldName); ... |
Field | findField(final Class> klaz, final String fieldName) find Field Class<?> type = klaz; do { for (final Field field : type.getDeclaredFields()) { final String name = field.getName(); if (!name.equals(fieldName)) { continue; field.setAccessible(true); ... |