get Declared Field in current class - Android java.lang.reflect

Android examples for java.lang.reflect:Field

Description

get Declared Field in current class

Demo Code


//package com.java2s;
import java.lang.reflect.Field;

public class Main {
    public static Field getDeclaredField(Object object, String fieldName) {
        Field field = null;/*from   www  .j av  a  2 s .co  m*/

        Class<?> clazz = object.getClass();

        for (; clazz != Object.class; clazz = clazz.getSuperclass()) {
            try {
                field = clazz.getDeclaredField(fieldName);
                return field;
            } catch (Exception e) {

            }
        }

        return null;
    }
}

Related Tutorials