Here you can find the source of findFieldInClass(Class> clazz, String fieldName)
private static Field findFieldInClass(Class<?> clazz, String fieldName) throws NoSuchFieldException
//package com.java2s; /******************************************************************************* * Copyright (c) 2014 Salesforce.com, inc.. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * //www . j a v a 2 s . co m * Contributors: * Salesforce.com, inc. - initial API and implementation ******************************************************************************/ import java.lang.reflect.Field; public class Main { @SuppressWarnings({ "unchecked" }) public static <T> T findFieldInClass(Object instance, String fieldName) throws NoSuchFieldException { Field field = findFieldInClass(instance.getClass(), fieldName); field.setAccessible(true); try { return (T) field.get(instance); } catch (IllegalAccessException e) { throw new NoSuchFieldException(); } } private static Field findFieldInClass(Class<?> clazz, String fieldName) throws NoSuchFieldException { try { return clazz.getDeclaredField(fieldName); } catch (NoSuchFieldException e) { // if we don't have a superclass, continue if (clazz.getSuperclass().equals(Object.class)) { throw e; } // check in the superclass return findFieldInClass(clazz.getSuperclass(), fieldName); } } }