28 import java.lang.reflect.AnnotatedElement;
29 import java.lang.reflect.Array;
30 import java.lang.reflect.GenericArrayType;
31 import java.lang.reflect.GenericDeclaration;
32 import java.lang.reflect.Member;
33 import java.lang.reflect.Field;
34 import java.lang.reflect.Executable;
35 import java.lang.reflect.Method;
36 import java.lang.reflect.Constructor;
37 import java.lang.reflect.Modifier;
38 import java.lang.reflect.Type;
39 import java.lang.reflect.TypeVariable;
40 import java.lang.reflect.InvocationTargetException;
41 import java.lang.reflect.AnnotatedType;
42 import java.lang.ref.SoftReference;
43 import java.io.InputStream;
44 import java.io.ObjectStreamField;
45 import java.security.AccessController;
46 import java.security.PrivilegedAction;
47 import java.util.ArrayList;
48 import java.util.Arrays;
49 import java.util.Collection;
50 import java.util.HashSet;
51 import java.util.LinkedHashMap;
52 import java.util.List;
53 import java.util.Set;
54 import java.util.Map;
55 import java.util.HashMap;
56 import java.util.Objects;
57 import java.util.StringJoiner;
58 import sun.misc.Unsafe;
59 import sun.reflect.CallerSensitive;
60 import sun.reflect.ConstantPool;
61 import sun.reflect.Reflection;
62 import sun.reflect.ReflectionFactory;
63 import sun.reflect.generics.factory.CoreReflectionFactory;
64 import sun.reflect.generics.factory.GenericsFactory;
65 import sun.reflect.generics.repository.ClassRepository;
66 import sun.reflect.generics.repository.MethodRepository;
67 import sun.reflect.generics.repository.ConstructorRepository;
68 import sun.reflect.generics.scope.ClassScope;
414 @CallerSensitive
415 public T newInstance()
416 throws InstantiationException, IllegalAccessException
417 {
418 if (System.getSecurityManager() != null) {
419 checkMemberAccess(Member.PUBLIC, Reflection.getCallerClass(), false);
420 }
421
422 // NOTE: the following code may not be strictly correct under
423 // the current Java memory model.
424
425 // Constructor lookup
426 if (cachedConstructor == null) {
427 if (this == Class.class) {
428 throw new IllegalAccessException(
429 "Can not call newInstance() on the Class for java.lang.Class"
430 );
431 }
432 try {
433 Class<?>[] empty = {};
434 final Constructor<T> c = getConstructor0(empty, Member.DECLARED);
435 // Disable accessibility checks on the constructor
436 // since we have to do the security check here anyway
437 // (the stack depth is wrong for the Constructor's
438 // security check to work)
439 java.security.AccessController.doPrivileged(
440 new java.security.PrivilegedAction<Void>() {
441 public Void run() {
442 c.setAccessible(true);
443 return null;
444 }
445 });
446 cachedConstructor = c;
447 } catch (NoSuchMethodException e) {
448 throw (InstantiationException)
449 new InstantiationException(getName()).initCause(e);
450 }
451 }
452 Constructor<T> tmpConstructor = cachedConstructor;
453 // Security check (same as in java.lang.reflect.Constructor)
454 int modifiers = tmpConstructor.getModifiers();
847 *
848 * <p>If this object represents an interface, the array contains objects
849 * representing all interfaces directly extended by the interface. The
850 * order of the interface objects in the array corresponds to the order of
851 * the interface names in the {@code extends} clause of the declaration of
852 * the interface represented by this object.
853 *
854 * <p>If this object represents a class or interface that implements no
855 * interfaces, the method returns an array of length 0.
856 *
857 * <p>If this object represents a primitive type or void, the method
858 * returns an array of length 0.
859 *
860 * <p>If this {@code Class} object represents an array type, the
861 * interfaces {@code Cloneable} and {@code java.io.Serializable} are
862 * returned in that order.
863 *
864 * @return an array of interfaces directly implemented by this class
865 */
866 public Class<?>[] getInterfaces() {
867 ReflectionData<T> rd = reflectionData();
868 if (rd == null) {
869 // no cloning required
870 return getInterfaces0();
871 } else {
872 Class<?>[] interfaces = rd.interfaces;
873 if (interfaces == null) {
874 interfaces = getInterfaces0();
875 rd.interfaces = interfaces;
876 }
877 // defensively copy before handing over to user code
878 return interfaces.clone();
879 }
880 }
881
882 private native Class<?>[] getInterfaces0();
883
884 /**
885 * Returns the {@code Type}s representing the interfaces
886 * directly implemented by the class or interface represented by
887 * this object.
888 *
889 * <p>If a superinterface is a parameterized type, the
890 * {@code Type} object returned for it must accurately reflect
891 * the actual type parameters used in the source code. The
892 * parameterized type representing each superinterface is created
893 * if it had not been created before. See the declaration of
894 * {@link java.lang.reflect.ParameterizedType ParameterizedType}
895 * for the semantics of the creation process for parameterized
896 * types.
897 *
898 * <p>If this object represents a class, the return value is an array
1731 * @throws SecurityException
1732 * If a security manager, <i>s</i>, is present and
1733 * the caller's class loader is not the same as or an
1734 * ancestor of the class loader for the current class and
1735 * invocation of {@link SecurityManager#checkPackageAccess
1736 * s.checkPackageAccess()} denies access to the package
1737 * of this class.
1738 *
1739 * @since 1.1
1740 * @jls 8.2 Class Members
1741 * @jls 8.3 Field Declarations
1742 */
1743 @CallerSensitive
1744 public Field getField(String name)
1745 throws NoSuchFieldException, SecurityException {
1746 checkMemberAccess(Member.PUBLIC, Reflection.getCallerClass(), true);
1747 Field field = getField0(name);
1748 if (field == null) {
1749 throw new NoSuchFieldException(name);
1750 }
1751 return field;
1752 }
1753
1754
1755 /**
1756 * Returns a {@code Method} object that reflects the specified public
1757 * member method of the class or interface represented by this
1758 * {@code Class} object. The {@code name} parameter is a
1759 * {@code String} specifying the simple name of the desired method. The
1760 * {@code parameterTypes} parameter is an array of {@code Class}
1761 * objects that identify the method's formal parameter types, in declared
1762 * order. If {@code parameterTypes} is {@code null}, it is
1763 * treated as if it were an empty array.
1764 *
1765 * <p> If the {@code name} is "{@code <init>}" or "{@code <clinit>}" a
1766 * {@code NoSuchMethodException} is raised. Otherwise, the method to
1767 * be reflected is determined by the algorithm that follows. Let C be the
1768 * class or interface represented by this object:
1769 * <OL>
1770 * <LI> C is searched for a <I>matching method</I>, as defined below. If a
1771 * matching method is found, it is reflected.</LI>
1814 * @throws SecurityException
1815 * If a security manager, <i>s</i>, is present and
1816 * the caller's class loader is not the same as or an
1817 * ancestor of the class loader for the current class and
1818 * invocation of {@link SecurityManager#checkPackageAccess
1819 * s.checkPackageAccess()} denies access to the package
1820 * of this class.
1821 *
1822 * @jls 8.2 Class Members
1823 * @jls 8.4 Method Declarations
1824 * @since 1.1
1825 */
1826 @CallerSensitive
1827 public Method getMethod(String name, Class<?>... parameterTypes)
1828 throws NoSuchMethodException, SecurityException {
1829 checkMemberAccess(Member.PUBLIC, Reflection.getCallerClass(), true);
1830 Method method = getMethod0(name, parameterTypes, true);
1831 if (method == null) {
1832 throw new NoSuchMethodException(getName() + "." + name + argumentTypesToString(parameterTypes));
1833 }
1834 return method;
1835 }
1836
1837
1838 /**
1839 * Returns a {@code Constructor} object that reflects the specified
1840 * public constructor of the class represented by this {@code Class}
1841 * object. The {@code parameterTypes} parameter is an array of
1842 * {@code Class} objects that identify the constructor's formal
1843 * parameter types, in declared order.
1844 *
1845 * If this {@code Class} object represents an inner class
1846 * declared in a non-static context, the formal parameter types
1847 * include the explicit enclosing instance as the first parameter.
1848 *
1849 * <p> The constructor to reflect is the public constructor of the class
1850 * represented by this {@code Class} object whose formal parameter
1851 * types match those specified by {@code parameterTypes}.
1852 *
1853 * @param parameterTypes the parameter array
1854 * @return the {@code Constructor} object of the public constructor that
1855 * matches the specified {@code parameterTypes}
1856 * @throws NoSuchMethodException if a matching method is not found.
1857 * @throws SecurityException
1858 * If a security manager, <i>s</i>, is present and
1859 * the caller's class loader is not the same as or an
1860 * ancestor of the class loader for the current class and
1861 * invocation of {@link SecurityManager#checkPackageAccess
1862 * s.checkPackageAccess()} denies access to the package
1863 * of this class.
1864 *
1865 * @since 1.1
1866 */
1867 @CallerSensitive
1868 public Constructor<T> getConstructor(Class<?>... parameterTypes)
1869 throws NoSuchMethodException, SecurityException {
1870 checkMemberAccess(Member.PUBLIC, Reflection.getCallerClass(), true);
1871 return getConstructor0(parameterTypes, Member.PUBLIC);
1872 }
1873
1874
1875 /**
1876 * Returns an array of {@code Class} objects reflecting all the
1877 * classes and interfaces declared as members of the class represented by
1878 * this {@code Class} object. This includes public, protected, default
1879 * (package) access, and private classes and interfaces declared by the
1880 * class, but excludes inherited classes and interfaces. This method
1881 * returns an array of length 0 if the class declares no classes or
1882 * interfaces as members, or if this {@code Class} object represents a
1883 * primitive type, an array class, or void.
1884 *
1885 * @return the array of {@code Class} objects representing all the
1886 * declared members of this class
1887 * @throws SecurityException
1888 * If a security manager, <i>s</i>, is present and any of the
1889 * following conditions is met:
1890 *
1891 * <ul>
2098 * <li> the caller's class loader is not the same as or an
2099 * ancestor of the class loader for the current class and
2100 * invocation of {@link SecurityManager#checkPackageAccess
2101 * s.checkPackageAccess()} denies access to the package
2102 * of this class
2103 *
2104 * </ul>
2105 *
2106 * @since 1.1
2107 * @jls 8.2 Class Members
2108 * @jls 8.3 Field Declarations
2109 */
2110 @CallerSensitive
2111 public Field getDeclaredField(String name)
2112 throws NoSuchFieldException, SecurityException {
2113 checkMemberAccess(Member.DECLARED, Reflection.getCallerClass(), true);
2114 Field field = searchFields(privateGetDeclaredFields(false), name);
2115 if (field == null) {
2116 throw new NoSuchFieldException(name);
2117 }
2118 return field;
2119 }
2120
2121
2122 /**
2123 * Returns a {@code Method} object that reflects the specified
2124 * declared method of the class or interface represented by this
2125 * {@code Class} object. The {@code name} parameter is a
2126 * {@code String} that specifies the simple name of the desired
2127 * method, and the {@code parameterTypes} parameter is an array of
2128 * {@code Class} objects that identify the method's formal parameter
2129 * types, in declared order. If more than one method with the same
2130 * parameter types is declared in a class, and one of these methods has a
2131 * return type that is more specific than any of the others, that method is
2132 * returned; otherwise one of the methods is chosen arbitrarily. If the
2133 * name is "<init>"or "<clinit>" a {@code NoSuchMethodException}
2134 * is raised.
2135 *
2136 * <p> If this {@code Class} object represents an array type, then this
2137 * method does not find the {@code clone()} method.
2138 *
2158 * <li> the caller's class loader is not the same as or an
2159 * ancestor of the class loader for the current class and
2160 * invocation of {@link SecurityManager#checkPackageAccess
2161 * s.checkPackageAccess()} denies access to the package
2162 * of this class
2163 *
2164 * </ul>
2165 *
2166 * @jls 8.2 Class Members
2167 * @jls 8.4 Method Declarations
2168 * @since 1.1
2169 */
2170 @CallerSensitive
2171 public Method getDeclaredMethod(String name, Class<?>... parameterTypes)
2172 throws NoSuchMethodException, SecurityException {
2173 checkMemberAccess(Member.DECLARED, Reflection.getCallerClass(), true);
2174 Method method = searchMethods(privateGetDeclaredMethods(false), name, parameterTypes);
2175 if (method == null) {
2176 throw new NoSuchMethodException(getName() + "." + name + argumentTypesToString(parameterTypes));
2177 }
2178 return method;
2179 }
2180
2181
2182 /**
2183 * Returns a {@code Constructor} object that reflects the specified
2184 * constructor of the class or interface represented by this
2185 * {@code Class} object. The {@code parameterTypes} parameter is
2186 * an array of {@code Class} objects that identify the constructor's
2187 * formal parameter types, in declared order.
2188 *
2189 * If this {@code Class} object represents an inner class
2190 * declared in a non-static context, the formal parameter types
2191 * include the explicit enclosing instance as the first parameter.
2192 *
2193 * @param parameterTypes the parameter array
2194 * @return The {@code Constructor} object for the constructor with the
2195 * specified parameter list
2196 * @throws NoSuchMethodException if a matching method is not found.
2197 * @throws SecurityException
2198 * If a security manager, <i>s</i>, is present and any of the
2204 * class loader of this class and invocation of
2205 * {@link SecurityManager#checkPermission
2206 * s.checkPermission} method with
2207 * {@code RuntimePermission("accessDeclaredMembers")}
2208 * denies access to the declared constructor
2209 *
2210 * <li> the caller's class loader is not the same as or an
2211 * ancestor of the class loader for the current class and
2212 * invocation of {@link SecurityManager#checkPackageAccess
2213 * s.checkPackageAccess()} denies access to the package
2214 * of this class
2215 *
2216 * </ul>
2217 *
2218 * @since 1.1
2219 */
2220 @CallerSensitive
2221 public Constructor<T> getDeclaredConstructor(Class<?>... parameterTypes)
2222 throws NoSuchMethodException, SecurityException {
2223 checkMemberAccess(Member.DECLARED, Reflection.getCallerClass(), true);
2224 return getConstructor0(parameterTypes, Member.DECLARED);
2225 }
2226
2227 /**
2228 * Finds a resource with a given name. The rules for searching resources
2229 * associated with a given class are implemented by the defining
2230 * {@linkplain ClassLoader class loader} of the class. This method
2231 * delegates to this object's class loader. If this object was loaded by
2232 * the bootstrap class loader, the method delegates to {@link
2233 * ClassLoader#getSystemResourceAsStream}.
2234 *
2235 * <p> Before delegation, an absolute resource name is constructed from the
2236 * given resource name using this algorithm:
2237 *
2238 * <ul>
2239 *
2240 * <li> If the {@code name} begins with a {@code '/'}
2241 * (<tt>'\u002f'</tt>), then the absolute name of the resource is the
2242 * portion of the {@code name} following the {@code '/'}.
2243 *
2244 * <li> Otherwise, the absolute name is of the following form:
2644 checkInitted();
2645 Field[] res;
2646 ReflectionData<T> rd = reflectionData();
2647 if (rd != null) {
2648 res = rd.publicFields;
2649 if (res != null) return res;
2650 }
2651
2652 // No cached value available; compute value recursively.
2653 // Traverse in correct order for getField().
2654 List<Field> fields = new ArrayList<>();
2655 if (traversedInterfaces == null) {
2656 traversedInterfaces = new HashSet<>();
2657 }
2658
2659 // Local fields
2660 Field[] tmp = privateGetDeclaredFields(true);
2661 addAll(fields, tmp);
2662
2663 // Direct superinterfaces, recursively
2664 for (Class<?> c : getInterfaces()) {
2665 if (!traversedInterfaces.contains(c)) {
2666 traversedInterfaces.add(c);
2667 addAll(fields, c.privateGetPublicFields(traversedInterfaces));
2668 }
2669 }
2670
2671 // Direct superclass, recursively
2672 if (!isInterface()) {
2673 Class<?> c = getSuperclass();
2674 if (c != null) {
2675 addAll(fields, c.privateGetPublicFields(traversedInterfaces));
2676 }
2677 }
2678
2679 res = new Field[fields.size()];
2680 fields.toArray(res);
2681 if (rd != null) {
2682 rd.publicFields = res;
2683 }
2684 return res;
2738 private Method[] privateGetDeclaredMethods(boolean publicOnly) {
2739 checkInitted();
2740 Method[] res;
2741 ReflectionData<T> rd = reflectionData();
2742 if (rd != null) {
2743 res = publicOnly ? rd.declaredPublicMethods : rd.declaredMethods;
2744 if (res != null) return res;
2745 }
2746 // No cached value available; request value from VM
2747 res = Reflection.filterMethods(this, getDeclaredMethods0(publicOnly));
2748 if (rd != null) {
2749 if (publicOnly) {
2750 rd.declaredPublicMethods = res;
2751 } else {
2752 rd.declaredMethods = res;
2753 }
2754 }
2755 return res;
2756 }
2757
2758 static class MethodArray {
2759 // Don't add or remove methods except by add() or remove() calls.
2760 private Method[] methods;
2761 private int length;
2762 private int defaults;
2763
2764 MethodArray() {
2765 this(20);
2766 }
2767
2768 MethodArray(int initialSize) {
2769 if (initialSize < 2)
2770 throw new IllegalArgumentException("Size should be 2 or more");
2771
2772 methods = new Method[initialSize];
2773 length = 0;
2774 defaults = 0;
2775 }
2776
2777 boolean hasDefaults() {
2778 return defaults != 0;
2779 }
2780
2781 void add(Method m) {
2782 if (length == methods.length) {
2783 methods = Arrays.copyOf(methods, 2 * methods.length);
2784 }
2785 methods[length++] = m;
2786
2787 if (m != null && m.isDefault())
2788 defaults++;
2789 }
2790
2791 void addAll(Method[] ma) {
2792 for (Method m : ma) {
2793 add(m);
2794 }
2795 }
2796
2797 void addAll(MethodArray ma) {
2798 for (int i = 0; i < ma.length(); i++) {
2799 add(ma.get(i));
2800 }
2801 }
2802
2803 void addIfNotPresent(Method newMethod) {
2804 for (int i = 0; i < length; i++) {
2805 Method m = methods[i];
2806 if (m == newMethod || (m != null && m.equals(newMethod))) {
2807 return;
2808 }
2809 }
2810 add(newMethod);
2811 }
2812
2813 void addAllIfNotPresent(MethodArray newMethods) {
2814 for (int i = 0; i < newMethods.length(); i++) {
2815 Method m = newMethods.get(i);
2816 if (m != null) {
2817 addIfNotPresent(m);
2818 }
2819 }
2820 }
2821
2822 /* Add Methods declared in an interface to this MethodArray.
2823 * Static methods declared in interfaces are not inherited.
2824 */
2825 void addInterfaceMethods(Method[] methods) {
2826 for (Method candidate : methods) {
2827 if (!Modifier.isStatic(candidate.getModifiers())) {
2828 add(candidate);
2829 }
2830 }
2831 }
2832
2833 int length() {
2834 return length;
2835 }
2836
2837 Method get(int i) {
2838 return methods[i];
2839 }
2840
2841 Method getFirst() {
2842 for (Method m : methods)
2843 if (m != null)
2844 return m;
2845 return null;
2846 }
2847
2848 void removeByNameAndDescriptor(Method toRemove) {
2849 for (int i = 0; i < length; i++) {
2850 Method m = methods[i];
2851 if (m != null && matchesNameAndDescriptor(m, toRemove)) {
2852 remove(i);
2853 }
2854 }
2855 }
2856
2857 private void remove(int i) {
2858 if (methods[i] != null && methods[i].isDefault())
2859 defaults--;
2860 methods[i] = null;
2861 }
2862
2863 private boolean matchesNameAndDescriptor(Method m1, Method m2) {
2864 return m1.getReturnType() == m2.getReturnType() &&
2865 m1.getName() == m2.getName() && // name is guaranteed to be interned
2866 arrayContentsEq(m1.getParameterTypes(),
2867 m2.getParameterTypes());
2868 }
2869
2870 void compactAndTrim() {
2871 int newPos = 0;
2872 // Get rid of null slots
2873 for (int pos = 0; pos < length; pos++) {
2874 Method m = methods[pos];
2875 if (m != null) {
2876 if (pos != newPos) {
2877 methods[newPos] = m;
2878 }
2879 newPos++;
2880 }
2881 }
2882 if (newPos != methods.length) {
2883 methods = Arrays.copyOf(methods, newPos);
2884 }
2885 }
2886
2887 /* Removes all Methods from this MethodArray that have a more specific
2888 * default Method in this MethodArray.
2889 *
2890 * Users of MethodArray are responsible for pruning Methods that have
2891 * a more specific <em>concrete</em> Method.
2892 */
2893 void removeLessSpecifics() {
2894 if (!hasDefaults())
2895 return;
2896
2897 for (int i = 0; i < length; i++) {
2898 Method m = get(i);
2899 if (m == null || !m.isDefault())
2900 continue;
2901
2902 for (int j = 0; j < length; j++) {
2903 if (i == j)
2904 continue;
2905
2906 Method candidate = get(j);
2907 if (candidate == null)
2908 continue;
2909
2910 if (!matchesNameAndDescriptor(m, candidate))
2911 continue;
2912
2913 if (hasMoreSpecificClass(m, candidate))
2914 remove(j);
2915 }
2916 }
2917 }
2918
2919 Method[] getArray() {
2920 return methods;
2921 }
2922
2923 // Returns true if m1 is more specific than m2
2924 static boolean hasMoreSpecificClass(Method m1, Method m2) {
2925 Class<?> m1Class = m1.getDeclaringClass();
2926 Class<?> m2Class = m2.getDeclaringClass();
2927 return m1Class != m2Class && m2Class.isAssignableFrom(m1Class);
2928 }
2929 }
2930
2931
2932 // Returns an array of "root" methods. These Method objects must NOT
2933 // be propagated to the outside world, but must instead be copied
2934 // via ReflectionFactory.copyMethod.
2935 private Method[] privateGetPublicMethods() {
2936 checkInitted();
2937 Method[] res;
2938 ReflectionData<T> rd = reflectionData();
2939 if (rd != null) {
2940 res = rd.publicMethods;
2941 if (res != null) return res;
2942 }
2943
2944 // No cached value available; compute value recursively.
2945 // Start by fetching public declared methods
2946 MethodArray methods = new MethodArray();
2947 {
2948 Method[] tmp = privateGetDeclaredMethods(true);
2949 methods.addAll(tmp);
2950 }
2951 // Now recur over superclass and direct superinterfaces.
2952 // Go over superinterfaces first so we can more easily filter
2953 // out concrete implementations inherited from superclasses at
2954 // the end.
2955 MethodArray inheritedMethods = new MethodArray();
2956 for (Class<?> i : getInterfaces()) {
2957 inheritedMethods.addInterfaceMethods(i.privateGetPublicMethods());
2958 }
2959 if (!isInterface()) {
2960 Class<?> c = getSuperclass();
2961 if (c != null) {
2962 MethodArray supers = new MethodArray();
2963 supers.addAll(c.privateGetPublicMethods());
2964 // Filter out concrete implementations of any
2965 // interface methods
2966 for (int i = 0; i < supers.length(); i++) {
2967 Method m = supers.get(i);
2968 if (m != null &&
2969 !Modifier.isAbstract(m.getModifiers()) &&
2970 !m.isDefault()) {
2971 inheritedMethods.removeByNameAndDescriptor(m);
2972 }
2973 }
2974 // Insert superclass's inherited methods before
2975 // superinterfaces' to satisfy getMethod's search
2976 // order
2977 supers.addAll(inheritedMethods);
2978 inheritedMethods = supers;
2979 }
2980 }
2981 // Filter out all local methods from inherited ones
2982 for (int i = 0; i < methods.length(); i++) {
2983 Method m = methods.get(i);
2984 inheritedMethods.removeByNameAndDescriptor(m);
2985 }
2986 methods.addAllIfNotPresent(inheritedMethods);
2987 methods.removeLessSpecifics();
2988 methods.compactAndTrim();
2989 res = methods.getArray();
2990 if (rd != null) {
2991 rd.publicMethods = res;
2992 }
2993 return res;
2994 }
2995
2996
2997 //
2998 // Helpers for fetchers of one field, method, or constructor
2999 //
3000
3001 private static Field searchFields(Field[] fields, String name) {
3002 String internedName = name.intern();
3003 for (Field field : fields) {
3004 if (field.getName() == internedName) {
3005 return getReflectionFactory().copyField(field);
3006 }
3007 }
3008 return null;
3009 }
3010
3011 private Field getField0(String name) throws NoSuchFieldException {
3012 // Note: the intent is that the search algorithm this routine
3013 // uses be equivalent to the ordering imposed by
3014 // privateGetPublicFields(). It fetches only the declared
3015 // public fields for each class, however, to reduce the number
3016 // of Field objects which have to be created for the common
3017 // case where the field being requested is declared in the
3018 // class which is being queried.
3019 Field res;
3020 // Search declared public fields
3021 if ((res = searchFields(privateGetDeclaredFields(true), name)) != null) {
3022 return res;
3023 }
3024 // Direct superinterfaces, recursively
3025 Class<?>[] interfaces = getInterfaces();
3026 for (Class<?> c : interfaces) {
3027 if ((res = c.getField0(name)) != null) {
3028 return res;
3029 }
3030 }
3031 // Direct superclass, recursively
3032 if (!isInterface()) {
3033 Class<?> c = getSuperclass();
3034 if (c != null) {
3035 if ((res = c.getField0(name)) != null) {
3036 return res;
3037 }
3038 }
3039 }
3040 return null;
3041 }
3042
3043 private static Method searchMethods(Method[] methods,
3044 String name,
3045 Class<?>[] parameterTypes)
3046 {
3047 Method res = null;
3048 String internedName = name.intern();
3049 for (Method m : methods) {
3050 if (m.getName() == internedName
3051 && arrayContentsEq(parameterTypes, m.getParameterTypes())
3052 && (res == null
3053 || res.getReturnType().isAssignableFrom(m.getReturnType())))
3054 res = m;
3055 }
3056
3057 return (res == null ? res : getReflectionFactory().copyMethod(res));
3058 }
3059
3060 private Method getMethod0(String name, Class<?>[] parameterTypes, boolean includeStaticMethods) {
3061 MethodArray interfaceCandidates = new MethodArray(2);
3062 Method res = privateGetMethodRecursive(name, parameterTypes, includeStaticMethods, interfaceCandidates);
3063 if (res != null)
3064 return res;
3065
3066 // Not found on class or superclass directly
3067 interfaceCandidates.removeLessSpecifics();
3068 return interfaceCandidates.getFirst(); // may be null
3069 }
3070
3071 private Method privateGetMethodRecursive(String name,
3072 Class<?>[] parameterTypes,
3073 boolean includeStaticMethods,
3074 MethodArray allInterfaceCandidates) {
3075 // Note: the intent is that the search algorithm this routine
3076 // uses be equivalent to the ordering imposed by
3077 // privateGetPublicMethods(). It fetches only the declared
3078 // public methods for each class, however, to reduce the
3079 // number of Method objects which have to be created for the
3080 // common case where the method being requested is declared in
3081 // the class which is being queried.
3082 //
3083 // Due to default methods, unless a method is found on a superclass,
3084 // methods declared in any superinterface needs to be considered.
3085 // Collect all candidates declared in superinterfaces in {@code
3086 // allInterfaceCandidates} and select the most specific if no match on
3087 // a superclass is found.
3088
3089 // Must _not_ return root methods
3090 Method res;
3091 // Search declared public methods
3092 if ((res = searchMethods(privateGetDeclaredMethods(true),
3093 name,
3094 parameterTypes)) != null) {
3095 if (includeStaticMethods || !Modifier.isStatic(res.getModifiers()))
3096 return res;
3097 }
3098 // Search superclass's methods
3099 if (!isInterface()) {
3100 Class<? super T> c = getSuperclass();
3101 if (c != null) {
3102 if ((res = c.getMethod0(name, parameterTypes, true)) != null) {
3103 return res;
3104 }
3105 }
3106 }
3107 // Search superinterfaces' methods
3108 Class<?>[] interfaces = getInterfaces();
3109 for (Class<?> c : interfaces)
3110 if ((res = c.getMethod0(name, parameterTypes, false)) != null)
3111 allInterfaceCandidates.add(res);
3112 // Not found
3113 return null;
3114 }
3115
3116 private Constructor<T> getConstructor0(Class<?>[] parameterTypes,
3117 int which) throws NoSuchMethodException
3118 {
3119 Constructor<T>[] constructors = privateGetDeclaredConstructors((which == Member.PUBLIC));
3120 for (Constructor<T> constructor : constructors) {
3121 if (arrayContentsEq(parameterTypes,
3122 constructor.getParameterTypes())) {
3123 return getReflectionFactory().copyConstructor(constructor);
3124 }
3125 }
3126 throw new NoSuchMethodException(getName() + ".<init>" + argumentTypesToString(parameterTypes));
3127 }
3128
3129 //
3130 // Other helpers and base implementation
3131 //
3132
3133 private static boolean arrayContentsEq(Object[] a1, Object[] a2) {
3134 if (a1 == null) {
3135 return a2 == null || a2.length == 0;
3136 }
3137
3138 if (a2 == null) {
3139 return a1.length == 0;
3140 }
3141
3142 if (a1.length != a2.length) {
3143 return false;
|
28 import java.lang.reflect.AnnotatedElement;
29 import java.lang.reflect.Array;
30 import java.lang.reflect.GenericArrayType;
31 import java.lang.reflect.GenericDeclaration;
32 import java.lang.reflect.Member;
33 import java.lang.reflect.Field;
34 import java.lang.reflect.Executable;
35 import java.lang.reflect.Method;
36 import java.lang.reflect.Constructor;
37 import java.lang.reflect.Modifier;
38 import java.lang.reflect.Type;
39 import java.lang.reflect.TypeVariable;
40 import java.lang.reflect.InvocationTargetException;
41 import java.lang.reflect.AnnotatedType;
42 import java.lang.ref.SoftReference;
43 import java.io.InputStream;
44 import java.io.ObjectStreamField;
45 import java.security.AccessController;
46 import java.security.PrivilegedAction;
47 import java.util.ArrayList;
48 import java.util.Collection;
49 import java.util.HashSet;
50 import java.util.LinkedHashMap;
51 import java.util.List;
52 import java.util.Set;
53 import java.util.Map;
54 import java.util.HashMap;
55 import java.util.Objects;
56 import java.util.StringJoiner;
57 import sun.misc.Unsafe;
58 import sun.reflect.CallerSensitive;
59 import sun.reflect.ConstantPool;
60 import sun.reflect.Reflection;
61 import sun.reflect.ReflectionFactory;
62 import sun.reflect.generics.factory.CoreReflectionFactory;
63 import sun.reflect.generics.factory.GenericsFactory;
64 import sun.reflect.generics.repository.ClassRepository;
65 import sun.reflect.generics.repository.MethodRepository;
66 import sun.reflect.generics.repository.ConstructorRepository;
67 import sun.reflect.generics.scope.ClassScope;
413 @CallerSensitive
414 public T newInstance()
415 throws InstantiationException, IllegalAccessException
416 {
417 if (System.getSecurityManager() != null) {
418 checkMemberAccess(Member.PUBLIC, Reflection.getCallerClass(), false);
419 }
420
421 // NOTE: the following code may not be strictly correct under
422 // the current Java memory model.
423
424 // Constructor lookup
425 if (cachedConstructor == null) {
426 if (this == Class.class) {
427 throw new IllegalAccessException(
428 "Can not call newInstance() on the Class for java.lang.Class"
429 );
430 }
431 try {
432 Class<?>[] empty = {};
433 // must copy constructor since we are modifying it
434 final Constructor<T> c = getReflectionFactory().copyConstructor(
435 getConstructor0(empty, Member.DECLARED));
436 // Disable accessibility checks on the constructor
437 // since we have to do the security check here anyway
438 // (the stack depth is wrong for the Constructor's
439 // security check to work)
440 java.security.AccessController.doPrivileged(
441 new java.security.PrivilegedAction<Void>() {
442 public Void run() {
443 c.setAccessible(true);
444 return null;
445 }
446 });
447 cachedConstructor = c;
448 } catch (NoSuchMethodException e) {
449 throw (InstantiationException)
450 new InstantiationException(getName()).initCause(e);
451 }
452 }
453 Constructor<T> tmpConstructor = cachedConstructor;
454 // Security check (same as in java.lang.reflect.Constructor)
455 int modifiers = tmpConstructor.getModifiers();
848 *
849 * <p>If this object represents an interface, the array contains objects
850 * representing all interfaces directly extended by the interface. The
851 * order of the interface objects in the array corresponds to the order of
852 * the interface names in the {@code extends} clause of the declaration of
853 * the interface represented by this object.
854 *
855 * <p>If this object represents a class or interface that implements no
856 * interfaces, the method returns an array of length 0.
857 *
858 * <p>If this object represents a primitive type or void, the method
859 * returns an array of length 0.
860 *
861 * <p>If this {@code Class} object represents an array type, the
862 * interfaces {@code Cloneable} and {@code java.io.Serializable} are
863 * returned in that order.
864 *
865 * @return an array of interfaces directly implemented by this class
866 */
867 public Class<?>[] getInterfaces() {
868 return getInterfaces(true);
869 }
870
871 private Class<?>[] getInterfaces(boolean cloneArray) {
872 ReflectionData<T> rd = reflectionData();
873 if (rd == null) {
874 // no cloning required
875 return getInterfaces0();
876 } else {
877 Class<?>[] interfaces = rd.interfaces;
878 if (interfaces == null) {
879 interfaces = getInterfaces0();
880 rd.interfaces = interfaces;
881 }
882 // defensively copy cached array before handing over to user code
883 return cloneArray ? interfaces.clone() : interfaces;
884 }
885 }
886
887 private native Class<?>[] getInterfaces0();
888
889 /**
890 * Returns the {@code Type}s representing the interfaces
891 * directly implemented by the class or interface represented by
892 * this object.
893 *
894 * <p>If a superinterface is a parameterized type, the
895 * {@code Type} object returned for it must accurately reflect
896 * the actual type parameters used in the source code. The
897 * parameterized type representing each superinterface is created
898 * if it had not been created before. See the declaration of
899 * {@link java.lang.reflect.ParameterizedType ParameterizedType}
900 * for the semantics of the creation process for parameterized
901 * types.
902 *
903 * <p>If this object represents a class, the return value is an array
1736 * @throws SecurityException
1737 * If a security manager, <i>s</i>, is present and
1738 * the caller's class loader is not the same as or an
1739 * ancestor of the class loader for the current class and
1740 * invocation of {@link SecurityManager#checkPackageAccess
1741 * s.checkPackageAccess()} denies access to the package
1742 * of this class.
1743 *
1744 * @since 1.1
1745 * @jls 8.2 Class Members
1746 * @jls 8.3 Field Declarations
1747 */
1748 @CallerSensitive
1749 public Field getField(String name)
1750 throws NoSuchFieldException, SecurityException {
1751 checkMemberAccess(Member.PUBLIC, Reflection.getCallerClass(), true);
1752 Field field = getField0(name);
1753 if (field == null) {
1754 throw new NoSuchFieldException(name);
1755 }
1756 return getReflectionFactory().copyField(field);
1757 }
1758
1759
1760 /**
1761 * Returns a {@code Method} object that reflects the specified public
1762 * member method of the class or interface represented by this
1763 * {@code Class} object. The {@code name} parameter is a
1764 * {@code String} specifying the simple name of the desired method. The
1765 * {@code parameterTypes} parameter is an array of {@code Class}
1766 * objects that identify the method's formal parameter types, in declared
1767 * order. If {@code parameterTypes} is {@code null}, it is
1768 * treated as if it were an empty array.
1769 *
1770 * <p> If the {@code name} is "{@code <init>}" or "{@code <clinit>}" a
1771 * {@code NoSuchMethodException} is raised. Otherwise, the method to
1772 * be reflected is determined by the algorithm that follows. Let C be the
1773 * class or interface represented by this object:
1774 * <OL>
1775 * <LI> C is searched for a <I>matching method</I>, as defined below. If a
1776 * matching method is found, it is reflected.</LI>
1819 * @throws SecurityException
1820 * If a security manager, <i>s</i>, is present and
1821 * the caller's class loader is not the same as or an
1822 * ancestor of the class loader for the current class and
1823 * invocation of {@link SecurityManager#checkPackageAccess
1824 * s.checkPackageAccess()} denies access to the package
1825 * of this class.
1826 *
1827 * @jls 8.2 Class Members
1828 * @jls 8.4 Method Declarations
1829 * @since 1.1
1830 */
1831 @CallerSensitive
1832 public Method getMethod(String name, Class<?>... parameterTypes)
1833 throws NoSuchMethodException, SecurityException {
1834 checkMemberAccess(Member.PUBLIC, Reflection.getCallerClass(), true);
1835 Method method = getMethod0(name, parameterTypes, true);
1836 if (method == null) {
1837 throw new NoSuchMethodException(getName() + "." + name + argumentTypesToString(parameterTypes));
1838 }
1839 return getReflectionFactory().copyMethod(method);
1840 }
1841
1842
1843 /**
1844 * Returns a {@code Constructor} object that reflects the specified
1845 * public constructor of the class represented by this {@code Class}
1846 * object. The {@code parameterTypes} parameter is an array of
1847 * {@code Class} objects that identify the constructor's formal
1848 * parameter types, in declared order.
1849 *
1850 * If this {@code Class} object represents an inner class
1851 * declared in a non-static context, the formal parameter types
1852 * include the explicit enclosing instance as the first parameter.
1853 *
1854 * <p> The constructor to reflect is the public constructor of the class
1855 * represented by this {@code Class} object whose formal parameter
1856 * types match those specified by {@code parameterTypes}.
1857 *
1858 * @param parameterTypes the parameter array
1859 * @return the {@code Constructor} object of the public constructor that
1860 * matches the specified {@code parameterTypes}
1861 * @throws NoSuchMethodException if a matching method is not found.
1862 * @throws SecurityException
1863 * If a security manager, <i>s</i>, is present and
1864 * the caller's class loader is not the same as or an
1865 * ancestor of the class loader for the current class and
1866 * invocation of {@link SecurityManager#checkPackageAccess
1867 * s.checkPackageAccess()} denies access to the package
1868 * of this class.
1869 *
1870 * @since 1.1
1871 */
1872 @CallerSensitive
1873 public Constructor<T> getConstructor(Class<?>... parameterTypes)
1874 throws NoSuchMethodException, SecurityException {
1875 checkMemberAccess(Member.PUBLIC, Reflection.getCallerClass(), true);
1876 return getReflectionFactory().copyConstructor(
1877 getConstructor0(parameterTypes, Member.PUBLIC));
1878 }
1879
1880
1881 /**
1882 * Returns an array of {@code Class} objects reflecting all the
1883 * classes and interfaces declared as members of the class represented by
1884 * this {@code Class} object. This includes public, protected, default
1885 * (package) access, and private classes and interfaces declared by the
1886 * class, but excludes inherited classes and interfaces. This method
1887 * returns an array of length 0 if the class declares no classes or
1888 * interfaces as members, or if this {@code Class} object represents a
1889 * primitive type, an array class, or void.
1890 *
1891 * @return the array of {@code Class} objects representing all the
1892 * declared members of this class
1893 * @throws SecurityException
1894 * If a security manager, <i>s</i>, is present and any of the
1895 * following conditions is met:
1896 *
1897 * <ul>
2104 * <li> the caller's class loader is not the same as or an
2105 * ancestor of the class loader for the current class and
2106 * invocation of {@link SecurityManager#checkPackageAccess
2107 * s.checkPackageAccess()} denies access to the package
2108 * of this class
2109 *
2110 * </ul>
2111 *
2112 * @since 1.1
2113 * @jls 8.2 Class Members
2114 * @jls 8.3 Field Declarations
2115 */
2116 @CallerSensitive
2117 public Field getDeclaredField(String name)
2118 throws NoSuchFieldException, SecurityException {
2119 checkMemberAccess(Member.DECLARED, Reflection.getCallerClass(), true);
2120 Field field = searchFields(privateGetDeclaredFields(false), name);
2121 if (field == null) {
2122 throw new NoSuchFieldException(name);
2123 }
2124 return getReflectionFactory().copyField(field);
2125 }
2126
2127
2128 /**
2129 * Returns a {@code Method} object that reflects the specified
2130 * declared method of the class or interface represented by this
2131 * {@code Class} object. The {@code name} parameter is a
2132 * {@code String} that specifies the simple name of the desired
2133 * method, and the {@code parameterTypes} parameter is an array of
2134 * {@code Class} objects that identify the method's formal parameter
2135 * types, in declared order. If more than one method with the same
2136 * parameter types is declared in a class, and one of these methods has a
2137 * return type that is more specific than any of the others, that method is
2138 * returned; otherwise one of the methods is chosen arbitrarily. If the
2139 * name is "<init>"or "<clinit>" a {@code NoSuchMethodException}
2140 * is raised.
2141 *
2142 * <p> If this {@code Class} object represents an array type, then this
2143 * method does not find the {@code clone()} method.
2144 *
2164 * <li> the caller's class loader is not the same as or an
2165 * ancestor of the class loader for the current class and
2166 * invocation of {@link SecurityManager#checkPackageAccess
2167 * s.checkPackageAccess()} denies access to the package
2168 * of this class
2169 *
2170 * </ul>
2171 *
2172 * @jls 8.2 Class Members
2173 * @jls 8.4 Method Declarations
2174 * @since 1.1
2175 */
2176 @CallerSensitive
2177 public Method getDeclaredMethod(String name, Class<?>... parameterTypes)
2178 throws NoSuchMethodException, SecurityException {
2179 checkMemberAccess(Member.DECLARED, Reflection.getCallerClass(), true);
2180 Method method = searchMethods(privateGetDeclaredMethods(false), name, parameterTypes);
2181 if (method == null) {
2182 throw new NoSuchMethodException(getName() + "." + name + argumentTypesToString(parameterTypes));
2183 }
2184 return getReflectionFactory().copyMethod(method);
2185 }
2186
2187
2188 /**
2189 * Returns a {@code Constructor} object that reflects the specified
2190 * constructor of the class or interface represented by this
2191 * {@code Class} object. The {@code parameterTypes} parameter is
2192 * an array of {@code Class} objects that identify the constructor's
2193 * formal parameter types, in declared order.
2194 *
2195 * If this {@code Class} object represents an inner class
2196 * declared in a non-static context, the formal parameter types
2197 * include the explicit enclosing instance as the first parameter.
2198 *
2199 * @param parameterTypes the parameter array
2200 * @return The {@code Constructor} object for the constructor with the
2201 * specified parameter list
2202 * @throws NoSuchMethodException if a matching method is not found.
2203 * @throws SecurityException
2204 * If a security manager, <i>s</i>, is present and any of the
2210 * class loader of this class and invocation of
2211 * {@link SecurityManager#checkPermission
2212 * s.checkPermission} method with
2213 * {@code RuntimePermission("accessDeclaredMembers")}
2214 * denies access to the declared constructor
2215 *
2216 * <li> the caller's class loader is not the same as or an
2217 * ancestor of the class loader for the current class and
2218 * invocation of {@link SecurityManager#checkPackageAccess
2219 * s.checkPackageAccess()} denies access to the package
2220 * of this class
2221 *
2222 * </ul>
2223 *
2224 * @since 1.1
2225 */
2226 @CallerSensitive
2227 public Constructor<T> getDeclaredConstructor(Class<?>... parameterTypes)
2228 throws NoSuchMethodException, SecurityException {
2229 checkMemberAccess(Member.DECLARED, Reflection.getCallerClass(), true);
2230 return getReflectionFactory().copyConstructor(
2231 getConstructor0(parameterTypes, Member.DECLARED));
2232 }
2233
2234 /**
2235 * Finds a resource with a given name. The rules for searching resources
2236 * associated with a given class are implemented by the defining
2237 * {@linkplain ClassLoader class loader} of the class. This method
2238 * delegates to this object's class loader. If this object was loaded by
2239 * the bootstrap class loader, the method delegates to {@link
2240 * ClassLoader#getSystemResourceAsStream}.
2241 *
2242 * <p> Before delegation, an absolute resource name is constructed from the
2243 * given resource name using this algorithm:
2244 *
2245 * <ul>
2246 *
2247 * <li> If the {@code name} begins with a {@code '/'}
2248 * (<tt>'\u002f'</tt>), then the absolute name of the resource is the
2249 * portion of the {@code name} following the {@code '/'}.
2250 *
2251 * <li> Otherwise, the absolute name is of the following form:
2651 checkInitted();
2652 Field[] res;
2653 ReflectionData<T> rd = reflectionData();
2654 if (rd != null) {
2655 res = rd.publicFields;
2656 if (res != null) return res;
2657 }
2658
2659 // No cached value available; compute value recursively.
2660 // Traverse in correct order for getField().
2661 List<Field> fields = new ArrayList<>();
2662 if (traversedInterfaces == null) {
2663 traversedInterfaces = new HashSet<>();
2664 }
2665
2666 // Local fields
2667 Field[] tmp = privateGetDeclaredFields(true);
2668 addAll(fields, tmp);
2669
2670 // Direct superinterfaces, recursively
2671 for (Class<?> c : getInterfaces(false)) {
2672 if (!traversedInterfaces.contains(c)) {
2673 traversedInterfaces.add(c);
2674 addAll(fields, c.privateGetPublicFields(traversedInterfaces));
2675 }
2676 }
2677
2678 // Direct superclass, recursively
2679 if (!isInterface()) {
2680 Class<?> c = getSuperclass();
2681 if (c != null) {
2682 addAll(fields, c.privateGetPublicFields(traversedInterfaces));
2683 }
2684 }
2685
2686 res = new Field[fields.size()];
2687 fields.toArray(res);
2688 if (rd != null) {
2689 rd.publicFields = res;
2690 }
2691 return res;
2745 private Method[] privateGetDeclaredMethods(boolean publicOnly) {
2746 checkInitted();
2747 Method[] res;
2748 ReflectionData<T> rd = reflectionData();
2749 if (rd != null) {
2750 res = publicOnly ? rd.declaredPublicMethods : rd.declaredMethods;
2751 if (res != null) return res;
2752 }
2753 // No cached value available; request value from VM
2754 res = Reflection.filterMethods(this, getDeclaredMethods0(publicOnly));
2755 if (rd != null) {
2756 if (publicOnly) {
2757 rd.declaredPublicMethods = res;
2758 } else {
2759 rd.declaredMethods = res;
2760 }
2761 }
2762 return res;
2763 }
2764
2765 private static final Method[] EMPTY_METHODS = new Method[0];
2766
2767 // Returns an array of "root" methods. These Method objects must NOT
2768 // be propagated to the outside world, but must instead be copied
2769 // via ReflectionFactory.copyMethod.
2770 private Method[] privateGetPublicMethods() {
2771 checkInitted();
2772 Method[] res;
2773 ReflectionData<T> rd = reflectionData();
2774 if (rd != null) {
2775 res = rd.publicMethods;
2776 if (res != null) return res;
2777 }
2778
2779 Method[] declaredMethods = privateGetDeclaredMethods(true);
2780 Class<?> superclass = getSuperclass();
2781 Class<?>[] interfaces = getInterfaces(false);
2782
2783 // optimization:
2784 // if we don't have a superclass (either we are j.l.Object or an interface)
2785 // and don't have (super)interfaces either, then public methods consist
2786 // of declared public methods
2787 if (superclass == null && interfaces.length == 0) {
2788 res = declaredMethods;
2789 } else {
2790 // we have to do some logic
2791 Method[] superclassMethods = (superclass == null)
2792 ? EMPTY_METHODS
2793 : superclass.privateGetPublicMethods();
2794 Method[][] interfacesMethods = new Method[interfaces.length][];
2795 int interfacesMethodsCount = 0;
2796 for (int i = 0; i < interfaces.length; i++) {
2797 interfacesMethods[i] = interfaces[i].privateGetPublicMethods();
2798 interfacesMethodsCount += interfacesMethods[i].length;
2799 }
2800
2801 // ensure enough working capacity
2802 // (MethodTable implementation may not support dynamic resizing)
2803 MethodTable methodTable = MethodTable.newInstance(
2804 declaredMethods.length +
2805 superclassMethods.length +
2806 interfacesMethodsCount
2807 );
2808
2809 // declared methods first
2810 for (Method m : declaredMethods) {
2811 methodTable.add(m);
2812 }
2813
2814 // inherited methods from superclass
2815 for (Method m : superclassMethods) {
2816 methodTable.addUnlessDeclaredExists(m, this);
2817 }
2818
2819 // inherited methods from (super)interfaces
2820 for (Method[] ms : interfacesMethods) {
2821 for (Method m : ms) {
2822 // interface static methods are not inherited
2823 if (!Modifier.isStatic(m.getModifiers())) {
2824 methodTable.consolidate(m, this);
2825 }
2826 }
2827 }
2828
2829 res = methodTable.getMethods();
2830 }
2831
2832 if (rd != null) {
2833 rd.publicMethods = res;
2834 }
2835 return res;
2836 }
2837
2838
2839 //
2840 // Helpers for fetchers of one field, method, or constructor
2841 //
2842
2843 /**
2844 * This method does not copy returned 'root' Field object. It MUST be copied
2845 * with ReflectionFactory before handed to any code outside java.lang.Class
2846 * or modified.
2847 */
2848 private static Field searchFields(Field[] fields, String name) {
2849 String internedName = name.intern();
2850 for (Field field : fields) {
2851 if (field.getName() == internedName) {
2852 return field;
2853 }
2854 }
2855 return null;
2856 }
2857
2858 /**
2859 * This method returns 'root' Field object. It MUST be copied with ReflectionFactory
2860 * before handed to any code outside java.lang.Class or modified.
2861 */
2862 private Field getField0(String name) throws NoSuchFieldException {
2863 // Note: the intent is that the search algorithm this routine
2864 // uses be equivalent to the ordering imposed by
2865 // privateGetPublicFields(). It fetches only the declared
2866 // public fields for each class, however, to reduce the number
2867 // of Field objects which have to be created for the common
2868 // case where the field being requested is declared in the
2869 // class which is being queried.
2870 Field res;
2871 // Search declared public fields
2872 if ((res = searchFields(privateGetDeclaredFields(true), name)) != null) {
2873 return res;
2874 }
2875 // Direct superinterfaces, recursively
2876 Class<?>[] interfaces = getInterfaces(false);
2877 for (Class<?> c : interfaces) {
2878 if ((res = c.getField0(name)) != null) {
2879 return res;
2880 }
2881 }
2882 // Direct superclass, recursively
2883 if (!isInterface()) {
2884 Class<?> c = getSuperclass();
2885 if (c != null) {
2886 if ((res = c.getField0(name)) != null) {
2887 return res;
2888 }
2889 }
2890 }
2891 return null;
2892 }
2893
2894 /**
2895 * This method does not copy returned 'root' Method object. It MUST be copied
2896 * with ReflectionFactory before handed to any code outside java.lang.Class
2897 * or modified.
2898 */
2899 private static Method searchMethods(Method[] methods,
2900 String name,
2901 Class<?>[] parameterTypes)
2902 {
2903 Method res = null;
2904 String internedName = name.intern();
2905 for (Method m : methods) {
2906 if (m.getName() == internedName
2907 && arrayContentsEq(parameterTypes, m.getParameterTypes())
2908 && (res == null
2909 || res.getReturnType().isAssignableFrom(m.getReturnType())))
2910 res = m;
2911 }
2912
2913 return res;
2914 }
2915
2916 /**
2917 * This method returns 'root' Method object. It MUST be copied with ReflectionFactory
2918 * before handed to any code outside java.lang.Class or modified.
2919 */
2920 private Method getMethod0(String name, Class<?>[] parameterTypes, boolean includeStaticMethods) {
2921 // Note: the intent is that the search algorithm this routine
2922 // uses be equivalent to the ordering imposed by
2923 // privateGetPublicMethods(). It fetches only the declared
2924 // public methods for each class, however, to reduce the
2925 // number of Method objects which have to be created for the
2926 // common case where the method being requested is declared in
2927 // the class which is being queried.
2928 Method res;
2929 // 1st search declared public methods
2930 if ((res = searchMethods(privateGetDeclaredMethods(true),
2931 name,
2932 parameterTypes)) != null &&
2933 (includeStaticMethods ||
2934 !Modifier.isStatic(res.getModifiers()))) {
2935 return res;
2936 }
2937
2938 // 2nd we try the superclass.
2939 Class<? super T> superclass = getSuperclass();
2940 if (superclass != null &&
2941 (res = superclass.getMethod0(name, parameterTypes, includeStaticMethods)) != null) {
2942 return res;
2943 }
2944
2945 // last we check (super) interfaces
2946 Class<?>[] interfaces = getInterfaces(false);
2947 if (interfaces.length == 0) {
2948 return null;
2949 }
2950 // have to construct a MethodTable to consolidate
2951 // public methods from (super)interfaces
2952 MethodTable methodTable = MethodTable.newInstance(interfaces.length);
2953 for (Class<?> c : interfaces) {
2954 if ((res = c.getMethod0(name, parameterTypes, false)) != null) {
2955 methodTable.consolidate(res, this);
2956 }
2957 }
2958
2959 // return the first method with the most specific return type
2960 return methodTable.getFirstMethodWithMostSpecificReturnType();
2961 }
2962
2963 /**
2964 * This method returns 'root' Constructor object. It MUST be copied with ReflectionFactory
2965 * before handed to any code outside java.lang.Class or modified.
2966 */
2967 private Constructor<T> getConstructor0(Class<?>[] parameterTypes,
2968 int which) throws NoSuchMethodException
2969 {
2970 Constructor<T>[] constructors = privateGetDeclaredConstructors((which == Member.PUBLIC));
2971 for (Constructor<T> constructor : constructors) {
2972 if (arrayContentsEq(parameterTypes,
2973 constructor.getParameterTypes())) {
2974 return constructor;
2975 }
2976 }
2977 throw new NoSuchMethodException(getName() + ".<init>" + argumentTypesToString(parameterTypes));
2978 }
2979
2980 //
2981 // Other helpers and base implementation
2982 //
2983
2984 private static boolean arrayContentsEq(Object[] a1, Object[] a2) {
2985 if (a1 == null) {
2986 return a2 == null || a2.length == 0;
2987 }
2988
2989 if (a2 == null) {
2990 return a1.length == 0;
2991 }
2992
2993 if (a1.length != a2.length) {
2994 return false;
|