get Super Class Reflect Field Value - Android java.lang.reflect

Android examples for java.lang.reflect:Field Value

Description

get Super Class Reflect Field Value

Demo Code


//package com.java2s;

import java.lang.reflect.Field;

public class Main {
    public static Object getSuperClassReflectFieldValue(Object obj,
            String field) {//from   w w  w .  j a v  a2 s .  c om
        try {
            Field f = null;
            Class<?> curClass = obj.getClass().getSuperclass();
            for (; curClass != null;) {
                try {
                    f = curClass.getDeclaredField(field);
                    if (f != null)
                        break;
                } catch (Exception e) {
                    curClass = curClass.getSuperclass();
                }
            }
            if (f != null) {
                f.setAccessible(true);
                return f.get(obj);
            }
        } catch (Exception e) {
        }

        return null;
    }
}

Related Tutorials