describe and output information about Class Or Interface - Android java.lang.reflect

Android examples for java.lang.reflect:Class

Description

describe and output information about Class Or Interface

Demo Code


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

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

import android.util.Log;

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

    static void describeClassOrInterface(Class<?> className, String name) {
        displayModifiers(className.getModifiers());
        displayFields(className.getDeclaredFields());
        displayMethods(className.getDeclaredMethods());

        if (className.isInterface()) {
            Log.d(TAG, "Interface: " + name);
        } else {/*  w w w  .  j ava  2s . c om*/
            Log.d(TAG, "Class: " + name);
            displayInterfaces(className.getInterfaces());
            displayConstructors(className.getDeclaredConstructors());
        }
    }

    static void displayModifiers(int m) {
        Log.d(TAG, "Modifiers: " + Modifier.toString(m));
    }

    static void displayFields(Field[] fields) {
        if (fields.length > 0) {
            Log.d(TAG, "Fields: ");
            for (int i = 0; i < fields.length; ++i)
                Log.d(TAG, fields[i].toString());
        }
    }

    static void displayMethods(Method[] methods) {
        if (methods.length > 0) {
            Log.d(TAG, "Methods: ");
            for (int i = 0; i < methods.length; ++i)
                Log.d(TAG, methods[i].toString());
        }
    }

    static void displayInterfaces(Class<?>[] interfaces) {
        if (interfaces.length > 0) {
            Log.d(TAG, "Interfaces: ");
            for (int i = 0; i < interfaces.length; ++i)
                Log.d("", interfaces[i].getName());
        }
    }

    static void displayConstructors(Constructor<?>[] constructors) {
        if (constructors.length > 0) {
            Log.d(TAG, "Constructors: ");
            for (int i = 0; i < constructors.length; ++i)
                Log.d(TAG, constructors[i].toString());
        }
    }
}

Related Tutorials