get Field from Class by field name - Android java.lang.reflect

Android examples for java.lang.reflect:Field Name

Description

get Field from Class by field name

Demo Code


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

public class Main {
    public static Field getField(Class cls, String field) {
        try {// ww w .  j  a v  a 2s  . c  om
            Field fld = cls.getDeclaredField(field);
            if (fld != null)
                fld.setAccessible(true);
            return fld;
        } catch (Exception e) {
            //if(DEBUG)
            //   Log.e(TAG,e.getMessage(), e);
            return null;
        }
    }

    public static Field getField(String clzz, String field) {
        try {
            Class clazz = Class.forName(clzz);
            if (clazz != null) {
                return getField(clazz, field);
            }
        } catch (Exception e) {

        }
        return null;
    }
}

Related Tutorials