java.lang.class学习2-获取构造方法的进程
java.lang.class学习2--获取构造方法的过程,
private Constructor<T> getConstructor0(Class[] parameterTypes, int which) throws NoSuchMethodException { Constructor[] constructors = privateGetDeclaredConstructors((which == Member.PUBLIC)); //获取public的构造方法 for (int i = 0; i < constructors.length; i++) { if (arrayContentsEq(parameterTypes, constructors[i].getParameterTypes())) { return getReflectionFactory().copyConstructor(constructors[i]); //返回构造方法的对象 } } throw new NoSuchMethodException(getName() + ".<init>" + argumentTypesToString(parameterTypes)); } // // Other helpers and base implementation // private static boolean arrayContentsEq(Object[] a1, Object[] a2) { if (a1 == null) {//比较构造方法的参数 return a2 == null || a2.length == 0; } if (a2 == null) { return a1.length == 0; } if (a1.length != a2.length) { return false; } for (int i = 0; i < a1.length; i++) { if (a1[i] != a2[i]) { return false; } } return true; } // // // java.lang.reflect.Constructor handling // // // Returns an array of "root" constructors. These Constructor // objects must NOT be propagated to the outside world, but must // instead be copied via ReflectionFactory.copyConstructor. private Constructor[] privateGetDeclaredConstructors(boolean publicOnly) { checkInitted(); Constructor[] res = null; if (useCaches) { clearCachesOnClassRedefinition(); if (publicOnly) { if (publicConstructors != null) { res = (Constructor[]) publicConstructors.get(); } } else { if (declaredConstructors != null) { res = (Constructor[]) declaredConstructors.get(); } } if (res != null) return res; } // No cached value available; request value from VM if (isInterface()) { res = new Constructor[0]; } else { res = getDeclaredConstructors0(publicOnly); //本地方法,获取声明的构造方法 } if (useCaches) { if (publicOnly) { publicConstructors = new SoftReference(res); } else { declaredConstructors = new SoftReference(res); } } return res; }