Java tutorial
//package com.java2s; /******************************************************************************* * * Pentaho Big Data * * Copyright (C) 2002-2013 by Pentaho : http://www.pentaho.com * ******************************************************************************* * * 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 { /** * Finds a method in the given class or any super class with the name {@code prefix + methodName} that accepts 0 * parameters. * * @param aClass * Class to search for method in * @param methodName * Camelcase'd method name to search for with any of the provided prefixes * @param parameterTypes * The parameter types the method signature must match. * @param prefixes * Prefixes to prepend to {@code methodName} when searching for method names, e.g. "get", "is" * @return The first method found to match the format {@code prefix + methodName} */ public static Method findMethod(Class<?> aClass, String methodName, Class<?>[] parameterTypes, String... prefixes) { for (String prefix : prefixes) { try { return aClass.getDeclaredMethod(prefix + methodName, parameterTypes); } catch (NoSuchMethodException ex) { // ignore, continue searching prefixes } } // If no method found with any prefixes search the super class aClass = aClass.getSuperclass(); return aClass == null ? null : findMethod(aClass, methodName, parameterTypes, prefixes); } }