Java Reflection Method Get from Object getMethod(final Object object, final String methodName, final Object... arguments)

Here you can find the source of getMethod(final Object object, final String methodName, final Object... arguments)

Description

get Method

License

Open Source License

Declaration

public static Object getMethod(final Object object, final String methodName, final Object... arguments)
            throws IllegalArgumentException, SecurityException, NoSuchMethodException 

Method Source Code

//package com.java2s;
/*/*  w  w  w  .j  a  v  a  2  s.c  o m*/
Copyright 2015 Google Inc. All Rights Reserved.
    
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
    
http://www.apache.org/licenses/LICENSE-2.0
    
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

import java.lang.reflect.Method;

public class Main {
    public static Method getMethod(final Object object, final String methodName, final Class<?>... arguments)
            throws IllegalArgumentException, SecurityException, NoSuchMethodException {
        final Class<?> c = object.getClass();

        final Class<?>[] parameterTypes = new Class<?>[arguments.length];

        int counter = 0;

        for (final Class<?> argument : arguments) {
            parameterTypes[counter++] = argument;
        }

        final Method method = c.getDeclaredMethod(methodName, parameterTypes);

        return method;
    }

    public static Object getMethod(final Object object, final String methodName, final Object... arguments)
            throws IllegalArgumentException, SecurityException, NoSuchMethodException {
        final Class<?> c = object.getClass();

        final Class<?>[] parameterTypes = new Class<?>[arguments.length];

        int counter = 0;

        for (final Object argument : arguments) {
            parameterTypes[counter++] = argument.getClass();
        }

        final Method method = c.getDeclaredMethod(methodName, parameterTypes);

        return method;
    }
}

Related

  1. getMethod(Class objectType, String methodName, Class... parameterTypes)
  2. getMethod(Class type, String name, Object[] args)
  3. getMethod(final Class clazz, final String methodName, final Class[] parTypes, final Object[] parameters)
  4. getMethod(final Object object, final String methodName, final Class... parameterClass)
  5. getMethod(final Object object, final String methodName, final Object... arguments)
  6. getMethod(final Object object, String methodName, final Class[] argTypes)
  7. getMethod(final Object target, final String methodName, final Class... argumentTypes)
  8. getMethod(final String methodName, final Object obj, final Class... argTypes)
  9. getMethod(Object bean, String propertyName)