print Methods For Class - Java Reflection

Java examples for Reflection:Method

Description

print Methods For Class

Demo Code


//package com.java2s;

import java.lang.reflect.Method;

public class Main {
    public static void main(String[] argv) throws Exception {
        String clazz = "java2s.com";
        printMethodsForClass(clazz);// ww  w .  j  a v a 2  s.  c  o  m
    }

    public static void printMethodsForClass(final String clazz) {
        try {
            final Class<?> util = Class.forName(clazz);
            System.out.println("Methods for " + clazz);
            final Method[] methods = util.getMethods();
            for (final Method method : methods) {
                String sep = "";
                final StringBuilder buf = new StringBuilder();
                final Class<?>[] types = method.getParameterTypes();
                for (int i = 0, n = types.length; i < n; ++i) {
                    buf.append(sep);
                    buf.append(types[i].getName());
                    sep = ", ";
                }
                System.out.println("\t" + method.getName() + ":"
                        + buf.toString());
            }
        } catch (final Throwable e) {
            e.printStackTrace();
        }
    }
}

Related Tutorials