get Is And Get Method Value - Java Reflection

Java examples for Reflection:Method

Description

get Is And Get Method Value

Demo Code


//package com.java2s;

import java.lang.reflect.Method;

public class Main {
    public static void main(String[] argv) throws Exception {
        Class c = String.class;
        System.out.println(getIsAndGetMethodValue(c));
    }/* ww  w.  j a  va  2s .  c  o m*/

    public static String getIsAndGetMethodValue(Class c) {

        StringBuffer sb = new StringBuffer();
        Method[] methods = c.getMethods();
        sb.append("[\n");
        try {
            Object object = c.newInstance();
            for (int i = 0; i < methods.length; i++) {
                if (methods[i].getName().startsWith("is")
                        || methods[i].getName().startsWith("get")
                        || methods[i].getName().startsWith("do")) {
                    sb.append("\t" + methods[i].getName() + " : "
                            + methods[i].invoke(object) + "\n");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        sb.append("]");
        return sb.toString();
    }
}

Related Tutorials