Here you can find the source of findField(Class> clazz, String fieldName)
public static Field findField(Class<?> clazz, String fieldName)
//package com.java2s; import java.lang.reflect.Field; import java.util.HashMap; public class Main { private static final HashMap<String, Field> fieldCache = new HashMap<String, Field>(); /**//from w w w.ja v a2 s .c om * Look up a field in a class and set it to accessible. The result is cached. * If the field was not found, a {@link NoSuchFieldError} will be thrown. */ public static Field findField(Class<?> clazz, String fieldName) { StringBuilder sb = new StringBuilder(clazz.getName()); sb.append('#'); sb.append(fieldName); String fullFieldName = sb.toString(); if (fieldCache.containsKey(fullFieldName)) { Field field = fieldCache.get(fullFieldName); if (field == null) throw new NoSuchFieldError(fullFieldName); return field; } try { Field field = findFieldRecursiveImpl(clazz, fieldName); field.setAccessible(true); fieldCache.put(fullFieldName, field); return field; } catch (NoSuchFieldException e) { fieldCache.put(fullFieldName, null); throw new NoSuchFieldError(fullFieldName); } } private static Field findFieldRecursiveImpl(Class<?> clazz, String fieldName) throws NoSuchFieldException { try { return clazz.getDeclaredField(fieldName); } catch (NoSuchFieldException e) { while (true) { clazz = clazz.getSuperclass(); if (clazz == null || clazz.equals(Object.class)) break; try { return clazz.getDeclaredField(fieldName); } catch (NoSuchFieldException ignored) { } } throw e; } } }