< prev index next >

src/java.base/share/classes/java/lang/Class.java

Print this page




  34 import java.lang.reflect.Member;
  35 import java.lang.reflect.Field;
  36 import java.lang.reflect.Executable;
  37 import java.lang.reflect.Method;
  38 import java.lang.reflect.Module;
  39 import java.lang.reflect.Constructor;
  40 import java.lang.reflect.Modifier;
  41 import java.lang.reflect.Type;
  42 import java.lang.reflect.TypeVariable;
  43 import java.lang.reflect.InvocationTargetException;
  44 import java.lang.reflect.AnnotatedType;
  45 import java.lang.reflect.Proxy;
  46 import java.lang.ref.SoftReference;
  47 import java.io.IOException;
  48 import java.io.InputStream;
  49 import java.io.ObjectStreamField;
  50 import java.net.URL;
  51 import java.security.AccessController;
  52 import java.security.PrivilegedAction;
  53 import java.util.ArrayList;
  54 import java.util.Arrays;
  55 import java.util.Collection;
  56 import java.util.HashSet;
  57 import java.util.LinkedHashMap;
  58 import java.util.List;
  59 import java.util.Set;
  60 import java.util.Map;
  61 import java.util.HashMap;
  62 import java.util.Objects;
  63 import java.util.StringJoiner;
  64 import jdk.internal.HotSpotIntrinsicCandidate;
  65 import jdk.internal.loader.BootLoader;
  66 import jdk.internal.loader.BuiltinClassLoader;
  67 import jdk.internal.misc.Unsafe;
  68 import jdk.internal.misc.VM;
  69 import jdk.internal.reflect.CallerSensitive;
  70 import jdk.internal.reflect.ConstantPool;
  71 import jdk.internal.reflect.Reflection;
  72 import jdk.internal.reflect.ReflectionFactory;
  73 import jdk.internal.vm.annotation.ForceInline;
  74 import sun.reflect.generics.factory.CoreReflectionFactory;


 517     @Deprecated(since="9")
 518     public T newInstance()
 519         throws InstantiationException, IllegalAccessException
 520     {
 521         if (System.getSecurityManager() != null) {
 522             checkMemberAccess(Member.PUBLIC, Reflection.getCallerClass(), false);
 523         }
 524 
 525         // NOTE: the following code may not be strictly correct under
 526         // the current Java memory model.
 527 
 528         // Constructor lookup
 529         if (cachedConstructor == null) {
 530             if (this == Class.class) {
 531                 throw new IllegalAccessException(
 532                     "Can not call newInstance() on the Class for java.lang.Class"
 533                 );
 534             }
 535             try {
 536                 Class<?>[] empty = {};
 537                 final Constructor<T> c = getConstructor0(empty, Member.DECLARED);

 538                 // Disable accessibility checks on the constructor
 539                 // since we have to do the security check here anyway
 540                 // (the stack depth is wrong for the Constructor's
 541                 // security check to work)
 542                 java.security.AccessController.doPrivileged(
 543                     new java.security.PrivilegedAction<>() {
 544                         public Void run() {
 545                                 c.setAccessible(true);
 546                                 return null;
 547                             }
 548                         });
 549                 cachedConstructor = c;
 550             } catch (NoSuchMethodException e) {
 551                 throw (InstantiationException)
 552                     new InstantiationException(getName()).initCause(e);
 553             }
 554         }
 555         Constructor<T> tmpConstructor = cachedConstructor;
 556         // Security check (same as in java.lang.reflect.Constructor)
 557         Class<?> caller = Reflection.getCallerClass();


1009      *
1010      * <p>If this object represents an interface, the array contains objects
1011      * representing all interfaces directly extended by the interface.  The
1012      * order of the interface objects in the array corresponds to the order of
1013      * the interface names in the {@code extends} clause of the declaration of
1014      * the interface represented by this object.
1015      *
1016      * <p>If this object represents a class or interface that implements no
1017      * interfaces, the method returns an array of length 0.
1018      *
1019      * <p>If this object represents a primitive type or void, the method
1020      * returns an array of length 0.
1021      *
1022      * <p>If this {@code Class} object represents an array type, the
1023      * interfaces {@code Cloneable} and {@code java.io.Serializable} are
1024      * returned in that order.
1025      *
1026      * @return an array of interfaces directly implemented by this class
1027      */
1028     public Class<?>[] getInterfaces() {





1029         ReflectionData<T> rd = reflectionData();
1030         if (rd == null) {
1031             // no cloning required
1032             return getInterfaces0();
1033         } else {
1034             Class<?>[] interfaces = rd.interfaces;
1035             if (interfaces == null) {
1036                 interfaces = getInterfaces0();
1037                 rd.interfaces = interfaces;
1038             }
1039             // defensively copy before handing over to user code
1040             return interfaces.clone();
1041         }
1042     }
1043 
1044     private native Class<?>[] getInterfaces0();
1045 
1046     /**
1047      * Returns the {@code Type}s representing the interfaces
1048      * directly implemented by the class or interface represented by
1049      * this object.
1050      *
1051      * <p>If a superinterface is a parameterized type, the
1052      * {@code Type} object returned for it must accurately reflect
1053      * the actual type parameters used in the source code. The
1054      * parameterized type representing each superinterface is created
1055      * if it had not been created before. See the declaration of
1056      * {@link java.lang.reflect.ParameterizedType ParameterizedType}
1057      * for the semantics of the creation process for parameterized
1058      * types.
1059      *
1060      * <p>If this object represents a class, the return value is an array


1873      * @throws SecurityException
1874      *         If a security manager, <i>s</i>, is present and
1875      *         the caller's class loader is not the same as or an
1876      *         ancestor of the class loader for the current class and
1877      *         invocation of {@link SecurityManager#checkPackageAccess
1878      *         s.checkPackageAccess()} denies access to the package
1879      *         of this class.
1880      *
1881      * @since 1.1
1882      * @jls 8.2 Class Members
1883      * @jls 8.3 Field Declarations
1884      */
1885     @CallerSensitive
1886     public Field getField(String name)
1887         throws NoSuchFieldException, SecurityException {
1888         checkMemberAccess(Member.PUBLIC, Reflection.getCallerClass(), true);
1889         Field field = getField0(name);
1890         if (field == null) {
1891             throw new NoSuchFieldException(name);
1892         }
1893         return field;
1894     }
1895 
1896 
1897     /**
1898      * Returns a {@code Method} object that reflects the specified public
1899      * member method of the class or interface represented by this
1900      * {@code Class} object. The {@code name} parameter is a
1901      * {@code String} specifying the simple name of the desired method. The
1902      * {@code parameterTypes} parameter is an array of {@code Class}
1903      * objects that identify the method's formal parameter types, in declared
1904      * order. If {@code parameterTypes} is {@code null}, it is
1905      * treated as if it were an empty array.
1906      *
1907      * <p> If the {@code name} is "{@code <init>}" or "{@code <clinit>}" a
1908      * {@code NoSuchMethodException} is raised. Otherwise, the method to
1909      * be reflected is determined by the algorithm that follows.  Let C be the
1910      * class or interface represented by this object:
1911      * <OL>
1912      * <LI> C is searched for a <I>matching method</I>, as defined below. If a
1913      *      matching method is found, it is reflected.</LI>


1952      *         {@code name} and {@code parameterTypes}
1953      * @throws NoSuchMethodException if a matching method is not found
1954      *         or if the name is "&lt;init&gt;"or "&lt;clinit&gt;".
1955      * @throws NullPointerException if {@code name} is {@code null}
1956      * @throws SecurityException
1957      *         If a security manager, <i>s</i>, is present and
1958      *         the caller's class loader is not the same as or an
1959      *         ancestor of the class loader for the current class and
1960      *         invocation of {@link SecurityManager#checkPackageAccess
1961      *         s.checkPackageAccess()} denies access to the package
1962      *         of this class.
1963      *
1964      * @jls 8.2 Class Members
1965      * @jls 8.4 Method Declarations
1966      * @since 1.1
1967      */
1968     @CallerSensitive
1969     public Method getMethod(String name, Class<?>... parameterTypes)
1970         throws NoSuchMethodException, SecurityException {
1971         checkMemberAccess(Member.PUBLIC, Reflection.getCallerClass(), true);
1972         Method method = getMethod0(name, parameterTypes, true);
1973         if (method == null) {
1974             throw new NoSuchMethodException(getName() + "." + name + argumentTypesToString(parameterTypes));
1975         }
1976         return method;
1977     }
1978 
1979 
1980     /**
1981      * Returns a {@code Constructor} object that reflects the specified
1982      * public constructor of the class represented by this {@code Class}
1983      * object. The {@code parameterTypes} parameter is an array of
1984      * {@code Class} objects that identify the constructor's formal
1985      * parameter types, in declared order.
1986      *
1987      * If this {@code Class} object represents an inner class
1988      * declared in a non-static context, the formal parameter types
1989      * include the explicit enclosing instance as the first parameter.
1990      *
1991      * <p> The constructor to reflect is the public constructor of the class
1992      * represented by this {@code Class} object whose formal parameter
1993      * types match those specified by {@code parameterTypes}.
1994      *
1995      * @param parameterTypes the parameter array
1996      * @return the {@code Constructor} object of the public constructor that
1997      *         matches the specified {@code parameterTypes}
1998      * @throws NoSuchMethodException if a matching method is not found.
1999      * @throws SecurityException
2000      *         If a security manager, <i>s</i>, is present and
2001      *         the caller's class loader is not the same as or an
2002      *         ancestor of the class loader for the current class and
2003      *         invocation of {@link SecurityManager#checkPackageAccess
2004      *         s.checkPackageAccess()} denies access to the package
2005      *         of this class.
2006      *
2007      * @since 1.1
2008      */
2009     @CallerSensitive
2010     public Constructor<T> getConstructor(Class<?>... parameterTypes)
2011         throws NoSuchMethodException, SecurityException {
2012         checkMemberAccess(Member.PUBLIC, Reflection.getCallerClass(), true);
2013         return getConstructor0(parameterTypes, Member.PUBLIC);

2014     }
2015 
2016 
2017     /**
2018      * Returns an array of {@code Class} objects reflecting all the
2019      * classes and interfaces declared as members of the class represented by
2020      * this {@code Class} object. This includes public, protected, default
2021      * (package) access, and private classes and interfaces declared by the
2022      * class, but excludes inherited classes and interfaces.  This method
2023      * returns an array of length 0 if the class declares no classes or
2024      * interfaces as members, or if this {@code Class} object represents a
2025      * primitive type, an array class, or void.
2026      *
2027      * @return the array of {@code Class} objects representing all the
2028      *         declared members of this class
2029      * @throws SecurityException
2030      *         If a security manager, <i>s</i>, is present and any of the
2031      *         following conditions is met:
2032      *
2033      *         <ul>


2240      *          <li> the caller's class loader is not the same as or an
2241      *          ancestor of the class loader for the current class and
2242      *          invocation of {@link SecurityManager#checkPackageAccess
2243      *          s.checkPackageAccess()} denies access to the package
2244      *          of this class
2245      *
2246      *          </ul>
2247      *
2248      * @since 1.1
2249      * @jls 8.2 Class Members
2250      * @jls 8.3 Field Declarations
2251      */
2252     @CallerSensitive
2253     public Field getDeclaredField(String name)
2254         throws NoSuchFieldException, SecurityException {
2255         checkMemberAccess(Member.DECLARED, Reflection.getCallerClass(), true);
2256         Field field = searchFields(privateGetDeclaredFields(false), name);
2257         if (field == null) {
2258             throw new NoSuchFieldException(name);
2259         }
2260         return field;
2261     }
2262 
2263 
2264     /**
2265      * Returns a {@code Method} object that reflects the specified
2266      * declared method of the class or interface represented by this
2267      * {@code Class} object. The {@code name} parameter is a
2268      * {@code String} that specifies the simple name of the desired
2269      * method, and the {@code parameterTypes} parameter is an array of
2270      * {@code Class} objects that identify the method's formal parameter
2271      * types, in declared order.  If more than one method with the same
2272      * parameter types is declared in a class, and one of these methods has a
2273      * return type that is more specific than any of the others, that method is
2274      * returned; otherwise one of the methods is chosen arbitrarily.  If the
2275      * name is "&lt;init&gt;"or "&lt;clinit&gt;" a {@code NoSuchMethodException}
2276      * is raised.
2277      *
2278      * <p> If this {@code Class} object represents an array type, then this
2279      * method does not find the {@code clone()} method.
2280      *


2300      *          <li> the caller's class loader is not the same as or an
2301      *          ancestor of the class loader for the current class and
2302      *          invocation of {@link SecurityManager#checkPackageAccess
2303      *          s.checkPackageAccess()} denies access to the package
2304      *          of this class
2305      *
2306      *          </ul>
2307      *
2308      * @jls 8.2 Class Members
2309      * @jls 8.4 Method Declarations
2310      * @since 1.1
2311      */
2312     @CallerSensitive
2313     public Method getDeclaredMethod(String name, Class<?>... parameterTypes)
2314         throws NoSuchMethodException, SecurityException {
2315         checkMemberAccess(Member.DECLARED, Reflection.getCallerClass(), true);
2316         Method method = searchMethods(privateGetDeclaredMethods(false), name, parameterTypes);
2317         if (method == null) {
2318             throw new NoSuchMethodException(getName() + "." + name + argumentTypesToString(parameterTypes));
2319         }
2320         return method;
2321     }
2322 
2323 
2324     /**
2325      * Returns a {@code Constructor} object that reflects the specified
2326      * constructor of the class or interface represented by this
2327      * {@code Class} object.  The {@code parameterTypes} parameter is
2328      * an array of {@code Class} objects that identify the constructor's
2329      * formal parameter types, in declared order.
2330      *
2331      * If this {@code Class} object represents an inner class
2332      * declared in a non-static context, the formal parameter types
2333      * include the explicit enclosing instance as the first parameter.
2334      *
2335      * @param parameterTypes the parameter array
2336      * @return  The {@code Constructor} object for the constructor with the
2337      *          specified parameter list
2338      * @throws  NoSuchMethodException if a matching method is not found.
2339      * @throws  SecurityException
2340      *          If a security manager, <i>s</i>, is present and any of the


2346      *          class loader of this class and invocation of
2347      *          {@link SecurityManager#checkPermission
2348      *          s.checkPermission} method with
2349      *          {@code RuntimePermission("accessDeclaredMembers")}
2350      *          denies access to the declared constructor
2351      *
2352      *          <li> the caller's class loader is not the same as or an
2353      *          ancestor of the class loader for the current class and
2354      *          invocation of {@link SecurityManager#checkPackageAccess
2355      *          s.checkPackageAccess()} denies access to the package
2356      *          of this class
2357      *
2358      *          </ul>
2359      *
2360      * @since 1.1
2361      */
2362     @CallerSensitive
2363     public Constructor<T> getDeclaredConstructor(Class<?>... parameterTypes)
2364         throws NoSuchMethodException, SecurityException {
2365         checkMemberAccess(Member.DECLARED, Reflection.getCallerClass(), true);
2366         return getConstructor0(parameterTypes, Member.DECLARED);

2367     }
2368 
2369     /**
2370      * Finds a resource with a given name. If this class is in a named {@link
2371      * Module Module}, and the caller of this method is in the same module,
2372      * then this method will attempt to find the resource in that module.
2373      * Otherwise, the rules for searching resources
2374      * associated with a given class are implemented by the defining
2375      * {@linkplain ClassLoader class loader} of the class.  This method
2376      * delegates to this object's class loader.  If this object was loaded by
2377      * the bootstrap class loader, the method delegates to {@link
2378      * ClassLoader#getSystemResourceAsStream}.
2379      *
2380      * <p> Before finding a resource in the caller's module or delegation to a
2381      * class loader, an absolute resource name is constructed from the given
2382      * resource name using this algorithm:
2383      *
2384      * <ul>
2385      *
2386      * <li> If the {@code name} begins with a {@code '/'}


2939     private Method[] privateGetDeclaredMethods(boolean publicOnly) {
2940         checkInitted();
2941         Method[] res;
2942         ReflectionData<T> rd = reflectionData();
2943         if (rd != null) {
2944             res = publicOnly ? rd.declaredPublicMethods : rd.declaredMethods;
2945             if (res != null) return res;
2946         }
2947         // No cached value available; request value from VM
2948         res = Reflection.filterMethods(this, getDeclaredMethods0(publicOnly));
2949         if (rd != null) {
2950             if (publicOnly) {
2951                 rd.declaredPublicMethods = res;
2952             } else {
2953                 rd.declaredMethods = res;
2954             }
2955         }
2956         return res;
2957     }
2958 
2959     static class MethodArray {
2960         // Don't add or remove methods except by add() or remove() calls.
2961         private Method[] methods;
2962         private int length;
2963         private int defaults;
2964 
2965         MethodArray() {
2966             this(20);
2967         }
2968 
2969         MethodArray(int initialSize) {
2970             if (initialSize < 2)
2971                 throw new IllegalArgumentException("Size should be 2 or more");
2972 
2973             methods = new Method[initialSize];
2974             length = 0;
2975             defaults = 0;
2976         }
2977 
2978         boolean hasDefaults() {
2979             return defaults != 0;
2980         }
2981 
2982         void add(Method m) {
2983             if (length == methods.length) {
2984                 methods = Arrays.copyOf(methods, 2 * methods.length);
2985             }
2986             methods[length++] = m;
2987 
2988             if (m != null && m.isDefault())
2989                 defaults++;
2990         }
2991 
2992         void addAll(Method[] ma) {
2993             for (Method m : ma) {
2994                 add(m);
2995             }
2996         }
2997 
2998         void addAll(MethodArray ma) {
2999             for (int i = 0; i < ma.length(); i++) {
3000                 add(ma.get(i));
3001             }
3002         }
3003 
3004         void addIfNotPresent(Method newMethod) {
3005             for (int i = 0; i < length; i++) {
3006                 Method m = methods[i];
3007                 if (m == newMethod || (m != null && m.equals(newMethod))) {
3008                     return;
3009                 }
3010             }
3011             add(newMethod);
3012         }
3013 
3014         void addAllIfNotPresent(MethodArray newMethods) {
3015             for (int i = 0; i < newMethods.length(); i++) {
3016                 Method m = newMethods.get(i);
3017                 if (m != null) {
3018                     addIfNotPresent(m);
3019                 }
3020             }
3021         }
3022 
3023         /* Add Methods declared in an interface to this MethodArray.
3024          * Static methods declared in interfaces are not inherited.
3025          */
3026         void addInterfaceMethods(Method[] methods) {
3027             for (Method candidate : methods) {
3028                 if (!Modifier.isStatic(candidate.getModifiers())) {
3029                     add(candidate);
3030                 }
3031             }
3032         }
3033 
3034         int length() {
3035             return length;
3036         }
3037 
3038         Method get(int i) {
3039             return methods[i];
3040         }
3041 
3042         Method getFirst() {
3043             for (Method m : methods)
3044                 if (m != null)
3045                     return m;
3046             return null;
3047         }
3048 
3049         void removeByNameAndDescriptor(Method toRemove) {
3050             for (int i = 0; i < length; i++) {
3051                 Method m = methods[i];
3052                 if (m != null && matchesNameAndDescriptor(m, toRemove)) {
3053                     remove(i);
3054                 }
3055             }
3056         }
3057 
3058         private void remove(int i) {
3059             if (methods[i] != null && methods[i].isDefault())
3060                 defaults--;
3061                     methods[i] = null;
3062                 }
3063 
3064         private boolean matchesNameAndDescriptor(Method m1, Method m2) {
3065             return m1.getReturnType() == m2.getReturnType() &&
3066                    m1.getName() == m2.getName() && // name is guaranteed to be interned
3067                    arrayContentsEq(m1.getParameterTypes(),
3068                            m2.getParameterTypes());
3069             }
3070 
3071         void compactAndTrim() {
3072             int newPos = 0;
3073             // Get rid of null slots
3074             for (int pos = 0; pos < length; pos++) {
3075                 Method m = methods[pos];
3076                 if (m != null) {
3077                     if (pos != newPos) {
3078                         methods[newPos] = m;
3079                     }
3080                     newPos++;
3081                 }
3082             }
3083             if (newPos != methods.length) {
3084                 methods = Arrays.copyOf(methods, newPos);
3085             }
3086         }
3087 
3088         /* Removes all Methods from this MethodArray that have a more specific
3089          * default Method in this MethodArray.
3090          *
3091          * Users of MethodArray are responsible for pruning Methods that have
3092          * a more specific <em>concrete</em> Method.
3093          */
3094         void removeLessSpecifics() {
3095             if (!hasDefaults())
3096                 return;
3097 
3098             for (int i = 0; i < length; i++) {
3099                 Method m = get(i);
3100                 if  (m == null || !m.isDefault())
3101                     continue;
3102 
3103                 for (int j  = 0; j < length; j++) {
3104                     if (i == j)
3105                         continue;
3106 
3107                     Method candidate = get(j);
3108                     if (candidate == null)
3109                         continue;
3110 
3111                     if (!matchesNameAndDescriptor(m, candidate))
3112                         continue;
3113 
3114                     if (hasMoreSpecificClass(m, candidate))
3115                         remove(j);
3116                 }
3117             }
3118         }
3119 
3120         Method[] getArray() {
3121             return methods;
3122         }
3123 
3124         // Returns true if m1 is more specific than m2
3125         static boolean hasMoreSpecificClass(Method m1, Method m2) {
3126             Class<?> m1Class = m1.getDeclaringClass();
3127             Class<?> m2Class = m2.getDeclaringClass();
3128             return m1Class != m2Class && m2Class.isAssignableFrom(m1Class);
3129         }
3130     }
3131 
3132 
3133     // Returns an array of "root" methods. These Method objects must NOT
3134     // be propagated to the outside world, but must instead be copied
3135     // via ReflectionFactory.copyMethod.
3136     private Method[] privateGetPublicMethods() {
3137         checkInitted();
3138         Method[] res;
3139         ReflectionData<T> rd = reflectionData();
3140         if (rd != null) {
3141             res = rd.publicMethods;
3142             if (res != null) return res;
3143         }
3144 
3145         // No cached value available; compute value recursively.
3146         // Start by fetching public declared methods
3147         MethodArray methods = new MethodArray();
3148         {
3149             Method[] tmp = privateGetDeclaredMethods(true);
3150             methods.addAll(tmp);
3151         }
3152         // Now recur over superclass and direct superinterfaces.
3153         // Go over superinterfaces first so we can more easily filter
3154         // out concrete implementations inherited from superclasses at
3155         // the end.
3156         MethodArray inheritedMethods = new MethodArray();
3157         for (Class<?> i : getInterfaces()) {
3158             inheritedMethods.addInterfaceMethods(i.privateGetPublicMethods());
3159         }
3160         if (!isInterface()) {
3161             Class<?> c = getSuperclass();
3162             if (c != null) {
3163                 MethodArray supers = new MethodArray();
3164                 supers.addAll(c.privateGetPublicMethods());
3165                 // Filter out concrete implementations of any
3166                 // interface methods
3167                 for (int i = 0; i < supers.length(); i++) {
3168                     Method m = supers.get(i);
3169                     if (m != null &&
3170                             !Modifier.isAbstract(m.getModifiers()) &&
3171                             !m.isDefault()) {
3172                         inheritedMethods.removeByNameAndDescriptor(m);
3173                     }
3174                 }
3175                 // Insert superclass's inherited methods before
3176                 // superinterfaces' to satisfy getMethod's search
3177                 // order
3178                 supers.addAll(inheritedMethods);
3179                 inheritedMethods = supers;
3180             }
3181         }
3182         // Filter out all local methods from inherited ones
3183         for (int i = 0; i < methods.length(); i++) {
3184             Method m = methods.get(i);
3185             inheritedMethods.removeByNameAndDescriptor(m);
3186         }
3187         methods.addAllIfNotPresent(inheritedMethods);
3188         methods.removeLessSpecifics();
3189         methods.compactAndTrim();
3190         res = methods.getArray();
3191         if (rd != null) {
3192             rd.publicMethods = res;
3193         }
3194         return res;
3195     }
3196 
3197 
3198     //
3199     // Helpers for fetchers of one field, method, or constructor
3200     //
3201 

