1.1 getMethod
public Method getMethod(String name, Class<?>... parameterTypes)throws NoSuchMethodException, SecurityException {checkMemberAccess(Member.PUBLIC, Reflection.getCallerClass(), true);Method method = getMethod0(name, parameterTypes, true);if (method == null) {throw new NoSuchMethodException(getName() + "." + name + argumentTypesToString(parameterTypes));}return method;}
1.1.1 getMethod0
private Method getMethod0(String name, Class<?>[] parameterTypes, boolean includeStaticMethods) {MethodArray interfaceCandidates = new MethodArray(2);Method res = privateGetMethodRecursive(name, parameterTypes, includeStaticMethods, interfaceCandidates);if (res != null)return res;// Not found on class or superclass directlyinterfaceCandidates.removeLessSpecifics();return interfaceCandidates.getFirst(); // may be null}
1.1.2 privateGetMethodRecursive
递归调用此类或者是超类或接口
private Method privateGetMethodRecursive(String name,Class<?>[] parameterTypes,boolean includeStaticMethods,MethodArray allInterfaceCandidates) {if ((res = searchMethods(privateGetDeclaredMethods(true),name,parameterTypes)) != null) {if (includeStaticMethods || !Modifier.isStatic(res.getModifiers()))return res;}// Search superclass's methodsif (!isInterface()) {Class<? super T> c = getSuperclass();if (c != null) {if ((res = c.getMethod0(name, parameterTypes, true)) != null) {return res;}}}// Search superinterfaces' methodsClass<?>[] interfaces = getInterfaces();for (Class<?> c : interfaces)if ((res = c.getMethod0(name, parameterTypes, false)) != null)allInterfaceCandidates.add(res);// Not foundreturn null;}
1.1.3 privateGetDeclaredMethods
从缓存或JVM中获取该Class中申明的方法列表
private Method[] privateGetDeclaredMethods(boolean publicOnly) {checkInitted();Method[] res;ReflectionData<T> rd = reflectionData();if (rd != null) {res = publicOnly ? rd.declaredPublicMethods : rd.declaredMethods;if (res != null) return res;}// No cached value available; request value from VMres = Reflection.filterMethods(this, getDeclaredMethods0(publicOnly));if (rd != null) {if (publicOnly) {rd.declaredPublicMethods = res;} else {rd.declaredMethods = res;}}return res;}
1.1.4 reflectionData
private ReflectionData<T> reflectionData() {SoftReference<ReflectionData<T>> reflectionData = this.reflectionData;int classRedefinedCount = this.classRedefinedCount;ReflectionData<T> rd;if (useCaches &&reflectionData != null &&(rd = reflectionData.get()) != null &&rd.redefinedCount == classRedefinedCount) {return rd;}// else no SoftReference or cleared SoftReference or stale ReflectionData// -> create and replace new instancereturn newReflectionData(reflectionData, classRedefinedCount);}
reflectionData对象是SoftReference类型的,说明在内存紧张时可能会被回收,如果被回收了,再调用反射方法,则会新建此对象
1.1.5 searchMethods
private static Method searchMethods(Method[] methods,String name,Class<?>[] parameterTypes){Method res = null;String internedName = name.intern();for (int i = 0; i < methods.length; i++) {Method m = methods[i];if (m.getName() == internedName&& arrayContentsEq(parameterTypes, m.getParameterTypes())&& (res == null|| res.getReturnType().isAssignableFrom(m.getReturnType())))res = m;}return (res == null ? res : getReflectionFactory().copyMethod(res));}
1.2 getDeclaredMethod
public Method getDeclaredMethod(String name, Class<?>... parameterTypes)throws NoSuchMethodException, SecurityException {checkMemberAccess(Member.DECLARED, Reflection.getCallerClass(), true);Method method = searchMethods(privateGetDeclaredMethods(false), name, parameterTypes);if (method == null) {throw new NoSuchMethodException(getName() + "." + name + argumentTypesToString(parameterTypes));}return method;}
privateGetDeclaredMethods(false)入参传false,其他和getMethod()基本一致
