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 type, Class annotationClass)
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);
FieldfindField(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);
...
ObjectfindField(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;
FieldfindField(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);
FieldfindField(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);
FieldfindField(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) {
...
FieldfindField(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;
FieldfindField(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);
FieldfindField(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);
...
FieldfindField(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);
...