get a field that we expect to return a boolean value - Android java.lang.reflect

Android examples for java.lang.reflect:Field Value

Description

get a field that we expect to return a boolean value

Demo Code

/**//from  w w  w .  j  av a 2  s.  c om
 * utilities to access and set fields in objects via reflection
 * @author Matthew
 * Copyright (c) 2013 Visible Automation LLC.  All Rights Reserved.
 */
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
import android.util.Log;
import android.view.View;

public class Main{
    /**
     * get a field that we expect to return a boolean value
     * @param o
     * @param c
     * @param fieldName
     * @return
     * @throws NoSuchFieldException
     * @throws IllegalAccessException
     */
    public static boolean getFieldBoolean(Object o, Class c,
            String fieldName) throws NoSuchFieldException,
            IllegalAccessException {
        Field field = c.getDeclaredField(fieldName);
        field.setAccessible(true);
        return field.getBoolean(o);
    }
    /**
     * variant which takes a Constants.Fields
     * @param o
     * @param c
     * @param fieldName
     * @return
     * @throws NoSuchFieldException
     * @throws IllegalAccessException
     */
    public static boolean getFieldBoolean(Object o, Class c,
            Constants.Fields fieldName) throws NoSuchFieldException,
            IllegalAccessException {
        Field field = c.getDeclaredField(fieldName.mName);
        field.setAccessible(true);
        return field.getBoolean(o);
    }
}

Related Tutorials