3202     private static Field searchFields(Field[] fields, String name) {
3203         String internedName = name.intern();
3204         for (Field field : fields) {
3205             if (field.getName() == internedName) {
3206                 return getReflectionFactory().copyField(field);
3207             }
3208         }
3209         return null;
3210     }
3211 
3212     private Field getField0(String name) throws NoSuchFieldException {



3213         // Note: the intent is that the search algorithm this routine
3214         // uses be equivalent to the ordering imposed by
3215         // privateGetPublicFields(). It fetches only the declared
3216         // public fields for each class, however, to reduce the number
3217         // of Field objects which have to be created for the common
3218         // case where the field being requested is declared in the
3219         // class which is being queried.
3220         Field res;
3221         // Search declared public fields
3222         if ((res = searchFields(privateGetDeclaredFields(true), name)) != null) {
3223             return res;
3224         }
3225         // Direct superinterfaces, recursively
3226         Class<?>[] interfaces = getInterfaces();
3227         for (Class<?> c : interfaces) {
3228             if ((res = c.getField0(name)) != null) {
3229                 return res;
3230             }
3231         }
3232         // Direct superclass, recursively
3233         if (!isInterface()) {
3234             Class<?> c = getSuperclass();
3235             if (c != null) {
3236                 if ((res = c.getField0(name)) != null) {
3237                     return res;
3238                 }
3239             }
3240         }
3241         return null;
3242     }
3243 

