Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

import java.lang.reflect.Method;
import java.util.HashMap;

public class Main {
    /**
     * getMethodNameList
     * @param theClass
     * @return
     */
    public final static HashMap<String, Integer> getMethodNameList(Class<?> theClass) {
        // Method - Number of params
        HashMap<String, Integer> methodsAndParams = new HashMap<String, Integer>();
        // Get the class name
        String className = theClass.getCanonicalName();

        Method[] classMethods = theClass.getDeclaredMethods();
        String currMethodCanonicalName = "";
        String currMethodName = "";
        int currMethodNumberParams = 0;
        for (int i = 0; i < classMethods.length; i++) {
            currMethodCanonicalName = classMethods[i].toString();

            // Get method name only
            String methodString = currMethodCanonicalName.substring(currMethodCanonicalName.lastIndexOf(className),
                    currMethodCanonicalName.indexOf('('));
            currMethodName = methodString.substring(methodString.lastIndexOf('.') + 1, methodString.length());

            // Get method number of params
            String params = currMethodCanonicalName.substring(currMethodCanonicalName.indexOf('(') + 1,
                    currMethodCanonicalName.lastIndexOf(')'));
            String theParams[] = params.split(",");
            currMethodNumberParams = theParams.length;

            methodsAndParams.put(currMethodName, currMethodNumberParams);
        }

        /**
        for (String name: methodsAndParams.keySet()){
           String key =name.toString();
           String value = methodsAndParams.get(name).toString();  
           System.out.println(key + " " + value);  
        } 
         */

        return methodsAndParams;

    }
}