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

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

Description

find Field

License

Apache License

Declaration

private static Field findField(final Class<?> clazz, final String name) throws NoSuchFieldException 

Method Source Code

//package com.java2s;
/**/*from   ww  w  .  j av  a  2s  . com*/
 * Copyright 2015 DuraSpace, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.lang.reflect.Field;

public class Main {
    private static Field findField(final Class<?> clazz, final String name) throws NoSuchFieldException {
        for (final Field f : clazz.getDeclaredFields()) {
            if (f.getName().equals(name)) {
                return f;
            }
        }
        if (clazz.getSuperclass() == null) {
            throw new NoSuchFieldException("Field " + name + " could not be found");
        }
        return findField(clazz.getSuperclass(), name);
    }
}

Related

  1. findField(Class pClass, String fieldName)
  2. findField(Class targetClass, String fieldName)
  3. findField(Class type, Class annotationClass)
  4. findField(Class type, String fieldName)
  5. findField(final Class aClass, final String fieldName)
  6. findField(final Class clazz, final String name)
  7. findField(final Class clazz, final String name, final Class type)
  8. findField(final Class cls, final String fieldName)
  9. findField(final Class currentClass, final String fieldName)