Dumps a Class 's Method s and Field s as a String. - Android java.lang.reflect

Android examples for java.lang.reflect:Method

Description

Dumps a Class 's Method s and Field s as a String.

Demo Code


import java.lang.reflect.*;
import android.util.Log;

public class Main{
    public static final String TAG = ReflectionUtils.class.getSimpleName();
    /**/*ww  w.  j av  a2s  .  c  o  m*/
     * Dumps a {@link Class}'s {@link Method}s and {@link Field}s
     * as a String.
     */
    public static final String dumpClass(Class<?> mClass, Object mInstance) {
        if (mClass == null || mInstance == null)
            return null;

        String mStr = mClass.getSimpleName() + "\n\n";

        mStr += "FIELDS\n\n";

        final Field[] mFields = mClass.getDeclaredFields();

        for (final Field mField : mFields) {
            mField.setAccessible(true);

            mStr += mField.getName() + " (" + mField.getType() + ") = ";

            try {
                mStr += mField.get(mInstance).toString();
            } catch (Exception e) {
                mStr += "null";
                Log.e(TAG, "Could not get Field `" + mField.getName()
                        + "`.", e);
            }

            mStr += "\n";
        }

        mStr += "METHODS\\nn";

        // Dump all methods.

        final Method[] mMethods = mClass.getMethods();

        for (final Method mMethod : mMethods) {
            mMethod.setAccessible(true);

            mStr += mMethod.getReturnType() + " " + mMethod.getName()
                    + "() = ";

            try {
                final Object mRet = mMethod.invoke(mInstance);
                mStr += (mRet == null) ? "null" : mMethod.invoke(mInstance)
                        .toString();
            } catch (Exception e) {
                mStr += "null";
                Log.e(TAG, "Could not get Method `" + mMethod.getName()
                        + "`.", e);
            }

            mStr += "\n";
        }

        return mStr;
    }
}

Related Tutorials