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

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

Description

Find the field by name.

License

Open Source License

Parameter

Parameter Description
clazz the class to look for field
name the name

Return

field or null if not found

Declaration

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

Method Source Code

//package com.java2s;
/*// w  w  w  .j  ava  2  s. co m
* JBoss, Home of Professional Open Source
* Copyright 2006, JBoss Inc., and individual contributors as indicated
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/

import java.lang.reflect.Field;

public class Main {
    /**
     * Find the field by name.
     *
     * @param clazz the class to look for field
     * @param name the name
     * @return field or null if not found
     */
    public static Field findField(Class<?> clazz, String name) {
        if (clazz == null)
            return null;

        Field[] fields = clazz.getDeclaredFields();
        if (fields.length != 0) {
            for (Field field : fields) {
                if (field.getName().equals(name))
                    return field;
            }
        }
        return findField(clazz.getSuperclass(), name);
    }
}

Related

  1. findField(Class clazz, String fname, boolean isSearchSuperclass)
  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 name)
  9. findField(Class clazz, String name)