print All Fields from Class via reflection - Android java.lang.reflect

Android examples for java.lang.reflect:Class

Description

print All Fields from Class via reflection

Demo Code

/*/*w  ww.ja v  a2s. c  o  m*/
 * ClassUtil.java
 * 
 * Avaya Inc. - Proprietary (Restricted) Solely for authorized persons having a
 * need to know pursuant to Company instructions.
 * 
 * Copyright 2013 Avaya Inc. All rights reserved. THIS IS UNPUBLISHED
 * PROPRIETARY SOURCE CODE OF Avaya Inc. The copyright notice above does not
 * evidence any actual or intended publication of such source code.
 */
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.HashMap;
import java.util.Map;
import android.util.Log;

public class Main{
    private static final String TAG = ClassUtil
            .getShortName(ClassUtil.class);
    public static void printAllFields(Object objectInstance)
            throws IllegalArgumentException, IllegalAccessException // NOSONAR
    {
        Class<? extends Object> clazz = objectInstance.getClass();

        Field fields[] = clazz.getDeclaredFields();
        for (int i = 0; i < fields.length; i++) {
            fields[i].setAccessible(true);

            String buf = String.format("Field Name: %s : %s",
                    fields[i].getName(), fields[i].get(objectInstance));

            Log.d(TAG, buf);

        }

    }
}

Related Tutorials