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

AnnotatedElementfindField0(Class clazz, String name, String methodName)
find Field
if (clazz == Object.class) {
    return null;
try {
    return clazz.getDeclaredField(name);
} catch (NoSuchFieldException e) {
    try {
        return clazz.getDeclaredMethod(methodName);
...
FieldfindFieldByName(Class owner, String name)
find Field By Name
try {
    return owner.getDeclaredField(name);
} catch (NoSuchFieldException e) {
    return findFieldByName(owner.getSuperclass(), name);
FieldfindFieldEx(Class type, Class annotationClass)
find Field Ex
for (Field field : type.getDeclaredFields()) {
    if (field.getAnnotation(annotationClass) != null) {
        return field;
throw new IllegalArgumentException("Specific ".concat(type.getName()).concat(" must have annotation ")
        .concat(annotationClass.getName()));
FieldfindFieldFromClassHierarchy(Class clazz, String fieldName)
Finds the requested field from the class hierarchy.
Class<?> current = clazz;
do {
    try {
        return current.getDeclaredField(fieldName);
    } catch (Exception e) {
} while ((current = current.getSuperclass()) != null);
throw new NoSuchFieldException(String.format("No field found with the field name %s", fieldName));
...
FieldfindFieldFromGetter(Class clazz, Method method)
Find field from getter.
String fieldName = getFieldNameFromGetter(method.getName());
Field found = findField(clazz, fieldName);
return found;
FieldfindFieldIn(Class type, String name)
find Field In
Field foundField = null;
Class<?> currentClass = type;
while (currentClass != null && currentClass != Object.class && foundField == null) {
    for (Field currentField : currentClass.getDeclaredFields()) {
        if (currentField.getName().equals(name)) {
            foundField = currentField;
            break;
    currentClass = currentClass.getSuperclass();
return foundField;
FieldfindFieldInClass(Class clazz, String fieldName)
find Field In Class
try {
    return clazz.getDeclaredField(fieldName);
catch (NoSuchFieldException e) {
    if (clazz.getSuperclass().equals(Object.class)) {
        throw e;
    return findFieldInClass(clazz.getSuperclass(), fieldName);
...
FieldfindFieldInClassHierarchy(Class clazz, String fieldName)
find Field In Class Hierarchy
Field field = getDeclaredField(clazz, fieldName);
while (field == null && clazz != Object.class) {
    clazz = clazz.getSuperclass();
    field = getDeclaredField(clazz, fieldName);
return field;
FieldfindFieldIncludeSuperclass(String fieldName, Class clazz)
find Field Include Superclass
Field retValue = null;
try {
    retValue = clazz.getDeclaredField(fieldName);
} catch (NoSuchFieldException e) {
    clazz = clazz.getSuperclass();
    if (clazz != null) {
        retValue = findFieldIncludeSuperclass(fieldName, clazz);
return retValue;
voidfindFieldInternal(Class currentClass, Class annotation, Set fields)
find Field Internal
for (Field field : currentClass.getDeclaredFields()) {
    if (isAnnotationListed(annotation, field.getDeclaredAnnotations())) {
        fields.add(field);