Here you can find the source of getMethod(final Class> clazz, final String methodName, final boolean factoryMethod, final boolean failIfNotFound)
private static Method getMethod(final Class<?> clazz, final String methodName, final boolean factoryMethod, final boolean failIfNotFound)
//package com.java2s; /*//from ww w .j a v a 2 s . c o m * Copyright (c) 2013, FOSS Nova Software foundation (FNSF), * and individual contributors as indicated by the @author tags. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software 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 * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ import java.lang.reflect.Method; import java.lang.reflect.Modifier; public class Main { private static final Class<?>[] emptyClassArray = new Class[0]; private static Method getMethod(final Class<?> clazz, final String methodName, final boolean factoryMethod, final boolean failIfNotFound) { if (methodName == null) return null; final Method method = findMethod(clazz, methodName, failIfNotFound); if (method == null && !failIfNotFound) return null; final int methodModifiers = method.getModifiers(); if (Modifier.isAbstract(methodModifiers)) throw new UnsupportedOperationException("Method '" + methodName + "' cannot be abstract"); if (Modifier.isStatic(methodModifiers) && !factoryMethod) throw new UnsupportedOperationException("Factory method '" + methodName + "' have to be static"); final Class<?> returnType = method.getReturnType(); if (factoryMethod) { if (returnType.equals(Void.class) || returnType.equals(Void.TYPE)) { throw new UnsupportedOperationException("Factory method '" + methodName + "' cannot return void"); } } else { if (!returnType.equals(Void.class) && !returnType.equals(Void.TYPE)) { throw new UnsupportedOperationException("Method '" + methodName + "' have to return void"); } } if (method.getParameterTypes().length > 0) { throw new UnsupportedOperationException("Method '" + methodName + "' have to have no parameters"); } assertNotThrowingCheckedExceptions(method); return method; } static Method getMethod(final Class<?> clazz, final String methodName, final boolean failIfNotFound) { return getMethod(clazz, methodName, false, failIfNotFound); } private static Method findMethod(final Class<?> clazz, final String methodName, final boolean failIfNotFound) { try { return clazz.getMethod(methodName, emptyClassArray); } catch (final NoSuchMethodException e) { if (failIfNotFound) { throw new UnsupportedOperationException("Method '" + methodName + "' without parameters not found", e); } } return null; } private static void assertNotThrowingCheckedExceptions(final Method method) { final Class<?>[] exceptions = method.getExceptionTypes(); for (final Class<?> exception : exceptions) { if (Exception.class.isAssignableFrom(exception)) { throw new UnsupportedOperationException("Method '" + method.getName() + "' cannot throw checked exception '" + exception.getSimpleName() + "'"); } } } }