3244     private static Method searchMethods(Method[] methods,
3245                                         String name,
3246                                         Class<?>[] parameterTypes)
3247     {

3248         Method res = null;
3249         String internedName = name.intern();
3250         for (Method m : methods) {
3251             if (m.getName() == internedName
3252                 && arrayContentsEq(parameterTypes, m.getParameterTypes())

3253                 && (res == null
3254                     || res.getReturnType().isAssignableFrom(m.getReturnType())))

3255                 res = m;
3256         }
3257 
3258         return (res == null ? res : getReflectionFactory().copyMethod(res));
3259     }
3260 
3261     private Method getMethod0(String name, Class<?>[] parameterTypes, boolean includeStaticMethods) {
3262         MethodArray interfaceCandidates = new MethodArray(2);
3263         Method res =  privateGetMethodRecursive(name, parameterTypes, includeStaticMethods, interfaceCandidates);
3264         if (res != null)
3265             return res;
3266 
3267         // Not found on class or superclass directly
3268         interfaceCandidates.removeLessSpecifics();
3269         return interfaceCandidates.getFirst(); // may be null






3270     }
3271 
3272     private Method privateGetMethodRecursive(String name,



3273             Class<?>[] parameterTypes,
3274             boolean includeStaticMethods,
3275             MethodArray allInterfaceCandidates) {
3276         // Note: the intent is that the search algorithm this routine
3277         // uses be equivalent to the ordering imposed by
3278         // privateGetPublicMethods(). It fetches only the declared
3279         // public methods for each class, however, to reduce the
3280         // number of Method objects which have to be created for the
3281         // common case where the method being requested is declared in
3282         // the class which is being queried.
3283         //
3284         // Due to default methods, unless a method is found on a superclass,
3285         // methods declared in any superinterface needs to be considered.
3286         // Collect all candidates declared in superinterfaces in {@code
3287         // allInterfaceCandidates} and select the most specific if no match on
3288         // a superclass is found.
3289 
3290         // Must _not_ return root methods
3291         Method res;
3292         // Search declared public methods
3293         if ((res = searchMethods(privateGetDeclaredMethods(true),
3294                                  name,
3295                                  parameterTypes)) != null) {
3296             if (includeStaticMethods || !Modifier.isStatic(res.getModifiers()))
3297                 return res;
3298         }
3299         // Search superclass's methods
3300         if (!isInterface()) {
3301             Class<? super T> c = getSuperclass();
3302             if (c != null) {
3303                 if ((res = c.getMethod0(name, parameterTypes, true)) != null) {
3304                     return res;
3305                 }






3306             }







3307         }
3308         // Search superinterfaces' methods
3309         Class<?>[] interfaces = getInterfaces();
3310         for (Class<?> c : interfaces)
3311             if ((res = c.getMethod0(name, parameterTypes, false)) != null)
3312                 allInterfaceCandidates.add(res);
3313         // Not found
3314         return null;
3315     }
3316 



