get Declared Field by field name - Android java.lang.reflect

Android examples for java.lang.reflect:Field Name

Description

get Declared Field by field name

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   w ww  .j  av a 2s  .  c o  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