check If Field Is Set - Android java.lang.reflect

Android examples for java.lang.reflect:Field

Description

check If Field Is Set

Demo Code


//package com.java2s;

import android.util.Log;

public class Main {
    public static final String TAG = "ReflectionHelper";

    public static boolean checkIfFieldIsSet(Object o, String baseClass,
            String fieldName) {/*  ww  w. j  a  va 2  s. c  om*/
        java.lang.reflect.Field field;

        try {
            //TODO: cache
            Class<?> viewObj = Class.forName(baseClass);
            field = viewObj.getDeclaredField(fieldName);
            field.setAccessible(true);

            boolean ret = (field.get(o) != null);
            Log.v(TAG, o.getClass().getCanonicalName() + " > " + fieldName
                    + " FOUND | " + ((ret) ? "ACTIVE" : "NOT ACTIVE"));
            return ret;

            /* NOTA:
             * Senza log
             *       return (field.get(o) != null);
             */
        } catch (Exception e) {
            Log.e(TAG, e.toString());
        }

        Log.v(TAG, o.getClass().getCanonicalName() + " > " + fieldName
                + " NOT FOUND");

        return false;
    }
}

Related Tutorials