get Field Value as String - Android java.lang.reflect

Android examples for java.lang.reflect:Field Value

Description

get Field Value as String

Demo Code


//package com.java2s;
import java.lang.reflect.Field;

public class Main {
    public static String getFieldValueString(Object obj, Field field) {

        Object value = getFieldValueObject(obj, field);

        if (value != null) {
            return value.toString();
        }/*from  w w  w  . j  a v a  2  s.co  m*/

        return "";
    }

    public static Object getFieldValueObject(Object obj, Field field) {

        try {
            return field.get(obj);
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        return null;
    }
}

Related Tutorials