3317     private Constructor<T> getConstructor0(Class<?>[] parameterTypes,
3318                                         int which) throws NoSuchMethodException
3319     {

3320         Constructor<T>[] constructors = privateGetDeclaredConstructors((which == Member.PUBLIC));
3321         for (Constructor<T> constructor : constructors) {
3322             if (arrayContentsEq(parameterTypes,
3323                                 constructor.getParameterTypes())) {
3324                 return getReflectionFactory().copyConstructor(constructor);
3325             }
3326         }
3327         throw new NoSuchMethodException(getName() + ".<init>" + argumentTypesToString(parameterTypes));
3328     }
3329 
3330     //
3331     // Other helpers and base implementation
3332     //
3333 
3334     private static boolean arrayContentsEq(Object[] a1, Object[] a2) {
3335         if (a1 == null) {
3336             return a2 == null || a2.length == 0;
3337         }
3338 
3339         if (a2 == null) {
3340             return a1.length == 0;
3341         }
3342 
3343         if (a1.length != a2.length) {
3344             return false;




  34 import java.lang.reflect.Member;
  35 import java.lang.reflect.Field;
  36 import java.lang.reflect.Executable;
  37 import java.lang.reflect.Method;
  38 import java.lang.reflect.Module;
  39 import java.lang.reflect.Constructor;
  40 import java.lang.reflect.Modifier;
  41 import java.lang.reflect.Type;
  42 import java.lang.reflect.TypeVariable;
  43 import java.lang.reflect.InvocationTargetException;
  44 import java.lang.reflect.AnnotatedType;
  45 import java.lang.reflect.Proxy;
  46 import java.lang.ref.SoftReference;
  47 import java.io.IOException;
  48 import java.io.InputStream;
  49 import java.io.ObjectStreamField;
  50 import java.net.URL;
  51 import java.security.AccessController;
  52 import java.security.PrivilegedAction;
  53 import java.util.ArrayList;

  54 import java.util.Collection;
  55 import java.util.HashSet;
  56 import java.util.LinkedHashMap;
  57 import java.util.List;
  58 import java.util.Set;
  59 import java.util.Map;
  60 import java.util.HashMap;
  61 import java.util.Objects;
  62 import java.util.StringJoiner;
  63 import jdk.internal.HotSpotIntrinsicCandidate;
  64 import jdk.internal.loader.BootLoader;
  65 import jdk.internal.loader.BuiltinClassLoader;
  66 import jdk.internal.misc.Unsafe;
  67 import jdk.internal.misc.VM;
  68 import jdk.internal.reflect.CallerSensitive;
  69 import jdk.internal.reflect.ConstantPool;
  70 import jdk.internal.reflect.Reflection;
  71 import jdk.internal.reflect.ReflectionFactory;
  72 import jdk.internal.vm.annotation.ForceInline;
  73 import sun.reflect.generics.factory.CoreReflectionFactory;


 516     @Deprecated(since="9")
 517     public T newInstance()
 518         throws InstantiationException, IllegalAccessException
 519     {
 520         if (System.getSecurityManager() != null) {
 521             checkMemberAccess(Member.PUBLIC, Reflection.getCallerClass(), false);
 522         }
 523 
 524         // NOTE: the following code may not be strictly correct under
 525         // the current Java memory model.
 526 
 527         // Constructor lookup
 528         if (cachedConstructor == null) {
 529             if (this == Class.class) {
 530                 throw new IllegalAccessException(
 531                     "Can not call newInstance() on the Class for java.lang.Class"
 532                 );
 533             }
 534             try {
 535                 Class<?>[] empty = {};
 536                 final Constructor<T> c = getReflectionFactory().copyConstructor(
 537                     getConstructor0(empty, Member.DECLARED));
 538                 // Disable accessibility checks on the constructor
 539                 // since we have to do the security check here anyway
 540                 // (the stack depth is wrong for the Constructor's
 541                 // security check to work)
 542                 java.security.AccessController.doPrivileged(
 543                     new java.security.PrivilegedAction<>() {
 544                         public Void run() {
 545                                 c.setAccessible(true);
 546                                 return null;
 547                             }
 548                         });
 549                 cachedConstructor = c;
 550             } catch (NoSuchMethodException e) {
 551                 throw (InstantiationException)
 552                     new InstantiationException(getName()).initCause(e);
 553             }
 554         }
 555         Constructor<T> tmpConstructor = cachedConstructor;
 556         // Security check (same as in java.lang.reflect.Constructor)
 557         Class<?> caller = Reflection.getCallerClass();


1009      *
1010      * <p>If this object represents an interface, the array contains objects
1011      * representing all interfaces directly extended by the interface.  The
1012      * order of the interface objects in the array corresponds to the order of
1013      * the interface names in the {@code extends} clause of the declaration of
1014      * the interface represented by this object.
1015      *
1016      * <p>If this object represents a class or interface that implements no
1017      * interfaces, the method returns an array of length 0.
1018      *
1019      * <p>If this object represents a primitive type or void, the method
1020      * returns an array of length 0.
1021      *
1022      * <p>If this {@code Class} object represents an array type, the
1023      * interfaces {@code Cloneable} and {@code java.io.Serializable} are
1024      * returned in that order.
1025      *
1026      * @return an array of interfaces directly implemented by this class
1027      */
1028     public Class<?>[] getInterfaces() {
1029         // defensively copy before handing over to user code
1030         return getInterfaces(true);
1031     }
1032 
1033     private Class<?>[] getInterfaces(boolean cloneArray) {
1034         ReflectionData<T> rd = reflectionData();
1035         if (rd == null) {
1036             // no cloning required
1037             return getInterfaces0();
1038         } else {
1039             Class<?>[] interfaces = rd.interfaces;
1040             if (interfaces == null) {
1041                 interfaces = getInterfaces0();
1042                 rd.interfaces = interfaces;
1043             }
1044             // defensively copy if requested
1045             return cloneArray ? interfaces.clone() : interfaces;
1046         }
1047     }
1048 
1049     private native Class<?>[] getInterfaces0();
1050 
1051     /**
1052      * Returns the {@code Type}s representing the interfaces
1053      * directly implemented by the class or interface represented by
1054      * this object.
1055      *
1056      * <p>If a superinterface is a parameterized type, the
1057      * {@code Type} object returned for it must accurately reflect
1058      * the actual type parameters used in the source code. The
1059      * parameterized type representing each superinterface is created
1060      * if it had not been created before. See the declaration of
1061      * {@link java.lang.reflect.ParameterizedType ParameterizedType}
1062      * for the semantics of the creation process for parameterized
1063      * types.
1064      *
1065      * <p>If this object represents a class, the return value is an array


1878      * @throws SecurityException
1879      *         If a security manager, <i>s</i>, is present and
1880      *         the caller's class loader is not the same as or an
1881      *         ancestor of the class loader for the current class and
1882      *         invocation of {@link SecurityManager#checkPackageAccess
1883      *         s.checkPackageAccess()} denies access to the package
1884      *         of this class.
1885      *
1886      * @since 1.1
1887      * @jls 8.2 Class Members
1888      * @jls 8.3 Field Declarations
1889      */
1890     @CallerSensitive
1891     public Field getField(String name)
1892         throws NoSuchFieldException, SecurityException {
1893         checkMemberAccess(Member.PUBLIC, Reflection.getCallerClass(), true);
1894         Field field = getField0(name);
1895         if (field == null) {
1896             throw new NoSuchFieldException(name);
1897         }
1898         return getReflectionFactory().copyField(field);
1899     }
1900 
1901 
1902     /**
1903      * Returns a {@code Method} object that reflects the specified public
1904      * member method of the class or interface represented by this
1905      * {@code Class} object. The {@code name} parameter is a
1906      * {@code String} specifying the simple name of the desired method. The
1907      * {@code parameterTypes} parameter is an array of {@code Class}
1908      * objects that identify the method's formal parameter types, in declared
1909      * order. If {@code parameterTypes} is {@code null}, it is
1910      * treated as if it were an empty array.
1911      *
1912      * <p> If the {@code name} is "{@code <init>}" or "{@code <clinit>}" a
1913      * {@code NoSuchMethodException} is raised. Otherwise, the method to
1914      * be reflected is determined by the algorithm that follows.  Let C be the
1915      * class or interface represented by this object:
1916      * <OL>
1917      * <LI> C is searched for a <I>matching method</I>, as defined below. If a
1918      *      matching method is found, it is reflected.</LI>


1957      *         {@code name} and {@code parameterTypes}
1958      * @throws NoSuchMethodException if a matching method is not found
1959      *         or if the name is "&lt;init&gt;"or "&lt;clinit&gt;".
1960      * @throws NullPointerException if {@code name} is {@code null}
1961      * @throws SecurityException
1962      *         If a security manager, <i>s</i>, is present and
1963      *         the caller's class loader is not the same as or an
1964      *         ancestor of the class loader for the current class and
1965      *         invocation of {@link SecurityManager#checkPackageAccess
1966      *         s.checkPackageAccess()} denies access to the package
1967      *         of this class.
1968      *
1969      * @jls 8.2 Class Members
1970      * @jls 8.4 Method Declarations
1971      * @since 1.1
1972      */
1973     @CallerSensitive
1974     public Method getMethod(String name, Class<?>... parameterTypes)
1975         throws NoSuchMethodException, SecurityException {
1976         checkMemberAccess(Member.PUBLIC, Reflection.getCallerClass(), true);
1977         Method method = getMethod0(name, parameterTypes);
1978         if (method == null) {
1979             throw new NoSuchMethodException(getName() + "." + name + argumentTypesToString(parameterTypes));
1980         }
1981         return getReflectionFactory().copyMethod(method);
1982     }
1983 
1984 
1985     /**
1986      * Returns a {@code Constructor} object that reflects the specified
1987      * public constructor of the class represented by this {@code Class}
1988      * object. The {@code parameterTypes} parameter is an array of
1989      * {@code Class} objects that identify the constructor's formal
1990      * parameter types, in declared order.
1991      *
1992      * If this {@code Class} object represents an inner class
1993      * declared in a non-static context, the formal parameter types
1994      * include the explicit enclosing instance as the first parameter.
1995      *
1996      * <p> The constructor to reflect is the public constructor of the class
1997      * represented by this {@code Class} object whose formal parameter
1998      * types match those specified by {@code parameterTypes}.
1999      *
2000      * @param parameterTypes the parameter array
2001      * @return the {@code Constructor} object of the public constructor that
2002      *         matches the specified {@code parameterTypes}
2003      * @throws NoSuchMethodException if a matching method is not found.
2004      * @throws SecurityException
2005      *         If a security manager, <i>s</i>, is present and
2006      *         the caller's class loader is not the same as or an
2007      *         ancestor of the class loader for the current class and
2008      *         invocation of {@link SecurityManager#checkPackageAccess
2009      *         s.checkPackageAccess()} denies access to the package
2010      *         of this class.
2011      *
2012      * @since 1.1
2013      */
2014     @CallerSensitive
2015     public Constructor<T> getConstructor(Class<?>... parameterTypes)
2016         throws NoSuchMethodException, SecurityException {
2017         checkMemberAccess(Member.PUBLIC, Reflection.getCallerClass(), true);
2018         return getReflectionFactory().copyConstructor(
2019             getConstructor0(parameterTypes, Member.PUBLIC));
2020     }
2021 
2022 
2023     /**
2024      * Returns an array of {@code Class} objects reflecting all the
2025      * classes and interfaces declared as members of the class represented by
2026      * this {@code Class} object. This includes public, protected, default
2027      * (package) access, and private classes and interfaces declared by the
2028      * class, but excludes inherited classes and interfaces.  This method
2029      * returns an array of length 0 if the class declares no classes or
2030      * interfaces as members, or if this {@code Class} object represents a
2031      * primitive type, an array class, or void.
2032      *
2033      * @return the array of {@code Class} objects representing all the
2034      *         declared members of this class
2035      * @throws SecurityException
2036      *         If a security manager, <i>s</i>, is present and any of the
2037      *         following conditions is met:
2038      *
2039      *         <ul>


2246      *          <li> the caller's class loader is not the same as or an
2247      *          ancestor of the class loader for the current class and
2248      *          invocation of {@link SecurityManager#checkPackageAccess
2249      *          s.checkPackageAccess()} denies access to the package
2250      *          of this class
2251      *
2252      *          </ul>
2253      *
2254      * @since 1.1
2255      * @jls 8.2 Class Members
2256      * @jls 8.3 Field Declarations
2257      */
2258     @CallerSensitive
2259     public Field getDeclaredField(String name)
2260         throws NoSuchFieldException, SecurityException {
2261         checkMemberAccess(Member.DECLARED, Reflection.getCallerClass(), true);
2262         Field field = searchFields(privateGetDeclaredFields(false), name);
2263         if (field == null) {
2264             throw new NoSuchFieldException(name);
2265         }
2266         return getReflectionFactory().copyField(field);
2267     }
2268 
2269 
2270     /**
2271      * Returns a {@code Method} object that reflects the specified
2272      * declared method of the class or interface represented by this
2273      * {@code Class} object. The {@code name} parameter is a
2274      * {@code String} that specifies the simple name of the desired
2275      * method, and the {@code parameterTypes} parameter is an array of
2276      * {@code Class} objects that identify the method's formal parameter
2277      * types, in declared order.  If more than one method with the same
2278      * parameter types is declared in a class, and one of these methods has a
2279      * return type that is more specific than any of the others, that method is
2280      * returned; otherwise one of the methods is chosen arbitrarily.  If the
2281      * name is "&lt;init&gt;"or "&lt;clinit&gt;" a {@code NoSuchMethodException}
2282      * is raised.
2283      *
2284      * <p> If this {@code Class} object represents an array type, then this
2285      * method does not find the {@code clone()} method.
2286      *


2306      *          <li> the caller's class loader is not the same as or an
2307      *          ancestor of the class loader for the current class and
2308      *          invocation of {@link SecurityManager#checkPackageAccess
2309      *          s.checkPackageAccess()} denies access to the package
2310      *          of this class
2311      *
2312      *          </ul>
2313      *
2314      * @jls 8.2 Class Members
2315      * @jls 8.4 Method Declarations
2316      * @since 1.1
2317      */
2318     @CallerSensitive
2319     public Method getDeclaredMethod(String name, Class<?>... parameterTypes)
2320         throws NoSuchMethodException, SecurityException {
2321         checkMemberAccess(Member.DECLARED, Reflection.getCallerClass(), true);
2322         Method method = searchMethods(privateGetDeclaredMethods(false), name, parameterTypes);
2323         if (method == null) {
2324             throw new NoSuchMethodException(getName() + "." + name + argumentTypesToString(parameterTypes));
2325         }
2326         return getReflectionFactory().copyMethod(method);
2327     }
2328 
2329 
2330     /**
2331      * Returns a {@code Constructor} object that reflects the specified
2332      * constructor of the class or interface represented by this
2333      * {@code Class} object.  The {@code parameterTypes} parameter is
2334      * an array of {@code Class} objects that identify the constructor's
2335      * formal parameter types, in declared order.
2336      *
2337      * If this {@code Class} object represents an inner class
2338      * declared in a non-static context, the formal parameter types
2339      * include the explicit enclosing instance as the first parameter.
2340      *
2341      * @param parameterTypes the parameter array
2342      * @return  The {@code Constructor} object for the constructor with the
2343      *          specified parameter list
2344      * @throws  NoSuchMethodException if a matching method is not found.
2345      * @throws  SecurityException
2346      *          If a security manager, <i>s</i>, is present and any of the


2352      *          class loader of this class and invocation of
2353      *          {@link SecurityManager#checkPermission
2354      *          s.checkPermission} method with
2355      *          {@code RuntimePermission("accessDeclaredMembers")}
2356      *          denies access to the declared constructor
2357      *
2358      *          <li> the caller's class loader is not the same as or an
2359      *          ancestor of the class loader for the current class and
2360      *          invocation of {@link SecurityManager#checkPackageAccess
2361      *          s.checkPackageAccess()} denies access to the package
2362      *          of this class
2363      *
2364      *          </ul>
2365      *
2366      * @since 1.1
2367      */
2368     @CallerSensitive
2369     public Constructor<T> getDeclaredConstructor(Class<?>... parameterTypes)
2370         throws NoSuchMethodException, SecurityException {
2371         checkMemberAccess(Member.DECLARED, Reflection.getCallerClass(), true);
2372         return getReflectionFactory().copyConstructor(
2373             getConstructor0(parameterTypes, Member.DECLARED));
2374     }
2375 
2376     /**
2377      * Finds a resource with a given name. If this class is in a named {@link
2378      * Module Module}, and the caller of this method is in the same module,
2379      * then this method will attempt to find the resource in that module.
2380      * Otherwise, the rules for searching resources
2381      * associated with a given class are implemented by the defining
2382      * {@linkplain ClassLoader class loader} of the class.  This method
2383      * delegates to this object's class loader.  If this object was loaded by
2384      * the bootstrap class loader, the method delegates to {@link
2385      * ClassLoader#getSystemResourceAsStream}.
2386      *
2387      * <p> Before finding a resource in the caller's module or delegation to a
2388      * class loader, an absolute resource name is constructed from the given
2389      * resource name using this algorithm:
2390      *
2391      * <ul>
2392      *
2393      * <li> If the {@code name} begins with a {@code '/'}


2946     private Method[] privateGetDeclaredMethods(boolean publicOnly) {
2947         checkInitted();
2948         Method[] res;
2949         ReflectionData<T> rd = reflectionData();
2950         if (rd != null) {
2951             res = publicOnly ? rd.declaredPublicMethods : rd.declaredMethods;
2952             if (res != null) return res;
2953         }
2954         // No cached value available; request value from VM
2955         res = Reflection.filterMethods(this, getDeclaredMethods0(publicOnly));
2956         if (rd != null) {
2957             if (publicOnly) {
2958                 rd.declaredPublicMethods = res;
2959             } else {
2960                 rd.declaredMethods = res;
2961             }
2962         }
2963         return res;
2964     }
2965 














































































































































































2966     // Returns an array of "root" methods. These Method objects must NOT
2967     // be propagated to the outside world, but must instead be copied
2968     // via ReflectionFactory.copyMethod.
2969     private Method[] privateGetPublicMethods() {
2970         checkInitted();
2971         Method[] res;
2972         ReflectionData<T> rd = reflectionData();
2973         if (rd != null) {
2974             res = rd.publicMethods;
2975             if (res != null) return res;
2976         }
2977 
2978         // No cached value available; compute value recursively.
2979         // Start by fetching public declared methods...
2980         PublicMethods pms = new PublicMethods();
2981         for (Method m : privateGetDeclaredMethods(/* publicOnly */ true)) {
2982             pms.consolidate(m);

2983         }
2984         // ...then recur over superclass methods...
2985         Class<?> sc = getSuperclass();
2986         if (sc != null) {
2987             for (Method m : sc.privateGetPublicMethods()) {
2988                 pms.consolidate(m);


2989             }
2990         }
2991         // ...and finally over direct superinterfaces.
2992         for (Class<?> intf : getInterfaces(/* cloneArray */ false)) {
2993             for (Method m : intf.privateGetPublicMethods()) {
2994                 // static interface methods are not inherited
2995                 if (!Modifier.isStatic(m.getModifiers())) {
2996                     pms.consolidate(m);
2997                 }
2998             }
2999         }
3000 
3001         res = pms.toArray();



















3002         if (rd != null) {
3003             rd.publicMethods = res;
3004         }
3005         return res;
3006     }
3007 
3008 
3009     //
3010     // Helpers for fetchers of one field, method, or constructor
3011     //
3012 
3013     // This method does not copy the returned Field object!
3014     private static Field searchFields(Field[] fields, String name) {

3015         for (Field field : fields) {
3016             if (field.getName().equals(name)) {
3017                 return field;
3018             }
3019         }
3020         return null;
3021     }
3022 
3023     // Returns a "root" Field object. This Field object must NOT
3024     // be propagated to the outside world, but must instead be copied
3025     // via ReflectionFactory.copyField.
3026     private Field getField0(String name) {
3027         // Note: the intent is that the search algorithm this routine
3028         // uses be equivalent to the ordering imposed by
3029         // privateGetPublicFields(). It fetches only the declared
3030         // public fields for each class, however, to reduce the number
3031         // of Field objects which have to be created for the common
3032         // case where the field being requested is declared in the
3033         // class which is being queried.
3034         Field res;
3035         // Search declared public fields
3036         if ((res = searchFields(privateGetDeclaredFields(true), name)) != null) {
3037             return res;
3038         }
3039         // Direct superinterfaces, recursively
3040         Class<?>[] interfaces = getInterfaces(/* cloneArray */ false);
3041         for (Class<?> c : interfaces) {
3042             if ((res = c.getField0(name)) != null) {
3043                 return res;
3044             }
3045         }
3046         // Direct superclass, recursively
3047         if (!isInterface()) {
3048             Class<?> c = getSuperclass();
3049             if (c != null) {
3050                 if ((res = c.getField0(name)) != null) {
3051                     return res;
3052                 }
3053             }
3054         }
3055         return null;
3056     }
3057 
3058     // This method does not copy the returned Method object!
3059     private static Method searchMethods(Method[] methods,
3060                                         String name,
3061                                         Class<?>[] parameterTypes)
3062     {
3063         ReflectionFactory fact = getReflectionFactory();
3064         Method res = null;

3065         for (Method m : methods) {
3066             if (m.getName().equals(name)
3067                 && arrayContentsEq(parameterTypes,
3068                                    fact.getExecutableSharedParameterTypes(m))
3069                 && (res == null
3070                     || (res.getReturnType() != m.getReturnType()
3071                         && res.getReturnType().isAssignableFrom(m.getReturnType()))))
3072                 res = m;
3073         }
3074         return res;

3075     }
3076 
3077     private static final Class<?>[] EMPTY_CLASS_ARRAY = new Class<?>[0];




