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

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

Description

searches for declared field in given class and all superclasses

License

Open Source License

Parameter

Parameter Description
clazz a parameter
name a parameter

Return

or null

Declaration

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

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2009, 2014 Xored Software Inc and others.
 * 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
 *
 * Contributors://from w  w  w.ja va  2  s.com
 *     Xored Software Inc - initial API and implementation and/or initial documentation
 *******************************************************************************/

import java.lang.reflect.Field;

public class Main {
    /**
     * searches for declared field in given class and all superclasses
     * 
     * @param clazz
     * @param name
     * @return {@link Field} or <code>null</code>
     */
    public static Field findField(Class<?> clazz, String name) {
        if (clazz == null) {
            return null;
        }
        try {
            return clazz.getDeclaredField(name);
        } catch (SecurityException e) {
            return null;
        } catch (NoSuchFieldException e) {
            return findField(clazz.getSuperclass(), name);
        }
    }
}

Related

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