Java Reflection Field Find findField(Class clazz, String name)

Here you can find the source of findField(Class clazz, String name)

Description

find Field

License

Open Source License

Declaration

public static Field findField(Class<?> clazz, String name) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2014,2015 Hideki Yatomi
 * 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
 ******************************************************************************/

import java.lang.reflect.*;

public class Main {
    public static Field findField(Class<?> clazz, String name) {
        while (clazz != null) {
            Field f;//w  w  w  . j  a  va2 s  .c  o m
            try {
                f = clazz.getField(name);
                if (f != null)
                    return f;
            } catch (NoSuchFieldException e) {
                // not found, try next
            }

            try {
                f = clazz.getDeclaredField(name);
                if (f != null)
                    return f;
            } catch (NoSuchFieldException e) {
                // not found, try next
            }

            clazz = clazz.getSuperclass();
        }
        return null;
    }
}

Related

  1. findField(Class clazz, String name)
  2. findField(Class clazz, String name)
  3. findField(Class clazz, String name)
  4. findField(Class clazz, String name)
  5. findField(Class clazz, String name)
  6. findField(Class clazz, String name)
  7. findField(Class clazz, String name)
  8. findField(Class clazz, String propName)
  9. findField(Class clazz, String targetName, Class targetType, boolean checkInheritance, boolean strictType)