Here you can find the source of getMethod(final Object target, final String methodName, final Class... argumentTypes)
Parameter | Description |
---|---|
target | Target object to check |
methodName | Name of the method being queried |
argumentTypes | Types of the method's arguments |
public static synchronized Method getMethod(final Object target, final String methodName, final Class... argumentTypes)
//package com.java2s; /**/*from w w w .jav a2s .co m*/ * A collection of small utilities for component management and reflection handling. * <p/> * <hr/> Copyright 2006-2012 Torsten Heup * <p/> * 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 * <p/> * http://www.apache.org/licenses/LICENSE-2.0 * <p/> * 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 { /** * Returns a the specified method of the target object, or null if the method does not exist. This is merely a * conveniance method to avoid reflection's exceptions. * * @param target Target object to check * @param methodName Name of the method being queried * @param argumentTypes Types of the method's arguments * @return Method instance if the method exists, false otherwise. */ public static synchronized Method getMethod(final Object target, final String methodName, final Class... argumentTypes) { return getMethod(target.getClass(), methodName, argumentTypes); } /** * Attempts to find a method with the given name and argument types on the specified class. Other than Class's * getMethod(String, Class...) method, this method will also return protected and private methods. * * @param clazz Class supposed to offer the method being searched. * @param methodName Name of the method. * @param argumentTypes The arguments found in the method signature, if any. * @return a method with the given name and argument types on the specified class, if any. */ public static synchronized Method getMethod(final Class clazz, final String methodName, final Class... argumentTypes) { try { return clazz.getMethod(methodName, argumentTypes); } catch (NoSuchMethodException e) { /* Ok, so there's no public method... */ } Class runner = clazz; while (runner != null) { try { return runner.getDeclaredMethod(methodName, argumentTypes); } catch (NoSuchMethodException e) { /* No luck here either */ } runner = runner.getSuperclass(); } /* Still no luck, means there is no suitable method */ return null; } }