Here you can find the source of getMethodFromClass(Class> cls, String methodName, Class> argClass, boolean onlyProtectedAndHigher)
Parameter | Description |
---|---|
cls | The class who should have (or inherit) the method |
methodName | The name of the method |
argClass | If provided, the type of the sole argument to the method. If null, no argument is assumed. |
onlyProtectedAndHigher | If true, we will ignore private methods in the superclasses. |
private static Method getMethodFromClass(Class<?> cls, String methodName, Class<?> argClass, boolean onlyProtectedAndHigher)
//package com.java2s; /*/* w ww .j a v a2 s . com*/ * The contents of this file are subject to the Automated Business Logic Public License Version 1.0 (the "License"), * which is derived from the Mozilla Public License version 1.1. You may not use this file except in compliance with the License. * You may obtain a copy of the License at http://www.automatedbusinesslogic.com/license/public-license * * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, * either express or implied. See the License for the specific language governing rights and limitations under the License. */ import java.lang.reflect.Method; import java.lang.reflect.Modifier; public class Main { /** * Get the method with the given name from the given class, provided that it takes one argument * of the provided type. * @param cls The class who should have (or inherit) the method * @param methodName The name of the method * @param argClass If provided, the type of the sole argument to the method. If null, no argument is assumed. * @param onlyProtectedAndHigher If true, we will ignore private methods in the superclasses. * @return The method if found, otherwise null. */ private static Method getMethodFromClass(Class<?> cls, String methodName, Class<?> argClass, boolean onlyProtectedAndHigher) { Method[] allMethods = cls.getDeclaredMethods(); for (Method meth : allMethods) { if (!meth.getName().equals(methodName)) continue; if (onlyProtectedAndHigher) { int modifiers = meth.getModifiers(); if (Modifier.isPrivate(modifiers)) continue; } if (argClass != null) { Class<?>[] paramTypes = meth.getParameterTypes(); if (paramTypes.length != 1) continue; Class<?> genericType = getGenericType(paramTypes[0]); if (!genericType.isAssignableFrom(argClass)) continue; } // Note that if we're trying to set a value to null, we obviously cannot check the // signature for overloading, and therefore we'll return the first method which takes // one parameter. I think that's not that unreasonable, but it could conceivably break // if someone does funny things with their bean. return meth; } return null; } /** * Unfortunately, int.class.isAssignableFrom(Integer.class) returns false, so we need * to convert primitive types to their class equivalent. * @param cls Any class * @return If the class is a primitive class (e.g. int.class, boolean.class, etc...) * return the class equivalent (e.g. Integer.class, Boolean.class, etc...), otherwise * return the given class. */ private static Class<?> getGenericType(Class<?> cls) { if (cls.equals(byte.class)) return Byte.class; if (cls.equals(short.class)) return Short.class; if (cls.equals(int.class)) return Integer.class; if (cls.equals(long.class)) return Long.class; if (cls.equals(float.class)) return Float.class; if (cls.equals(double.class)) return Double.class; if (cls.equals(boolean.class)) return Boolean.class; if (cls.equals(char.class)) return Character.class; return cls; } }