Java Reflection Field Find findField(final Class klaz, final String fieldName)

Here you can find the source of findField(final Class klaz, final String fieldName)

Description

find Field

License

BSD License

Declaration

public static Field findField(final Class<?> klaz, final String fieldName) 

Method Source Code

//package com.java2s;
/**// w  w w . j ava 2 s  .  c om
 * Copyright (C) 2010-2013 Andrei Pozolotin <Andrei.Pozolotin@gmail.com>
 *
 * All rights reserved. Licensed under the OSI BSD License.
 *
 * http://www.opensource.org/licenses/bsd-license.php
 */

import java.lang.reflect.Field;

public class Main {
    public static Field findField(final Class<?> klaz, final String fieldName) {

        Class<?> type = klaz;

        do {

            for (final Field field : type.getDeclaredFields()) {

                final String name = field.getName();

                if (!name.equals(fieldName)) {
                    continue;
                }

                field.setAccessible(true);

                return field;

            }

            type = type.getSuperclass();

        } while (klaz != null);

        return null;

    }
}

Related

  1. findField(final Class clazz, final String name)
  2. findField(final Class clazz, final String name, final Class type)
  3. findField(final Class cls, final String fieldName)
  4. findField(final Class currentClass, final String fieldName)
  5. findField(final Class declaringClass, final String fieldName)
  6. findField(final Object instance, final String name)
  7. findField(final Object src, final String fieldName)
  8. findField(final String className, final String fieldName)
  9. findField(Object container, String memberName)