given an object, its class, and a fieldName, return the value of that field for the object because there's a lot of stuff you can set in android, but you can't get it. - Android java.lang.reflect

Android examples for java.lang.reflect:Field Value

Description

given an object, its class, and a fieldName, return the value of that field for the object because there's a lot of stuff you can set in android, but you can't get it.

Demo Code

/**//from  w w  w.  j  a va 2s. c om
 * utility functions to extract and set fields in objects using java reflection
 * @author Matthew
 * Copyright (c) 2013 Visible Automation LLC.  All Rights Reserved.
 *
 */
//package com.java2s;
import java.lang.reflect.Field;

public class Main {
    /**
     * given an object, its class, and a fieldName, return the value of that field for the object
     * because there's a lot of stuff you can set in android, but you can't get it.
     * IMPORTANT NOTE: The desired field must be a member of the specified class, not a class that it derives from. 
     * TODO: Modify this function to iterate up the class hierarchy to find the field.
     * @param o our intended victim
     * @param c object class (proletariat, bourgeois, or plutocrat)
     * @param fieldName name of the field (it better match)
     * @return
     * @throws NoSuchFieldException the field didn't match anything the class had
     * @throws IllegalAccessException I hope this never happens
     */
    public static Object getFieldValue(Object o, Class c, String fieldName)
            throws NoSuchFieldException, IllegalAccessException {
        Field field = c.getDeclaredField(fieldName);
        field.setAccessible(true);
        return field.get(o);
    }
}

Related Tutorials