Java Utililty Methods Reflection Field Find

List of utility methods to do Reflection Field Find

Description

The list of methods to do Reflection Field Find are organized into topic(s).

Method

FieldfindField(Class clazz, String propName)
find Field
Class<?> c = clazz;
while (c != null) {
    for (Field f : c.getDeclaredFields()) {
        if (!f.getName().equals(propName)) {
            continue;
        int modifiers = f.getModifiers();
        if (Modifier.isStatic(modifiers)) {
...
FieldfindField(Class clazz, String targetName, Class targetType, boolean checkInheritance, boolean strictType)
find Field
if (clazz == null)
    throw new NoSuchFieldException("No class");
if (targetName == null)
    throw new NoSuchFieldException("No field name");
try {
    Field field = clazz.getDeclaredField(targetName);
    if (strictType) {
        if (field.getType().equals(targetType))
...
FieldfindField(Class cls, String fieldName)
Get the Field-Object for a Field specified by name.
For retrieving the Field this method recursively scans super-Classes as well.
Field field = null;
while (field == null) {
    try {
        field = cls.getDeclaredField(fieldName);
    } catch (NoSuchFieldException e) {
        cls = cls.getSuperclass();
        if (cls.equals(Object.class))
            throw e;
...
FieldfindField(Class cls, String fieldName)
Try find a field with given name.
Field f = null;
try {
    f = cls.getDeclaredField(fieldName);
} catch (SecurityException ex) {
} catch (NoSuchFieldException ex) {
if (f != null) {
    return f;
...
FieldfindField(Class cls, String fieldName)
find Field
Field f = null;
try {
    f = cls.getDeclaredField(fieldName);
} catch (SecurityException ex) {
} catch (NoSuchFieldException ex) {
if (f != null)
    return f;
...
FieldfindField(Class currentClass, String fieldName)
find Field
while (isUserDefined(currentClass)) {
    try {
        Field f = currentClass.getDeclaredField(fieldName);
        if (f != null) {
            return f;
    } catch (Exception e) {
    } finally {
...
FieldfindField(Class inClass, String fieldName)
Finds a field by name within the given class and its super-classes.
Provides private fields too.
Class<?> c = inClass;
while (c != null) {
    try {
        return inClass.getDeclaredField(fieldName);
    } catch (SecurityException e) {
        throw new RuntimeException(
                "Security does not allow to access field '" + fieldName + "' of class " + c, e);
    } catch (NoSuchFieldException e) {
...
FieldfindField(Class klass, String name)
find Field
Field result = null;
for (Class<?> iter = klass; iter != null; iter = iter.getSuperclass()) {
    try {
        result = iter.getDeclaredField(name);
        break;
    } catch (NoSuchFieldException e) {
return result;
FieldfindField(Class pClass, String fieldName)
Find a field with the given name on the given class, regardless of if it is declared on the class or on of its superclasses.
Field[] fields = getAllFields(pClass);
for (int i = 0; i < fields.length; i++) {
    if (fields[i].getName().equals(fieldName)) {
        return fields[i];
throw new NoSuchFieldException(
        "Unable to find field " + fieldName + " on class " + pClass.getCanonicalName());
...
FieldfindField(Class targetClass, String fieldName)
find Field
Field theField;
try {
    theField = targetClass.getDeclaredField(fieldName);
    return accessible(theField);
} catch (NoSuchFieldException e) {
    if (targetClass.getSuperclass() != null)
        return findField(targetClass.getSuperclass(), fieldName);
    else
...