Java Reflection Method Get from Object getMethod(Object obj, String name, Class... types)

Here you can find the source of getMethod(Object obj, String name, Class... types)

Description

Easy way to get accessible methods from inherited children

License

Open Source License

Parameter

Parameter Description
obj The object
name name of method to pull from the project
types argument types

Exception

Parameter Description
IllegalArgumentException an exception
IllegalAccessException an exception
NoSuchMethodException an exception

Return

accessible method

Declaration

public static Method getMethod(Object obj, String name, Class... types)
        throws IllegalArgumentException, IllegalAccessException, NoSuchMethodException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2013 WPI-Suite/*from w  w  w  .  j a  va  2  s .  co m*/
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors: Team YOCO (You Only Compile Once)
 ******************************************************************************/

import java.lang.reflect.Method;

public class Main {
    /**
     * Easy way to get accessible methods from inherited children
     * @param obj The object
     * @param name name of method to pull from the project
     * @param types argument types
     * @return accessible method
     * @throws IllegalArgumentException
     * @throws IllegalAccessException
     * @throws NoSuchMethodException
     */
    public static Method getMethod(Object obj, String name, Class... types)
            throws IllegalArgumentException, IllegalAccessException, NoSuchMethodException {
        Class c = obj.getClass();
        while (c != Object.class) {
            try {
                Method m = c.getDeclaredMethod(name, types);
                m.setAccessible(true);
                return m;
            } catch (NoSuchMethodException e) {
                c = c.getSuperclass();
            }
        }
        throw new NoSuchMethodException(name);
    }
}

Related

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