3078 
3079     // Returns a "root" Method object. This Method object must NOT
3080     // be propagated to the outside world, but must instead be copied
3081     // via ReflectionFactory.copyMethod.
3082     private Method getMethod0(String name, Class<?>[] parameterTypes) {
3083         PublicMethods.MethodList res = getMethodsRecursive(
3084             name,
3085             parameterTypes == null ? EMPTY_CLASS_ARRAY : parameterTypes,
3086             /* includeStatic */ true);
3087         return res == null ? null : res.getMostSpecific();
3088     }
3089 
3090     // Returns a list of "root" Method objects. These Method objects must NOT
3091     // be propagated to the outside world, but must instead be copied
3092     // via ReflectionFactory.copyMethod.
3093     private PublicMethods.MethodList getMethodsRecursive(String name,
3094                                                          Class<?>[] parameterTypes,
3095                                                          boolean includeStatic) {
3096         // 1st check declared public methods
3097         Method[] methods = privateGetDeclaredMethods(/* publicOnly */ true);
3098         PublicMethods.MethodList res = PublicMethods.MethodList
3099             .filter(methods, name, parameterTypes, includeStatic);
3100         // if there is at least one match among declared methods, we need not
3101         // search any further as such match surely overrides matching methods
3102         // declared in superclass(es) or interface(s).
3103         if (res != null) {





















3104             return res;
3105         }
3106 
3107         // if there was no match among declared methods,
3108         // we must consult the superclass (if any) recursively...
3109         Class<?> sc = getSuperclass();
3110         if (sc != null) {
3111             res = sc.getMethodsRecursive(name, parameterTypes, includeStatic);
3112         }
3113 
3114         // ...and consolidate the superclass methods with methods obtained
3115         // from directly implemented interfaces excluding static methods...
3116         for (Class<?> intf : getInterfaces(/* cloneArray */ false)) {
3117             res = PublicMethods.MethodList.consolidate(
3118                 res, intf.getMethodsRecursive(name, parameterTypes,
3119                                               /* includeStatic */ false));
3120         }
3121 
3122         return res;





3123     }
3124 
3125     // Returns a "root" Constructor object. This Constructor object must NOT
3126     // be propagated to the outside world, but must instead be copied
3127     // via ReflectionFactory.copyConstructor.
3128     private Constructor<T> getConstructor0(Class<?>[] parameterTypes,
3129                                         int which) throws NoSuchMethodException
3130     {
3131         ReflectionFactory fact = getReflectionFactory();
3132         Constructor<T>[] constructors = privateGetDeclaredConstructors((which == Member.PUBLIC));
3133         for (Constructor<T> constructor : constructors) {
3134             if (arrayContentsEq(parameterTypes,
3135                                 fact.getExecutableSharedParameterTypes(constructor))) {
3136                 return constructor;
3137             }
3138         }
3139         throw new NoSuchMethodException(getName() + ".<init>" + argumentTypesToString(parameterTypes));
3140     }
3141 
3142     //
3143     // Other helpers and base implementation
3144     //
3145 
3146     private static boolean arrayContentsEq(Object[] a1, Object[] a2) {
3147         if (a1 == null) {
3148             return a2 == null || a2.length == 0;
3149         }
3150 
3151         if (a2 == null) {
3152             return a1.length == 0;
3153         }
3154 
3155         if (a1.length != a2.length) {
3156             return false;


< prev index next >