Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.lang.reflect.Method;

public class Main {
    public static Method[] convertStringsToMethods(Class clazz, String[] methodNames) {
        Method[] methods = new Method[methodNames.length];
        for (int i = 0; i < methods.length; i++) {
            Method m = getMethod(clazz, methodNames[i]);
            if (m == null) {
                throw new IllegalArgumentException("Not found method. methodName: " + methodNames[i]);
            }
            methods[i] = m;
        }
        return methods;
    }

    public static Method getMethod(Class clazz, String methodName) {
        Method[] methods = clazz.getDeclaredMethods();
        for (int i = 0; i < methods.length; i++) {
            if (methods[i].getName().equalsIgnoreCase(methodName)
                    || methods[i].getName().equalsIgnoreCase("get" + methodName)) {
                return methods[i];
            }
        }
        return null;
    }
}