zoukankan      html  css  js  c++  java
  • java反射-通过invoke方法深入探索反射机制

      虽说有一定原因是在考科三,但是第三篇没有及时完成还是有一定原因是因为五一都出去浪了,这一篇算是额外的。

      我一直在犹豫invoke方法要不要单开一篇写,因为虽说invoke(调用)方法会在某些报错的时候在报错的位置中可以看到这个函数,可以看出这个方法应用比较广泛,但是我实际接触的其实也还少,一时间对于它的应用也找不到例子,只能拎出源码看看。

    1、invoke()

    invoke()方法定义在java.lang.reflect.Method中,源码如下,前面长长的一串注释详细地描述了invoke方法的使用以及可能抛出的错误。

    • invoke方法应该由Method对象调用,并传入调用该method的实例对象obj以及需要传入的参数args。

    • 当使用invoke调用static方法的时候,obj参数应当为null

    • 当使用invoke调用的方法无参数的时候,args参数的长度应当为0或者为null

    • 当使用invoke调用实例方法,则使用《 Java语言规范,第二版》第15.12.4.4节中记录的动态方法查找来调用它;

    • 当使用invoke调用类的static方法时类还未被加载,则会触发类加载。

    • 根据invoke调用的方法返回返回值,返回值都是Object,这个Object可能指向某个类的实例,也可能是是空(void),也可能返回数组对象,数组对象需要使用类Array进行操作。

    /**
         * Invokes the underlying method represented by this {@code Method}
         * object, on the specified object with the specified parameters.
         * Individual parameters are automatically unwrapped to match
         * primitive formal parameters, and both primitive and reference
         * parameters are subject to method invocation conversions as
         * necessary.
         *
         * <p>If the underlying method is static, then the specified {@code obj}
         * argument is ignored. It may be null.
         *
         * <p>If the number of formal parameters required by the underlying method is
         * 0, the supplied {@code args} array may be of length 0 or null.
         *
         * <p>If the underlying method is an instance method, it is invoked
         * using dynamic method lookup as documented in The Java Language
         * Specification, Second Edition, section 15.12.4.4; in particular,
         * overriding based on the runtime type of the target object will occur.
         *
         * <p>If the underlying method is static, the class that declared
         * the method is initialized if it has not already been initialized.
         *
         * <p>If the method completes normally, the value it returns is
         * returned to the caller of invoke; if the value has a primitive
         * type, it is first appropriately wrapped in an object. However,
         * if the value has the type of an array of a primitive type, the
         * elements of the array are <i>not</i> wrapped in objects; in
         * other words, an array of primitive type is returned.  If the
         * underlying method return type is void, the invocation returns
         * null.
         *
         * @param obj  the object the underlying method is invoked from
         * @param args the arguments used for the method call
         * @return the result of dispatching the method represented by
         * this object on {@code obj} with parameters
         * {@code args}
         *
         * @exception IllegalAccessException    if this {@code Method} object
         *              is enforcing Java language access control and the underlying
         *              method is inaccessible.
         * @exception IllegalArgumentException  if the method is an
         *              instance method and the specified object argument
         *              is not an instance of the class or interface
         *              declaring the underlying method (or of a subclass
         *              or implementor thereof); if the number of actual
         *              and formal parameters differ; if an unwrapping
         *              conversion for primitive arguments fails; or if,
         *              after possible unwrapping, a parameter value
         *              cannot be converted to the corresponding formal
         *              parameter type by a method invocation conversion.
         * @exception InvocationTargetException if the underlying method
         *              throws an exception.
         * @exception NullPointerException      if the specified object is null
         *              and the method is an instance method.
         * @exception ExceptionInInitializerError if the initialization
         * provoked by this method fails.
         */
        @CallerSensitive
        public Object invoke(Object obj, Object... args)
            throws IllegalAccessException, IllegalArgumentException,
               InvocationTargetException
        {
            if (!override) {
                if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) {
                    Class<?> caller = Reflection.getCallerClass();
                    checkAccess(caller, clazz, obj, modifiers);
                }
            }
            MethodAccessor ma = methodAccessor;             // read volatile
            if (ma == null) {
                ma = acquireMethodAccessor();
            }
            return ma.invoke(obj, args);
        }
            try {
                Class c = String.class;
                char[] chr = {'0','1','2','3'};
                Object str = c.getConstructor(char[].class).newInstance(chr);
                
                Method method = c.getMethod("charAt", int.class);
                System.out.println(method.invoke(str,2));
                //返回数组
                method = c.getMethod("toCharArray");
                Object chr2 = method.invoke(str);
                System.out.println(Array.get(chr2,2));
               //调用static对象
                method = c.getMethod("valueOf",char[].class);
                Object str2 = method.invoke(null,chr);
                System.out.println(str2);
                
    ​
            } catch (Exception e) {
                e.printStackTrace();
            }

    2、invoke()源码分析

        
    @CallerSensitive
        public Object invoke(Object obj, Object... args)
            throws IllegalAccessException, IllegalArgumentException,
               InvocationTargetException
        {
            if (!override) {
                if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) {
                    Class<?> caller = Reflection.getCallerClass();
                    checkAccess(caller, clazz, obj, modifiers);
                }
            }
            MethodAccessor ma = methodAccessor;             // read volatile
            if (ma == null) {
                ma = acquireMethodAccessor();
            }
            return ma.invoke(obj, args);
        }

    如果从头到尾进行分析……

    2.1 @CallerSensitive注解

    Improve the security of the JDK's method-handle implementation by replacing the existing hand-maintained list of caller-sensitive methods with a mechanism that accurately identifies such methods and allows their callers to be discovered reliably.

    通过使用准确识别此类方法并允许可靠地发现其调用方的机制来替换JDK的方法句柄实现的安全性,以替换现有的手动维护的调用方敏感方法列表。

      当一个调用敏感者(caller-sensitive)需要确定它的直接调用者的类,它会通过调用native方法sun.reflect.Reflection.getCallerClass() 来实现。Reflection类位于调用栈中的0帧位置,sun.reflect.Reflection.getCallerClass()方法返回调用栈中从0帧开始的第x帧中的类实例。

      这个注解@CallerSensitive是为了堵住漏洞用的。曾经有黑客通过构造双重反射 来提升权限,原理是当时反射只检查固定深度的调用者的类,看它有没有特权,例如固定看两层的调用者(getCallerClass(2))。如果我的类本来没足够权限群访问某些信息,那我就可以通过双重反射去达到目的:反射相关的类是有很高权限的,而在 我->反射1->反射2 这样的调用链上,反射2检查权限时看到的是反射1的类,这就被欺骗了,导致安全漏洞。使用CallerSensitive后,getCallerClass不再用固定深度去寻找actual caller(“我”),而是把所有跟反射相关的接口方法都标注上CallerSensitive,搜索时凡看到该注解都直接跳过,这样就有效解决了前面举例的问题

    2.2 invoke可能抛出的异常

    • IllegalAccessException: 当使用invoke()调用权限不足(譬如private修饰)的方法,并且没有使用setAccessible(true)会抛出这个异常。

    • IllegalArgumentException:传入invoke方法的实例对象不是指定类、接口或其子类的实例,或者传入的参数长度与调用的方法所需的不符合,抛出这个异常。

    • InvocationTargetException:当使用invoke调用的方法中报错,抛出这个异常。

    • NullPointerException:当需要传入实例对象的时候传入了null

    • ExceptionInInitializerError:如果类的初始化报错

    2.3 invoke权限检查

     if (!override) {
                if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) {
                    Class<?> caller = Reflection.getCallerClass();
                    checkAccess(caller, clazz, obj, modifiers);
                }
            }

    a) override--是否忽略权限检查

    首先,变量override定义在AccessibleObject 类中,AccessibleObject 类是 FieldMethodConstructor 对象的基类。override变量提供了将反射的对象标记为在使用时取消默认 Java 语言访问控制检查的能力,它值默认是false,表示需要权限调用规则,调用方法时需要检查权限;我们也可以用setAccessible方法设置为true,若override的值为true,表示忽略权限规则,调用方法时无需检查权限(也就是说可以调用任意的private方法,违反了封装)。

    如果override属性为默认值false,则进行进一步的权限检查:

    b)检查方法是否为public

    Reflection.quickCheckMemberAccess(clazz, modifiers)方法检查方法是否为public,如果是的话跳出本步;如果不是public方法,那么用Reflection.getCallerClass()方法获取调用这个方法的Class对象,调用checkAccess再进行一次快速的权限检验。

    可以看到sun.reflect.Reflection.quickCheckMemberAccess()通过native方法getClassAccessFlags检索写入类文件的访问标志与传入的modifiers标志取与之后进行Modifier.isPublic()的判断,这个方法总的来说就是检查需要调用的方法是否为public。

    //sun.reflect.Reflection
      /** A quick "fast-path" check to try to avoid getCallerClass() calls. */public static boolean quickCheckMemberAccess(Class<?> memberClass,
                                                     int modifiers)
        {
            return Modifier.isPublic(getClassAccessFlags(memberClass) & modifiers);
        }
    ​
    //sun.reflect.Reflection
    /** Retrieves the access flags written to the class file. For
            inner classes these flags may differ from those returned by
            Class.getModifiers(), which searches the InnerClasses
            attribute to find the source-level access flags. This is used
            instead of Class.getModifiers() for run-time access checks due
            to compatibility reasons; see 4471811. Only the values of the
            low 13 bits (i.e., a mask of 0x1FFF) are guaranteed to be
            valid. */
        public static native int getClassAccessFlags(Class<?> c);
    //java.lang.reflect.Modifier
        /**
         * Return {@code true} if the integer argument includes the
         * {@code public} modifier, {@code false} otherwise.
         *
         * @param   mod a set of modifiers
         * @return {@code true} if {@code mod} includes the
         * {@code public} modifier; {@code false} otherwise.
         */
        public static boolean isPublic(int mod) {
            return (mod & PUBLIC) != 0;
        }
    ​
        /*
         * Access modifier flag constants from tables 4.1, 4.4, 4.5, and 4.7 of
         * <cite>The Java&trade; Virtual Machine Specification</cite>
         *//**
         * The {@code int} value representing the {@code public}
         * modifier.
         */
        public static final int PUBLIC           = 0x00000001;

    c)checkAccess--进行更加深入的权限检查

    checkAccess方法定义在AccessibleObject 类中。

        // Shared access checking logic.
    // For non-public members or members in package-private classes,
        // it is necessary to perform somewhat expensive security checks.
        // If the security check succeeds for a given class, it will
        // always succeed (it is not affected by the granting or revoking
        // of permissions); we speed up the check in the common case by
        // remembering the last Class for which the check succeeded.
        //
        // The simple security check for Constructor is to see if
        // the caller has already been seen, verified, and cached.
        // (See also Class.newInstance(), which uses a similar method.)
        //
        // A more complicated security check cache is needed for Method and Field
        // The cache can be either null (empty cache), a 2-array of {caller,target},
        // or a caller (with target implicitly equal to this.clazz).
        // In the 2-array case, the target is always different from the clazz.
        volatile Object securityCheckCache;
    ​
        void checkAccess(Class<?> caller, Class<?> clazz, Object obj, int modifiers)
            throws IllegalAccessException
        {
            if (caller == clazz) {  // quick check
                return;             // ACCESS IS OK
            }
            Object cache = securityCheckCache;  // read volatile
            Class<?> targetClass = clazz;
            if (obj != null
                && Modifier.isProtected(modifiers)
                && ((targetClass = obj.getClass()) != clazz)) {
                // Must match a 2-list of { caller, targetClass }.
                if (cache instanceof Class[]) {
                    Class<?>[] cache2 = (Class<?>[]) cache;
                    if (cache2[1] == targetClass &&
                        cache2[0] == caller) {
                        return;     // ACCESS IS OK
                    }
                    // (Test cache[1] first since range check for [1]
                    // subsumes range check for [0].)
                }
            } else if (cache == caller) {
                // Non-protected case (or obj.class == this.clazz).
                return;             // ACCESS IS OK
            }
    ​
            // If no return, fall through to the slow path.
            slowCheckMemberAccess(caller, clazz, obj, modifiers, targetClass);
        }

    首先先执行一次快速校验,一旦调用方法的Class正确则权限检查通过。 若未通过,则读取volatile变量securityCheckCache,检查是否已经通过过slowCheckMemberAccess检查。 如果上面的所有权限检查都未通过,那么将执行更详细的检查,其实现为:

      
     // Keep all this slow stuff out of line:
        void slowCheckMemberAccess(Class<?> caller, Class<?> clazz, Object obj, int modifiers,
                                   Class<?> targetClass)
            throws IllegalAccessException
        {
            Reflection.ensureMemberAccess(caller, clazz, obj, modifiers);
    ​
            // Success: Update the cache.
            Object cache = ((targetClass == clazz)
                            ? caller
                            : new Class<?>[] { caller, targetClass });
    ​
            // Note:  The two cache elements are not volatile,
            // but they are effectively final.  The Java memory model
            // guarantees that the initializing stores for the cache
            // elements will occur before the volatile write.
            securityCheckCache = cache;         // write volatile
        }

    2.4 调用MethodAccessor的invoke方法

    若未通过权限检查则抛出异常,若通过了权限检查,则进行方法调用。

            MethodAccessor ma = methodAccessor;             // read volatile
            if (ma == null) {
                ma = acquireMethodAccessor();
            }
            return ma.invoke(obj, args);

    可以看到Method.invoke()实际上并不是自己实现的反射调用逻辑,而是委托给sun.reflect.MethodAccessor来处理。

    //`java.lang.reflect.Method` 
    private volatile MethodAccessor methodAccessor;
        // For sharing of MethodAccessors. This branching structure is
        // currently only two levels deep (i.e., one root Method and
        // potentially many Method objects pointing to it.)
        //
        // If this branching structure would ever contain cycles, deadlocks can
        // occur in annotation code.
        private Method              root;

       Method对象的每个Java方法有且只有一个Method对象作为root,它相当于根对象,对用户不可见。当我们创建Method对象时,我们代码中获得的Method对象都相当于它的副本(或引用)。root对象持有一个MethodAccessor对象,所以所有获取到的Method对象都共享这一个MethodAccessor对象,因此必须保证它在内存中的可见性。如同上述代码所示,第一次读取methodAccessor时,methodAccessor为空,通过acquireMethodAccessor方法进行创建,之后每一次访问都是通过访问已经创建好的methodAccessor调用methodAccessor.invoke(obj,args)

    那么sun.reflect.MethodAccessor.invoke()方法又实现了什么呢?MethodAccessor是一个接口,只定义了一个虚方法invoke

    /** This interface provides the declaration for
        java.lang.reflect.Method.invoke(). Each Method object is
        configured with a (possibly dynamically-generated) class which
        implements this interface.
    */public interface MethodAccessor {
        /** Matches specification in {@link java.lang.reflect.Method} */
        public Object invoke(Object obj, Object[] args)
            throws IllegalArgumentException, InvocationTargetException;
    }

    那么其实现应该具体看到底是创建了哪一个实现了该接口的类的实例,它具体实现的类包括:

    • sun.reflect.DelegatingMethodAccessorImpl

    • sun.reflect.MethodAccessorImpl

    • sun.reflect.NativeMethodAccessorImpl

     

    a)整个源码过程

        //0、java.lang.reflect.Method.acquireMethodAccessor()
        /*MethodAccessor对象在一个指定method中唯一的,所以即使这个方法中未定义同步,但是要尽量保证同步问题*/
        private MethodAccessor acquireMethodAccessor() {
            // First check to see if one has been created yet, and take it
            // if so
            MethodAccessor tmp = null;
            if (root != null) tmp = root.getMethodAccessor();
            if (tmp != null) {
                methodAccessor = tmp;
            } else {
                // Otherwise fabricate one and propagate it up to the root
                tmp = reflectionFactory.newMethodAccessor(this);
                //1、reflectionFactory定义在父类AccessibleObject中,是一个ReflectionFactory类的实例
                //2、newMethodAccessor()在进行了一些初始化后进行判断,后创建了不同的子类的实例
                setMethodAccessor(tmp);
                //3、设置MethodAccessor
            }
    ​
            return tmp;
        }
    ​
        //1.1、变量reflectionFactory定义在父类java.lang.reflect.AccessibleObject中
        
        /*ReflectionFactory对象用于构建字段、方法、构造器的访问器
        *最后返回的是一个static修饰的ReflectionFactory的实例
        */
        static final ReflectionFactory reflectionFactory =
            AccessController.doPrivileged(
                new sun.reflect.ReflectionFactory.GetReflectionFactoryAction());
        //1.1.1 sun.reflect.ReflectionFactory.GetReflectionFactoryAction类
        /*用户获取实例化反射对象的类
         *相比于直接使用getReflectionFactory()的原始调用,这个类可以避免受到调用者权限的限制
         *可作为AccessController.doPrivileged()的参数
         *最后返回的是一个static修饰的ReflectionFactory的实例
         */
        public static final class GetReflectionFactoryAction
            implements PrivilegedAction<ReflectionFactory> {
            public ReflectionFactory run() {
                return getReflectionFactory();
            }
        }
        //1.1.1.1 sun.reflect.ReflectionFactory.getReflectionFactory()方法
    /**
         * Provides the caller with the capability to instantiate reflective
         * objects.
         *
         * <p> First, if there is a security manager, its
         * <code>checkPermission</code> method is called with a {@link
         * java.lang.RuntimePermission} with target
         * <code>"reflectionFactoryAccess"</code>.  This may result in a
         * security exception.
         *
         * <p> The returned <code>ReflectionFactory</code> object should be
         * carefully guarded by the caller, since it can be used to read and
         * write private data and invoke private methods, as well as to load
         * unverified bytecodes.  It must never be passed to untrusted code.
         *
         * @exception SecurityException if a security manager exists and its
         *             <code>checkPermission</code> method doesn't allow
         *             access to the RuntimePermission "reflectionFactoryAccess".  */
        public static ReflectionFactory getReflectionFactory() {
            SecurityManager security = System.getSecurityManager();
            if (security != null) {
                // TO DO: security.checkReflectionFactoryAccess();
                security.checkPermission(reflectionFactoryAccessPerm);
            }
            return soleInstance;
        }
        //1.1.1.1.1 最后返回的是一个static修饰的ReflectionFactory的实例
        private static final ReflectionFactory soleInstance = new ReflectionFactory();
    ​
        //1.1.1.1.2 java.security.PrivilegedAction
        public interface PrivilegedAction<T> {
            /**
            * Performs the computation.  This method will be called by
            * {@code AccessController.doPrivileged} after enabling privileges.
            *
            * @return a class-dependent value that may represent the results of the
            *         computation. Each class that implements
            *         {@code PrivilegedAction}
            *         should document what (if anything) this value represents.
            * @see AccessController#doPrivileged(PrivilegedAction)
            * @see AccessController#doPrivileged(PrivilegedAction,
            *                                     AccessControlContext)
            */
            T run();
        }
    ​
        //2.1 sun.reflect.ReflectionFactory.newMethodAccessor()
        /*
        *进行必须的初始化工作,后进行判断:
        *a) 当类是虚拟机匿名类时:
        *b) 其他情况时:
        */
        public MethodAccessor newMethodAccessor(Method method) {
            checkInitted();//确保系统属性表已完全初始化。 
            //ReflectUtil类是访问权限检查工具,检查类是否为虚拟机匿名类(这与Java语言中的匿名内部类不同)
            if (noInflation && !ReflectUtil.isVMAnonymousClass(method.getDeclaringClass())) {
                return new MethodAccessorGenerator().
                    generateMethod(method.getDeclaringClass(),
                                   method.getName(),
                                   method.getParameterTypes(),
                                   method.getReturnType(),
                                   method.getExceptionTypes(),
                                   method.getModifiers());
            } else {
                NativeMethodAccessorImpl acc =
                    new NativeMethodAccessorImpl(method);
                DelegatingMethodAccessorImpl res =
                    new DelegatingMethodAccessorImpl(acc);
                acc.setParent(res);
                return res;
            }
        }
        //2.1.1 sun.reflect.ReflectionFactory.checkInitted()
    /** 在运行静态初始值设定项完成之前,需要推迟对该类的完全初始化。因为java.lang.reflect.Method(准确来说是java.lang.reflect.AccessibleObject)的静态初始值设定项会导致系统属性设置好之前运行此类 */
        private static void checkInitted() {
            if (initted) return;//如果已经初始化过,返回
            AccessController.doPrivileged(
                new PrivilegedAction<Void>() {
                    public Void run() {
                        //测试以确保系统属性表已完全初始化。 
                        // 因为反射代码在初始化过程中很早就被调用(在解析命令行参数之前,因此已安装了这些用户可设置的属性。)
                        // 我们假设如果System.out为非null,则System类已经 完全初始化,并且大部分启动代码已运行。
    if (System.out == null) {
                            // java.lang.System not yet fully initialized
                            return null;
                        }
    ​
                        String val = System.getProperty("sun.reflect.noInflation");
                        if (val != null && val.equals("true")) {
                            noInflation = true;
                        }
    ​
                        val = System.getProperty("sun.reflect.inflationThreshold");
                        if (val != null) {
                            try {
                                inflationThreshold = Integer.parseInt(val);
                            } catch (NumberFormatException e) {
                                throw new RuntimeException("Unable to parse property sun.reflect.inflationThreshold", e);
                            }
                        }
    ​
                        initted = true;
                        return null;
                    }
                });
        }
    ​
        //2.1.2 sun.reflect.misc.ReflectUtil.isVMAnonymousClass()
        /**
         * Checks if {@code Class cls} is a VM-anonymous class as defined by {@link jdk.internal.misc.Unsafe#defineAnonymousClass}
         * (not to be confused with a Java Language anonymous inner class).
         */
        // 检查cls是否为虚拟机匿名类(这与Java语言中的匿名内部类不同)
        public static boolean isVMAnonymousClass(Class<?> cls) {
            // 类名中包含'/'
            return cls.getName().indexOf('/')>-1;
        }
    ​
    ​
    ​
       //3.1 sun.reflect.ReflectionFactory.setMethodAccessor()
       
        /** Sets the MethodAccessor object for a java.lang.reflect.Method */
        public void setMethodAccessor(Method m, MethodAccessor accessor) {
            langReflectAccess().setMethodAccessor(m, accessor);
    //不明白的是langReflectAccess()返回了ReflectionFactory类中的static ReflectionFactory实例soleInstance
    //然后又调用ReflectionFactory.setMethodAccessor(),似乎进入了循环?
        }
        //3.1.1 sun.reflect.ReflectionFactory.langReflectAccess
        // Provides access to package-private mechanisms in java.lang.reflect
        private static volatile LangReflectAccess langReflectAccess;
    ​
        //3.1.2 sun.reflect.ReflectionFactory.langReflectAccess()
        private static LangReflectAccess langReflectAccess() {
            if (langReflectAccess == null) {
                // 调用Modifier的静态方法以触发Modifier的初始化,然后运行Modifier类中的静态方法区设置langReflectAccess
                Modifier.isPublic(Modifier.PUBLIC);
            }
            return langReflectAccess;
        }
        //3.1.2.1 java.lang.reflect.Modifier静态方法区
        /*
         * Bootstrapping protocol between java.lang and java.lang.reflect
         *  packages
         */
        static {
            sun.reflect.ReflectionFactory factory =
                AccessController.doPrivileged(
                    new ReflectionFactory.GetReflectionFactoryAction());//返回了ReflectionFactory类中的static ReflectionFactory实例soleInstance
            factory.setLangReflectAccess(new java.lang.reflect.ReflectAccess());
        }
    //3.1.2.1.1 java.lang.reflect.ReflectionFactory.setLangReflectAccess()
    /** Called only by java.lang.reflect.Modifier's static initializer */
        public void setLangReflectAccess(LangReflectAccess access) {
            langReflectAccess = access;
        }
    
    //3.1.3 sun.reflect.LangReflectAccess,接口
    public void setMethodAccessor(Method m, MethodAccessor accessor);
    ​
    ​
    整个源码过程,没必要看

    b)acquireMethodAccessor()newMethodAccessor()

    //0、java.lang.reflect.Method.acquireMethodAccessor()
        /*MethodAccessor对象在一个指定method中唯一的,所以即使这个方法中未定义同步,但是要尽量保证同步问题*/
        private MethodAccessor acquireMethodAccessor() {
            // First check to see if one has been created yet, and take it
            // if so
            MethodAccessor tmp = null;
            if (root != null) tmp = root.getMethodAccessor();
            if (tmp != null) {
                methodAccessor = tmp;
            } else {
                // Otherwise fabricate one and propagate it up to the root
                tmp = reflectionFactory.newMethodAccessor(this);
                //1、reflectionFactory定义在父类AccessibleObject中,是一个ReflectionFactory类的实例
                //2、newMethodAccessor()在进行了一些初始化后进行判断,后创建了不同的子类的实例
                setMethodAccessor(tmp);
                //3、设置MethodAccessor
            }
    ​
            return tmp;
        }
    ​
        //2.1 sun.reflect.ReflectionFactory.newMethodAccessor()
        /*
        *进行必须的初始化工作,后进行判断:
        *a) 当类是虚拟机匿名类时:
        *b) 其他情况时:
        */
        public MethodAccessor newMethodAccessor(Method method) {
            checkInitted();//确保系统属性表已完全初始化。 
            //ReflectUtil类是访问权限检查工具,检查类是否为虚拟机匿名类(这与Java语言中的匿名内部类不同)
            if (noInflation && !ReflectUtil.isVMAnonymousClass(method.getDeclaringClass())) {
                return new MethodAccessorGenerator().
                    generateMethod(method.getDeclaringClass(),
                                   method.getName(),
                                   method.getParameterTypes(),
                                   method.getReturnType(),
                                   method.getExceptionTypes(),
                                   method.getModifiers());
            } else {
                NativeMethodAccessorImpl acc =
                    new NativeMethodAccessorImpl(method);
                DelegatingMethodAccessorImpl res =
                    new DelegatingMethodAccessorImpl(acc);
                acc.setParent(res);
                return res;
            }
        }
    ​

    根据代码可知,acquireMethodAccessor()返回的methodAccessor有两种可能:

    一种是MethodAccessorGenerator().generateMethod()返回的methodAccessor实例

    一种是DelegatingMethodAccessorImplNativeMethodAccessorImpl的实例

    c)MethodAccessorGenerator().generateMethod()

    事实上这个我并没有找到最终的结果,因为一个generate()方法我找不到来源。它既不定义在MethodAccessorGenerator中,也没有定义在它的父类AccessorGenerator中,也不在导入的某个类之中……或许是我粗心没有注意到,但是既然没有找到只能到此为止了。这是一个针对虚拟机匿名类的methodAccessor获取方法,后续有机会再继续了解吧。

    //sun.reflect.MethodAccessorGenerator.generateMethod()
    /** This routine is not thread-safe */
        public MethodAccessor generateMethod(Class<?> declaringClass,
                                             String   name,
                                             Class<?>[] parameterTypes,
                                             Class<?>   returnType,
                                             Class<?>[] checkedExceptions,
                                             int modifiers)
        {
    ​
            return (MethodAccessor) generate(declaringClass,
                                             name,
                                             parameterTypes,
                                             returnType,
                                             checkedExceptions,
                                             modifiers,
                                             false,
                                             false,
                                             null);
        }

    d)DelegatingMethodAccessorImplNativeMethodAccessorImpl

    MethodAccessor是一个接口,它具体实现的类包含sun.reflect.MethodAccessorImpl, sun.reflect.DelegatingMethodAccessorImplsun.reflect.NativeMethodAccessorImpl

    其中MethodAccessorImpl是一个实现了MethodAccessor的虚类,并未实现invoke()方法。

    DelegatingMethodAccessorImplNativeMethodAccessorImpl都是MethodAccessorImpl的子类,但是DelegatingMethodAccessorImpl构造方法接收一个MethodAccessorImpl实例并维护这个MethodAccessorImpl实例,在invoke()方法中依赖传入的MethodAccessorImpl实例进行调用,仅仅是做了一层封装,并未涉及invoke()的具体实现。invoke()的具体实现在NativeMethodAccessorImpl之中。

    //sun.reflect.MethodAccessor
    /*
     * Copyright (c) 2001, Oracle and/or its affiliates. All rights reserved.
     * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     *
     * This code is free software; you can redistribute it and/or modify it
     * under the terms of the GNU General Public License version 2 only, as
     * published by the Free Software Foundation.  Oracle designates this
     * particular file as subject to the "Classpath" exception as provided
     * by Oracle in the LICENSE file that accompanied this code.
     *
     * This code is distributed in the hope that it will be useful, but WITHOUT
     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
     * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
     * version 2 for more details (a copy is included in the LICENSE file that
     * accompanied this code).
     *
     * You should have received a copy of the GNU General Public License version
     * 2 along with this work; if not, write to the Free Software Foundation,
     * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
     *
     * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
     * or visit www.oracle.com if you need additional information or have any
     * questions.
     */package sun.reflect;
    ​
    import java.lang.reflect.InvocationTargetException;
    ​
    /** This interface provides the declaration for
        java.lang.reflect.Method.invoke(). Each Method object is
        configured with a (possibly dynamically-generated) class which
        implements this interface.
    */public interface MethodAccessor {
        /** Matches specification in {@link java.lang.reflect.Method} */
        public Object invoke(Object obj, Object[] args)
            throws IllegalArgumentException, InvocationTargetException;
    }
    ​
    //sun.reflect.MethodAccessorImpl
    /*
     * Copyright (c) 2001, Oracle and/or its affiliates. All rights reserved.
     * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     *
     * This code is free software; you can redistribute it and/or modify it
     * under the terms of the GNU General Public License version 2 only, as
     * published by the Free Software Foundation.  Oracle designates this
     * particular file as subject to the "Classpath" exception as provided
     * by Oracle in the LICENSE file that accompanied this code.
     *
     * This code is distributed in the hope that it will be useful, but WITHOUT
     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
     * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
     * version 2 for more details (a copy is included in the LICENSE file that
     * accompanied this code).
     *
     * You should have received a copy of the GNU General Public License version
     * 2 along with this work; if not, write to the Free Software Foundation,
     * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
     *
     * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
     * or visit www.oracle.com if you need additional information or have any
     * questions.
     */package sun.reflect;
    ​
    import java.lang.reflect.InvocationTargetException;
    ​
    /** <P> Package-private implementation of the MethodAccessor interface
        which has access to all classes and all fields, regardless of
        language restrictions. See MagicAccessor. </P>
    ​
        <P> This class is known to the VM; do not change its name without
        also changing the VM's code. </P>
    ​
        <P> NOTE: ALL methods of subclasses are skipped during security
        walks up the stack. The assumption is that the only such methods
        that will persistently show up on the stack are the implementing
        methods for java.lang.reflect.Method.invoke(). </P>
    */abstract class MethodAccessorImpl extends MagicAccessorImpl
        implements MethodAccessor {
        /** Matches specification in {@link java.lang.reflect.Method} */
        public abstract Object invoke(Object obj, Object[] args)
            throws IllegalArgumentException, InvocationTargetException;
    }
    //sun.reflect.DelegatingMethodAccessorImpl
    /*
     * Copyright (c) 2001, Oracle and/or its affiliates. All rights reserved.
     * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     *
     * This code is free software; you can redistribute it and/or modify it
     * under the terms of the GNU General Public License version 2 only, as
     * published by the Free Software Foundation.  Oracle designates this
     * particular file as subject to the "Classpath" exception as provided
     * by Oracle in the LICENSE file that accompanied this code.
     *
     * This code is distributed in the hope that it will be useful, but WITHOUT
     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
     * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
     * version 2 for more details (a copy is included in the LICENSE file that
     * accompanied this code).
     *
     * You should have received a copy of the GNU General Public License version
     * 2 along with this work; if not, write to the Free Software Foundation,
     * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
     *
     * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
     * or visit www.oracle.com if you need additional information or have any
     * questions.
     */package sun.reflect;
    ​
    import java.lang.reflect.InvocationTargetException;
    ​
    /** Delegates its invocation to another MethodAccessorImpl and can
        change its delegate at run time. */class DelegatingMethodAccessorImpl extends MethodAccessorImpl {
        private MethodAccessorImpl delegate;
    ​
        DelegatingMethodAccessorImpl(MethodAccessorImpl delegate) {
            setDelegate(delegate);
        }
    ​
        public Object invoke(Object obj, Object[] args)
            throws IllegalArgumentException, InvocationTargetException
        {
            return delegate.invoke(obj, args);
        }
    ​
        void setDelegate(MethodAccessorImpl delegate) {
            this.delegate = delegate;
        }
    }
    //sun.reflect.NativeMethodAccessorImpl
    /*
     * Copyright (c) 2001, 2013, Oracle and/or its affiliates. All rights reserved.
     * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     *
     * This code is free software; you can redistribute it and/or modify it
     * under the terms of the GNU General Public License version 2 only, as
     * published by the Free Software Foundation.  Oracle designates this
     * particular file as subject to the "Classpath" exception as provided
     * by Oracle in the LICENSE file that accompanied this code.
     *
     * This code is distributed in the hope that it will be useful, but WITHOUT
     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
     * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
     * version 2 for more details (a copy is included in the LICENSE file that
     * accompanied this code).
     *
     * You should have received a copy of the GNU General Public License version
     * 2 along with this work; if not, write to the Free Software Foundation,
     * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
     *
     * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
     * or visit www.oracle.com if you need additional information or have any
     * questions.
     */package sun.reflect;
    ​
    import java.lang.reflect.*;
    import sun.reflect.misc.ReflectUtil;
    ​
    /** Used only for the first few invocations of a Method; afterward,
        switches to bytecode-based implementation */class NativeMethodAccessorImpl extends MethodAccessorImpl {
        private final Method method;
        private DelegatingMethodAccessorImpl parent;
        private int numInvocations;
    ​
        NativeMethodAccessorImpl(Method method) {
            this.method = method;
        }
    ​
        public Object invoke(Object obj, Object[] args)
            throws IllegalArgumentException, InvocationTargetException
        {
            // 是否匿名类中的方法?如果是,则将封装类的实例parent中的维护的MethodAccessorImpl替换
            if (++numInvocations > ReflectionFactory.inflationThreshold()
                    && !ReflectUtil.isVMAnonymousClass(method.getDeclaringClass())) {
                MethodAccessorImpl acc = (MethodAccessorImpl)
                    new MethodAccessorGenerator().
                        generateMethod(method.getDeclaringClass(),
                                       method.getName(),
                                       method.getParameterTypes(),
                                       method.getReturnType(),
                                       method.getExceptionTypes(),
                                       method.getModifiers());
                parent.setDelegate(acc);
            }
            //调用native方法invoke0()
            return invoke0(method, obj, args);
        }
    ​
        void setParent(DelegatingMethodAccessorImpl parent) {
            this.parent = parent;
        }
    ​
        private static native Object invoke0(Method m, Object obj, Object[] args);
    }
    ​
     

    NativeMethodAccessorImpl之中维护了一个封装类DelegatingMethodAccessorImpl类的实例parent,以及一个常量Method类的实例,以及一个int类型的数值numInvocations

    然后在invoke()方法中继续判断method是否匿名类中的方法,如果是,替换调用的MethodAccessor的实例类型,之后返回native方法invoke0(method, obj, args)

    那么这次就到此为止吧。

    3、总结

    大部分都在找源码,有时候不知道如何组织描述的语言,不过牵着一条线走下去然后慢慢还原整个过程的这个过程还算有趣吧,也比预料之中深入了更多,花的时间也比想象地多了许多。java反射相关的类除了java.lang.Class,大多包含在java.lang.reflect.*之下,但是也涉及了许多sun.reflect.*之下的类。不过这次有关invoke()方法的深入探究,更多涉及的是Method相关的类、权限访问类或者某些类中Method相关方法,下面仅仅做部分类的整理。

    3.1 java.lang.reflect.AccessibleObject

    AccessibleObject类是FieldMethodConstructor类对象的基类。 它提供了将反射对象标记为在使用它时抑制默认Java语言访问控制检查的功能。 当使用FieldsMethodsConstructors类对象来设置或获取字段,调用方法,或创建和初始化新的类实例时,执行访问分别检查(对于public,默认(包)访问,protected和private成员) 。 在反射对象中设置可访问标志允许具有足够权限的复杂应用程序(如Java对象序列化或其他持久性机制)以被禁止的方式操作对象。

    //java.lang.reflect.AccessibleObject
    /*
     * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
     * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     *
     * This code is free software; you can redistribute it and/or modify it
     * under the terms of the GNU General Public License version 2 only, as
     * published by the Free Software Foundation.  Oracle designates this
     * particular file as subject to the "Classpath" exception as provided
     * by Oracle in the LICENSE file that accompanied this code.
     *
     * This code is distributed in the hope that it will be useful, but WITHOUT
     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
     * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
     * version 2 for more details (a copy is included in the LICENSE file that
     * accompanied this code).
     *
     * You should have received a copy of the GNU General Public License version
     * 2 along with this work; if not, write to the Free Software Foundation,
     * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
     *
     * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
     * or visit www.oracle.com if you need additional information or have any
     * questions.
     */package java.lang.reflect;
    ​
    import java.security.AccessController;
    import sun.reflect.Reflection;
    import sun.reflect.ReflectionFactory;
    import java.lang.annotation.Annotation;
    ​
    /**
     * The AccessibleObject class is the base class for Field, Method and
     * Constructor objects.  It provides the ability to flag a reflected
     * object as suppressing default Java language access control checks
     * when it is used.  The access checks--for public, default (package)
     * access, protected, and private members--are performed when Fields,
     * Methods or Constructors are used to set or get fields, to invoke
     * methods, or to create and initialize new instances of classes,
     * respectively.
     *
     * <p>Setting the {@code accessible} flag in a reflected object
     * permits sophisticated applications with sufficient privilege, such
     * as Java Object Serialization or other persistence mechanisms, to
     * manipulate objects in a manner that would normally be prohibited.
     *
     * <p>By default, a reflected object is <em>not</em> accessible.
     *
     * @see Field
     * @see Method
     * @see Constructor
     * @see ReflectPermission
     *
     * @since 1.2
     */
    public class AccessibleObject implements AnnotatedElement {
    ​
        /**
         * The Permission object that is used to check whether a client
         * has sufficient privilege to defeat Java language access
         * control checks.
         */
        static final private java.security.Permission ACCESS_PERMISSION =
            new ReflectPermission("suppressAccessChecks");
    ​
        /**
         * Convenience method to set the {@code accessible} flag for an
         * array of objects with a single security check (for efficiency).
         *
         * <p>First, if there is a security manager, its
         * {@code checkPermission} method is called with a
         * {@code ReflectPermission("suppressAccessChecks")} permission.
         *
         * <p>A {@code SecurityException} is raised if {@code flag} is
         * {@code true} but accessibility of any of the elements of the input
         * {@code array} may not be changed (for example, if the element
         * object is a {@link Constructor} object for the class {@link
         * java.lang.Class}).  In the event of such a SecurityException, the
         * accessibility of objects is set to {@code flag} for array elements
         * upto (and excluding) the element for which the exception occurred; the
         * accessibility of elements beyond (and including) the element for which
         * the exception occurred is unchanged.
         *
         * @param array the array of AccessibleObjects
         * @param flag  the new value for the {@code accessible} flag
         *              in each object
         * @throws SecurityException if the request is denied.
         * @see SecurityManager#checkPermission
         * @see java.lang.RuntimePermission
         */
        public static void setAccessible(AccessibleObject[] array, boolean flag)
            throws SecurityException {
            SecurityManager sm = System.getSecurityManager();
            if (sm != null) sm.checkPermission(ACCESS_PERMISSION);
            for (int i = 0; i < array.length; i++) {
                setAccessible0(array[i], flag);
            }
        }
    ​
        /**
         * Set the {@code accessible} flag for this object to
         * the indicated boolean value.  A value of {@code true} indicates that
         * the reflected object should suppress Java language access
         * checking when it is used.  A value of {@code false} indicates
         * that the reflected object should enforce Java language access checks.
         *
         * <p>First, if there is a security manager, its
         * {@code checkPermission} method is called with a
         * {@code ReflectPermission("suppressAccessChecks")} permission.
         *
         * <p>A {@code SecurityException} is raised if {@code flag} is
         * {@code true} but accessibility of this object may not be changed
         * (for example, if this element object is a {@link Constructor} object for
         * the class {@link java.lang.Class}).
         *
         * <p>A {@code SecurityException} is raised if this object is a {@link
         * java.lang.reflect.Constructor} object for the class
         * {@code java.lang.Class}, and {@code flag} is true.
         *
         * @param flag the new value for the {@code accessible} flag
         * @throws SecurityException if the request is denied.
         * @see SecurityManager#checkPermission
         * @see java.lang.RuntimePermission
         */
        public void setAccessible(boolean flag) throws SecurityException {
            SecurityManager sm = System.getSecurityManager();
            if (sm != null) sm.checkPermission(ACCESS_PERMISSION);
            setAccessible0(this, flag);
        }
    ​
        /* Check that you aren't exposing java.lang.Class.<init> or sensitive
           fields in java.lang.Class. */
        private static void setAccessible0(AccessibleObject obj, boolean flag)
            throws SecurityException
        {
            if (obj instanceof Constructor && flag == true) {
                Constructor<?> c = (Constructor<?>)obj;
                if (c.getDeclaringClass() == Class.class) {
                    throw new SecurityException("Cannot make a java.lang.Class" +
                                                " constructor accessible");
                }
            }
            obj.override = flag;
        }
    ​
        /**
         * Get the value of the {@code accessible} flag for this object.
         *
         * @return the value of the object's {@code accessible} flag
         */
        public boolean isAccessible() {
            return override;
        }
    ​
        /**
         * Constructor: only used by the Java Virtual Machine.
         */
        protected AccessibleObject() {}
    ​
        // Indicates whether language-level access checks are overridden
        // by this object. Initializes to "false". This field is used by
        // Field, Method, and Constructor.
        //
        // NOTE: for security purposes, this field must not be visible
        // outside this package.
        boolean override;
    ​
        // Reflection factory used by subclasses for creating field,
        // method, and constructor accessors. Note that this is called
        // very early in the bootstrapping process.
        static final ReflectionFactory reflectionFactory =
            AccessController.doPrivileged(
                new sun.reflect.ReflectionFactory.GetReflectionFactoryAction());
    ​
        /**
         * @throws NullPointerException {@inheritDoc}
         * @since 1.5
         */
        public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
            throw new AssertionError("All subclasses should override this method");
        }
    ​
        /**
         * {@inheritDoc}
         * @throws NullPointerException {@inheritDoc}
         * @since 1.5
         */
        @Override
        public boolean isAnnotationPresent(Class<? extends Annotation> annotationClass) {
            return AnnotatedElement.super.isAnnotationPresent(annotationClass);
        }
    ​
       /**
         * @throws NullPointerException {@inheritDoc}
         * @since 1.8
         */
        @Override
        public <T extends Annotation> T[] getAnnotationsByType(Class<T> annotationClass) {
            throw new AssertionError("All subclasses should override this method");
        }
    ​
        /**
         * @since 1.5
         */
        public Annotation[] getAnnotations() {
            return getDeclaredAnnotations();
        }
    ​
        /**
         * @throws NullPointerException {@inheritDoc}
         * @since 1.8
         */
        @Override
        public <T extends Annotation> T getDeclaredAnnotation(Class<T> annotationClass) {
            // Only annotations on classes are inherited, for all other
            // objects getDeclaredAnnotation is the same as
            // getAnnotation.
            return getAnnotation(annotationClass);
        }
    ​
        /**
         * @throws NullPointerException {@inheritDoc}
         * @since 1.8
         */
        @Override
        public <T extends Annotation> T[] getDeclaredAnnotationsByType(Class<T> annotationClass) {
            // Only annotations on classes are inherited, for all other
            // objects getDeclaredAnnotationsByType is the same as
            // getAnnotationsByType.
            return getAnnotationsByType(annotationClass);
        }
    ​
        /**
         * @since 1.5
         */
        public Annotation[] getDeclaredAnnotations()  {
            throw new AssertionError("All subclasses should override this method");
        }
    ​
    ​
        // Shared access checking logic.
    // For non-public members or members in package-private classes,
        // it is necessary to perform somewhat expensive security checks.
        // If the security check succeeds for a given class, it will
        // always succeed (it is not affected by the granting or revoking
        // of permissions); we speed up the check in the common case by
        // remembering the last Class for which the check succeeded.
        //
        // The simple security check for Constructor is to see if
        // the caller has already been seen, verified, and cached.
        // (See also Class.newInstance(), which uses a similar method.)
        //
        // A more complicated security check cache is needed for Method and Field
        // The cache can be either null (empty cache), a 2-array of {caller,target},
        // or a caller (with target implicitly equal to this.clazz).
        // In the 2-array case, the target is always different from the clazz.
        volatile Object securityCheckCache;
    ​
        void checkAccess(Class<?> caller, Class<?> clazz, Object obj, int modifiers)
            throws IllegalAccessException
        {
            if (caller == clazz) {  // quick check
                return;             // ACCESS IS OK
            }
            Object cache = securityCheckCache;  // read volatile
            Class<?> targetClass = clazz;
            if (obj != null
                && Modifier.isProtected(modifiers)
                && ((targetClass = obj.getClass()) != clazz)) {
                // Must match a 2-list of { caller, targetClass }.
                if (cache instanceof Class[]) {
                    Class<?>[] cache2 = (Class<?>[]) cache;
                    if (cache2[1] == targetClass &&
                        cache2[0] == caller) {
                        return;     // ACCESS IS OK
                    }
                    // (Test cache[1] first since range check for [1]
                    // subsumes range check for [0].)
                }
            } else if (cache == caller) {
                // Non-protected case (or obj.class == this.clazz).
                return;             // ACCESS IS OK
            }
    ​
            // If no return, fall through to the slow path.
            slowCheckMemberAccess(caller, clazz, obj, modifiers, targetClass);
        }
    ​
        // Keep all this slow stuff out of line:
        void slowCheckMemberAccess(Class<?> caller, Class<?> clazz, Object obj, int modifiers,
                                   Class<?> targetClass)
            throws IllegalAccessException
        {
            Reflection.ensureMemberAccess(caller, clazz, obj, modifiers);
    ​
            // Success: Update the cache.
            Object cache = ((targetClass == clazz)
                            ? caller
                            : new Class<?>[] { caller, targetClass });
    ​
            // Note:  The two cache elements are not volatile,
            // but they are effectively final.  The Java memory model
            // guarantees that the initializing stores for the cache
            // elements will occur before the volatile write.
            securityCheckCache = cache;         // write volatile
        }
    }
    java.lang.reflect.AccessibleObject
     

    3.2 java.lang.reflect.Array

    java.lang.reflect.Array类提供静态方法来动态创建和访问Java数组。 Array允许在get或set操作期间扩展转换,但如果发生缩小转换,则会抛出IllegalArgumentException异常

    /*
     * Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved.
     * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     *
     * This code is free software; you can redistribute it and/or modify it
     * under the terms of the GNU General Public License version 2 only, as
     * published by the Free Software Foundation.  Oracle designates this
     * particular file as subject to the "Classpath" exception as provided
     * by Oracle in the LICENSE file that accompanied this code.
     *
     * This code is distributed in the hope that it will be useful, but WITHOUT
     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
     * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
     * version 2 for more details (a copy is included in the LICENSE file that
     * accompanied this code).
     *
     * You should have received a copy of the GNU General Public License version
     * 2 along with this work; if not, write to the Free Software Foundation,
     * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
     *
     * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
     * or visit www.oracle.com if you need additional information or have any
     * questions.
     */package java.lang.reflect;
    ​
    /**
     * The {@code Array} class provides static methods to dynamically create and
     * access Java arrays.
     *
     * <p>{@code Array} permits widening conversions to occur during a get or set
     * operation, but throws an {@code IllegalArgumentException} if a narrowing
     * conversion would occur.
     *
     * @author Nakul Saraiya
     */
    public final
    class Array {
    ​
        /**
         * Constructor.  Class Array is not instantiable.
         */
        private Array() {}
    ​
        /**
         * Creates a new array with the specified component type and
         * length.
         * Invoking this method is equivalent to creating an array
         * as follows:
         * <blockquote>
         * <pre>
         * int[] x = {length};
         * Array.newInstance(componentType, x);
         * </pre>
         * </blockquote>
         *
         * <p>The number of dimensions of the new array must not
         * exceed 255.
         *
         * @param componentType the {@code Class} object representing the
         * component type of the new array
         * @param length the length of the new array
         * @return the new array
         * @exception NullPointerException if the specified
         * {@code componentType} parameter is null
         * @exception IllegalArgumentException if componentType is {@link
         * Void#TYPE} or if the number of dimensions of the requested array
         * instance exceed 255.
         * @exception NegativeArraySizeException if the specified {@code length}
         * is negative
         */
        public static Object newInstance(Class<?> componentType, int length)
            throws NegativeArraySizeException {
            return newArray(componentType, length);
        }
    ​
        /**
         * Creates a new array
         * with the specified component type and dimensions.
         * If {@code componentType}
         * represents a non-array class or interface, the new array
         * has {@code dimensions.length} dimensions and
         * {@code componentType} as its component type. If
         * {@code componentType} represents an array class, the
         * number of dimensions of the new array is equal to the sum
         * of {@code dimensions.length} and the number of
         * dimensions of {@code componentType}. In this case, the
         * component type of the new array is the component type of
         * {@code componentType}.
         *
         * <p>The number of dimensions of the new array must not
         * exceed 255.
         *
         * @param componentType the {@code Class} object representing the component
         * type of the new array
         * @param dimensions an array of {@code int} representing the dimensions of
         * the new array
         * @return the new array
         * @exception NullPointerException if the specified
         * {@code componentType} argument is null
         * @exception IllegalArgumentException if the specified {@code dimensions}
         * argument is a zero-dimensional array, if componentType is {@link
         * Void#TYPE}, or if the number of dimensions of the requested array
         * instance exceed 255.
         * @exception NegativeArraySizeException if any of the components in
         * the specified {@code dimensions} argument is negative.
         */
        public static Object newInstance(Class<?> componentType, int... dimensions)
            throws IllegalArgumentException, NegativeArraySizeException {
            return multiNewArray(componentType, dimensions);
        }
    ​
        /**
         * Returns the length of the specified array object, as an {@code int}.
         *
         * @param array the array
         * @return the length of the array
         * @exception IllegalArgumentException if the object argument is not
         * an array
         */
        public static native int getLength(Object array)
            throws IllegalArgumentException;
    ​
        /**
         * Returns the value of the indexed component in the specified
         * array object.  The value is automatically wrapped in an object
         * if it has a primitive type.
         *
         * @param array the array
         * @param index the index
         * @return the (possibly wrapped) value of the indexed component in
         * the specified array
         * @exception NullPointerException If the specified object is null
         * @exception IllegalArgumentException If the specified object is not
         * an array
         * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
         * argument is negative, or if it is greater than or equal to the
         * length of the specified array
         */
        public static native Object get(Object array, int index)
            throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
    ​
        /**
         * Returns the value of the indexed component in the specified
         * array object, as a {@code boolean}.
         *
         * @param array the array
         * @param index the index
         * @return the value of the indexed component in the specified array
         * @exception NullPointerException If the specified object is null
         * @exception IllegalArgumentException If the specified object is not
         * an array, or if the indexed element cannot be converted to the
         * return type by an identity or widening conversion
         * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
         * argument is negative, or if it is greater than or equal to the
         * length of the specified array
         * @see Array#get
         */
        public static native boolean getBoolean(Object array, int index)
            throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
    ​
        /**
         * Returns the value of the indexed component in the specified
         * array object, as a {@code byte}.
         *
         * @param array the array
         * @param index the index
         * @return the value of the indexed component in the specified array
         * @exception NullPointerException If the specified object is null
         * @exception IllegalArgumentException If the specified object is not
         * an array, or if the indexed element cannot be converted to the
         * return type by an identity or widening conversion
         * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
         * argument is negative, or if it is greater than or equal to the
         * length of the specified array
         * @see Array#get
         */
        public static native byte getByte(Object array, int index)
            throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
    ​
        /**
         * Returns the value of the indexed component in the specified
         * array object, as a {@code char}.
         *
         * @param array the array
         * @param index the index
         * @return the value of the indexed component in the specified array
         * @exception NullPointerException If the specified object is null
         * @exception IllegalArgumentException If the specified object is not
         * an array, or if the indexed element cannot be converted to the
         * return type by an identity or widening conversion
         * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
         * argument is negative, or if it is greater than or equal to the
         * length of the specified array
         * @see Array#get
         */
        public static native char getChar(Object array, int index)
            throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
    ​
        /**
         * Returns the value of the indexed component in the specified
         * array object, as a {@code short}.
         *
         * @param array the array
         * @param index the index
         * @return the value of the indexed component in the specified array
         * @exception NullPointerException If the specified object is null
         * @exception IllegalArgumentException If the specified object is not
         * an array, or if the indexed element cannot be converted to the
         * return type by an identity or widening conversion
         * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
         * argument is negative, or if it is greater than or equal to the
         * length of the specified array
         * @see Array#get
         */
        public static native short getShort(Object array, int index)
            throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
    ​
        /**
         * Returns the value of the indexed component in the specified
         * array object, as an {@code int}.
         *
         * @param array the array
         * @param index the index
         * @return the value of the indexed component in the specified array
         * @exception NullPointerException If the specified object is null
         * @exception IllegalArgumentException If the specified object is not
         * an array, or if the indexed element cannot be converted to the
         * return type by an identity or widening conversion
         * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
         * argument is negative, or if it is greater than or equal to the
         * length of the specified array
         * @see Array#get
         */
        public static native int getInt(Object array, int index)
            throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
    ​
        /**
         * Returns the value of the indexed component in the specified
         * array object, as a {@code long}.
         *
         * @param array the array
         * @param index the index
         * @return the value of the indexed component in the specified array
         * @exception NullPointerException If the specified object is null
         * @exception IllegalArgumentException If the specified object is not
         * an array, or if the indexed element cannot be converted to the
         * return type by an identity or widening conversion
         * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
         * argument is negative, or if it is greater than or equal to the
         * length of the specified array
         * @see Array#get
         */
        public static native long getLong(Object array, int index)
            throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
    ​
        /**
         * Returns the value of the indexed component in the specified
         * array object, as a {@code float}.
         *
         * @param array the array
         * @param index the index
         * @return the value of the indexed component in the specified array
         * @exception NullPointerException If the specified object is null
         * @exception IllegalArgumentException If the specified object is not
         * an array, or if the indexed element cannot be converted to the
         * return type by an identity or widening conversion
         * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
         * argument is negative, or if it is greater than or equal to the
         * length of the specified array
         * @see Array#get
         */
        public static native float getFloat(Object array, int index)
            throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
    ​
        /**
         * Returns the value of the indexed component in the specified
         * array object, as a {@code double}.
         *
         * @param array the array
         * @param index the index
         * @return the value of the indexed component in the specified array
         * @exception NullPointerException If the specified object is null
         * @exception IllegalArgumentException If the specified object is not
         * an array, or if the indexed element cannot be converted to the
         * return type by an identity or widening conversion
         * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
         * argument is negative, or if it is greater than or equal to the
         * length of the specified array
         * @see Array#get
         */
        public static native double getDouble(Object array, int index)
            throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
    ​
        /**
         * Sets the value of the indexed component of the specified array
         * object to the specified new value.  The new value is first
         * automatically unwrapped if the array has a primitive component
         * type.
         * @param array the array
         * @param index the index into the array
         * @param value the new value of the indexed component
         * @exception NullPointerException If the specified object argument
         * is null
         * @exception IllegalArgumentException If the specified object argument
         * is not an array, or if the array component type is primitive and
         * an unwrapping conversion fails
         * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
         * argument is negative, or if it is greater than or equal to
         * the length of the specified array
         */
        public static native void set(Object array, int index, Object value)
            throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
    ​
        /**
         * Sets the value of the indexed component of the specified array
         * object to the specified {@code boolean} value.
         * @param array the array
         * @param index the index into the array
         * @param z the new value of the indexed component
         * @exception NullPointerException If the specified object argument
         * is null
         * @exception IllegalArgumentException If the specified object argument
         * is not an array, or if the specified value cannot be converted
         * to the underlying array's component type by an identity or a
         * primitive widening conversion
         * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
         * argument is negative, or if it is greater than or equal to
         * the length of the specified array
         * @see Array#set
         */
        public static native void setBoolean(Object array, int index, boolean z)
            throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
    ​
        /**
         * Sets the value of the indexed component of the specified array
         * object to the specified {@code byte} value.
         * @param array the array
         * @param index the index into the array
         * @param b the new value of the indexed component
         * @exception NullPointerException If the specified object argument
         * is null
         * @exception IllegalArgumentException If the specified object argument
         * is not an array, or if the specified value cannot be converted
         * to the underlying array's component type by an identity or a
         * primitive widening conversion
         * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
         * argument is negative, or if it is greater than or equal to
         * the length of the specified array
         * @see Array#set
         */
        public static native void setByte(Object array, int index, byte b)
            throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
    ​
        /**
         * Sets the value of the indexed component of the specified array
         * object to the specified {@code char} value.
         * @param array the array
         * @param index the index into the array
         * @param c the new value of the indexed component
         * @exception NullPointerException If the specified object argument
         * is null
         * @exception IllegalArgumentException If the specified object argument
         * is not an array, or if the specified value cannot be converted
         * to the underlying array's component type by an identity or a
         * primitive widening conversion
         * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
         * argument is negative, or if it is greater than or equal to
         * the length of the specified array
         * @see Array#set
         */
        public static native void setChar(Object array, int index, char c)
            throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
    ​
        /**
         * Sets the value of the indexed component of the specified array
         * object to the specified {@code short} value.
         * @param array the array
         * @param index the index into the array
         * @param s the new value of the indexed component
         * @exception NullPointerException If the specified object argument
         * is null
         * @exception IllegalArgumentException If the specified object argument
         * is not an array, or if the specified value cannot be converted
         * to the underlying array's component type by an identity or a
         * primitive widening conversion
         * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
         * argument is negative, or if it is greater than or equal to
         * the length of the specified array
         * @see Array#set
         */
        public static native void setShort(Object array, int index, short s)
            throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
    ​
        /**
         * Sets the value of the indexed component of the specified array
         * object to the specified {@code int} value.
         * @param array the array
         * @param index the index into the array
         * @param i the new value of the indexed component
         * @exception NullPointerException If the specified object argument
         * is null
         * @exception IllegalArgumentException If the specified object argument
         * is not an array, or if the specified value cannot be converted
         * to the underlying array's component type by an identity or a
         * primitive widening conversion
         * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
         * argument is negative, or if it is greater than or equal to
         * the length of the specified array
         * @see Array#set
         */
        public static native void setInt(Object array, int index, int i)
            throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
    ​
        /**
         * Sets the value of the indexed component of the specified array
         * object to the specified {@code long} value.
         * @param array the array
         * @param index the index into the array
         * @param l the new value of the indexed component
         * @exception NullPointerException If the specified object argument
         * is null
         * @exception IllegalArgumentException If the specified object argument
         * is not an array, or if the specified value cannot be converted
         * to the underlying array's component type by an identity or a
         * primitive widening conversion
         * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
         * argument is negative, or if it is greater than or equal to
         * the length of the specified array
         * @see Array#set
         */
        public static native void setLong(Object array, int index, long l)
            throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
    ​
        /**
         * Sets the value of the indexed component of the specified array
         * object to the specified {@code float} value.
         * @param array the array
         * @param index the index into the array
         * @param f the new value of the indexed component
         * @exception NullPointerException If the specified object argument
         * is null
         * @exception IllegalArgumentException If the specified object argument
         * is not an array, or if the specified value cannot be converted
         * to the underlying array's component type by an identity or a
         * primitive widening conversion
         * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
         * argument is negative, or if it is greater than or equal to
         * the length of the specified array
         * @see Array#set
         */
        public static native void setFloat(Object array, int index, float f)
            throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
    ​
        /**
         * Sets the value of the indexed component of the specified array
         * object to the specified {@code double} value.
         * @param array the array
         * @param index the index into the array
         * @param d the new value of the indexed component
         * @exception NullPointerException If the specified object argument
         * is null
         * @exception IllegalArgumentException If the specified object argument
         * is not an array, or if the specified value cannot be converted
         * to the underlying array's component type by an identity or a
         * primitive widening conversion
         * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
         * argument is negative, or if it is greater than or equal to
         * the length of the specified array
         * @see Array#set
         */
        public static native void setDouble(Object array, int index, double d)
            throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
    ​
        /*
         * Private
         */private static native Object newArray(Class<?> componentType, int length)
            throws NegativeArraySizeException;
    ​
        private static native Object multiNewArray(Class<?> componentType,
            int[] dimensions)
            throws IllegalArgumentException, NegativeArraySizeException;
    ​
    ​
    }
    java.lang.reflect.Array

     

    3.3 java.lang.reflect Constructor<T>

    java.lang.reflect.Constructor类提供了一个类的单个构造函数的信息和访问权限。 构造函数允许在将实际参数传到newInstance()与底层构造函数的形式参数进行匹配时进行扩展转换,但如果发生缩小转换,则会抛出IllegalArgumentException异常。

    /*
     * Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved.
     * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     *
     * This code is free software; you can redistribute it and/or modify it
     * under the terms of the GNU General Public License version 2 only, as
     * published by the Free Software Foundation.  Oracle designates this
     * particular file as subject to the "Classpath" exception as provided
     * by Oracle in the LICENSE file that accompanied this code.
     *
     * This code is distributed in the hope that it will be useful, but WITHOUT
     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
     * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
     * version 2 for more details (a copy is included in the LICENSE file that
     * accompanied this code).
     *
     * You should have received a copy of the GNU General Public License version
     * 2 along with this work; if not, write to the Free Software Foundation,
     * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
     *
     * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
     * or visit www.oracle.com if you need additional information or have any
     * questions.
     */package java.lang.reflect;
    ​
    import sun.reflect.CallerSensitive;
    import sun.reflect.ConstructorAccessor;
    import sun.reflect.Reflection;
    import sun.reflect.annotation.TypeAnnotation;
    import sun.reflect.annotation.TypeAnnotationParser;
    import sun.reflect.generics.repository.ConstructorRepository;
    import sun.reflect.generics.factory.CoreReflectionFactory;
    import sun.reflect.generics.factory.GenericsFactory;
    import sun.reflect.generics.scope.ConstructorScope;
    import java.lang.annotation.Annotation;
    import java.lang.annotation.AnnotationFormatError;
    ​
    /**
     * {@code Constructor} provides information about, and access to, a single
     * constructor for a class.
     *
     * <p>{@code Constructor} permits widening conversions to occur when matching the
     * actual parameters to newInstance() with the underlying
     * constructor's formal parameters, but throws an
     * {@code IllegalArgumentException} if a narrowing conversion would occur.
     *
     * @param <T> the class in which the constructor is declared
     *
     * @see Member
     * @see java.lang.Class
     * @see java.lang.Class#getConstructors()
     * @see java.lang.Class#getConstructor(Class[])
     * @see java.lang.Class#getDeclaredConstructors()
     *
     * @author      Kenneth Russell
     * @author      Nakul Saraiya
     */
    public final class Constructor<T> extends Executable {
        private Class<T>            clazz;
        private int                 slot;
        private Class<?>[]          parameterTypes;
        private Class<?>[]          exceptionTypes;
        private int                 modifiers;
        // Generics and annotations support
        private transient String    signature;
        // generic info repository; lazily initialized
        private transient ConstructorRepository genericInfo;
        private byte[]              annotations;
        private byte[]              parameterAnnotations;
    ​
        // Generics infrastructure
        // Accessor for factory
        private GenericsFactory getFactory() {
            // create scope and factory
            return CoreReflectionFactory.make(this, ConstructorScope.make(this));
        }
    ​
        // Accessor for generic info repository
        @Override
        ConstructorRepository getGenericInfo() {
            // lazily initialize repository if necessary
            if (genericInfo == null) {
                // create and cache generic info repository
                genericInfo =
                    ConstructorRepository.make(getSignature(),
                                               getFactory());
            }
            return genericInfo; //return cached repository
        }
    ​
        private volatile ConstructorAccessor constructorAccessor;
        // For sharing of ConstructorAccessors. This branching structure
        // is currently only two levels deep (i.e., one root Constructor
        // and potentially many Constructor objects pointing to it.)
        //
        // If this branching structure would ever contain cycles, deadlocks can
        // occur in annotation code.
        private Constructor<T>      root;
    ​
        /**
         * Used by Excecutable for annotation sharing.
         */
        @Override
        Executable getRoot() {
            return root;
        }
    ​
        /**
         * Package-private constructor used by ReflectAccess to enable
         * instantiation of these objects in Java code from the java.lang
         * package via sun.reflect.LangReflectAccess.
         */
        Constructor(Class<T> declaringClass,
                    Class<?>[] parameterTypes,
                    Class<?>[] checkedExceptions,
                    int modifiers,
                    int slot,
                    String signature,
                    byte[] annotations,
                    byte[] parameterAnnotations) {
            this.clazz = declaringClass;
            this.parameterTypes = parameterTypes;
            this.exceptionTypes = checkedExceptions;
            this.modifiers = modifiers;
            this.slot = slot;
            this.signature = signature;
            this.annotations = annotations;
            this.parameterAnnotations = parameterAnnotations;
        }
    ​
        /**
         * Package-private routine (exposed to java.lang.Class via
         * ReflectAccess) which returns a copy of this Constructor. The copy's
         * "root" field points to this Constructor.
         */
        Constructor<T> copy() {
            // This routine enables sharing of ConstructorAccessor objects
            // among Constructor objects which refer to the same underlying
            // method in the VM. (All of this contortion is only necessary
            // because of the "accessibility" bit in AccessibleObject,
            // which implicitly requires that new java.lang.reflect
            // objects be fabricated for each reflective call on Class
            // objects.)
            if (this.root != null)
                throw new IllegalArgumentException("Can not copy a non-root Constructor");
    ​
            Constructor<T> res = new Constructor<>(clazz,
                                                   parameterTypes,
                                                   exceptionTypes, modifiers, slot,
                                                   signature,
                                                   annotations,
                                                   parameterAnnotations);
            res.root = this;
            // Might as well eagerly propagate this if already present
            res.constructorAccessor = constructorAccessor;
            return res;
        }
    ​
        @Override
        boolean hasGenericInformation() {
            return (getSignature() != null);
        }
    ​
        @Override
        byte[] getAnnotationBytes() {
            return annotations;
        }
    ​
        /**
         * {@inheritDoc}
         */
        @Override
        public Class<T> getDeclaringClass() {
            return clazz;
        }
    ​
        /**
         * Returns the name of this constructor, as a string.  This is
         * the binary name of the constructor's declaring class.
         */
        @Override
        public String getName() {
            return getDeclaringClass().getName();
        }
    ​
        /**
         * {@inheritDoc}
         */
        @Override
        public int getModifiers() {
            return modifiers;
        }
    ​
        /**
         * {@inheritDoc}
         * @throws GenericSignatureFormatError {@inheritDoc}
         * @since 1.5
         */
        @Override
        @SuppressWarnings({"rawtypes", "unchecked"})
        public TypeVariable<Constructor<T>>[] getTypeParameters() {
          if (getSignature() != null) {
            return (TypeVariable<Constructor<T>>[])getGenericInfo().getTypeParameters();
          } else
              return (TypeVariable<Constructor<T>>[])new TypeVariable[0];
        }
    ​
    ​
        /**
         * {@inheritDoc}
         */
        @Override
        public Class<?>[] getParameterTypes() {
            return parameterTypes.clone();
        }
    ​
        /**
         * {@inheritDoc}
         * @since 1.8
         */
        public int getParameterCount() { return parameterTypes.length; }
    ​
        /**
         * {@inheritDoc}
         * @throws GenericSignatureFormatError {@inheritDoc}
         * @throws TypeNotPresentException {@inheritDoc}
         * @throws MalformedParameterizedTypeException {@inheritDoc}
         * @since 1.5
         */
        @Override
        public Type[] getGenericParameterTypes() {
            return super.getGenericParameterTypes();
        }
    ​
        /**
         * {@inheritDoc}
         */
        @Override
        public Class<?>[] getExceptionTypes() {
            return exceptionTypes.clone();
        }
    ​
    ​
        /**
         * {@inheritDoc}
         * @throws GenericSignatureFormatError {@inheritDoc}
         * @throws TypeNotPresentException {@inheritDoc}
         * @throws MalformedParameterizedTypeException {@inheritDoc}
         * @since 1.5
         */
        @Override
        public Type[] getGenericExceptionTypes() {
            return super.getGenericExceptionTypes();
        }
    ​
        /**
         * Compares this {@code Constructor} against the specified object.
         * Returns true if the objects are the same.  Two {@code Constructor} objects are
         * the same if they were declared by the same class and have the
         * same formal parameter types.
         */
        public boolean equals(Object obj) {
            if (obj != null && obj instanceof Constructor) {
                Constructor<?> other = (Constructor<?>)obj;
                if (getDeclaringClass() == other.getDeclaringClass()) {
                    return equalParamTypes(parameterTypes, other.parameterTypes);
                }
            }
            return false;
        }
    ​
        /**
         * Returns a hashcode for this {@code Constructor}. The hashcode is
         * the same as the hashcode for the underlying constructor's
         * declaring class name.
         */
        public int hashCode() {
            return getDeclaringClass().getName().hashCode();
        }
    ​
        /**
         * Returns a string describing this {@code Constructor}.  The string is
         * formatted as the constructor access modifiers, if any,
         * followed by the fully-qualified name of the declaring class,
         * followed by a parenthesized, comma-separated list of the
         * constructor's formal parameter types.  For example:
         * <pre>
         *    public java.util.Hashtable(int,float)
         * </pre>
         *
         * <p>The only possible modifiers for constructors are the access
         * modifiers {@code public}, {@code protected} or
         * {@code private}.  Only one of these may appear, or none if the
         * constructor has default (package) access.
         *
         * @return a string describing this {@code Constructor}
         * @jls 8.8.3. Constructor Modifiers
         */
        public String toString() {
            return sharedToString(Modifier.constructorModifiers(),
                                  false,
                                  parameterTypes,
                                  exceptionTypes);
        }
    ​
        @Override
        void specificToStringHeader(StringBuilder sb) {
            sb.append(getDeclaringClass().getTypeName());
        }
    ​
        /**
         * Returns a string describing this {@code Constructor},
         * including type parameters.  The string is formatted as the
         * constructor access modifiers, if any, followed by an
         * angle-bracketed comma separated list of the constructor's type
         * parameters, if any, followed by the fully-qualified name of the
         * declaring class, followed by a parenthesized, comma-separated
         * list of the constructor's generic formal parameter types.
         *
         * If this constructor was declared to take a variable number of
         * arguments, instead of denoting the last parameter as
         * "<tt><i>Type</i>[]</tt>", it is denoted as
         * "<tt><i>Type</i>...</tt>".
         *
         * A space is used to separate access modifiers from one another
         * and from the type parameters or return type.  If there are no
         * type parameters, the type parameter list is elided; if the type
         * parameter list is present, a space separates the list from the
         * class name.  If the constructor is declared to throw
         * exceptions, the parameter list is followed by a space, followed
         * by the word "{@code throws}" followed by a
         * comma-separated list of the thrown exception types.
         *
         * <p>The only possible modifiers for constructors are the access
         * modifiers {@code public}, {@code protected} or
         * {@code private}.  Only one of these may appear, or none if the
         * constructor has default (package) access.
         *
         * @return a string describing this {@code Constructor},
         * include type parameters
         *
         * @since 1.5
         * @jls 8.8.3. Constructor Modifiers
         */
        @Override
        public String toGenericString() {
            return sharedToGenericString(Modifier.constructorModifiers(), false);
        }
    ​
        @Override
        void specificToGenericStringHeader(StringBuilder sb) {
            specificToStringHeader(sb);
        }
    ​
        /**
         * Uses the constructor represented by this {@code Constructor} object to
         * create and initialize a new instance of the constructor's
         * declaring class, with the specified initialization parameters.
         * Individual parameters are automatically unwrapped to match
         * primitive formal parameters, and both primitive and reference
         * parameters are subject to method invocation conversions as necessary.
         *
         * <p>If the number of formal parameters required by the underlying constructor
         * is 0, the supplied {@code initargs} array may be of length 0 or null.
         *
         * <p>If the constructor's declaring class is an inner class in a
         * non-static context, the first argument to the constructor needs
         * to be the enclosing instance; see section 15.9.3 of
         * <cite>The Java&trade; Language Specification</cite>.
         *
         * <p>If the required access and argument checks succeed and the
         * instantiation will proceed, the constructor's declaring class
         * is initialized if it has not already been initialized.
         *
         * <p>If the constructor completes normally, returns the newly
         * created and initialized instance.
         *
         * @param initargs array of objects to be passed as arguments to
         * the constructor call; values of primitive types are wrapped in
         * a wrapper object of the appropriate type (e.g. a {@code float}
         * in a {@link java.lang.Float Float})
         *
         * @return a new object created by calling the constructor
         * this object represents
         *
         * @exception IllegalAccessException    if this {@code Constructor} object
         *              is enforcing Java language access control and the underlying
         *              constructor is inaccessible.
         * @exception IllegalArgumentException  if the number of actual
         *              and formal parameters differ; if an unwrapping
         *              conversion for primitive arguments fails; or if,
         *              after possible unwrapping, a parameter value
         *              cannot be converted to the corresponding formal
         *              parameter type by a method invocation conversion; if
         *              this constructor pertains to an enum type.
         * @exception InstantiationException    if the class that declares the
         *              underlying constructor represents an abstract class.
         * @exception InvocationTargetException if the underlying constructor
         *              throws an exception.
         * @exception ExceptionInInitializerError if the initialization provoked
         *              by this method fails.
         */
        @CallerSensitive
        public T newInstance(Object ... initargs)
            throws InstantiationException, IllegalAccessException,
                   IllegalArgumentException, InvocationTargetException
        {
            if (!override) {
                if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) {
                    Class<?> caller = Reflection.getCallerClass();
                    checkAccess(caller, clazz, null, modifiers);
                }
            }
            if ((clazz.getModifiers() & Modifier.ENUM) != 0)
                throw new IllegalArgumentException("Cannot reflectively create enum objects");
            ConstructorAccessor ca = constructorAccessor;   // read volatile
            if (ca == null) {
                ca = acquireConstructorAccessor();
            }
            @SuppressWarnings("unchecked")
            T inst = (T) ca.newInstance(initargs);
            return inst;
        }
    ​
        /**
         * {@inheritDoc}
         * @since 1.5
         */
        @Override
        public boolean isVarArgs() {
            return super.isVarArgs();
        }
    ​
        /**
         * {@inheritDoc}
         * @jls 13.1 The Form of a Binary
         * @since 1.5
         */
        @Override
        public boolean isSynthetic() {
            return super.isSynthetic();
        }
    ​
        // NOTE that there is no synchronization used here. It is correct
        // (though not efficient) to generate more than one
        // ConstructorAccessor for a given Constructor. However, avoiding
        // synchronization will probably make the implementation more
        // scalable.
        private ConstructorAccessor acquireConstructorAccessor() {
            // First check to see if one has been created yet, and take it
            // if so.
            ConstructorAccessor tmp = null;
            if (root != null) tmp = root.getConstructorAccessor();
            if (tmp != null) {
                constructorAccessor = tmp;
            } else {
                // Otherwise fabricate one and propagate it up to the root
                tmp = reflectionFactory.newConstructorAccessor(this);
                setConstructorAccessor(tmp);
            }
    ​
            return tmp;
        }
    ​
        // Returns ConstructorAccessor for this Constructor object, not
        // looking up the chain to the root
        ConstructorAccessor getConstructorAccessor() {
            return constructorAccessor;
        }
    ​
        // Sets the ConstructorAccessor for this Constructor object and
        // (recursively) its root
        void setConstructorAccessor(ConstructorAccessor accessor) {
            constructorAccessor = accessor;
            // Propagate up
            if (root != null) {
                root.setConstructorAccessor(accessor);
            }
        }
    ​
        int getSlot() {
            return slot;
        }
    ​
        String getSignature() {
            return signature;
        }
    ​
        byte[] getRawAnnotations() {
            return annotations;
        }
    ​
        byte[] getRawParameterAnnotations() {
            return parameterAnnotations;
        }
    ​
    ​
        /**
         * {@inheritDoc}
         * @throws NullPointerException  {@inheritDoc}
         * @since 1.5
         */
        public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
            return super.getAnnotation(annotationClass);
        }
    ​
        /**
         * {@inheritDoc}
         * @since 1.5
         */
        public Annotation[] getDeclaredAnnotations()  {
            return super.getDeclaredAnnotations();
        }
    ​
        /**
         * {@inheritDoc}
         * @since 1.5
         */
        @Override
        public Annotation[][] getParameterAnnotations() {
            return sharedGetParameterAnnotations(parameterTypes, parameterAnnotations);
        }
    ​
        @Override
        void handleParameterNumberMismatch(int resultLength, int numParameters) {
            Class<?> declaringClass = getDeclaringClass();
            if (declaringClass.isEnum() ||
                declaringClass.isAnonymousClass() ||
                declaringClass.isLocalClass() )
                return ; // Can't do reliable parameter counting
            else {
                if (!declaringClass.isMemberClass() || // top-level
                    // Check for the enclosing instance parameter for
                    // non-static member classes
                    (declaringClass.isMemberClass() &&
                     ((declaringClass.getModifiers() & Modifier.STATIC) == 0)  &&
                     resultLength + 1 != numParameters) ) {
                    throw new AnnotationFormatError(
                              "Parameter annotations don't match number of parameters");
                }
            }
        }
    ​
        /**
         * {@inheritDoc}
         * @since 1.8
         */
        @Override
        public AnnotatedType getAnnotatedReturnType() {
            return getAnnotatedReturnType0(getDeclaringClass());
        }
    ​
        /**
         * {@inheritDoc}
         * @since 1.8
         */
        @Override
        public AnnotatedType getAnnotatedReceiverType() {
            if (getDeclaringClass().getEnclosingClass() == null)
                return super.getAnnotatedReceiverType();
    ​
            return TypeAnnotationParser.buildAnnotatedType(getTypeAnnotationBytes0(),
                    sun.misc.SharedSecrets.getJavaLangAccess().
                            getConstantPool(getDeclaringClass()),
                    this,
                    getDeclaringClass(),
                    getDeclaringClass().getEnclosingClass(),
                    TypeAnnotation.TypeAnnotationTarget.METHOD_RECEIVER);
        }
    }
    ​
    java.lang.reflect.Constructor
    
    

    3.4 java.lang.reflect.Field

    java.lang.reflect.Field类提供有关类或接口的单个字段的信息和动态访问。反射的字段可以是类(静态)字段或实例字段。字段允许在获取或设置访问操作期间扩展转换,但如果发生缩小转换,则会引发IllegalArgumentException

    /*
     * Copyright (c) 1996, 2015, Oracle and/or its affiliates. All rights reserved.
     * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     *
     * This code is free software; you can redistribute it and/or modify it
     * under the terms of the GNU General Public License version 2 only, as
     * published by the Free Software Foundation.  Oracle designates this
     * particular file as subject to the "Classpath" exception as provided
     * by Oracle in the LICENSE file that accompanied this code.
     *
     * This code is distributed in the hope that it will be useful, but WITHOUT
     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
     * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
     * version 2 for more details (a copy is included in the LICENSE file that
     * accompanied this code).
     *
     * You should have received a copy of the GNU General Public License version
     * 2 along with this work; if not, write to the Free Software Foundation,
     * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
     *
     * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
     * or visit www.oracle.com if you need additional information or have any
     * questions.
     */package java.lang.reflect;
    ​
    import sun.reflect.CallerSensitive;
    import sun.reflect.FieldAccessor;
    import sun.reflect.Reflection;
    import sun.reflect.generics.repository.FieldRepository;
    import sun.reflect.generics.factory.CoreReflectionFactory;
    import sun.reflect.generics.factory.GenericsFactory;
    import sun.reflect.generics.scope.ClassScope;
    import java.lang.annotation.Annotation;
    import java.util.Map;
    import java.util.Objects;
    import sun.reflect.annotation.AnnotationParser;
    import sun.reflect.annotation.AnnotationSupport;
    import sun.reflect.annotation.TypeAnnotation;
    import sun.reflect.annotation.TypeAnnotationParser;
    ​
    /**
     * A {@code Field} provides information about, and dynamic access to, a
     * single field of a class or an interface.  The reflected field may
     * be a class (static) field or an instance field.
     *
     * <p>A {@code Field} permits widening conversions to occur during a get or
     * set access operation, but throws an {@code IllegalArgumentException} if a
     * narrowing conversion would occur.
     *
     * @see Member
     * @see java.lang.Class
     * @see java.lang.Class#getFields()
     * @see java.lang.Class#getField(String)
     * @see java.lang.Class#getDeclaredFields()
     * @see java.lang.Class#getDeclaredField(String)
     *
     * @author Kenneth Russell
     * @author Nakul Saraiya
     */
    public final
    class Field extends AccessibleObject implements Member {
    ​
        private Class<?>            clazz;
        private int                 slot;
        // This is guaranteed to be interned by the VM in the 1.4
        // reflection implementation
        private String              name;
        private Class<?>            type;
        private int                 modifiers;
        // Generics and annotations support
        private transient String    signature;
        // generic info repository; lazily initialized
        private transient FieldRepository genericInfo;
        private byte[]              annotations;
        // Cached field accessor created without override
        private FieldAccessor fieldAccessor;
        // Cached field accessor created with override
        private FieldAccessor overrideFieldAccessor;
        // For sharing of FieldAccessors. This branching structure is
        // currently only two levels deep (i.e., one root Field and
        // potentially many Field objects pointing to it.)
        //
        // If this branching structure would ever contain cycles, deadlocks can
        // occur in annotation code.
        private Field               root;
    ​
        // Generics infrastructure
    private String getGenericSignature() {return signature;}
    ​
        // Accessor for factory
        private GenericsFactory getFactory() {
            Class<?> c = getDeclaringClass();
            // create scope and factory
            return CoreReflectionFactory.make(c, ClassScope.make(c));
        }
    ​
        // Accessor for generic info repository
        private FieldRepository getGenericInfo() {
            // lazily initialize repository if necessary
            if (genericInfo == null) {
                // create and cache generic info repository
                genericInfo = FieldRepository.make(getGenericSignature(),
                                                   getFactory());
            }
            return genericInfo; //return cached repository
        }
    ​
    ​
        /**
         * Package-private constructor used by ReflectAccess to enable
         * instantiation of these objects in Java code from the java.lang
         * package via sun.reflect.LangReflectAccess.
         */
        Field(Class<?> declaringClass,
              String name,
              Class<?> type,
              int modifiers,
              int slot,
              String signature,
              byte[] annotations)
        {
            this.clazz = declaringClass;
            this.name = name;
            this.type = type;
            this.modifiers = modifiers;
            this.slot = slot;
            this.signature = signature;
            this.annotations = annotations;
        }
    ​
        /**
         * Package-private routine (exposed to java.lang.Class via
         * ReflectAccess) which returns a copy of this Field. The copy's
         * "root" field points to this Field.
         */
        Field copy() {
            // This routine enables sharing of FieldAccessor objects
            // among Field objects which refer to the same underlying
            // method in the VM. (All of this contortion is only necessary
            // because of the "accessibility" bit in AccessibleObject,
            // which implicitly requires that new java.lang.reflect
            // objects be fabricated for each reflective call on Class
            // objects.)
            if (this.root != null)
                throw new IllegalArgumentException("Can not copy a non-root Field");
    ​
            Field res = new Field(clazz, name, type, modifiers, slot, signature, annotations);
            res.root = this;
            // Might as well eagerly propagate this if already present
            res.fieldAccessor = fieldAccessor;
            res.overrideFieldAccessor = overrideFieldAccessor;
    ​
            return res;
        }
    ​
        /**
         * Returns the {@code Class} object representing the class or interface
         * that declares the field represented by this {@code Field} object.
         */
        public Class<?> getDeclaringClass() {
            return clazz;
        }
    ​
        /**
         * Returns the name of the field represented by this {@code Field} object.
         */
        public String getName() {
            return name;
        }
    ​
        /**
         * Returns the Java language modifiers for the field represented
         * by this {@code Field} object, as an integer. The {@code Modifier} class should
         * be used to decode the modifiers.
         *
         * @see Modifier
         */
        public int getModifiers() {
            return modifiers;
        }
    ​
        /**
         * Returns {@code true} if this field represents an element of
         * an enumerated type; returns {@code false} otherwise.
         *
         * @return {@code true} if and only if this field represents an element of
         * an enumerated type.
         * @since 1.5
         */
        public boolean isEnumConstant() {
            return (getModifiers() & Modifier.ENUM) != 0;
        }
    ​
        /**
         * Returns {@code true} if this field is a synthetic
         * field; returns {@code false} otherwise.
         *
         * @return true if and only if this field is a synthetic
         * field as defined by the Java Language Specification.
         * @since 1.5
         */
        public boolean isSynthetic() {
            return Modifier.isSynthetic(getModifiers());
        }
    ​
        /**
         * Returns a {@code Class} object that identifies the
         * declared type for the field represented by this
         * {@code Field} object.
         *
         * @return a {@code Class} object identifying the declared
         * type of the field represented by this object
         */
        public Class<?> getType() {
            return type;
        }
    ​
        /**
         * Returns a {@code Type} object that represents the declared type for
         * the field represented by this {@code Field} object.
         *
         * <p>If the {@code Type} is a parameterized type, the
         * {@code Type} object returned must accurately reflect the
         * actual type parameters used in the source code.
         *
         * <p>If the type of the underlying field is a type variable or a
         * parameterized type, it is created. Otherwise, it is resolved.
         *
         * @return a {@code Type} object that represents the declared type for
         *     the field represented by this {@code Field} object
         * @throws GenericSignatureFormatError if the generic field
         *     signature does not conform to the format specified in
         *     <cite>The Java&trade; Virtual Machine Specification</cite>
         * @throws TypeNotPresentException if the generic type
         *     signature of the underlying field refers to a non-existent
         *     type declaration
         * @throws MalformedParameterizedTypeException if the generic
         *     signature of the underlying field refers to a parameterized type
         *     that cannot be instantiated for any reason
         * @since 1.5
         */
        public Type getGenericType() {
            if (getGenericSignature() != null)
                return getGenericInfo().getGenericType();
            else
                return getType();
        }
    ​
    ​
        /**
         * Compares this {@code Field} against the specified object.  Returns
         * true if the objects are the same.  Two {@code Field} objects are the same if
         * they were declared by the same class and have the same name
         * and type.
         */
        public boolean equals(Object obj) {
            if (obj != null && obj instanceof Field) {
                Field other = (Field)obj;
                return (getDeclaringClass() == other.getDeclaringClass())
                    && (getName() == other.getName())
                    && (getType() == other.getType());
            }
            return false;
        }
    ​
        /**
         * Returns a hashcode for this {@code Field}.  This is computed as the
         * exclusive-or of the hashcodes for the underlying field's
         * declaring class name and its name.
         */
        public int hashCode() {
            return getDeclaringClass().getName().hashCode() ^ getName().hashCode();
        }
    ​
        /**
         * Returns a string describing this {@code Field}.  The format is
         * the access modifiers for the field, if any, followed
         * by the field type, followed by a space, followed by
         * the fully-qualified name of the class declaring the field,
         * followed by a period, followed by the name of the field.
         * For example:
         * <pre>
         *    public static final int java.lang.Thread.MIN_PRIORITY
         *    private int java.io.FileDescriptor.fd
         * </pre>
         *
         * <p>The modifiers are placed in canonical order as specified by
         * "The Java Language Specification".  This is {@code public},
         * {@code protected} or {@code private} first, and then other
         * modifiers in the following order: {@code static}, {@code final},
         * {@code transient}, {@code volatile}.
         *
         * @return a string describing this {@code Field}
         * @jls 8.3.1 Field Modifiers
         */
        public String toString() {
            int mod = getModifiers();
            return (((mod == 0) ? "" : (Modifier.toString(mod) + " "))
                + getType().getTypeName() + " "
                + getDeclaringClass().getTypeName() + "."
                + getName());
        }
    ​
        /**
         * Returns a string describing this {@code Field}, including
         * its generic type.  The format is the access modifiers for the
         * field, if any, followed by the generic field type, followed by
         * a space, followed by the fully-qualified name of the class
         * declaring the field, followed by a period, followed by the name
         * of the field.
         *
         * <p>The modifiers are placed in canonical order as specified by
         * "The Java Language Specification".  This is {@code public},
         * {@code protected} or {@code private} first, and then other
         * modifiers in the following order: {@code static}, {@code final},
         * {@code transient}, {@code volatile}.
         *
         * @return a string describing this {@code Field}, including
         * its generic type
         *
         * @since 1.5
         * @jls 8.3.1 Field Modifiers
         */
        public String toGenericString() {
            int mod = getModifiers();
            Type fieldType = getGenericType();
            return (((mod == 0) ? "" : (Modifier.toString(mod) + " "))
                + fieldType.getTypeName() + " "
                + getDeclaringClass().getTypeName() + "."
                + getName());
        }
    ​
        /**
         * Returns the value of the field represented by this {@code Field}, on
         * the specified object. The value is automatically wrapped in an
         * object if it has a primitive type.
         *
         * <p>The underlying field's value is obtained as follows:
         *
         * <p>If the underlying field is a static field, the {@code obj} argument
         * is ignored; it may be null.
         *
         * <p>Otherwise, the underlying field is an instance field.  If the
         * specified {@code obj} argument is null, the method throws a
         * {@code NullPointerException}. If the specified object is not an
         * instance of the class or interface declaring the underlying
         * field, the method throws an {@code IllegalArgumentException}.
         *
         * <p>If this {@code Field} object is enforcing Java language access control, and
         * the underlying field is inaccessible, the method throws an
         * {@code IllegalAccessException}.
         * If the underlying field is static, the class that declared the
         * field is initialized if it has not already been initialized.
         *
         * <p>Otherwise, the value is retrieved from the underlying instance
         * or static field.  If the field has a primitive type, the value
         * is wrapped in an object before being returned, otherwise it is
         * returned as is.
         *
         * <p>If the field is hidden in the type of {@code obj},
         * the field's value is obtained according to the preceding rules.
         *
         * @param obj object from which the represented field's value is
         * to be extracted
         * @return the value of the represented field in object
         * {@code obj}; primitive values are wrapped in an appropriate
         * object before being returned
         *
         * @exception IllegalAccessException    if this {@code Field} object
         *              is enforcing Java language access control and the underlying
         *              field is inaccessible.
         * @exception IllegalArgumentException  if the specified object is not an
         *              instance of the class or interface declaring the underlying
         *              field (or a subclass or implementor thereof).
         * @exception NullPointerException      if the specified object is null
         *              and the field is an instance field.
         * @exception ExceptionInInitializerError if the initialization provoked
         *              by this method fails.
         */
        @CallerSensitive
        public Object get(Object obj)
            throws IllegalArgumentException, IllegalAccessException
        {
            if (!override) {
                if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) {
                    Class<?> caller = Reflection.getCallerClass();
                    checkAccess(caller, clazz, obj, modifiers);
                }
            }
            return getFieldAccessor(obj).get(obj);
        }
    ​
        /**
         * Gets the value of a static or instance {@code boolean} field.
         *
         * @param obj the object to extract the {@code boolean} value
         * from
         * @return the value of the {@code boolean} field
         *
         * @exception IllegalAccessException    if this {@code Field} object
         *              is enforcing Java language access control and the underlying
         *              field is inaccessible.
         * @exception IllegalArgumentException  if the specified object is not
         *              an instance of the class or interface declaring the
         *              underlying field (or a subclass or implementor
         *              thereof), or if the field value cannot be
         *              converted to the type {@code boolean} by a
         *              widening conversion.
         * @exception NullPointerException      if the specified object is null
         *              and the field is an instance field.
         * @exception ExceptionInInitializerError if the initialization provoked
         *              by this method fails.
         * @see       Field#get
         */
        @CallerSensitive
        public boolean getBoolean(Object obj)
            throws IllegalArgumentException, IllegalAccessException
        {
            if (!override) {
                if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) {
                    Class<?> caller = Reflection.getCallerClass();
                    checkAccess(caller, clazz, obj, modifiers);
                }
            }
            return getFieldAccessor(obj).getBoolean(obj);
        }
    ​
        /**
         * Gets the value of a static or instance {@code byte} field.
         *
         * @param obj the object to extract the {@code byte} value
         * from
         * @return the value of the {@code byte} field
         *
         * @exception IllegalAccessException    if this {@code Field} object
         *              is enforcing Java language access control and the underlying
         *              field is inaccessible.
         * @exception IllegalArgumentException  if the specified object is not
         *              an instance of the class or interface declaring the
         *              underlying field (or a subclass or implementor
         *              thereof), or if the field value cannot be
         *              converted to the type {@code byte} by a
         *              widening conversion.
         * @exception NullPointerException      if the specified object is null
         *              and the field is an instance field.
         * @exception ExceptionInInitializerError if the initialization provoked
         *              by this method fails.
         * @see       Field#get
         */
        @CallerSensitive
        public byte getByte(Object obj)
            throws IllegalArgumentException, IllegalAccessException
        {
            if (!override) {
                if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) {
                    Class<?> caller = Reflection.getCallerClass();
                    checkAccess(caller, clazz, obj, modifiers);
                }
            }
            return getFieldAccessor(obj).getByte(obj);
        }
    ​
        /**
         * Gets the value of a static or instance field of type
         * {@code char} or of another primitive type convertible to
         * type {@code char} via a widening conversion.
         *
         * @param obj the object to extract the {@code char} value
         * from
         * @return the value of the field converted to type {@code char}
         *
         * @exception IllegalAccessException    if this {@code Field} object
         *              is enforcing Java language access control and the underlying
         *              field is inaccessible.
         * @exception IllegalArgumentException  if the specified object is not
         *              an instance of the class or interface declaring the
         *              underlying field (or a subclass or implementor
         *              thereof), or if the field value cannot be
         *              converted to the type {@code char} by a
         *              widening conversion.
         * @exception NullPointerException      if the specified object is null
         *              and the field is an instance field.
         * @exception ExceptionInInitializerError if the initialization provoked
         *              by this method fails.
         * @see Field#get
         */
        @CallerSensitive
        public char getChar(Object obj)
            throws IllegalArgumentException, IllegalAccessException
        {
            if (!override) {
                if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) {
                    Class<?> caller = Reflection.getCallerClass();
                    checkAccess(caller, clazz, obj, modifiers);
                }
            }
            return getFieldAccessor(obj).getChar(obj);
        }
    ​
        /**
         * Gets the value of a static or instance field of type
         * {@code short} or of another primitive type convertible to
         * type {@code short} via a widening conversion.
         *
         * @param obj the object to extract the {@code short} value
         * from
         * @return the value of the field converted to type {@code short}
         *
         * @exception IllegalAccessException    if this {@code Field} object
         *              is enforcing Java language access control and the underlying
         *              field is inaccessible.
         * @exception IllegalArgumentException  if the specified object is not
         *              an instance of the class or interface declaring the
         *              underlying field (or a subclass or implementor
         *              thereof), or if the field value cannot be
         *              converted to the type {@code short} by a
         *              widening conversion.
         * @exception NullPointerException      if the specified object is null
         *              and the field is an instance field.
         * @exception ExceptionInInitializerError if the initialization provoked
         *              by this method fails.
         * @see       Field#get
         */
        @CallerSensitive
        public short getShort(Object obj)
            throws IllegalArgumentException, IllegalAccessException
        {
            if (!override) {
                if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) {
                    Class<?> caller = Reflection.getCallerClass();
                    checkAccess(caller, clazz, obj, modifiers);
                }
            }
            return getFieldAccessor(obj).getShort(obj);
        }
    ​
        /**
         * Gets the value of a static or instance field of type
         * {@code int} or of another primitive type convertible to
         * type {@code int} via a widening conversion.
         *
         * @param obj the object to extract the {@code int} value
         * from
         * @return the value of the field converted to type {@code int}
         *
         * @exception IllegalAccessException    if this {@code Field} object
         *              is enforcing Java language access control and the underlying
         *              field is inaccessible.
         * @exception IllegalArgumentException  if the specified object is not
         *              an instance of the class or interface declaring the
         *              underlying field (or a subclass or implementor
         *              thereof), or if the field value cannot be
         *              converted to the type {@code int} by a
         *              widening conversion.
         * @exception NullPointerException      if the specified object is null
         *              and the field is an instance field.
         * @exception ExceptionInInitializerError if the initialization provoked
         *              by this method fails.
         * @see       Field#get
         */
        @CallerSensitive
        public int getInt(Object obj)
            throws IllegalArgumentException, IllegalAccessException
        {
            if (!override) {
                if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) {
                    Class<?> caller = Reflection.getCallerClass();
                    checkAccess(caller, clazz, obj, modifiers);
                }
            }
            return getFieldAccessor(obj).getInt(obj);
        }
    ​
        /**
         * Gets the value of a static or instance field of type
         * {@code long} or of another primitive type convertible to
         * type {@code long} via a widening conversion.
         *
         * @param obj the object to extract the {@code long} value
         * from
         * @return the value of the field converted to type {@code long}
         *
         * @exception IllegalAccessException    if this {@code Field} object
         *              is enforcing Java language access control and the underlying
         *              field is inaccessible.
         * @exception IllegalArgumentException  if the specified object is not
         *              an instance of the class or interface declaring the
         *              underlying field (or a subclass or implementor
         *              thereof), or if the field value cannot be
         *              converted to the type {@code long} by a
         *              widening conversion.
         * @exception NullPointerException      if the specified object is null
         *              and the field is an instance field.
         * @exception ExceptionInInitializerError if the initialization provoked
         *              by this method fails.
         * @see       Field#get
         */
        @CallerSensitive
        public long getLong(Object obj)
            throws IllegalArgumentException, IllegalAccessException
        {
            if (!override) {
                if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) {
                    Class<?> caller = Reflection.getCallerClass();
                    checkAccess(caller, clazz, obj, modifiers);
                }
            }
            return getFieldAccessor(obj).getLong(obj);
        }
    ​
        /**
         * Gets the value of a static or instance field of type
         * {@code float} or of another primitive type convertible to
         * type {@code float} via a widening conversion.
         *
         * @param obj the object to extract the {@code float} value
         * from
         * @return the value of the field converted to type {@code float}
         *
         * @exception IllegalAccessException    if this {@code Field} object
         *              is enforcing Java language access control and the underlying
         *              field is inaccessible.
         * @exception IllegalArgumentException  if the specified object is not
         *              an instance of the class or interface declaring the
         *              underlying field (or a subclass or implementor
         *              thereof), or if the field value cannot be
         *              converted to the type {@code float} by a
         *              widening conversion.
         * @exception NullPointerException      if the specified object is null
         *              and the field is an instance field.
         * @exception ExceptionInInitializerError if the initialization provoked
         *              by this method fails.
         * @see Field#get
         */
        @CallerSensitive
        public float getFloat(Object obj)
            throws IllegalArgumentException, IllegalAccessException
        {
            if (!override) {
                if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) {
                    Class<?> caller = Reflection.getCallerClass();
                    checkAccess(caller, clazz, obj, modifiers);
                }
            }
            return getFieldAccessor(obj).getFloat(obj);
        }
    ​
        /**
         * Gets the value of a static or instance field of type
         * {@code double} or of another primitive type convertible to
         * type {@code double} via a widening conversion.
         *
         * @param obj the object to extract the {@code double} value
         * from
         * @return the value of the field converted to type {@code double}
         *
         * @exception IllegalAccessException    if this {@code Field} object
         *              is enforcing Java language access control and the underlying
         *              field is inaccessible.
         * @exception IllegalArgumentException  if the specified object is not
         *              an instance of the class or interface declaring the
         *              underlying field (or a subclass or implementor
         *              thereof), or if the field value cannot be
         *              converted to the type {@code double} by a
         *              widening conversion.
         * @exception NullPointerException      if the specified object is null
         *              and the field is an instance field.
         * @exception ExceptionInInitializerError if the initialization provoked
         *              by this method fails.
         * @see       Field#get
         */
        @CallerSensitive
        public double getDouble(Object obj)
            throws IllegalArgumentException, IllegalAccessException
        {
            if (!override) {
                if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) {
                    Class<?> caller = Reflection.getCallerClass();
                    checkAccess(caller, clazz, obj, modifiers);
                }
            }
            return getFieldAccessor(obj).getDouble(obj);
        }
    ​
        /**
         * Sets the field represented by this {@code Field} object on the
         * specified object argument to the specified new value. The new
         * value is automatically unwrapped if the underlying field has a
         * primitive type.
         *
         * <p>The operation proceeds as follows:
         *
         * <p>If the underlying field is static, the {@code obj} argument is
         * ignored; it may be null.
         *
         * <p>Otherwise the underlying field is an instance field.  If the
         * specified object argument is null, the method throws a
         * {@code NullPointerException}.  If the specified object argument is not
         * an instance of the class or interface declaring the underlying
         * field, the method throws an {@code IllegalArgumentException}.
         *
         * <p>If this {@code Field} object is enforcing Java language access control, and
         * the underlying field is inaccessible, the method throws an
         * {@code IllegalAccessException}.
         *
         * <p>If the underlying field is final, the method throws an
         * {@code IllegalAccessException} unless {@code setAccessible(true)}
         * has succeeded for this {@code Field} object
         * and the field is non-static. Setting a final field in this way
         * is meaningful only during deserialization or reconstruction of
         * instances of classes with blank final fields, before they are
         * made available for access by other parts of a program. Use in
         * any other context may have unpredictable effects, including cases
         * in which other parts of a program continue to use the original
         * value of this field.
         *
         * <p>If the underlying field is of a primitive type, an unwrapping
         * conversion is attempted to convert the new value to a value of
         * a primitive type.  If this attempt fails, the method throws an
         * {@code IllegalArgumentException}.
         *
         * <p>If, after possible unwrapping, the new value cannot be
         * converted to the type of the underlying field by an identity or
         * widening conversion, the method throws an
         * {@code IllegalArgumentException}.
         *
         * <p>If the underlying field is static, the class that declared the
         * field is initialized if it has not already been initialized.
         *
         * <p>The field is set to the possibly unwrapped and widened new value.
         *
         * <p>If the field is hidden in the type of {@code obj},
         * the field's value is set according to the preceding rules.
         *
         * @param obj the object whose field should be modified
         * @param value the new value for the field of {@code obj}
         * being modified
         *
         * @exception IllegalAccessException    if this {@code Field} object
         *              is enforcing Java language access control and the underlying
         *              field is either inaccessible or final.
         * @exception IllegalArgumentException  if the specified object is not an
         *              instance of the class or interface declaring the underlying
         *              field (or a subclass or implementor thereof),
         *              or if an unwrapping conversion fails.
         * @exception NullPointerException      if the specified object is null
         *              and the field is an instance field.
         * @exception ExceptionInInitializerError if the initialization provoked
         *              by this method fails.
         */
        @CallerSensitive
        public void set(Object obj, Object value)
            throws IllegalArgumentException, IllegalAccessException
        {
            if (!override) {
                if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) {
                    Class<?> caller = Reflection.getCallerClass();
                    checkAccess(caller, clazz, obj, modifiers);
                }
            }
            getFieldAccessor(obj).set(obj, value);
        }
    ​
        /**
         * Sets the value of a field as a {@code boolean} on the specified object.
         * This method is equivalent to
         * {@code set(obj, zObj)},
         * where {@code zObj} is a {@code Boolean} object and
         * {@code zObj.booleanValue() == z}.
         *
         * @param obj the object whose field should be modified
         * @param z   the new value for the field of {@code obj}
         * being modified
         *
         * @exception IllegalAccessException    if this {@code Field} object
         *              is enforcing Java language access control and the underlying
         *              field is either inaccessible or final.
         * @exception IllegalArgumentException  if the specified object is not an
         *              instance of the class or interface declaring the underlying
         *              field (or a subclass or implementor thereof),
         *              or if an unwrapping conversion fails.
         * @exception NullPointerException      if the specified object is null
         *              and the field is an instance field.
         * @exception ExceptionInInitializerError if the initialization provoked
         *              by this method fails.
         * @see       Field#set
         */
        @CallerSensitive
        public void setBoolean(Object obj, boolean z)
            throws IllegalArgumentException, IllegalAccessException
        {
            if (!override) {
                if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) {
                    Class<?> caller = Reflection.getCallerClass();
                    checkAccess(caller, clazz, obj, modifiers);
                }
            }
            getFieldAccessor(obj).setBoolean(obj, z);
        }
    ​
        /**
         * Sets the value of a field as a {@code byte} on the specified object.
         * This method is equivalent to
         * {@code set(obj, bObj)},
         * where {@code bObj} is a {@code Byte} object and
         * {@code bObj.byteValue() == b}.
         *
         * @param obj the object whose field should be modified
         * @param b   the new value for the field of {@code obj}
         * being modified
         *
         * @exception IllegalAccessException    if this {@code Field} object
         *              is enforcing Java language access control and the underlying
         *              field is either inaccessible or final.
         * @exception IllegalArgumentException  if the specified object is not an
         *              instance of the class or interface declaring the underlying
         *              field (or a subclass or implementor thereof),
         *              or if an unwrapping conversion fails.
         * @exception NullPointerException      if the specified object is null
         *              and the field is an instance field.
         * @exception ExceptionInInitializerError if the initialization provoked
         *              by this method fails.
         * @see       Field#set
         */
        @CallerSensitive
        public void setByte(Object obj, byte b)
            throws IllegalArgumentException, IllegalAccessException
        {
            if (!override) {
                if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) {
                    Class<?> caller = Reflection.getCallerClass();
                    checkAccess(caller, clazz, obj, modifiers);
                }
            }
            getFieldAccessor(obj).setByte(obj, b);
        }
    ​
        /**
         * Sets the value of a field as a {@code char} on the specified object.
         * This method is equivalent to
         * {@code set(obj, cObj)},
         * where {@code cObj} is a {@code Character} object and
         * {@code cObj.charValue() == c}.
         *
         * @param obj the object whose field should be modified
         * @param c   the new value for the field of {@code obj}
         * being modified
         *
         * @exception IllegalAccessException    if this {@code Field} object
         *              is enforcing Java language access control and the underlying
         *              field is either inaccessible or final.
         * @exception IllegalArgumentException  if the specified object is not an
         *              instance of the class or interface declaring the underlying
         *              field (or a subclass or implementor thereof),
         *              or if an unwrapping conversion fails.
         * @exception NullPointerException      if the specified object is null
         *              and the field is an instance field.
         * @exception ExceptionInInitializerError if the initialization provoked
         *              by this method fails.
         * @see       Field#set
         */
        @CallerSensitive
        public void setChar(Object obj, char c)
            throws IllegalArgumentException, IllegalAccessException
        {
            if (!override) {
                if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) {
                    Class<?> caller = Reflection.getCallerClass();
                    checkAccess(caller, clazz, obj, modifiers);
                }
            }
            getFieldAccessor(obj).setChar(obj, c);
        }
    ​
        /**
         * Sets the value of a field as a {@code short} on the specified object.
         * This method is equivalent to
         * {@code set(obj, sObj)},
         * where {@code sObj} is a {@code Short} object and
         * {@code sObj.shortValue() == s}.
         *
         * @param obj the object whose field should be modified
         * @param s   the new value for the field of {@code obj}
         * being modified
         *
         * @exception IllegalAccessException    if this {@code Field} object
         *              is enforcing Java language access control and the underlying
         *              field is either inaccessible or final.
         * @exception IllegalArgumentException  if the specified object is not an
         *              instance of the class or interface declaring the underlying
         *              field (or a subclass or implementor thereof),
         *              or if an unwrapping conversion fails.
         * @exception NullPointerException      if the specified object is null
         *              and the field is an instance field.
         * @exception ExceptionInInitializerError if the initialization provoked
         *              by this method fails.
         * @see       Field#set
         */
        @CallerSensitive
        public void setShort(Object obj, short s)
            throws IllegalArgumentException, IllegalAccessException
        {
            if (!override) {
                if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) {
                    Class<?> caller = Reflection.getCallerClass();
                    checkAccess(caller, clazz, obj, modifiers);
                }
            }
            getFieldAccessor(obj).setShort(obj, s);
        }
    ​
        /**
         * Sets the value of a field as an {@code int} on the specified object.
         * This method is equivalent to
         * {@code set(obj, iObj)},
         * where {@code iObj} is a {@code Integer} object and
         * {@code iObj.intValue() == i}.
         *
         * @param obj the object whose field should be modified
         * @param i   the new value for the field of {@code obj}
         * being modified
         *
         * @exception IllegalAccessException    if this {@code Field} object
         *              is enforcing Java language access control and the underlying
         *              field is either inaccessible or final.
         * @exception IllegalArgumentException  if the specified object is not an
         *              instance of the class or interface declaring the underlying
         *              field (or a subclass or implementor thereof),
         *              or if an unwrapping conversion fails.
         * @exception NullPointerException      if the specified object is null
         *              and the field is an instance field.
         * @exception ExceptionInInitializerError if the initialization provoked
         *              by this method fails.
         * @see       Field#set
         */
        @CallerSensitive
        public void setInt(Object obj, int i)
            throws IllegalArgumentException, IllegalAccessException
        {
            if (!override) {
                if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) {
                    Class<?> caller = Reflection.getCallerClass();
                    checkAccess(caller, clazz, obj, modifiers);
                }
            }
            getFieldAccessor(obj).setInt(obj, i);
        }
    ​
        /**
         * Sets the value of a field as a {@code long} on the specified object.
         * This method is equivalent to
         * {@code set(obj, lObj)},
         * where {@code lObj} is a {@code Long} object and
         * {@code lObj.longValue() == l}.
         *
         * @param obj the object whose field should be modified
         * @param l   the new value for the field of {@code obj}
         * being modified
         *
         * @exception IllegalAccessException    if this {@code Field} object
         *              is enforcing Java language access control and the underlying
         *              field is either inaccessible or final.
         * @exception IllegalArgumentException  if the specified object is not an
         *              instance of the class or interface declaring the underlying
         *              field (or a subclass or implementor thereof),
         *              or if an unwrapping conversion fails.
         * @exception NullPointerException      if the specified object is null
         *              and the field is an instance field.
         * @exception ExceptionInInitializerError if the initialization provoked
         *              by this method fails.
         * @see       Field#set
         */
        @CallerSensitive
        public void setLong(Object obj, long l)
            throws IllegalArgumentException, IllegalAccessException
        {
            if (!override) {
                if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) {
                    Class<?> caller = Reflection.getCallerClass();
                    checkAccess(caller, clazz, obj, modifiers);
                }
            }
            getFieldAccessor(obj).setLong(obj, l);
        }
    ​
        /**
         * Sets the value of a field as a {@code float} on the specified object.
         * This method is equivalent to
         * {@code set(obj, fObj)},
         * where {@code fObj} is a {@code Float} object and
         * {@code fObj.floatValue() == f}.
         *
         * @param obj the object whose field should be modified
         * @param f   the new value for the field of {@code obj}
         * being modified
         *
         * @exception IllegalAccessException    if this {@code Field} object
         *              is enforcing Java language access control and the underlying
         *              field is either inaccessible or final.
         * @exception IllegalArgumentException  if the specified object is not an
         *              instance of the class or interface declaring the underlying
         *              field (or a subclass or implementor thereof),
         *              or if an unwrapping conversion fails.
         * @exception NullPointerException      if the specified object is null
         *              and the field is an instance field.
         * @exception ExceptionInInitializerError if the initialization provoked
         *              by this method fails.
         * @see       Field#set
         */
        @CallerSensitive
        public void setFloat(Object obj, float f)
            throws IllegalArgumentException, IllegalAccessException
        {
            if (!override) {
                if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) {
                    Class<?> caller = Reflection.getCallerClass();
                    checkAccess(caller, clazz, obj, modifiers);
                }
            }
            getFieldAccessor(obj).setFloat(obj, f);
        }
    ​
        /**
         * Sets the value of a field as a {@code double} on the specified object.
         * This method is equivalent to
         * {@code set(obj, dObj)},
         * where {@code dObj} is a {@code Double} object and
         * {@code dObj.doubleValue() == d}.
         *
         * @param obj the object whose field should be modified
         * @param d   the new value for the field of {@code obj}
         * being modified
         *
         * @exception IllegalAccessException    if this {@code Field} object
         *              is enforcing Java language access control and the underlying
         *              field is either inaccessible or final.
         * @exception IllegalArgumentException  if the specified object is not an
         *              instance of the class or interface declaring the underlying
         *              field (or a subclass or implementor thereof),
         *              or if an unwrapping conversion fails.
         * @exception NullPointerException      if the specified object is null
         *              and the field is an instance field.
         * @exception ExceptionInInitializerError if the initialization provoked
         *              by this method fails.
         * @see       Field#set
         */
        @CallerSensitive
        public void setDouble(Object obj, double d)
            throws IllegalArgumentException, IllegalAccessException
        {
            if (!override) {
                if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) {
                    Class<?> caller = Reflection.getCallerClass();
                    checkAccess(caller, clazz, obj, modifiers);
                }
            }
            getFieldAccessor(obj).setDouble(obj, d);
        }
    ​
        // security check is done before calling this method
        private FieldAccessor getFieldAccessor(Object obj)
            throws IllegalAccessException
        {
            boolean ov = override;
            FieldAccessor a = (ov) ? overrideFieldAccessor : fieldAccessor;
            return (a != null) ? a : acquireFieldAccessor(ov);
        }
    ​
        // NOTE that there is no synchronization used here. It is correct
        // (though not efficient) to generate more than one FieldAccessor
        // for a given Field. However, avoiding synchronization will
        // probably make the implementation more scalable.
        private FieldAccessor acquireFieldAccessor(boolean overrideFinalCheck) {
            // First check to see if one has been created yet, and take it
            // if so
            FieldAccessor tmp = null;
            if (root != null) tmp = root.getFieldAccessor(overrideFinalCheck);
            if (tmp != null) {
                if (overrideFinalCheck)
                    overrideFieldAccessor = tmp;
                else
                    fieldAccessor = tmp;
            } else {
                // Otherwise fabricate one and propagate it up to the root
                tmp = reflectionFactory.newFieldAccessor(this, overrideFinalCheck);
                setFieldAccessor(tmp, overrideFinalCheck);
            }
    ​
            return tmp;
        }
    ​
        // Returns FieldAccessor for this Field object, not looking up
        // the chain to the root
        private FieldAccessor getFieldAccessor(boolean overrideFinalCheck) {
            return (overrideFinalCheck)? overrideFieldAccessor : fieldAccessor;
        }
    ​
        // Sets the FieldAccessor for this Field object and
        // (recursively) its root
        private void setFieldAccessor(FieldAccessor accessor, boolean overrideFinalCheck) {
            if (overrideFinalCheck)
                overrideFieldAccessor = accessor;
            else
                fieldAccessor = accessor;
            // Propagate up
            if (root != null) {
                root.setFieldAccessor(accessor, overrideFinalCheck);
            }
        }
    ​
        /**
         * @throws NullPointerException {@inheritDoc}
         * @since 1.5
         */
        public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
            Objects.requireNonNull(annotationClass);
            return annotationClass.cast(declaredAnnotations().get(annotationClass));
        }
    ​
        /**
         * {@inheritDoc}
         * @throws NullPointerException {@inheritDoc}
         * @since 1.8
         */
        @Override
        public <T extends Annotation> T[] getAnnotationsByType(Class<T> annotationClass) {
            Objects.requireNonNull(annotationClass);
    ​
            return AnnotationSupport.getDirectlyAndIndirectlyPresent(declaredAnnotations(), annotationClass);
        }
    ​
        /**
         * {@inheritDoc}
         */
        public Annotation[] getDeclaredAnnotations()  {
            return AnnotationParser.toArray(declaredAnnotations());
        }
    ​
        private transient volatile Map<Class<? extends Annotation>, Annotation> declaredAnnotations;
    ​
        private Map<Class<? extends Annotation>, Annotation> declaredAnnotations() {
            Map<Class<? extends Annotation>, Annotation> declAnnos;
            if ((declAnnos = declaredAnnotations) == null) {
                synchronized (this) {
                    if ((declAnnos = declaredAnnotations) == null) {
                        Field root = this.root;
                        if (root != null) {
                            declAnnos = root.declaredAnnotations();
                        } else {
                            declAnnos = AnnotationParser.parseAnnotations(
                                    annotations,
                                    sun.misc.SharedSecrets.getJavaLangAccess()
                                            .getConstantPool(getDeclaringClass()),
                                    getDeclaringClass());
                        }
                        declaredAnnotations = declAnnos;
                    }
                }
            }
            return declAnnos;
        }
    ​
        private native byte[] getTypeAnnotationBytes0();
    ​
        /**
         * Returns an AnnotatedType object that represents the use of a type to specify
         * the declared type of the field represented by this Field.
         * @return an object representing the declared type of the field
         * represented by this Field
         *
         * @since 1.8
         */
        public AnnotatedType getAnnotatedType() {
            return TypeAnnotationParser.buildAnnotatedType(getTypeAnnotationBytes0(),
                                                           sun.misc.SharedSecrets.getJavaLangAccess().
                                                               getConstantPool(getDeclaringClass()),
                                                           this,
                                                           getDeclaringClass(),
                                                           getGenericType(),
                                                           TypeAnnotation.TypeAnnotationTarget.FIELD);
    }
    }
    ​
    java.lang.reflect.Field

    3.5 java.lang.reflect.Method

    java.lang.reflect.Method类提供有关类或接口上单个方法的信息和访问权限。反映的方法可以是类方法或实例方法(包括抽象方法)。 当匹配实际参数以使用底层方法的形式参数调用时,方法允许扩展转换,但如果发生缩小转换,则会引发IllegalArgumentException异常。

    /*
     * Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved.
     * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     *
     * This code is free software; you can redistribute it and/or modify it
     * under the terms of the GNU General Public License version 2 only, as
     * published by the Free Software Foundation.  Oracle designates this
     * particular file as subject to the "Classpath" exception as provided
     * by Oracle in the LICENSE file that accompanied this code.
     *
     * This code is distributed in the hope that it will be useful, but WITHOUT
     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
     * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
     * version 2 for more details (a copy is included in the LICENSE file that
     * accompanied this code).
     *
     * You should have received a copy of the GNU General Public License version
     * 2 along with this work; if not, write to the Free Software Foundation,
     * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
     *
     * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
     * or visit www.oracle.com if you need additional information or have any
     * questions.
     */package java.lang.reflect;
    ​
    import sun.reflect.CallerSensitive;
    import sun.reflect.MethodAccessor;
    import sun.reflect.Reflection;
    import sun.reflect.generics.repository.MethodRepository;
    import sun.reflect.generics.factory.CoreReflectionFactory;
    import sun.reflect.generics.factory.GenericsFactory;
    import sun.reflect.generics.scope.MethodScope;
    import sun.reflect.annotation.AnnotationType;
    import sun.reflect.annotation.AnnotationParser;
    import java.lang.annotation.Annotation;
    import java.lang.annotation.AnnotationFormatError;
    import java.nio.ByteBuffer;
    ​
    /**
     * A {@code Method} provides information about, and access to, a single method
     * on a class or interface.  The reflected method may be a class method
     * or an instance method (including an abstract method).
     *
     * <p>A {@code Method} permits widening conversions to occur when matching the
     * actual parameters to invoke with the underlying method's formal
     * parameters, but it throws an {@code IllegalArgumentException} if a
     * narrowing conversion would occur.
     *
     * @see Member
     * @see java.lang.Class
     * @see java.lang.Class#getMethods()
     * @see java.lang.Class#getMethod(String, Class[])
     * @see java.lang.Class#getDeclaredMethods()
     * @see java.lang.Class#getDeclaredMethod(String, Class[])
     *
     * @author Kenneth Russell
     * @author Nakul Saraiya
     */
    public final class Method extends Executable {
        private Class<?>            clazz;
        private int                 slot;
        // This is guaranteed to be interned by the VM in the 1.4
        // reflection implementation
        private String              name;
        private Class<?>            returnType;
        private Class<?>[]          parameterTypes;
        private Class<?>[]          exceptionTypes;
        private int                 modifiers;
        // Generics and annotations support
        private transient String              signature;
        // generic info repository; lazily initialized
        private transient MethodRepository genericInfo;
        private byte[]              annotations;
        private byte[]              parameterAnnotations;
        private byte[]              annotationDefault;
        private volatile MethodAccessor methodAccessor;
        // For sharing of MethodAccessors. This branching structure is
        // currently only two levels deep (i.e., one root Method and
        // potentially many Method objects pointing to it.)
        //
        // If this branching structure would ever contain cycles, deadlocks can
        // occur in annotation code.
        private Method              root;
    ​
        // Generics infrastructure
        private String getGenericSignature() {return signature;}
    ​
        // Accessor for factory
        private GenericsFactory getFactory() {
            // create scope and factory
            return CoreReflectionFactory.make(this, MethodScope.make(this));
        }
    ​
        // Accessor for generic info repository
        @Override
        MethodRepository getGenericInfo() {
            // lazily initialize repository if necessary
            if (genericInfo == null) {
                // create and cache generic info repository
                genericInfo = MethodRepository.make(getGenericSignature(),
                                                    getFactory());
            }
            return genericInfo; //return cached repository
        }
    ​
        /**
         * Package-private constructor used by ReflectAccess to enable
         * instantiation of these objects in Java code from the java.lang
         * package via sun.reflect.LangReflectAccess.
         */
        Method(Class<?> declaringClass,
               String name,
               Class<?>[] parameterTypes,
               Class<?> returnType,
               Class<?>[] checkedExceptions,
               int modifiers,
               int slot,
               String signature,
               byte[] annotations,
               byte[] parameterAnnotations,
               byte[] annotationDefault) {
            this.clazz = declaringClass;
            this.name = name;
            this.parameterTypes = parameterTypes;
            this.returnType = returnType;
            this.exceptionTypes = checkedExceptions;
            this.modifiers = modifiers;
            this.slot = slot;
            this.signature = signature;
            this.annotations = annotations;
            this.parameterAnnotations = parameterAnnotations;
            this.annotationDefault = annotationDefault;
        }
    ​
        /**
         * Package-private routine (exposed to java.lang.Class via
         * ReflectAccess) which returns a copy of this Method. The copy's
         * "root" field points to this Method.
         */
        Method copy() {
            // This routine enables sharing of MethodAccessor objects
            // among Method objects which refer to the same underlying
            // method in the VM. (All of this contortion is only necessary
            // because of the "accessibility" bit in AccessibleObject,
            // which implicitly requires that new java.lang.reflect
            // objects be fabricated for each reflective call on Class
            // objects.)
            if (this.root != null)
                throw new IllegalArgumentException("Can not copy a non-root Method");
    ​
            Method res = new Method(clazz, name, parameterTypes, returnType,
                                    exceptionTypes, modifiers, slot, signature,
                                    annotations, parameterAnnotations, annotationDefault);
            res.root = this;
            // Might as well eagerly propagate this if already present
            res.methodAccessor = methodAccessor;
            return res;
        }
    ​
        /**
         * Used by Excecutable for annotation sharing.
         */
        @Override
        Executable getRoot() {
            return root;
        }
    ​
        @Override
        boolean hasGenericInformation() {
            return (getGenericSignature() != null);
        }
    ​
        @Override
        byte[] getAnnotationBytes() {
            return annotations;
        }
    ​
        /**
         * {@inheritDoc}
         */
        @Override
        public Class<?> getDeclaringClass() {
            return clazz;
        }
    ​
        /**
         * Returns the name of the method represented by this {@code Method}
         * object, as a {@code String}.
         */
        @Override
        public String getName() {
            return name;
        }
    ​
        /**
         * {@inheritDoc}
         */
        @Override
        public int getModifiers() {
            return modifiers;
        }
    ​
        /**
         * {@inheritDoc}
         * @throws GenericSignatureFormatError {@inheritDoc}
         * @since 1.5
         */
        @Override
        @SuppressWarnings({"rawtypes", "unchecked"})
        public TypeVariable<Method>[] getTypeParameters() {
            if (getGenericSignature() != null)
                return (TypeVariable<Method>[])getGenericInfo().getTypeParameters();
            else
                return (TypeVariable<Method>[])new TypeVariable[0];
        }
    ​
        /**
         * Returns a {@code Class} object that represents the formal return type
         * of the method represented by this {@code Method} object.
         *
         * @return the return type for the method this object represents
         */
        public Class<?> getReturnType() {
            return returnType;
        }
    ​
        /**
         * Returns a {@code Type} object that represents the formal return
         * type of the method represented by this {@code Method} object.
         *
         * <p>If the return type is a parameterized type,
         * the {@code Type} object returned must accurately reflect
         * the actual type parameters used in the source code.
         *
         * <p>If the return type is a type variable or a parameterized type, it
         * is created. Otherwise, it is resolved.
         *
         * @return  a {@code Type} object that represents the formal return
         *     type of the underlying  method
         * @throws GenericSignatureFormatError
         *     if the generic method signature does not conform to the format
         *     specified in
         *     <cite>The Java&trade; Virtual Machine Specification</cite>
         * @throws TypeNotPresentException if the underlying method's
         *     return type refers to a non-existent type declaration
         * @throws MalformedParameterizedTypeException if the
         *     underlying method's return typed refers to a parameterized
         *     type that cannot be instantiated for any reason
         * @since 1.5
         */
        public Type getGenericReturnType() {
          if (getGenericSignature() != null) {
            return getGenericInfo().getReturnType();
          } else { return getReturnType();}
        }
    ​
        /**
         * {@inheritDoc}
         */
        @Override
        public Class<?>[] getParameterTypes() {
            return parameterTypes.clone();
        }
    ​
        /**
         * {@inheritDoc}
         * @since 1.8
         */
        public int getParameterCount() { return parameterTypes.length; }
    ​
    ​
        /**
         * {@inheritDoc}
         * @throws GenericSignatureFormatError {@inheritDoc}
         * @throws TypeNotPresentException {@inheritDoc}
         * @throws MalformedParameterizedTypeException {@inheritDoc}
         * @since 1.5
         */
        @Override
        public Type[] getGenericParameterTypes() {
            return super.getGenericParameterTypes();
        }
    ​
        /**
         * {@inheritDoc}
         */
        @Override
        public Class<?>[] getExceptionTypes() {
            return exceptionTypes.clone();
        }
    ​
        /**
         * {@inheritDoc}
         * @throws GenericSignatureFormatError {@inheritDoc}
         * @throws TypeNotPresentException {@inheritDoc}
         * @throws MalformedParameterizedTypeException {@inheritDoc}
         * @since 1.5
         */
        @Override
        public Type[] getGenericExceptionTypes() {
            return super.getGenericExceptionTypes();
        }
    ​
        /**
         * Compares this {@code Method} against the specified object.  Returns
         * true if the objects are the same.  Two {@code Methods} are the same if
         * they were declared by the same class and have the same name
         * and formal parameter types and return type.
         */
        public boolean equals(Object obj) {
            if (obj != null && obj instanceof Method) {
                Method other = (Method)obj;
                if ((getDeclaringClass() == other.getDeclaringClass())
                    && (getName() == other.getName())) {
                    if (!returnType.equals(other.getReturnType()))
                        return false;
                    return equalParamTypes(parameterTypes, other.parameterTypes);
                }
            }
            return false;
        }
    ​
        /**
         * Returns a hashcode for this {@code Method}.  The hashcode is computed
         * as the exclusive-or of the hashcodes for the underlying
         * method's declaring class name and the method's name.
         */
        public int hashCode() {
            return getDeclaringClass().getName().hashCode() ^ getName().hashCode();
        }
    ​
        /**
         * Returns a string describing this {@code Method}.  The string is
         * formatted as the method access modifiers, if any, followed by
         * the method return type, followed by a space, followed by the
         * class declaring the method, followed by a period, followed by
         * the method name, followed by a parenthesized, comma-separated
         * list of the method's formal parameter types. If the method
         * throws checked exceptions, the parameter list is followed by a
         * space, followed by the word throws followed by a
         * comma-separated list of the thrown exception types.
         * For example:
         * <pre>
         *    public boolean java.lang.Object.equals(java.lang.Object)
         * </pre>
         *
         * <p>The access modifiers are placed in canonical order as
         * specified by "The Java Language Specification".  This is
         * {@code public}, {@code protected} or {@code private} first,
         * and then other modifiers in the following order:
         * {@code abstract}, {@code default}, {@code static}, {@code final},
         * {@code synchronized}, {@code native}, {@code strictfp}.
         *
         * @return a string describing this {@code Method}
         *
         * @jls 8.4.3 Method Modifiers
         */
        public String toString() {
            return sharedToString(Modifier.methodModifiers(),
                                  isDefault(),
                                  parameterTypes,
                                  exceptionTypes);
        }
    ​
        @Override
        void specificToStringHeader(StringBuilder sb) {
            sb.append(getReturnType().getTypeName()).append(' ');
            sb.append(getDeclaringClass().getTypeName()).append('.');
            sb.append(getName());
        }
    ​
        /**
         * Returns a string describing this {@code Method}, including
         * type parameters.  The string is formatted as the method access
         * modifiers, if any, followed by an angle-bracketed
         * comma-separated list of the method's type parameters, if any,
         * followed by the method's generic return type, followed by a
         * space, followed by the class declaring the method, followed by
         * a period, followed by the method name, followed by a
         * parenthesized, comma-separated list of the method's generic
         * formal parameter types.
         *
         * If this method was declared to take a variable number of
         * arguments, instead of denoting the last parameter as
         * "<tt><i>Type</i>[]</tt>", it is denoted as
         * "<tt><i>Type</i>...</tt>".
         *
         * A space is used to separate access modifiers from one another
         * and from the type parameters or return type.  If there are no
         * type parameters, the type parameter list is elided; if the type
         * parameter list is present, a space separates the list from the
         * class name.  If the method is declared to throw exceptions, the
         * parameter list is followed by a space, followed by the word
         * throws followed by a comma-separated list of the generic thrown
         * exception types.
         *
         * <p>The access modifiers are placed in canonical order as
         * specified by "The Java Language Specification".  This is
         * {@code public}, {@code protected} or {@code private} first,
         * and then other modifiers in the following order:
         * {@code abstract}, {@code default}, {@code static}, {@code final},
         * {@code synchronized}, {@code native}, {@code strictfp}.
         *
         * @return a string describing this {@code Method},
         * include type parameters
         *
         * @since 1.5
         *
         * @jls 8.4.3 Method Modifiers
         */
        @Override
        public String toGenericString() {
            return sharedToGenericString(Modifier.methodModifiers(), isDefault());
        }
    ​
        @Override
        void specificToGenericStringHeader(StringBuilder sb) {
            Type genRetType = getGenericReturnType();
            sb.append(genRetType.getTypeName()).append(' ');
            sb.append(getDeclaringClass().getTypeName()).append('.');
            sb.append(getName());
        }
    ​
        /**
         * Invokes the underlying method represented by this {@code Method}
         * object, on the specified object with the specified parameters.
         * Individual parameters are automatically unwrapped to match
         * primitive formal parameters, and both primitive and reference
         * parameters are subject to method invocation conversions as
         * necessary.
         *
         * <p>If the underlying method is static, then the specified {@code obj}
         * argument is ignored. It may be null.
         *
         * <p>If the number of formal parameters required by the underlying method is
         * 0, the supplied {@code args} array may be of length 0 or null.
         *
         * <p>If the underlying method is an instance method, it is invoked
         * using dynamic method lookup as documented in The Java Language
         * Specification, Second Edition, section 15.12.4.4; in particular,
         * overriding based on the runtime type of the target object will occur.
         *
         * <p>If the underlying method is static, the class that declared
         * the method is initialized if it has not already been initialized.
         *
         * <p>If the method completes normally, the value it returns is
         * returned to the caller of invoke; if the value has a primitive
         * type, it is first appropriately wrapped in an object. However,
         * if the value has the type of an array of a primitive type, the
         * elements of the array are <i>not</i> wrapped in objects; in
         * other words, an array of primitive type is returned.  If the
         * underlying method return type is void, the invocation returns
         * null.
         *
         * @param obj  the object the underlying method is invoked from
         * @param args the arguments used for the method call
         * @return the result of dispatching the method represented by
         * this object on {@code obj} with parameters
         * {@code args}
         *
         * @exception IllegalAccessException    if this {@code Method} object
         *              is enforcing Java language access control and the underlying
         *              method is inaccessible.
         * @exception IllegalArgumentException  if the method is an
         *              instance method and the specified object argument
         *              is not an instance of the class or interface
         *              declaring the underlying method (or of a subclass
         *              or implementor thereof); if the number of actual
         *              and formal parameters differ; if an unwrapping
         *              conversion for primitive arguments fails; or if,
         *              after possible unwrapping, a parameter value
         *              cannot be converted to the corresponding formal
         *              parameter type by a method invocation conversion.
         * @exception InvocationTargetException if the underlying method
         *              throws an exception.
         * @exception NullPointerException      if the specified object is null
         *              and the method is an instance method.
         * @exception ExceptionInInitializerError if the initialization
         * provoked by this method fails.
         */
        @CallerSensitive
        public Object invoke(Object obj, Object... args)
            throws IllegalAccessException, IllegalArgumentException,
               InvocationTargetException
        {
            if (!override) {
                if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) {
                    Class<?> caller = Reflection.getCallerClass();
                    checkAccess(caller, clazz, obj, modifiers);
                }
            }
            MethodAccessor ma = methodAccessor;             // read volatile
            if (ma == null) {
                ma = acquireMethodAccessor();
            }
            return ma.invoke(obj, args);
        }
    ​
        /**
         * Returns {@code true} if this method is a bridge
         * method; returns {@code false} otherwise.
         *
         * @return true if and only if this method is a bridge
         * method as defined by the Java Language Specification.
         * @since 1.5
         */
        public boolean isBridge() {
            return (getModifiers() & Modifier.BRIDGE) != 0;
        }
    ​
        /**
         * {@inheritDoc}
         * @since 1.5
         */
        @Override
        public boolean isVarArgs() {
            return super.isVarArgs();
        }
    ​
        /**
         * {@inheritDoc}
         * @jls 13.1 The Form of a Binary
         * @since 1.5
         */
        @Override
        public boolean isSynthetic() {
            return super.isSynthetic();
        }
    ​
        /**
         * Returns {@code true} if this method is a default
         * method; returns {@code false} otherwise.
         *
         * A default method is a public non-abstract instance method, that
         * is, a non-static method with a body, declared in an interface
         * type.
         *
         * @return true if and only if this method is a default
         * method as defined by the Java Language Specification.
         * @since 1.8
         */
        public boolean isDefault() {
            // Default methods are public non-abstract instance methods
            // declared in an interface.
            return ((getModifiers() & (Modifier.ABSTRACT | Modifier.PUBLIC | Modifier.STATIC)) ==
                    Modifier.PUBLIC) && getDeclaringClass().isInterface();
        }
    ​
        // NOTE that there is no synchronization used here. It is correct
        // (though not efficient) to generate more than one MethodAccessor
        // for a given Method. However, avoiding synchronization will
        // probably make the implementation more scalable.
        private MethodAccessor acquireMethodAccessor() {
            // First check to see if one has been created yet, and take it
            // if so
            MethodAccessor tmp = null;
            if (root != null) tmp = root.getMethodAccessor();
            if (tmp != null) {
                methodAccessor = tmp;
            } else {
                // Otherwise fabricate one and propagate it up to the root
                tmp = reflectionFactory.newMethodAccessor(this);
                setMethodAccessor(tmp);
            }
    ​
            return tmp;
        }
    ​
        // Returns MethodAccessor for this Method object, not looking up
        // the chain to the root
        MethodAccessor getMethodAccessor() {
            return methodAccessor;
        }
    ​
        // Sets the MethodAccessor for this Method object and
        // (recursively) its root
        void setMethodAccessor(MethodAccessor accessor) {
            methodAccessor = accessor;
            // Propagate up
            if (root != null) {
                root.setMethodAccessor(accessor);
            }
        }
    ​
        /**
         * Returns the default value for the annotation member represented by
         * this {@code Method} instance.  If the member is of a primitive type,
         * an instance of the corresponding wrapper type is returned. Returns
         * null if no default is associated with the member, or if the method
         * instance does not represent a declared member of an annotation type.
         *
         * @return the default value for the annotation member represented
         *     by this {@code Method} instance.
         * @throws TypeNotPresentException if the annotation is of type
         *     {@link Class} and no definition can be found for the
         *     default class value.
         * @since  1.5
         */
        public Object getDefaultValue() {
            if  (annotationDefault == null)
                return null;
            Class<?> memberType = AnnotationType.invocationHandlerReturnType(
                getReturnType());
            Object result = AnnotationParser.parseMemberValue(
                memberType, ByteBuffer.wrap(annotationDefault),
                sun.misc.SharedSecrets.getJavaLangAccess().
                    getConstantPool(getDeclaringClass()),
                getDeclaringClass());
            if (result instanceof sun.reflect.annotation.ExceptionProxy)
                throw new AnnotationFormatError("Invalid default: " + this);
            return result;
        }
    ​
        /**
         * {@inheritDoc}
         * @throws NullPointerException  {@inheritDoc}
         * @since 1.5
         */
        public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
            return super.getAnnotation(annotationClass);
        }
    ​
        /**
         * {@inheritDoc}
         * @since 1.5
         */
        public Annotation[] getDeclaredAnnotations()  {
            return super.getDeclaredAnnotations();
        }
    ​
        /**
         * {@inheritDoc}
         * @since 1.5
         */
        @Override
        public Annotation[][] getParameterAnnotations() {
            return sharedGetParameterAnnotations(parameterTypes, parameterAnnotations);
        }
    ​
        /**
         * {@inheritDoc}
         * @since 1.8
         */
        @Override
        public AnnotatedType getAnnotatedReturnType() {
            return getAnnotatedReturnType0(getGenericReturnType());
        }
    ​
        @Override
        void handleParameterNumberMismatch(int resultLength, int numParameters) {
            throw new AnnotationFormatError("Parameter annotations don't match number of parameters");
        }
    }
    ​
    java.lang.reflect.Method

    3.6 java.lang.reflect.Modifier

    java.lang.reflect.Modifier类提供了用于解码类和成员访问修饰符的静态方法和常量。修饰符集合被表示为具有表示不同修饰符的不同位位置的整数。

    /*
     * Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved.
     * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     *
     * This code is free software; you can redistribute it and/or modify it
     * under the terms of the GNU General Public License version 2 only, as
     * published by the Free Software Foundation.  Oracle designates this
     * particular file as subject to the "Classpath" exception as provided
     * by Oracle in the LICENSE file that accompanied this code.
     *
     * This code is distributed in the hope that it will be useful, but WITHOUT
     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
     * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
     * version 2 for more details (a copy is included in the LICENSE file that
     * accompanied this code).
     *
     * You should have received a copy of the GNU General Public License version
     * 2 along with this work; if not, write to the Free Software Foundation,
     * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
     *
     * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
     * or visit www.oracle.com if you need additional information or have any
     * questions.
     */package java.lang.reflect;
    ​
    import java.security.AccessController;
    import sun.reflect.LangReflectAccess;
    import sun.reflect.ReflectionFactory;
    ​
    /**
     * The Modifier class provides {@code static} methods and
     * constants to decode class and member access modifiers.  The sets of
     * modifiers are represented as integers with distinct bit positions
     * representing different modifiers.  The values for the constants
     * representing the modifiers are taken from the tables in sections 4.1, 4.4, 4.5, and 4.7 of
     * <cite>The Java&trade; Virtual Machine Specification</cite>.
     *
     * @see Class#getModifiers()
     * @see Member#getModifiers()
     *
     * @author Nakul Saraiya
     * @author Kenneth Russell
     */
    public class Modifier {
    ​
        /*
         * Bootstrapping protocol between java.lang and java.lang.reflect
         *  packages
         */
        static {
            sun.reflect.ReflectionFactory factory =
                AccessController.doPrivileged(
                    new ReflectionFactory.GetReflectionFactoryAction());
            factory.setLangReflectAccess(new java.lang.reflect.ReflectAccess());
        }
    ​
        /**
         * Return {@code true} if the integer argument includes the
         * {@code public} modifier, {@code false} otherwise.
         *
         * @param   mod a set of modifiers
         * @return {@code true} if {@code mod} includes the
         * {@code public} modifier; {@code false} otherwise.
         */
        public static boolean isPublic(int mod) {
            return (mod & PUBLIC) != 0;
        }
    ​
        /**
         * Return {@code true} if the integer argument includes the
         * {@code private} modifier, {@code false} otherwise.
         *
         * @param   mod a set of modifiers
         * @return {@code true} if {@code mod} includes the
         * {@code private} modifier; {@code false} otherwise.
         */
        public static boolean isPrivate(int mod) {
            return (mod & PRIVATE) != 0;
        }
    ​
        /**
         * Return {@code true} if the integer argument includes the
         * {@code protected} modifier, {@code false} otherwise.
         *
         * @param   mod a set of modifiers
         * @return {@code true} if {@code mod} includes the
         * {@code protected} modifier; {@code false} otherwise.
         */
        public static boolean isProtected(int mod) {
            return (mod & PROTECTED) != 0;
        }
    ​
        /**
         * Return {@code true} if the integer argument includes the
         * {@code static} modifier, {@code false} otherwise.
         *
         * @param   mod a set of modifiers
         * @return {@code true} if {@code mod} includes the
         * {@code static} modifier; {@code false} otherwise.
         */
        public static boolean isStatic(int mod) {
            return (mod & STATIC) != 0;
        }
    ​
        /**
         * Return {@code true} if the integer argument includes the
         * {@code final} modifier, {@code false} otherwise.
         *
         * @param   mod a set of modifiers
         * @return {@code true} if {@code mod} includes the
         * {@code final} modifier; {@code false} otherwise.
         */
        public static boolean isFinal(int mod) {
            return (mod & FINAL) != 0;
        }
    ​
        /**
         * Return {@code true} if the integer argument includes the
         * {@code synchronized} modifier, {@code false} otherwise.
         *
         * @param   mod a set of modifiers
         * @return {@code true} if {@code mod} includes the
         * {@code synchronized} modifier; {@code false} otherwise.
         */
        public static boolean isSynchronized(int mod) {
            return (mod & SYNCHRONIZED) != 0;
        }
    ​
        /**
         * Return {@code true} if the integer argument includes the
         * {@code volatile} modifier, {@code false} otherwise.
         *
         * @param   mod a set of modifiers
         * @return {@code true} if {@code mod} includes the
         * {@code volatile} modifier; {@code false} otherwise.
         */
        public static boolean isVolatile(int mod) {
            return (mod & VOLATILE) != 0;
        }
    ​
        /**
         * Return {@code true} if the integer argument includes the
         * {@code transient} modifier, {@code false} otherwise.
         *
         * @param   mod a set of modifiers
         * @return {@code true} if {@code mod} includes the
         * {@code transient} modifier; {@code false} otherwise.
         */
        public static boolean isTransient(int mod) {
            return (mod & TRANSIENT) != 0;
        }
    ​
        /**
         * Return {@code true} if the integer argument includes the
         * {@code native} modifier, {@code false} otherwise.
         *
         * @param   mod a set of modifiers
         * @return {@code true} if {@code mod} includes the
         * {@code native} modifier; {@code false} otherwise.
         */
        public static boolean isNative(int mod) {
            return (mod & NATIVE) != 0;
        }
    ​
        /**
         * Return {@code true} if the integer argument includes the
         * {@code interface} modifier, {@code false} otherwise.
         *
         * @param   mod a set of modifiers
         * @return {@code true} if {@code mod} includes the
         * {@code interface} modifier; {@code false} otherwise.
         */
        public static boolean isInterface(int mod) {
            return (mod & INTERFACE) != 0;
        }
    ​
        /**
         * Return {@code true} if the integer argument includes the
         * {@code abstract} modifier, {@code false} otherwise.
         *
         * @param   mod a set of modifiers
         * @return {@code true} if {@code mod} includes the
         * {@code abstract} modifier; {@code false} otherwise.
         */
        public static boolean isAbstract(int mod) {
            return (mod & ABSTRACT) != 0;
        }
    ​
        /**
         * Return {@code true} if the integer argument includes the
         * {@code strictfp} modifier, {@code false} otherwise.
         *
         * @param   mod a set of modifiers
         * @return {@code true} if {@code mod} includes the
         * {@code strictfp} modifier; {@code false} otherwise.
         */
        public static boolean isStrict(int mod) {
            return (mod & STRICT) != 0;
        }
    ​
        /**
         * Return a string describing the access modifier flags in
         * the specified modifier. For example:
         * <blockquote><pre>
         *    public final synchronized strictfp
         * </pre></blockquote>
         * The modifier names are returned in an order consistent with the
         * suggested modifier orderings given in sections 8.1.1, 8.3.1, 8.4.3, 8.8.3, and 9.1.1 of
         * <cite>The Java&trade; Language Specification</cite>.
         * The full modifier ordering used by this method is:
         * <blockquote> {@code
         * public protected private abstract static final transient
         * volatile synchronized native strictfp
         * interface } </blockquote>
         * The {@code interface} modifier discussed in this class is
         * not a true modifier in the Java language and it appears after
         * all other modifiers listed by this method.  This method may
         * return a string of modifiers that are not valid modifiers of a
         * Java entity; in other words, no checking is done on the
         * possible validity of the combination of modifiers represented
         * by the input.
         *
         * Note that to perform such checking for a known kind of entity,
         * such as a constructor or method, first AND the argument of
         * {@code toString} with the appropriate mask from a method like
         * {@link #constructorModifiers} or {@link #methodModifiers}.
         *
         * @param   mod a set of modifiers
         * @return  a string representation of the set of modifiers
         * represented by {@code mod}
         */
        public static String toString(int mod) {
            StringBuilder sb = new StringBuilder();
            int len;
    ​
            if ((mod & PUBLIC) != 0)        sb.append("public ");
            if ((mod & PROTECTED) != 0)     sb.append("protected ");
            if ((mod & PRIVATE) != 0)       sb.append("private ");
    ​
            /* Canonical order */
            if ((mod & ABSTRACT) != 0)      sb.append("abstract ");
            if ((mod & STATIC) != 0)        sb.append("static ");
            if ((mod & FINAL) != 0)         sb.append("final ");
            if ((mod & TRANSIENT) != 0)     sb.append("transient ");
            if ((mod & VOLATILE) != 0)      sb.append("volatile ");
            if ((mod & SYNCHRONIZED) != 0)  sb.append("synchronized ");
            if ((mod & NATIVE) != 0)        sb.append("native ");
            if ((mod & STRICT) != 0)        sb.append("strictfp ");
            if ((mod & INTERFACE) != 0)     sb.append("interface ");
    ​
            if ((len = sb.length()) > 0)    /* trim trailing space */
                return sb.toString().substring(0, len-1);
            return "";
        }
    ​
        /*
         * Access modifier flag constants from tables 4.1, 4.4, 4.5, and 4.7 of
         * <cite>The Java&trade; Virtual Machine Specification</cite>
         *//**
         * The {@code int} value representing the {@code public}
         * modifier.
         */
        public static final int PUBLIC           = 0x00000001;
    ​
        /**
         * The {@code int} value representing the {@code private}
         * modifier.
         */
        public static final int PRIVATE          = 0x00000002;
    ​
        /**
         * The {@code int} value representing the {@code protected}
         * modifier.
         */
        public static final int PROTECTED        = 0x00000004;
    ​
        /**
         * The {@code int} value representing the {@code static}
         * modifier.
         */
        public static final int STATIC           = 0x00000008;
    ​
        /**
         * The {@code int} value representing the {@code final}
         * modifier.
         */
        public static final int FINAL            = 0x00000010;
    ​
        /**
         * The {@code int} value representing the {@code synchronized}
         * modifier.
         */
        public static final int SYNCHRONIZED     = 0x00000020;
    ​
        /**
         * The {@code int} value representing the {@code volatile}
         * modifier.
         */
        public static final int VOLATILE         = 0x00000040;
    ​
        /**
         * The {@code int} value representing the {@code transient}
         * modifier.
         */
        public static final int TRANSIENT        = 0x00000080;
    ​
        /**
         * The {@code int} value representing the {@code native}
         * modifier.
         */
        public static final int NATIVE           = 0x00000100;
    ​
        /**
         * The {@code int} value representing the {@code interface}
         * modifier.
         */
        public static final int INTERFACE        = 0x00000200;
    ​
        /**
         * The {@code int} value representing the {@code abstract}
         * modifier.
         */
        public static final int ABSTRACT         = 0x00000400;
    ​
        /**
         * The {@code int} value representing the {@code strictfp}
         * modifier.
         */
        public static final int STRICT           = 0x00000800;
    ​
        // Bits not (yet) exposed in the public API either because they
        // have different meanings for fields and methods and there is no
        // way to distinguish between the two in this class, or because
        // they are not Java programming language keywords
        static final int BRIDGE    = 0x00000040;
        static final int VARARGS   = 0x00000080;
        static final int SYNTHETIC = 0x00001000;
        static final int ANNOTATION  = 0x00002000;
        static final int ENUM      = 0x00004000;
        static final int MANDATED  = 0x00008000;
        static boolean isSynthetic(int mod) {
          return (mod & SYNTHETIC) != 0;
        }
    ​
        static boolean isMandated(int mod) {
          return (mod & MANDATED) != 0;
        }
    ​
        // Note on the FOO_MODIFIERS fields and fooModifiers() methods:
        // the sets of modifiers are not guaranteed to be constants
        // across time and Java SE releases. Therefore, it would not be
        // appropriate to expose an external interface to this information
        // that would allow the values to be treated as Java-level
        // constants since the values could be constant folded and updates
        // to the sets of modifiers missed. Thus, the fooModifiers()
        // methods return an unchanging values for a given release, but a
        // value that can potentially change over time.
    /**
         * The Java source modifiers that can be applied to a class.
         * @jls 8.1.1 Class Modifiers
         */
        private static final int CLASS_MODIFIERS =
            Modifier.PUBLIC         | Modifier.PROTECTED    | Modifier.PRIVATE |
            Modifier.ABSTRACT       | Modifier.STATIC       | Modifier.FINAL   |
            Modifier.STRICT;
    ​
        /**
         * The Java source modifiers that can be applied to an interface.
         * @jls 9.1.1 Interface Modifiers
         */
        private static final int INTERFACE_MODIFIERS =
            Modifier.PUBLIC         | Modifier.PROTECTED    | Modifier.PRIVATE |
            Modifier.ABSTRACT       | Modifier.STATIC       | Modifier.STRICT;
    ​
    ​
        /**
         * The Java source modifiers that can be applied to a constructor.
         * @jls 8.8.3 Constructor Modifiers
         */
        private static final int CONSTRUCTOR_MODIFIERS =
            Modifier.PUBLIC         | Modifier.PROTECTED    | Modifier.PRIVATE;
    ​
        /**
         * The Java source modifiers that can be applied to a method.
         * @jls8.4.3  Method Modifiers
         */
        private static final int METHOD_MODIFIERS =
            Modifier.PUBLIC         | Modifier.PROTECTED    | Modifier.PRIVATE |
            Modifier.ABSTRACT       | Modifier.STATIC       | Modifier.FINAL   |
            Modifier.SYNCHRONIZED   | Modifier.NATIVE       | Modifier.STRICT;
    ​
        /**
         * The Java source modifiers that can be applied to a field.
         * @jls 8.3.1  Field Modifiers
         */
        private static final int FIELD_MODIFIERS =
            Modifier.PUBLIC         | Modifier.PROTECTED    | Modifier.PRIVATE |
            Modifier.STATIC         | Modifier.FINAL        | Modifier.TRANSIENT |
            Modifier.VOLATILE;
    ​
        /**
         * The Java source modifiers that can be applied to a method or constructor parameter.
         * @jls 8.4.1 Formal Parameters
         */
        private static final int PARAMETER_MODIFIERS =
            Modifier.FINAL;
    ​
        /**
         *
         */
        static final int ACCESS_MODIFIERS =
            Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE;
    ​
        /**
         * Return an {@code int} value OR-ing together the source language
         * modifiers that can be applied to a class.
         * @return an {@code int} value OR-ing together the source language
         * modifiers that can be applied to a class.
         *
         * @jls 8.1.1 Class Modifiers
         * @since 1.7
         */
        public static int classModifiers() {
            return CLASS_MODIFIERS;
        }
    ​
        /**
         * Return an {@code int} value OR-ing together the source language
         * modifiers that can be applied to an interface.
         * @return an {@code int} value OR-ing together the source language
         * modifiers that can be applied to an interface.
         *
         * @jls 9.1.1 Interface Modifiers
         * @since 1.7
         */
        public static int interfaceModifiers() {
            return INTERFACE_MODIFIERS;
        }
    ​
        /**
         * Return an {@code int} value OR-ing together the source language
         * modifiers that can be applied to a constructor.
         * @return an {@code int} value OR-ing together the source language
         * modifiers that can be applied to a constructor.
         *
         * @jls 8.8.3 Constructor Modifiers
         * @since 1.7
         */
        public static int constructorModifiers() {
            return CONSTRUCTOR_MODIFIERS;
        }
    ​
        /**
         * Return an {@code int} value OR-ing together the source language
         * modifiers that can be applied to a method.
         * @return an {@code int} value OR-ing together the source language
         * modifiers that can be applied to a method.
         *
         * @jls 8.4.3 Method Modifiers
         * @since 1.7
         */
        public static int methodModifiers() {
            return METHOD_MODIFIERS;
        }
    ​
        /**
         * Return an {@code int} value OR-ing together the source language
         * modifiers that can be applied to a field.
         * @return an {@code int} value OR-ing together the source language
         * modifiers that can be applied to a field.
         *
         * @jls 8.3.1 Field Modifiers
         * @since 1.7
         */
        public static int fieldModifiers() {
            return FIELD_MODIFIERS;
        }
    ​
        /**
         * Return an {@code int} value OR-ing together the source language
         * modifiers that can be applied to a parameter.
         * @return an {@code int} value OR-ing together the source language
         * modifiers that can be applied to a parameter.
         *
         * @jls 8.4.1 Formal Parameters
         * @since 1.8
         */
        public static int parameterModifiers() {
            return PARAMETER_MODIFIERS;
        }
    }
    java.lang.reflect.Modifier
     

    3.7 java.lang.reflect.Proxy

    java.lang.reflect.Proxy类提供用于创建动态代理类和实例的静态方法,它也是由这些方法创建的所有动态代理类的超类。

    /*
     * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
     * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     *
     * This code is free software; you can redistribute it and/or modify it
     * under the terms of the GNU General Public License version 2 only, as
     * published by the Free Software Foundation.  Oracle designates this
     * particular file as subject to the "Classpath" exception as provided
     * by Oracle in the LICENSE file that accompanied this code.
     *
     * This code is distributed in the hope that it will be useful, but WITHOUT
     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
     * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
     * version 2 for more details (a copy is included in the LICENSE file that
     * accompanied this code).
     *
     * You should have received a copy of the GNU General Public License version
     * 2 along with this work; if not, write to the Free Software Foundation,
     * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
     *
     * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
     * or visit www.oracle.com if you need additional information or have any
     * questions.
     */package java.lang.reflect;
    ​
    import java.lang.ref.WeakReference;
    import java.security.AccessController;
    import java.security.PrivilegedAction;
    import java.util.Arrays;
    import java.util.IdentityHashMap;
    import java.util.Map;
    import java.util.Objects;
    import java.util.concurrent.atomic.AtomicLong;
    import java.util.function.BiFunction;
    import sun.misc.ProxyGenerator;
    import sun.misc.VM;
    import sun.reflect.CallerSensitive;
    import sun.reflect.Reflection;
    import sun.reflect.misc.ReflectUtil;
    import sun.security.util.SecurityConstants;
    ​
    /**
     * {@code Proxy} provides static methods for creating dynamic proxy
     * classes and instances, and it is also the superclass of all
     * dynamic proxy classes created by those methods.
     *
     * <p>To create a proxy for some interface {@code Foo}:
     * <pre>
     *     InvocationHandler handler = new MyInvocationHandler(...);
     *     Class&lt;?&gt; proxyClass = Proxy.getProxyClass(Foo.class.getClassLoader(), Foo.class);
     *     Foo f = (Foo) proxyClass.getConstructor(InvocationHandler.class).
     *                     newInstance(handler);
     * </pre>
     * or more simply:
     * <pre>
     *     Foo f = (Foo) Proxy.newProxyInstance(Foo.class.getClassLoader(),
     *                                          new Class&lt;?&gt;[] { Foo.class },
     *                                          handler);
     * </pre>
     *
     * <p>A <i>dynamic proxy class</i> (simply referred to as a <i>proxy
     * class</i> below) is a class that implements a list of interfaces
     * specified at runtime when the class is created, with behavior as
     * described below.
     *
     * A <i>proxy interface</i> is such an interface that is implemented
     * by a proxy class.
     *
     * A <i>proxy instance</i> is an instance of a proxy class.
     *
     * Each proxy instance has an associated <i>invocation handler</i>
     * object, which implements the interface {@link InvocationHandler}.
     * A method invocation on a proxy instance through one of its proxy
     * interfaces will be dispatched to the {@link InvocationHandler#invoke
     * invoke} method of the instance's invocation handler, passing the proxy
     * instance, a {@code java.lang.reflect.Method} object identifying
     * the method that was invoked, and an array of type {@code Object}
     * containing the arguments.  The invocation handler processes the
     * encoded method invocation as appropriate and the result that it
     * returns will be returned as the result of the method invocation on
     * the proxy instance.
     *
     * <p>A proxy class has the following properties:
     *
     * <ul>
     * <li>Proxy classes are <em>public, final, and not abstract</em> if
     * all proxy interfaces are public.</li>
     *
     * <li>Proxy classes are <em>non-public, final, and not abstract</em> if
     * any of the proxy interfaces is non-public.</li>
     *
     * <li>The unqualified name of a proxy class is unspecified.  The space
     * of class names that begin with the string {@code "$Proxy"}
     * should be, however, reserved for proxy classes.
     *
     * <li>A proxy class extends {@code java.lang.reflect.Proxy}.
     *
     * <li>A proxy class implements exactly the interfaces specified at its
     * creation, in the same order.
     *
     * <li>If a proxy class implements a non-public interface, then it will
     * be defined in the same package as that interface.  Otherwise, the
     * package of a proxy class is also unspecified.  Note that package
     * sealing will not prevent a proxy class from being successfully defined
     * in a particular package at runtime, and neither will classes already
     * defined by the same class loader and the same package with particular
     * signers.
     *
     * <li>Since a proxy class implements all of the interfaces specified at
     * its creation, invoking {@code getInterfaces} on its
     * {@code Class} object will return an array containing the same
     * list of interfaces (in the order specified at its creation), invoking
     * {@code getMethods} on its {@code Class} object will return
     * an array of {@code Method} objects that include all of the
     * methods in those interfaces, and invoking {@code getMethod} will
     * find methods in the proxy interfaces as would be expected.
     *
     * <li>The {@link Proxy#isProxyClass Proxy.isProxyClass} method will
     * return true if it is passed a proxy class-- a class returned by
     * {@code Proxy.getProxyClass} or the class of an object returned by
     * {@code Proxy.newProxyInstance}-- and false otherwise.
     *
     * <li>The {@code java.security.ProtectionDomain} of a proxy class
     * is the same as that of system classes loaded by the bootstrap class
     * loader, such as {@code java.lang.Object}, because the code for a
     * proxy class is generated by trusted system code.  This protection
     * domain will typically be granted
     * {@code java.security.AllPermission}.
     *
     * <li>Each proxy class has one public constructor that takes one argument,
     * an implementation of the interface {@link InvocationHandler}, to set
     * the invocation handler for a proxy instance.  Rather than having to use
     * the reflection API to access the public constructor, a proxy instance
     * can be also be created by calling the {@link Proxy#newProxyInstance
     * Proxy.newProxyInstance} method, which combines the actions of calling
     * {@link Proxy#getProxyClass Proxy.getProxyClass} with invoking the
     * constructor with an invocation handler.
     * </ul>
     *
     * <p>A proxy instance has the following properties:
     *
     * <ul>
     * <li>Given a proxy instance {@code proxy} and one of the
     * interfaces implemented by its proxy class {@code Foo}, the
     * following expression will return true:
     * <pre>
     *     {@code proxy instanceof Foo}
     * </pre>
     * and the following cast operation will succeed (rather than throwing
     * a {@code ClassCastException}):
     * <pre>
     *     {@code (Foo) proxy}
     * </pre>
     *
     * <li>Each proxy instance has an associated invocation handler, the one
     * that was passed to its constructor.  The static
     * {@link Proxy#getInvocationHandler Proxy.getInvocationHandler} method
     * will return the invocation handler associated with the proxy instance
     * passed as its argument.
     *
     * <li>An interface method invocation on a proxy instance will be
     * encoded and dispatched to the invocation handler's {@link
     * InvocationHandler#invoke invoke} method as described in the
     * documentation for that method.
     *
     * <li>An invocation of the {@code hashCode},
     * {@code equals}, or {@code toString} methods declared in
     * {@code java.lang.Object} on a proxy instance will be encoded and
     * dispatched to the invocation handler's {@code invoke} method in
     * the same manner as interface method invocations are encoded and
     * dispatched, as described above.  The declaring class of the
     * {@code Method} object passed to {@code invoke} will be
     * {@code java.lang.Object}.  Other public methods of a proxy
     * instance inherited from {@code java.lang.Object} are not
     * overridden by a proxy class, so invocations of those methods behave
     * like they do for instances of {@code java.lang.Object}.
     * </ul>
     *
     * <h3>Methods Duplicated in Multiple Proxy Interfaces</h3>
     *
     * <p>When two or more interfaces of a proxy class contain a method with
     * the same name and parameter signature, the order of the proxy class's
     * interfaces becomes significant.  When such a <i>duplicate method</i>
     * is invoked on a proxy instance, the {@code Method} object passed
     * to the invocation handler will not necessarily be the one whose
     * declaring class is assignable from the reference type of the interface
     * that the proxy's method was invoked through.  This limitation exists
     * because the corresponding method implementation in the generated proxy
     * class cannot determine which interface it was invoked through.
     * Therefore, when a duplicate method is invoked on a proxy instance,
     * the {@code Method} object for the method in the foremost interface
     * that contains the method (either directly or inherited through a
     * superinterface) in the proxy class's list of interfaces is passed to
     * the invocation handler's {@code invoke} method, regardless of the
     * reference type through which the method invocation occurred.
     *
     * <p>If a proxy interface contains a method with the same name and
     * parameter signature as the {@code hashCode}, {@code equals},
     * or {@code toString} methods of {@code java.lang.Object},
     * when such a method is invoked on a proxy instance, the
     * {@code Method} object passed to the invocation handler will have
     * {@code java.lang.Object} as its declaring class.  In other words,
     * the public, non-final methods of {@code java.lang.Object}
     * logically precede all of the proxy interfaces for the determination of
     * which {@code Method} object to pass to the invocation handler.
     *
     * <p>Note also that when a duplicate method is dispatched to an
     * invocation handler, the {@code invoke} method may only throw
     * checked exception types that are assignable to one of the exception
     * types in the {@code throws} clause of the method in <i>all</i> of
     * the proxy interfaces that it can be invoked through.  If the
     * {@code invoke} method throws a checked exception that is not
     * assignable to any of the exception types declared by the method in one
     * of the proxy interfaces that it can be invoked through, then an
     * unchecked {@code UndeclaredThrowableException} will be thrown by
     * the invocation on the proxy instance.  This restriction means that not
     * all of the exception types returned by invoking
     * {@code getExceptionTypes} on the {@code Method} object
     * passed to the {@code invoke} method can necessarily be thrown
     * successfully by the {@code invoke} method.
     *
     * @author      Peter Jones
     * @see         InvocationHandler
     * @since       1.3
     */
    public class Proxy implements java.io.Serializable {
    ​
        private static final long serialVersionUID = -2222568056686623797L;
    ​
        /** parameter types of a proxy class constructor */
        private static final Class<?>[] constructorParams =
            { InvocationHandler.class };
    ​
        /**
         * a cache of proxy classes
         */
        private static final WeakCache<ClassLoader, Class<?>[], Class<?>>
            proxyClassCache = new WeakCache<>(new KeyFactory(), new ProxyClassFactory());
    ​
        /**
         * the invocation handler for this proxy instance.
         * @serial
         */
        protected InvocationHandler h;
    ​
        /**
         * Prohibits instantiation.
         */
        private Proxy() {
        }
    ​
        /**
         * Constructs a new {@code Proxy} instance from a subclass
         * (typically, a dynamic proxy class) with the specified value
         * for its invocation handler.
         *
         * @param  h the invocation handler for this proxy instance
         *
         * @throws NullPointerException if the given invocation handler, {@code h},
         *         is {@code null}.
         */
        protected Proxy(InvocationHandler h) {
            Objects.requireNonNull(h);
            this.h = h;
        }
    ​
        /**
         * Returns the {@code java.lang.Class} object for a proxy class
         * given a class loader and an array of interfaces.  The proxy class
         * will be defined by the specified class loader and will implement
         * all of the supplied interfaces.  If any of the given interfaces
         * is non-public, the proxy class will be non-public. If a proxy class
         * for the same permutation of interfaces has already been defined by the
         * class loader, then the existing proxy class will be returned; otherwise,
         * a proxy class for those interfaces will be generated dynamically
         * and defined by the class loader.
         *
         * <p>There are several restrictions on the parameters that may be
         * passed to {@code Proxy.getProxyClass}:
         *
         * <ul>
         * <li>All of the {@code Class} objects in the
         * {@code interfaces} array must represent interfaces, not
         * classes or primitive types.
         *
         * <li>No two elements in the {@code interfaces} array may
         * refer to identical {@code Class} objects.
         *
         * <li>All of the interface types must be visible by name through the
         * specified class loader.  In other words, for class loader
         * {@code cl} and every interface {@code i}, the following
         * expression must be true:
         * <pre>
         *     Class.forName(i.getName(), false, cl) == i
         * </pre>
         *
         * <li>All non-public interfaces must be in the same package;
         * otherwise, it would not be possible for the proxy class to
         * implement all of the interfaces, regardless of what package it is
         * defined in.
         *
         * <li>For any set of member methods of the specified interfaces
         * that have the same signature:
         * <ul>
         * <li>If the return type of any of the methods is a primitive
         * type or void, then all of the methods must have that same
         * return type.
         * <li>Otherwise, one of the methods must have a return type that
         * is assignable to all of the return types of the rest of the
         * methods.
         * </ul>
         *
         * <li>The resulting proxy class must not exceed any limits imposed
         * on classes by the virtual machine.  For example, the VM may limit
         * the number of interfaces that a class may implement to 65535; in
         * that case, the size of the {@code interfaces} array must not
         * exceed 65535.
         * </ul>
         *
         * <p>If any of these restrictions are violated,
         * {@code Proxy.getProxyClass} will throw an
         * {@code IllegalArgumentException}.  If the {@code interfaces}
         * array argument or any of its elements are {@code null}, a
         * {@code NullPointerException} will be thrown.
         *
         * <p>Note that the order of the specified proxy interfaces is
         * significant: two requests for a proxy class with the same combination
         * of interfaces but in a different order will result in two distinct
         * proxy classes.
         *
         * @param   loader the class loader to define the proxy class
         * @param   interfaces the list of interfaces for the proxy class
         *          to implement
         * @return  a proxy class that is defined in the specified class loader
         *          and that implements the specified interfaces
         * @throws  IllegalArgumentException if any of the restrictions on the
         *          parameters that may be passed to {@code getProxyClass}
         *          are violated
         * @throws  SecurityException if a security manager, <em>s</em>, is present
         *          and any of the following conditions is met:
         *          <ul>
         *             <li> the given {@code loader} is {@code null} and
         *             the caller's class loader is not {@code null} and the
         *             invocation of {@link SecurityManager#checkPermission
         *             s.checkPermission} with
         *             {@code RuntimePermission("getClassLoader")} permission
         *             denies access.</li>
         *             <li> for each proxy interface, {@code intf},
         *             the caller's class loader is not the same as or an
         *             ancestor of the class loader for {@code intf} and
         *             invocation of {@link SecurityManager#checkPackageAccess
         *             s.checkPackageAccess()} denies access to {@code intf}.</li>
         *          </ul>
    ​
         * @throws  NullPointerException if the {@code interfaces} array
         *          argument or any of its elements are {@code null}
         */
        @CallerSensitive
        public static Class<?> getProxyClass(ClassLoader loader,
                                             Class<?>... interfaces)
            throws IllegalArgumentException
        {
            final Class<?>[] intfs = interfaces.clone();
            final SecurityManager sm = System.getSecurityManager();
            if (sm != null) {
                checkProxyAccess(Reflection.getCallerClass(), loader, intfs);
            }
    ​
            return getProxyClass0(loader, intfs);
        }
    ​
        /*
         * Check permissions required to create a Proxy class.
         *
         * To define a proxy class, it performs the access checks as in
         * Class.forName (VM will invoke ClassLoader.checkPackageAccess):
         * 1. "getClassLoader" permission check if loader == null
         * 2. checkPackageAccess on the interfaces it implements
         *
         * To get a constructor and new instance of a proxy class, it performs
         * the package access check on the interfaces it implements
         * as in Class.getConstructor.
         *
         * If an interface is non-public, the proxy class must be defined by
         * the defining loader of the interface.  If the caller's class loader
         * is not the same as the defining loader of the interface, the VM
         * will throw IllegalAccessError when the generated proxy class is
         * being defined via the defineClass0 method.
         */
        private static void checkProxyAccess(Class<?> caller,
                                             ClassLoader loader,
                                             Class<?>... interfaces)
        {
            SecurityManager sm = System.getSecurityManager();
            if (sm != null) {
                ClassLoader ccl = caller.getClassLoader();
                if (VM.isSystemDomainLoader(loader) && !VM.isSystemDomainLoader(ccl)) {
                    sm.checkPermission(SecurityConstants.GET_CLASSLOADER_PERMISSION);
                }
                ReflectUtil.checkProxyPackageAccess(ccl, interfaces);
            }
        }
    ​
        /**
         * Generate a proxy class.  Must call the checkProxyAccess method
         * to perform permission checks before calling this.
         */
        private static Class<?> getProxyClass0(ClassLoader loader,
                                               Class<?>... interfaces) {
            if (interfaces.length > 65535) {
                throw new IllegalArgumentException("interface limit exceeded");
            }
    ​
            // If the proxy class defined by the given loader implementing
            // the given interfaces exists, this will simply return the cached copy;
            // otherwise, it will create the proxy class via the ProxyClassFactory
            return proxyClassCache.get(loader, interfaces);
        }
    ​
        /*
         * a key used for proxy class with 0 implemented interfaces
         */
        private static final Object key0 = new Object();
    ​
        /*
         * Key1 and Key2 are optimized for the common use of dynamic proxies
         * that implement 1 or 2 interfaces.
         *//*
         * a key used for proxy class with 1 implemented interface
         */
        private static final class Key1 extends WeakReference<Class<?>> {
            private final int hash;
    ​
            Key1(Class<?> intf) {
                super(intf);
                this.hash = intf.hashCode();
            }
    ​
            @Override
            public int hashCode() {
                return hash;
            }
    ​
            @Override
            public boolean equals(Object obj) {
                Class<?> intf;
                return this == obj ||
                       obj != null &&
                       obj.getClass() == Key1.class &&
                       (intf = get()) != null &&
                       intf == ((Key1) obj).get();
            }
        }
    ​
        /*
         * a key used for proxy class with 2 implemented interfaces
         */
        private static final class Key2 extends WeakReference<Class<?>> {
            private final int hash;
            private final WeakReference<Class<?>> ref2;
    ​
            Key2(Class<?> intf1, Class<?> intf2) {
                super(intf1);
                hash = 31 * intf1.hashCode() + intf2.hashCode();
                ref2 = new WeakReference<Class<?>>(intf2);
            }
    ​
            @Override
            public int hashCode() {
                return hash;
            }
    ​
            @Override
            public boolean equals(Object obj) {
                Class<?> intf1, intf2;
                return this == obj ||
                       obj != null &&
                       obj.getClass() == Key2.class &&
                       (intf1 = get()) != null &&
                       intf1 == ((Key2) obj).get() &&
                       (intf2 = ref2.get()) != null &&
                       intf2 == ((Key2) obj).ref2.get();
            }
        }
    ​
        /*
         * a key used for proxy class with any number of implemented interfaces
         * (used here for 3 or more only)
         */
        private static final class KeyX {
            private final int hash;
            private final WeakReference<Class<?>>[] refs;
    ​
            @SuppressWarnings("unchecked")
            KeyX(Class<?>[] interfaces) {
                hash = Arrays.hashCode(interfaces);
                refs = (WeakReference<Class<?>>[])new WeakReference<?>[interfaces.length];
                for (int i = 0; i < interfaces.length; i++) {
                    refs[i] = new WeakReference<>(interfaces[i]);
                }
            }
    ​
            @Override
            public int hashCode() {
                return hash;
            }
    ​
            @Override
            public boolean equals(Object obj) {
                return this == obj ||
                       obj != null &&
                       obj.getClass() == KeyX.class &&
                       equals(refs, ((KeyX) obj).refs);
            }
    ​
            private static boolean equals(WeakReference<Class<?>>[] refs1,
                                          WeakReference<Class<?>>[] refs2) {
                if (refs1.length != refs2.length) {
                    return false;
                }
                for (int i = 0; i < refs1.length; i++) {
                    Class<?> intf = refs1[i].get();
                    if (intf == null || intf != refs2[i].get()) {
                        return false;
                    }
                }
                return true;
            }
        }
    ​
        /**
         * A function that maps an array of interfaces to an optimal key where
         * Class objects representing interfaces are weakly referenced.
         */
        private static final class KeyFactory
            implements BiFunction<ClassLoader, Class<?>[], Object>
        {
            @Override
            public Object apply(ClassLoader classLoader, Class<?>[] interfaces) {
                switch (interfaces.length) {
                    case 1: return new Key1(interfaces[0]); // the most frequent
                    case 2: return new Key2(interfaces[0], interfaces[1]);
                    case 0: return key0;
                    default: return new KeyX(interfaces);
                }
            }
        }
    ​
        /**
         * A factory function that generates, defines and returns the proxy class given
         * the ClassLoader and array of interfaces.
         */
        private static final class ProxyClassFactory
            implements BiFunction<ClassLoader, Class<?>[], Class<?>>
        {
            // prefix for all proxy class names
            private static final String proxyClassNamePrefix = "$Proxy";
    ​
            // next number to use for generation of unique proxy class names
            private static final AtomicLong nextUniqueNumber = new AtomicLong();
    ​
            @Override
            public Class<?> apply(ClassLoader loader, Class<?>[] interfaces) {
    ​
                Map<Class<?>, Boolean> interfaceSet = new IdentityHashMap<>(interfaces.length);
                for (Class<?> intf : interfaces) {
                    /*
                     * Verify that the class loader resolves the name of this
                     * interface to the same Class object.
                     */
                    Class<?> interfaceClass = null;
                    try {
                        interfaceClass = Class.forName(intf.getName(), false, loader);
                    } catch (ClassNotFoundException e) {
                    }
                    if (interfaceClass != intf) {
                        throw new IllegalArgumentException(
                            intf + " is not visible from class loader");
                    }
                    /*
                     * Verify that the Class object actually represents an
                     * interface.
                     */
                    if (!interfaceClass.isInterface()) {
                        throw new IllegalArgumentException(
                            interfaceClass.getName() + " is not an interface");
                    }
                    /*
                     * Verify that this interface is not a duplicate.
                     */
                    if (interfaceSet.put(interfaceClass, Boolean.TRUE) != null) {
                        throw new IllegalArgumentException(
                            "repeated interface: " + interfaceClass.getName());
                    }
                }
    ​
                String proxyPkg = null;     // package to define proxy class in
                int accessFlags = Modifier.PUBLIC | Modifier.FINAL;
    ​
                /*
                 * Record the package of a non-public proxy interface so that the
                 * proxy class will be defined in the same package.  Verify that
                 * all non-public proxy interfaces are in the same package.
                 */
                for (Class<?> intf : interfaces) {
                    int flags = intf.getModifiers();
                    if (!Modifier.isPublic(flags)) {
                        accessFlags = Modifier.FINAL;
                        String name = intf.getName();
                        int n = name.lastIndexOf('.');
                        String pkg = ((n == -1) ? "" : name.substring(0, n + 1));
                        if (proxyPkg == null) {
                            proxyPkg = pkg;
                        } else if (!pkg.equals(proxyPkg)) {
                            throw new IllegalArgumentException(
                                "non-public interfaces from different packages");
                        }
                    }
                }
    ​
                if (proxyPkg == null) {
                    // if no non-public proxy interfaces, use com.sun.proxy package
                    proxyPkg = ReflectUtil.PROXY_PACKAGE + ".";
                }
    ​
                /*
                 * Choose a name for the proxy class to generate.
                 */
                long num = nextUniqueNumber.getAndIncrement();
                String proxyName = proxyPkg + proxyClassNamePrefix + num;
    ​
                /*
                 * Generate the specified proxy class.
                 */
                byte[] proxyClassFile = ProxyGenerator.generateProxyClass(
                    proxyName, interfaces, accessFlags);
                try {
                    return defineClass0(loader, proxyName,
                                        proxyClassFile, 0, proxyClassFile.length);
                } catch (ClassFormatError e) {
                    /*
                     * A ClassFormatError here means that (barring bugs in the
                     * proxy class generation code) there was some other
                     * invalid aspect of the arguments supplied to the proxy
                     * class creation (such as virtual machine limitations
                     * exceeded).
                     */
                    throw new IllegalArgumentException(e.toString());
                }
            }
        }
    ​
        /**
         * Returns an instance of a proxy class for the specified interfaces
         * that dispatches method invocations to the specified invocation
         * handler.
         *
         * <p>{@code Proxy.newProxyInstance} throws
         * {@code IllegalArgumentException} for the same reasons that
         * {@code Proxy.getProxyClass} does.
         *
         * @param   loader the class loader to define the proxy class
         * @param   interfaces the list of interfaces for the proxy class
         *          to implement
         * @param   h the invocation handler to dispatch method invocations to
         * @return  a proxy instance with the specified invocation handler of a
         *          proxy class that is defined by the specified class loader
         *          and that implements the specified interfaces
         * @throws  IllegalArgumentException if any of the restrictions on the
         *          parameters that may be passed to {@code getProxyClass}
         *          are violated
         * @throws  SecurityException if a security manager, <em>s</em>, is present
         *          and any of the following conditions is met:
         *          <ul>
         *          <li> the given {@code loader} is {@code null} and
         *               the caller's class loader is not {@code null} and the
         *               invocation of {@link SecurityManager#checkPermission
         *               s.checkPermission} with
         *               {@code RuntimePermission("getClassLoader")} permission
         *               denies access;</li>
         *          <li> for each proxy interface, {@code intf},
         *               the caller's class loader is not the same as or an
         *               ancestor of the class loader for {@code intf} and
         *               invocation of {@link SecurityManager#checkPackageAccess
         *               s.checkPackageAccess()} denies access to {@code intf};</li>
         *          <li> any of the given proxy interfaces is non-public and the
         *               caller class is not in the same {@linkplain Package runtime package}
         *               as the non-public interface and the invocation of
         *               {@link SecurityManager#checkPermission s.checkPermission} with
         *               {@code ReflectPermission("newProxyInPackage.{package name}")}
         *               permission denies access.</li>
         *          </ul>
         * @throws  NullPointerException if the {@code interfaces} array
         *          argument or any of its elements are {@code null}, or
         *          if the invocation handler, {@code h}, is
         *          {@code null}
         */
        @CallerSensitive
        public static Object newProxyInstance(ClassLoader loader,
                                              Class<?>[] interfaces,
                                              InvocationHandler h)
            throws IllegalArgumentException
        {
            Objects.requireNonNull(h);
    ​
            final Class<?>[] intfs = interfaces.clone();
            final SecurityManager sm = System.getSecurityManager();
            if (sm != null) {
                checkProxyAccess(Reflection.getCallerClass(), loader, intfs);
            }
    ​
            /*
             * Look up or generate the designated proxy class.
             */
            Class<?> cl = getProxyClass0(loader, intfs);
    ​
            /*
             * Invoke its constructor with the designated invocation handler.
             */
            try {
                if (sm != null) {
                    checkNewProxyPermission(Reflection.getCallerClass(), cl);
                }
    ​
                final Constructor<?> cons = cl.getConstructor(constructorParams);
                final InvocationHandler ih = h;
                if (!Modifier.isPublic(cl.getModifiers())) {
                    AccessController.doPrivileged(new PrivilegedAction<Void>() {
                        public Void run() {
                            cons.setAccessible(true);
                            return null;
                        }
                    });
                }
                return cons.newInstance(new Object[]{h});
            } catch (IllegalAccessException|InstantiationException e) {
                throw new InternalError(e.toString(), e);
            } catch (InvocationTargetException e) {
                Throwable t = e.getCause();
                if (t instanceof RuntimeException) {
                    throw (RuntimeException) t;
                } else {
                    throw new InternalError(t.toString(), t);
                }
            } catch (NoSuchMethodException e) {
                throw new InternalError(e.toString(), e);
            }
        }
    ​
        private static void checkNewProxyPermission(Class<?> caller, Class<?> proxyClass) {
            SecurityManager sm = System.getSecurityManager();
            if (sm != null) {
                if (ReflectUtil.isNonPublicProxyClass(proxyClass)) {
                    ClassLoader ccl = caller.getClassLoader();
                    ClassLoader pcl = proxyClass.getClassLoader();
    ​
                    // do permission check if the caller is in a different runtime package
                    // of the proxy class
                    int n = proxyClass.getName().lastIndexOf('.');
                    String pkg = (n == -1) ? "" : proxyClass.getName().substring(0, n);
    ​
                    n = caller.getName().lastIndexOf('.');
                    String callerPkg = (n == -1) ? "" : caller.getName().substring(0, n);
    ​
                    if (pcl != ccl || !pkg.equals(callerPkg)) {
                        sm.checkPermission(new ReflectPermission("newProxyInPackage." + pkg));
                    }
                }
            }
        }
    ​
        /**
         * Returns true if and only if the specified class was dynamically
         * generated to be a proxy class using the {@code getProxyClass}
         * method or the {@code newProxyInstance} method.
         *
         * <p>The reliability of this method is important for the ability
         * to use it to make security decisions, so its implementation should
         * not just test if the class in question extends {@code Proxy}.
         *
         * @param   cl the class to test
         * @return  {@code true} if the class is a proxy class and
         *          {@code false} otherwise
         * @throws  NullPointerException if {@code cl} is {@code null}
         */
        public static boolean isProxyClass(Class<?> cl) {
            return Proxy.class.isAssignableFrom(cl) && proxyClassCache.containsValue(cl);
        }
    ​
        /**
         * Returns the invocation handler for the specified proxy instance.
         *
         * @param   proxy the proxy instance to return the invocation handler for
         * @return  the invocation handler for the proxy instance
         * @throws  IllegalArgumentException if the argument is not a
         *          proxy instance
         * @throws  SecurityException if a security manager, <em>s</em>, is present
         *          and the caller's class loader is not the same as or an
         *          ancestor of the class loader for the invocation handler
         *          and invocation of {@link SecurityManager#checkPackageAccess
         *          s.checkPackageAccess()} denies access to the invocation
         *          handler's class.
         */
        @CallerSensitive
        public static InvocationHandler getInvocationHandler(Object proxy)
            throws IllegalArgumentException
        {
            /*
             * Verify that the object is actually a proxy instance.
             */
            if (!isProxyClass(proxy.getClass())) {
                throw new IllegalArgumentException("not a proxy instance");
            }
    ​
            final Proxy p = (Proxy) proxy;
            final InvocationHandler ih = p.h;
            if (System.getSecurityManager() != null) {
                Class<?> ihClass = ih.getClass();
                Class<?> caller = Reflection.getCallerClass();
                if (ReflectUtil.needsPackageAccessCheck(caller.getClassLoader(),
                                                        ihClass.getClassLoader()))
                {
                    ReflectUtil.checkPackageAccess(ihClass);
                }
            }
    ​
            return ih;
        }
    ​
        private static native Class<?> defineClass0(ClassLoader loader, String name,
                                                    byte[] b, int off, int len);
    }
    ​
    java.lang.reflect.Proxy

    3.8 sun.reflect.MethodAccessor接口及其子类

    MethodAccessor接口的子类包含虚类MethodAccessorImpl,以及虚类MethodAccessorImpl的子类NativeMethodAccessorImpl,以及封装维护了一个MethodAccessorImpl实例的子类DelegatingMethodAccessorImpl。主要是在维护方法invoke()

    在调用方法invoke()实现的过程中,主要是进行匿名类方法和其他方法调用的区分,然而我至今未知sun.reflect.MethodAccessorGenerator.generateMethod()中的generate()到底做了什么。

    3.9 sun.reflect.ReflectionFactory

    ReflectionFactory类用于构建字段、方法、构造器的访问器

    /*
     * Copyright (c) 2001, 2013, Oracle and/or its affiliates. All rights reserved.
     * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     *
     * This code is free software; you can redistribute it and/or modify it
     * under the terms of the GNU General Public License version 2 only, as
     * published by the Free Software Foundation.  Oracle designates this
     * particular file as subject to the "Classpath" exception as provided
     * by Oracle in the LICENSE file that accompanied this code.
     *
     * This code is distributed in the hope that it will be useful, but WITHOUT
     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
     * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
     * version 2 for more details (a copy is included in the LICENSE file that
     * accompanied this code).
     *
     * You should have received a copy of the GNU General Public License version
     * 2 along with this work; if not, write to the Free Software Foundation,
     * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
     *
     * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
     * or visit www.oracle.com if you need additional information or have any
     * questions.
     */package sun.reflect;
    ​
    import java.io.Externalizable;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.io.ObjectStreamClass;
    import java.io.OptionalDataException;
    import java.io.Serializable;
    import java.lang.invoke.MethodHandle;
    import java.lang.invoke.MethodHandles;
    import java.lang.reflect.Field;
    import java.lang.reflect.Executable;
    import java.lang.reflect.InvocationTargetException;
    import java.lang.reflect.Method;
    import java.lang.reflect.Constructor;
    import java.lang.reflect.Modifier;
    import java.security.AccessController;
    import java.security.Permission;
    import java.security.PrivilegedAction;
    import java.util.Objects;
    ​
    import sun.reflect.misc.ReflectUtil;
    ​
    ​
    /** <P> The master factory for all reflective objects, both those in
        java.lang.reflect (Fields, Methods, Constructors) as well as their
        delegates (FieldAccessors, MethodAccessors, ConstructorAccessors).
        </P>
    ​
        <P> The methods in this class are extremely unsafe and can cause
        subversion of both the language and the verifier. For this reason,
        they are all instance methods, and access to the constructor of
        this factory is guarded by a security check, in similar style to
        {@link sun.misc.Unsafe}. </P>
    */public class ReflectionFactory {
    ​
        private static boolean initted = false;
        private static final Permission reflectionFactoryAccessPerm
            = new RuntimePermission("reflectionFactoryAccess");
        private static final ReflectionFactory soleInstance = new ReflectionFactory();
        // Provides access to package-private mechanisms in java.lang.reflect
        private static volatile LangReflectAccess langReflectAccess;
    ​
        /* Method for static class initializer <clinit>, or null */
        private static volatile Method hasStaticInitializerMethod;
    ​
        //
        // "Inflation" mechanism. Loading bytecodes to implement
        // Method.invoke() and Constructor.newInstance() currently costs
        // 3-4x more than an invocation via native code for the first
        // invocation (though subsequent invocations have been benchmarked
        // to be over 20x faster). Unfortunately this cost increases
        // startup time for certain applications that use reflection
        // intensively (but only once per class) to bootstrap themselves.
        // To avoid this penalty we reuse the existing JVM entry points
        // for the first few invocations of Methods and Constructors and
        // then switch to the bytecode-based implementations.
        //
        // Package-private to be accessible to NativeMethodAccessorImpl
        // and NativeConstructorAccessorImpl
        private static boolean noInflation        = false;
        private static int     inflationThreshold = 15;
    ​
        private ReflectionFactory() {}
    ​
        /**
         * A convenience class for acquiring the capability to instantiate
         * reflective objects.  Use this instead of a raw call to {@link
         * #getReflectionFactory} in order to avoid being limited by the
         * permissions of your callers.
         *
         * <p>An instance of this class can be used as the argument of
         * <code>AccessController.doPrivileged</code>.
         */
        public static final class GetReflectionFactoryAction
            implements PrivilegedAction<ReflectionFactory> {
            public ReflectionFactory run() {
                return getReflectionFactory();
            }
        }
    ​
        /**
         * Provides the caller with the capability to instantiate reflective
         * objects.
         *
         * <p> First, if there is a security manager, its
         * <code>checkPermission</code> method is called with a {@link
         * java.lang.RuntimePermission} with target
         * <code>"reflectionFactoryAccess"</code>.  This may result in a
         * security exception.
         *
         * <p> The returned <code>ReflectionFactory</code> object should be
         * carefully guarded by the caller, since it can be used to read and
         * write private data and invoke private methods, as well as to load
         * unverified bytecodes.  It must never be passed to untrusted code.
         *
         * @exception SecurityException if a security manager exists and its
         *             <code>checkPermission</code> method doesn't allow
         *             access to the RuntimePermission "reflectionFactoryAccess".  */
        public static ReflectionFactory getReflectionFactory() {
            SecurityManager security = System.getSecurityManager();
            if (security != null) {
                // TO DO: security.checkReflectionFactoryAccess();
                security.checkPermission(reflectionFactoryAccessPerm);
            }
            return soleInstance;
        }
    ​
        //--------------------------------------------------------------------------
        //
        // Routines used by java.lang.reflect
        //
        //
    /** Called only by java.lang.reflect.Modifier's static initializer */
        public void setLangReflectAccess(LangReflectAccess access) {
            langReflectAccess = access;
        }
    ​
        /**
         * Note: this routine can cause the declaring class for the field
         * be initialized and therefore must not be called until the
         * first get/set of this field.
         * @param field the field
         * @param override true if caller has overridden aaccessibility
         */
        public FieldAccessor newFieldAccessor(Field field, boolean override) {
            checkInitted();
            return UnsafeFieldAccessorFactory.newFieldAccessor(field, override);
        }
    ​
        public MethodAccessor newMethodAccessor(Method method) {
            checkInitted();
    ​
            if (noInflation && !ReflectUtil.isVMAnonymousClass(method.getDeclaringClass())) {
                return new MethodAccessorGenerator().
                    generateMethod(method.getDeclaringClass(),
                                   method.getName(),
                                   method.getParameterTypes(),
                                   method.getReturnType(),
                                   method.getExceptionTypes(),
                                   method.getModifiers());
            } else {
                NativeMethodAccessorImpl acc =
                    new NativeMethodAccessorImpl(method);
                DelegatingMethodAccessorImpl res =
                    new DelegatingMethodAccessorImpl(acc);
                acc.setParent(res);
                return res;
            }
        }
    ​
        public ConstructorAccessor newConstructorAccessor(Constructor<?> c) {
            checkInitted();
    ​
            Class<?> declaringClass = c.getDeclaringClass();
            if (Modifier.isAbstract(declaringClass.getModifiers())) {
                return new InstantiationExceptionConstructorAccessorImpl(null);
            }
            if (declaringClass == Class.class) {
                return new InstantiationExceptionConstructorAccessorImpl
                    ("Can not instantiate java.lang.Class");
            }
            // Bootstrapping issue: since we use Class.newInstance() in
            // the ConstructorAccessor generation process, we have to
            // break the cycle here.
            if (Reflection.isSubclassOf(declaringClass,
                                        ConstructorAccessorImpl.class)) {
                return new BootstrapConstructorAccessorImpl(c);
            }
    ​
            if (noInflation && !ReflectUtil.isVMAnonymousClass(c.getDeclaringClass())) {
                return new MethodAccessorGenerator().
                    generateConstructor(c.getDeclaringClass(),
                                        c.getParameterTypes(),
                                        c.getExceptionTypes(),
                                        c.getModifiers());
            } else {
                NativeConstructorAccessorImpl acc =
                    new NativeConstructorAccessorImpl(c);
                DelegatingConstructorAccessorImpl res =
                    new DelegatingConstructorAccessorImpl(acc);
                acc.setParent(res);
                return res;
            }
        }
    ​
        //--------------------------------------------------------------------------
        //
        // Routines used by java.lang
        //
        //
    /** Creates a new java.lang.reflect.Field. Access checks as per
            java.lang.reflect.AccessibleObject are not overridden. */
        public Field newField(Class<?> declaringClass,
                              String name,
                              Class<?> type,
                              int modifiers,
                              int slot,
                              String signature,
                              byte[] annotations)
        {
            return langReflectAccess().newField(declaringClass,
                                                name,
                                                type,
                                                modifiers,
                                                slot,
                                                signature,
                                                annotations);
        }
    ​
        /** Creates a new java.lang.reflect.Method. Access checks as per
            java.lang.reflect.AccessibleObject are not overridden. */
        public Method newMethod(Class<?> declaringClass,
                                String name,
                                Class<?>[] parameterTypes,
                                Class<?> returnType,
                                Class<?>[] checkedExceptions,
                                int modifiers,
                                int slot,
                                String signature,
                                byte[] annotations,
                                byte[] parameterAnnotations,
                                byte[] annotationDefault)
        {
            return langReflectAccess().newMethod(declaringClass,
                                                 name,
                                                 parameterTypes,
                                                 returnType,
                                                 checkedExceptions,
                                                 modifiers,
                                                 slot,
                                                 signature,
                                                 annotations,
                                                 parameterAnnotations,
                                                 annotationDefault);
        }
    ​
        /** Creates a new java.lang.reflect.Constructor. Access checks as
            per java.lang.reflect.AccessibleObject are not overridden. */
        public Constructor<?> newConstructor(Class<?> declaringClass,
                                             Class<?>[] parameterTypes,
                                             Class<?>[] checkedExceptions,
                                             int modifiers,
                                             int slot,
                                             String signature,
                                             byte[] annotations,
                                             byte[] parameterAnnotations)
        {
            return langReflectAccess().newConstructor(declaringClass,
                                                      parameterTypes,
                                                      checkedExceptions,
                                                      modifiers,
                                                      slot,
                                                      signature,
                                                      annotations,
                                                      parameterAnnotations);
        }
    ​
        /** Gets the MethodAccessor object for a java.lang.reflect.Method */
        public MethodAccessor getMethodAccessor(Method m) {
            return langReflectAccess().getMethodAccessor(m);
        }
    ​
        /** Sets the MethodAccessor object for a java.lang.reflect.Method */
        public void setMethodAccessor(Method m, MethodAccessor accessor) {
            langReflectAccess().setMethodAccessor(m, accessor);
        }
    ​
        /** Gets the ConstructorAccessor object for a
            java.lang.reflect.Constructor */
        public ConstructorAccessor getConstructorAccessor(Constructor<?> c) {
            return langReflectAccess().getConstructorAccessor(c);
        }
    ​
        /** Sets the ConstructorAccessor object for a
            java.lang.reflect.Constructor */
        public void setConstructorAccessor(Constructor<?> c,
                                           ConstructorAccessor accessor)
        {
            langReflectAccess().setConstructorAccessor(c, accessor);
        }
    ​
        /** Makes a copy of the passed method. The returned method is a
            "child" of the passed one; see the comments in Method.java for
            details. */
        public Method copyMethod(Method arg) {
            return langReflectAccess().copyMethod(arg);
        }
    ​
        /** Makes a copy of the passed field. The returned field is a
            "child" of the passed one; see the comments in Field.java for
            details. */
        public Field copyField(Field arg) {
            return langReflectAccess().copyField(arg);
        }
    ​
        /** Makes a copy of the passed constructor. The returned
            constructor is a "child" of the passed one; see the comments
            in Constructor.java for details. */
        public <T> Constructor<T> copyConstructor(Constructor<T> arg) {
            return langReflectAccess().copyConstructor(arg);
        }
    ​
        /** Gets the byte[] that encodes TypeAnnotations on an executable.
         */
        public byte[] getExecutableTypeAnnotationBytes(Executable ex) {
            return langReflectAccess().getExecutableTypeAnnotationBytes(ex);
        }
    ​
        //--------------------------------------------------------------------------
        //
        // Routines used by serialization
        //
        //
    /**
         * Returns an accessible constructor capable of creating instances
         * of the given class, initialized by the given constructor.
         *
         * @param classToInstantiate the class to instantiate
         * @param constructorToCall the constructor to call
         * @return an accessible constructor
         */
        public Constructor<?> newConstructorForSerialization
            (Class<?> classToInstantiate, Constructor<?> constructorToCall)
        {
            // Fast path
            if (constructorToCall.getDeclaringClass() == classToInstantiate) {
                return constructorToCall;
            }
            return generateConstructor(classToInstantiate, constructorToCall);
        }
    ​
        /**
         * Returns an accessible no-arg constructor for a class.
         * The no-arg constructor is found searching the class and its supertypes.
         *
         * @param cl the class to instantiate
         * @return a no-arg constructor for the class or {@code null} if
         *     the class or supertypes do not have a suitable no-arg constructor
         */
        public final Constructor<?> newConstructorForSerialization(Class<?> cl) {
            Class<?> initCl = cl;
            while (Serializable.class.isAssignableFrom(initCl)) {
                if ((initCl = initCl.getSuperclass()) == null) {
                    return null;
                }
            }
            Constructor<?> constructorToCall;
            try {
                constructorToCall = initCl.getDeclaredConstructor();
                int mods = constructorToCall.getModifiers();
                if ((mods & Modifier.PRIVATE) != 0 ||
                        ((mods & (Modifier.PUBLIC | Modifier.PROTECTED)) == 0 &&
                                !packageEquals(cl, initCl))) {
                    return null;
                }
            } catch (NoSuchMethodException ex) {
                return null;
            }
            return generateConstructor(cl, constructorToCall);
        }
    ​
        private final Constructor<?> generateConstructor(Class<?> classToInstantiate,
                                                         Constructor<?> constructorToCall) {
    ​
    ​
            ConstructorAccessor acc = new MethodAccessorGenerator().
                generateSerializationConstructor(classToInstantiate,
                                                 constructorToCall.getParameterTypes(),
                                                 constructorToCall.getExceptionTypes(),
                                                 constructorToCall.getModifiers(),
                                                 constructorToCall.getDeclaringClass());
            Constructor<?> c = newConstructor(constructorToCall.getDeclaringClass(),
                                              constructorToCall.getParameterTypes(),
                                              constructorToCall.getExceptionTypes(),
                                              constructorToCall.getModifiers(),
                                              langReflectAccess().
                                              getConstructorSlot(constructorToCall),
                                              langReflectAccess().
                                              getConstructorSignature(constructorToCall),
                                              langReflectAccess().
                                              getConstructorAnnotations(constructorToCall),
                                              langReflectAccess().
                                              getConstructorParameterAnnotations(constructorToCall));
            setConstructorAccessor(c, acc);
            c.setAccessible(true);
            return c;
        }
    ​
        /**
         * Returns an accessible no-arg constructor for an externalizable class to be
         * initialized using a public no-argument constructor.
         *
         * @param cl the class to instantiate
         * @return A no-arg constructor for the class; returns {@code null} if
         *     the class does not implement {@link java.io.Externalizable}
         */
        public final Constructor<?> newConstructorForExternalization(Class<?> cl) {
            if (!Externalizable.class.isAssignableFrom(cl)) {
                return null;
            }
            try {
                Constructor<?> cons = cl.getConstructor();
                cons.setAccessible(true);
                return cons;
            } catch (NoSuchMethodException ex) {
                return null;
            }
        }
    ​
        /**
         * Returns a direct MethodHandle for the {@code readObject} method on
         * a Serializable class.
         * The first argument of {@link MethodHandle#invoke} is the serializable
         * object and the second argument is the {@code ObjectInputStream} passed to
         * {@code readObject}.
         *
         * @param cl a Serializable class
         * @return  a direct MethodHandle for the {@code readObject} method of the class or
         *          {@code null} if the class does not have a {@code readObject} method
         */
        public final MethodHandle readObjectForSerialization(Class<?> cl) {
            return findReadWriteObjectForSerialization(cl, "readObject", ObjectInputStream.class);
        }
    ​
        /**
         * Returns a direct MethodHandle for the {@code readObjectNoData} method on
         * a Serializable class.
         * The first argument of {@link MethodHandle#invoke} is the serializable
         * object and the second argument is the {@code ObjectInputStream} passed to
         * {@code readObjectNoData}.
         *
         * @param cl a Serializable class
         * @return  a direct MethodHandle for the {@code readObjectNoData} method
         *          of the class or {@code null} if the class does not have a
         *          {@code readObjectNoData} method
         */
        public final MethodHandle readObjectNoDataForSerialization(Class<?> cl) {
            return findReadWriteObjectForSerialization(cl, "readObjectNoData", ObjectInputStream.class);
        }
    ​
        /**
         * Returns a direct MethodHandle for the {@code writeObject} method on
         * a Serializable class.
         * The first argument of {@link MethodHandle#invoke} is the serializable
         * object and the second argument is the {@code ObjectOutputStream} passed to
         * {@code writeObject}.
         *
         * @param cl a Serializable class
         * @return  a direct MethodHandle for the {@code writeObject} method of the class or
         *          {@code null} if the class does not have a {@code writeObject} method
         */
        public final MethodHandle writeObjectForSerialization(Class<?> cl) {
            return findReadWriteObjectForSerialization(cl, "writeObject", ObjectOutputStream.class);
        }
    ​
        private final MethodHandle findReadWriteObjectForSerialization(Class<?> cl,
                                                                       String methodName,
                                                                       Class<?> streamClass) {
            if (!Serializable.class.isAssignableFrom(cl)) {
                return null;
            }
    ​
            try {
                Method meth = cl.getDeclaredMethod(methodName, streamClass);
                int mods = meth.getModifiers();
                if (meth.getReturnType() != Void.TYPE ||
                        Modifier.isStatic(mods) ||
                        !Modifier.isPrivate(mods)) {
                    return null;
                }
                meth.setAccessible(true);
                return MethodHandles.lookup().unreflect(meth);
            } catch (NoSuchMethodException ex) {
                return null;
            } catch (IllegalAccessException ex1) {
                throw new InternalError("Error", ex1);
            }
        }
    ​
        /**
         * Returns a direct MethodHandle for the {@code readResolve} method on
         * a serializable class.
         * The single argument of {@link MethodHandle#invoke} is the serializable
         * object.
         *
         * @param cl the Serializable class
         * @return  a direct MethodHandle for the {@code readResolve} method of the class or
         *          {@code null} if the class does not have a {@code readResolve} method
         */
        public final MethodHandle readResolveForSerialization(Class<?> cl) {
            return getReplaceResolveForSerialization(cl, "readResolve");
        }
    ​
        /**
         * Returns a direct MethodHandle for the {@code writeReplace} method on
         * a serializable class.
         * The single argument of {@link MethodHandle#invoke} is the serializable
         * object.
         *
         * @param cl the Serializable class
         * @return  a direct MethodHandle for the {@code writeReplace} method of the class or
         *          {@code null} if the class does not have a {@code writeReplace} method
         */
        public final MethodHandle writeReplaceForSerialization(Class<?> cl) {
            return getReplaceResolveForSerialization(cl, "writeReplace");
        }
    ​
        /**
         * Returns a direct MethodHandle for the {@code writeReplace} method on
         * a serializable class.
         * The single argument of {@link MethodHandle#invoke} is the serializable
         * object.
         *
         * @param cl the Serializable class
         * @return  a direct MethodHandle for the {@code writeReplace} method of the class or
         *          {@code null} if the class does not have a {@code writeReplace} method
         */
        private MethodHandle getReplaceResolveForSerialization(Class<?> cl,
                                                               String methodName) {
            if (!Serializable.class.isAssignableFrom(cl)) {
                return null;
            }
    ​
            Class<?> defCl = cl;
            while (defCl != null) {
                try {
                    Method m = defCl.getDeclaredMethod(methodName);
                    if (m.getReturnType() != Object.class) {
                        return null;
                    }
                    int mods = m.getModifiers();
                    if (Modifier.isStatic(mods) | Modifier.isAbstract(mods)) {
                        return null;
                    } else if (Modifier.isPublic(mods) | Modifier.isProtected(mods)) {
                        // fall through
                    } else if (Modifier.isPrivate(mods) && (cl != defCl)) {
                        return null;
                    } else if (!packageEquals(cl, defCl)) {
                        return null;
                    }
                    try {
                        // Normal return
                        m.setAccessible(true);
                        return MethodHandles.lookup().unreflect(m);
                    } catch (IllegalAccessException ex0) {
                        // setAccessible should prevent IAE
                        throw new InternalError("Error", ex0);
                    }
                } catch (NoSuchMethodException ex) {
                    defCl = defCl.getSuperclass();
                }
            }
            return null;
        }
    ​
        /**
         * Returns true if the class has a static initializer.
         * The presence of a static initializer is used to compute the serialVersionUID.
         * @param cl a serializable classLook
         * @return {@code true} if the class has a static initializer,
         *          otherwise {@code false}
         */
        public final boolean hasStaticInitializerForSerialization(Class<?> cl) {
            Method m = hasStaticInitializerMethod;
            if (m == null) {
                try {
                    m = ObjectStreamClass.class.getDeclaredMethod("hasStaticInitializer",
                            new Class<?>[]{Class.class});
                    m.setAccessible(true);
                    hasStaticInitializerMethod = m;
                } catch (NoSuchMethodException ex) {
                    throw new InternalError("No such method hasStaticInitializer on "
                            + ObjectStreamClass.class, ex);
                }
            }
            try {
                return (Boolean) m.invoke(null, cl);
            } catch (InvocationTargetException | IllegalAccessException ex) {
                throw new InternalError("Exception invoking hasStaticInitializer", ex);
            }
        }
    ​
        /**
         * Returns a new OptionalDataException with {@code eof} set to {@code true}
         * or {@code false}.
         * @param bool the value of {@code eof} in the created OptionalDataException
         * @return  a new OptionalDataException
         */
        public final OptionalDataException newOptionalDataExceptionForSerialization(boolean bool) {
            try {
                Constructor<OptionalDataException> boolCtor =
                        OptionalDataException.class.getDeclaredConstructor(Boolean.TYPE);
                boolCtor.setAccessible(true);
                return boolCtor.newInstance(bool);
            } catch (NoSuchMethodException | InstantiationException|
                    IllegalAccessException|InvocationTargetException ex) {
                throw new InternalError("unable to create OptionalDataException", ex);
            }
        }
    ​
        //--------------------------------------------------------------------------
        //
        // Internals only below this point
        //
    static int inflationThreshold() {
            return inflationThreshold;
        }
    ​
        /** We have to defer full initialization of this class until after
            the static initializer is run since java.lang.reflect.Method's
            static initializer (more properly, that for
            java.lang.reflect.AccessibleObject) causes this class's to be
            run, before the system properties are set up. */
        private static void checkInitted() {
            if (initted) return;
            AccessController.doPrivileged(
                new PrivilegedAction<Void>() {
                    public Void run() {
                        // Tests to ensure the system properties table is fully
                        //                    // initialized. This is needed because reflection code is
                        //                    // called very early in the initialization process (before
                        //                    // command-line arguments have been parsed and therefore
                        //                    // these user-settable properties installed.) We assume that
                        //                    // if System.out is non-null then the System class has been
                        //                    // fully initialized and that the bulk of the startup code
                        //                    // has been run.
    if (System.out == null) {
                            // java.lang.System not yet fully initialized
                            return null;
                        }
    ​
                        String val = System.getProperty("sun.reflect.noInflation");
                        if (val != null && val.equals("true")) {
                            noInflation = true;
                        }
    ​
                        val = System.getProperty("sun.reflect.inflationThreshold");
                        if (val != null) {
                            try {
                                inflationThreshold = Integer.parseInt(val);
                            } catch (NumberFormatException e) {
                                throw new RuntimeException("Unable to parse property sun.reflect.inflationThreshold", e);
                            }
                        }
    ​
                        initted = true;
                        return null;
                    }
                });
        }
    ​
        private static LangReflectAccess langReflectAccess() {
            if (langReflectAccess == null) {
                // Call a static method to get class java.lang.reflect.Modifier
                // initialized. Its static initializer will cause
                // setLangReflectAccess() to be called from the context of the
                // java.lang.reflect package.
                Modifier.isPublic(Modifier.PUBLIC);
            }
            return langReflectAccess;
        }
    ​
        /**
         * Returns true if classes are defined in the classloader and same package, false
         * otherwise.
         * @param cl1 a class
         * @param cl2 another class
         * @returns true if the two classes are in the same classloader and package
         */
        private static boolean packageEquals(Class<?> cl1, Class<?> cl2) {
            return cl1.getClassLoader() == cl2.getClassLoader() &&
                    Objects.equals(cl1.getPackage(), cl2.getPackage());
        }
    ​
    }
    sun.reflect.ReflectionFactory

    参考

    深入解析Java反射(2) - invoke方法

    JEP 176: Mechanical Checking of Caller-Sensitive Methods

    JVM注解@CallSensitive

    Java中的java、javax、sun、org包有什么区别

    JAVA中反射机制六(java.lang.reflect包)

     

    当你深入了解,你就会发现世界如此广袤,而你对世界的了解则是如此浅薄,请永远保持谦卑的态度。
  • 相关阅读:
    redis同步指定key数据到其他redis中
    Golang 生成随机数
    怎么理解“平均负载”? 进行分析等
    Golang打印空心金字塔for循环实现
    python十几行代码实现三级菜单
    mysql增量恢复
    python内建函数
    python练习题总结
    迭代器和生成器
    python基础数据类型
  • 原文地址:https://www.cnblogs.com/liwxmyself/p/14758632.html
Copyright © 2011-2022 走看看