Java Reflection Field Find findField(Class c, Class fieldtype)

Here you can find the source of findField(Class c, Class fieldtype)

Description

find Field

License

Open Source License

Declaration

public static Field findField(Class c, Class fieldtype) throws Exception 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.lang.reflect.Field;

public class Main {
    public static Field findField(Class c, Class fieldtype) throws Exception {
        while (c != null && c != Object.class) {
            for (Field field : c.getDeclaredFields()) {
                if (field.getType() == fieldtype) {
                    return field;
                }/*from  w  w  w.  j a va 2s .  c  o  m*/
            }
            c = c.getSuperclass();
        }
        return null;
    }

    public static Field findField(Class c, String name) throws Exception {
        while (c != null && c != Object.class) {
            Field f = c.getDeclaredField(name);
            if (f != null) {
                return f;
            }
            c = c.getSuperclass();
        }
        return null;
    }
}

Related

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