Here you can find the source of getMethod(Class clazz, String methodName, Class>... parameterTypes)
Parameter | Description |
---|---|
clazz | a parameter |
public static Method getMethod(Class clazz, String methodName, Class<?>... parameterTypes) throws NoSuchMethodException
//package com.java2s; /*//from ww w. j av a 2 s .c o m * This file is part of AtlasMapper server and clients. * * Copyright (C) 2011 Australian Institute of Marine Science * * Contact: Gael Lafond <g.lafond@aims.org.au> * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ import java.lang.reflect.Method; public class Main { /** * Search the method in the class and it's parent classes. * @param clazz * @return */ public static Method getMethod(Class clazz, String methodName, Class<?>... parameterTypes) throws NoSuchMethodException { return _getMethod(null, clazz, methodName, parameterTypes); } private static Method _getMethod(NoSuchMethodException noSuchMethodException, Class clazz, String methodName, Class<?>... parameterTypes) throws NoSuchMethodException { if (clazz == null) { // The method is realy not found... throw noSuchMethodException; } Method foundMethod = null; try { foundMethod = clazz.getDeclaredMethod(methodName, parameterTypes); } catch (NoSuchMethodException ex) { // Don't panic, the method may be in the parent class if (noSuchMethodException == null) { // Keep the first exception, it's the more explicit. noSuchMethodException = ex; } } if (foundMethod == null) { foundMethod = _getMethod(noSuchMethodException, clazz.getSuperclass(), methodName, parameterTypes); } return foundMethod; } }