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

Android examples for java.lang.reflect:Field Value

Description

set Super Class Reflect Field Value

Demo Code


//package com.java2s;

import java.lang.reflect.Field;

public class Main {
    public static void setSuperClassReflectFieldValue(Object obj,
            String field, Object value) {
        try {/*from  w ww  . ja  va  2s  .  c  o m*/
            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);
                f.set(obj, value);
            }
        } catch (Exception e) {
        }
    }
}

Related Tutorials