Java Reflection Field Find findField(Class aClass, String aFieldName)

Here you can find the source of findField(Class aClass, String aFieldName)

Description

Attempts to find a public field on the given class, searching its superclasses if necessary.

License

Open Source License

Parameter

Parameter Description
aClass the class.
aFieldName the field name to search for.

Return

the Field, or null if no field can be found.

Declaration

public static Field findField(Class aClass, String aFieldName) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright 2000, 2006 Visual Systems Corporation.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License version 2
 * which accompanies this distribution in a file named "COPYING".
 * //  w  w  w  .j  a v a 2 s .  c  om
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *      
 * This program 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 General Public License for more details.
 *      
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *******************************************************************************/

import java.lang.reflect.Field;

public class Main {
    /**
     * Attempts to find a public field on the given class, searching its superclasses if necessary.
     * 
     * @param aClass the class.
     * @param aFieldName the field name to search for.
     * 
     * @return the Field, or null if no field can be found.
     */
    public static Field findField(Class aClass, String aFieldName) {
        try {
            return aClass.getField(aFieldName);
        } catch (NoSuchFieldException e) {
            // Ignore - return null.
        }

        return null;
    }
}

Related

  1. findField(@Nonnull Class clazz, @Nonnull String name, Class type)
  2. findField(Class c, Class fieldtype)
  3. findField(Class c, String fieldName)
  4. findField(Class classBeFind, String name)
  5. findField(Class classType, String fieldName, Class fieldType)