Java Reflection Method Get from Object getMethod(Object obj, String methodName)

Here you can find the source of getMethod(Object obj, String methodName)

Description

get Method

License

LGPL

Declaration

static Method getMethod(Object obj, String methodName) 

Method Source Code


//package com.java2s;
//License from project: LGPL 

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

public class Main {
    static Method getMethod(Object obj, String methodName) {

        Method[] ms = obj.getClass().getMethods();

        for (Method m : ms) {
            if (m.getName().equals(methodName)) {
                return m;
            }/*ww w  .j  a  v  a 2 s.  c  o m*/
        }

        if (methodName.contains("_")) {

            String[] nameSet = methodName.split("_");
            StringBuilder name = new StringBuilder(nameSet[0]);
            for (int i = 1; i < nameSet.length; i++) {
                String item = nameSet[i];
                if (item.length() > 0) {
                    name.append(item.substring(0, 1).toUpperCase(Locale.ENGLISH));
                    name.append(item.substring(1));
                }
            }

            return getMethod(obj, name.toString());
        }

        return null;
    }
}

Related

  1. getMethod(Object o, String methodName)
  2. getMethod(Object o, String methodName, Class[] args)
  3. getMethod(Object o, String methodName, Class[] paramTypes)
  4. getMethod(Object obj, Class[] paramTypes, String methodName)
  5. getMethod(Object obj, String fieldName)
  6. getMethod(Object obj, String methodName, Class paramClass)
  7. getMethod(Object obj, String methodName, Class... parameterTypes)
  8. getMethod(Object obj, String name, Class... types)
  9. getMethod(Object object, String methodName, Class... arguments)