Java Reflection Method Get from Object getMethod(final String methodName, final Object obj, final Class... argTypes)

Here you can find the source of getMethod(final String methodName, final Object obj, final Class... argTypes)

Description

Returns a method (public or private) object for the given methodName if it exists.

License

Apache License

Parameter

Parameter Description
methodName the name of the method
obj the target object
argTypes the argument class types

Return

a object representing the method

Declaration

public static Method getMethod(final String methodName, final Object obj, final Class<?>... argTypes) 

Method Source Code

//package com.java2s;
/**//  w w  w .  j  ava  2  s  .  c o m
 * Copyright (C) [2013] [The FURTHeR Project]
 *
 * 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.Array;

import java.lang.reflect.GenericArrayType;

import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

public class Main {
    /**
     * Returns a method (public or private) object for the given methodName if it exists.
     * Note, this method wraps all exceptions as {@link RuntimeException}'s and it is the
     * responsibility of the caller to decide whether or not to catch such exception, by
     * default it is an unchecked exception.
     *
     * @param methodName
     *            the name of the method
     * @param obj
     *            the target object
     * @param argTypes
     *            the argument class types
     * @return a {@link Method} object representing the method
     */
    public static Method getMethod(final String methodName, final Object obj, final Class<?>... argTypes) {
        Method method = null;
        try {
            method = obj.getClass().getDeclaredMethod(methodName, argTypes);
            method.setAccessible(true);
        } catch (final SecurityException e) {
            throw new RuntimeException(e);
        } catch (final NoSuchMethodException e) {
            throw new RuntimeException(e);
        }

        return method;
    }

    /**
     * Get the underlying class for a type, or <code>null</code> if the type is a variable
     * type.
     *
     * @param type
     *            the type
     * @return the underlying class
     * @author Ian Robertson
     * @see http://www.artima.com/weblogs/viewpost.jsp?thread=208860
     */
    public static Class<?> getClass(final Type type) {
        if (instanceOf(type, Class.class)) {
            return (Class<?>) type;
        } else if (instanceOf(type, ParameterizedType.class)) {
            return getClass(((ParameterizedType) type).getRawType());
        } else if (instanceOf(type, GenericArrayType.class)) {
            final Type componentType = ((GenericArrayType) type).getGenericComponentType();
            final Class<?> componentClass = getClass(componentType);
            if (componentClass != null) {
                return Array.newInstance(componentClass, 0).getClass();
            }
            return null;
        } else {
            return null;
        }
    }

    /**
     * Check if an object is an instance of a type.
     *
     * @param o
     *            object to verify
     * @param type
     *            a type
     * @return <code>true</code> if and only if <code>o</code> is an instance of
     *         <code>type</code>. If <code>type</code> is <code>null</code>, returns
     *         <code>false</code>
     *
     */
    public static boolean instanceOf(final Object o, final Class<?> type) {
        return ((o == null) || (type == null)) ? false : type.isAssignableFrom(o.getClass());
    }

    /**
     * Leads to OSGi classpath problems.
     *
     * @param <T>
     * @param fullyQualifiedClass
     * @return
     */
    public static <T> T newInstance(final String fullyQualifiedClass) {
        try {
            return (T) Thread.currentThread().getContextClassLoader().loadClass(fullyQualifiedClass).newInstance();
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * Loads the given class using the context class loader
     * 
     * @return
     */
    public static Class<?> loadClass(final String clazz) {
        Class<?> clazzObject;
        try {
            clazzObject = Thread.currentThread().getContextClassLoader().loadClass(clazz);
        } catch (final ClassNotFoundException e) {
            throw new RuntimeException("Unable to load " + clazz, e);
        }
        return clazzObject;
    }
}

Related

  1. getMethod(final Object object, final String methodName, final Class... parameterClass)
  2. getMethod(final Object object, final String methodName, final Object... arguments)
  3. getMethod(final Object object, final String methodName, final Object... arguments)
  4. getMethod(final Object object, String methodName, final Class[] argTypes)
  5. getMethod(final Object target, final String methodName, final Class... argumentTypes)
  6. getMethod(Object bean, String propertyName)
  7. getMethod(Object instance, String methodName, Class... argsClass)
  8. getMethod(Object instance, String methodName, Class[] parameters)
  9. getMethod(Object o, String methodName)