`
RednaxelaFX
  • 浏览: 3013700 次
  • 性别: Icon_minigender_1
  • 来自: 海外
社区版块
存档分类
最新评论

关于反射调用方法的一个log

    博客分类:
  • Java
阅读更多
刚才在JavaEye问答频道看到了这么一个问题
liu0013 写道
[Loaded sun.reflect.GeneratedMethodAccessor197 from __JVM_DefineClass__]
请问报这个是什么意思?


这是Sun实现的Java标准库的一个细节。下面举例稍微讲解一下。
假如有这么一个类A:
public class A {
    public void foo(String name) {
        System.out.println("Hello, " + name);
    }
}

可以编写另外一个类来反射调用A上的方法:
import java.lang.reflect.Method;

public class TestClassLoad {
    public static void main(String[] args) throws Exception {
        Class<?> clz = Class.forName("A");
        Object o = clz.newInstance();
        Method m = clz.getMethod("foo", String.class);
        for (int i = 0; i < 16; i++) {
            m.invoke(o, Integer.toString(i));
        }
    }
}

注意到TestClassLoad类上不会有对类A的符号依赖——也就是说在加载并初始化TestClassLoad类时不需要关心类A的存在与否,而是等到main()方法执行到调用Class.forName()时才试图对类A做动态加载;这里用的是一个参数版的forName(),也就是使用当前方法所在类的ClassLoader来加载,并且初始化新加载的类。……好吧这个细节跟主题没啥关系。

回到主题。这次我的测试环境是Sun的JDK 1.6.0 update 13 build 03。编译上述代码,并在执行TestClassLoad时加入-XX:+TraceClassLoading参数(或者-verbose:class或者直接-verbose都行),如下:
java -XX:+TraceClassLoading TestClassLoad

可以看到输出了一大堆log,把其中相关的部分截取出来如下:(完整的log可以从附件下载)
[Loaded TestClassLoad from file:/D:/temp_code/test_java_classload/]
[Loaded A from file:/D:/temp_code/test_java_classload/]
[Loaded sun.reflect.NativeMethodAccessorImpl from shared objects file]
[Loaded sun.reflect.DelegatingMethodAccessorImpl from shared objects file]
Hello, 0
Hello, 1
Hello, 2
Hello, 3
Hello, 4
Hello, 5
Hello, 6
Hello, 7
Hello, 8
Hello, 9
Hello, 10
Hello, 11
Hello, 12
Hello, 13
Hello, 14
[Loaded sun.reflect.ClassFileConstants from shared objects file]
[Loaded sun.reflect.AccessorGenerator from shared objects file]
[Loaded sun.reflect.MethodAccessorGenerator from shared objects file]
[Loaded sun.reflect.ByteVectorFactory from shared objects file]
[Loaded sun.reflect.ByteVector from shared objects file]
[Loaded sun.reflect.ByteVectorImpl from shared objects file]
[Loaded sun.reflect.ClassFileAssembler from shared objects file]
[Loaded sun.reflect.UTF8 from shared objects file]
[Loaded java.lang.Void from shared objects file]
[Loaded sun.reflect.Label from shared objects file]
[Loaded sun.reflect.Label$PatchInfo from shared objects file]
[Loaded java.util.AbstractList$Itr from shared objects file]
[Loaded sun.reflect.MethodAccessorGenerator$1 from shared objects file]
[Loaded sun.reflect.ClassDefiner from shared objects file]
[Loaded sun.reflect.ClassDefiner$1 from shared objects file]
[Loaded sun.reflect.GeneratedMethodAccessor1 from __JVM_DefineClass__]
Hello, 15

可以看到前15次反射调用A.foo()方法并没有什么稀奇的地方,但在第16次反射调用时似乎有什么东西被触发了,导致JVM新加载了一堆类,其中就包括[Loaded sun.reflect.GeneratedMethodAccessor1 from __JVM_DefineClass__]这么一行。这是哪里来的呢?

先来看看JDK里Method.invoke()是怎么实现的。
java.lang.reflect.Method:
public final
    class Method extends AccessibleObject implements GenericDeclaration, 
                             Member {
    // ...
    
    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.)
    private Method              root;

    // ...
    
    public Object invoke(Object obj, Object... args)
            throws IllegalAccessException, IllegalArgumentException,
            InvocationTargetException
    {
        if (!override) {
            if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) {
                Class caller = Reflection.getCallerClass(1);
                Class targetClass = ((obj == null || !Modifier.isProtected(modifiers))
                                     ? clazz
                                     : obj.getClass());
                boolean cached;
                synchronized (this) {
                    cached = (securityCheckCache == caller)
                        && (securityCheckTargetClassCache == targetClass);
                }
                if (!cached) {
                    Reflection.ensureMemberAccess(caller, clazz, obj, modifiers);
                    synchronized (this) {
                    securityCheckCache = caller;
                    securityCheckTargetClassCache = targetClass;
                    }
                }
            }
        }
        if (methodAccessor == null) acquireMethodAccessor();
        return methodAccessor.invoke(obj, args);
    }
    
    // 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 void 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;
            return;
        }
        // Otherwise fabricate one and propagate it up to the root
        tmp = reflectionFactory.newMethodAccessor(this);
        setMethodAccessor(tmp);
    }
    
    // ...
}

可以看到Method.invoke()实际上并不是自己实现的反射调用逻辑,而是委托给sun.reflect.MethodAccessor来处理。
每个实际的Java方法只有一个对应的Method对象作为root,。这个root是不会暴露给用户的,而是每次在通过反射获取Method对象时新创建Method对象把root包装起来再给用户。在第一次调用一个实际Java方法对应得Method对象的invoke()方法之前,实现调用逻辑的MethodAccessor对象还没创建;等第一次调用时才新创建MethodAccessor并更新给root,然后调用MethodAccessor.invoke()真正完成反射调用。

那么MethodAccessor是啥呢?
sun.reflect.MethodAccessor:
public interface MethodAccessor {
    /** Matches specification in {@link java.lang.reflect.Method} */
    public Object invoke(Object obj, Object[] args)
        throws IllegalArgumentException, InvocationTargetException;
}

可以看到它只是一个单方法接口,其invoke()方法与Method.invoke()的对应。

创建MethodAccessor实例的是ReflectionFactory。
sun.reflect.ReflectionFactory:
public class ReflectionFactory {
    
    private static boolean initted = false;
    
    // ...

    //
    // "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;
    
    // ...
    
    /** 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() {
                public Object 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 (RuntimeException) 
                                new RuntimeException("Unable to parse property sun.reflect.inflationThreshold").
                                    initCause(e);
                        }
                    }

                    initted = true;
                    return null;
                }
            });
    }
    
    // ...
    
    public MethodAccessor newMethodAccessor(Method method) {
        checkInitted();

        if (noInflation) {
            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;
        }
    }
}

这里就可以看到有趣的地方了。如注释所述,实际的MethodAccessor实现有两个版本,一个是Java实现的,另一个是native code实现的。Java实现的版本在初始化时需要较多时间,但长久来说性能较好;native版本正好相反,启动时相对较快,但运行时间长了之后速度就比不过Java版了。这是HotSpot的优化方式带来的性能特性,同时也是许多虚拟机的共同点:跨越native边界会对优化有阻碍作用,它就像个黑箱一样让虚拟机难以分析也将其内联,于是运行时间长了之后反而是托管版本的代码更快些。
为了权衡两个版本的性能,Sun的JDK使用了“inflation”的技巧:让Java方法在被反射调用时,开头若干次使用native版,等反射调用次数超过阈值时则生成一个专用的MethodAccessor实现类,生成其中的invoke()方法的字节码,以后对该Java方法的反射调用就会使用Java版。
Sun的JDK是从1.4系开始采用这种优化的,主要作者是Ken Russell

上面看到了ReflectionFactory.newMethodAccessor()生产MethodAccessor的逻辑,在“开头若干次”时用到的DelegatingMethodAccessorImpl代码如下:
sun.reflect.DelegatingMethodAccessorImpl:
/** 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;
    }
}

这是一个间接层,方便在native与Java版的MethodAccessor之间实现切换。

然后下面就是native版MethodAccessor的Java一侧的声明:
sun.reflect.NativeMethodAccessorImpl:
/** Used only for the first few invocations of a Method; afterward,
    switches to bytecode-based implementation */

class NativeMethodAccessorImpl extends MethodAccessorImpl {
    private Method method;
    private DelegatingMethodAccessorImpl parent;
    private int numInvocations;

    NativeMethodAccessorImpl(Method method) {
        this.method = method;
    }    

    public Object invoke(Object obj, Object[] args)
        throws IllegalArgumentException, InvocationTargetException
    {
        if (++numInvocations > ReflectionFactory.inflationThreshold()) {
            MethodAccessorImpl acc = (MethodAccessorImpl)
                new MethodAccessorGenerator().
                    generateMethod(method.getDeclaringClass(),
                                   method.getName(),
                                   method.getParameterTypes(),
                                   method.getReturnType(),
                                   method.getExceptionTypes(),
                                   method.getModifiers());
            parent.setDelegate(acc);
        }
        
        return invoke0(method, obj, args);
    }

    void setParent(DelegatingMethodAccessorImpl parent) {
        this.parent = parent;
    }

    private static native Object invoke0(Method m, Object obj, Object[] args);
}

每次NativeMethodAccessorImpl.invoke()方法被调用时,都会增加一个调用次数计数器,看超过阈值没有;一旦超过,则调用MethodAccessorGenerator.generateMethod()来生成Java版的MethodAccessor的实现类,并且改变DelegatingMethodAccessorImpl所引用的MethodAccessor为Java版。后续经由DelegatingMethodAccessorImpl.invoke()调用到的就是Java版的实现了。

注意到关键的invoke0()方法是个native方法。它在HotSpot VM里是由JVM_InvokeMethod()函数所支持的:
JNIEXPORT jobject JNICALL Java_sun_reflect_NativeMethodAccessorImpl_invoke0
(JNIEnv *env, jclass unused, jobject m, jobject obj, jobjectArray args)
{
    return JVM_InvokeMethod(env, m, obj, args);
}

JVM_ENTRY(jobject, JVM_InvokeMethod(JNIEnv *env, jobject method, jobject obj, jobjectArray args0))
  JVMWrapper("JVM_InvokeMethod");
  Handle method_handle;
  if (thread->stack_available((address) &method_handle) >= JVMInvokeMethodSlack) {
    method_handle = Handle(THREAD, JNIHandles::resolve(method));
    Handle receiver(THREAD, JNIHandles::resolve(obj));
    objArrayHandle args(THREAD, objArrayOop(JNIHandles::resolve(args0)));
    oop result = Reflection::invoke_method(method_handle(), receiver, args, CHECK_NULL);
    jobject res = JNIHandles::make_local(env, result);
    if (JvmtiExport::should_post_vm_object_alloc()) {
      oop ret_type = java_lang_reflect_Method::return_type(method_handle());
      assert(ret_type != NULL, "sanity check: ret_type oop must not be NULL!");
      if (java_lang_Class::is_primitive(ret_type)) {
        // Only for primitive type vm allocates memory for java object.
        // See box() method.
        JvmtiExport::post_vm_object_alloc(JavaThread::current(), result);
      }
    }
    return res;
  } else {
    THROW_0(vmSymbols::java_lang_StackOverflowError());
  }
JVM_END

其中的关键又是Reflection::invoke_method():
// This would be nicer if, say, java.lang.reflect.Method was a subclass
// of java.lang.reflect.Constructor

oop Reflection::invoke_method(oop method_mirror, Handle receiver, objArrayHandle args, TRAPS) {
  oop mirror             = java_lang_reflect_Method::clazz(method_mirror);
  int slot               = java_lang_reflect_Method::slot(method_mirror);
  bool override          = java_lang_reflect_Method::override(method_mirror) != 0;
  objArrayHandle ptypes(THREAD, objArrayOop(java_lang_reflect_Method::parameter_types(method_mirror)));

  oop return_type_mirror = java_lang_reflect_Method::return_type(method_mirror);
  BasicType rtype;
  if (java_lang_Class::is_primitive(return_type_mirror)) {
    rtype = basic_type_mirror_to_basic_type(return_type_mirror, CHECK_NULL);
  } else {
    rtype = T_OBJECT;
  }

  instanceKlassHandle klass(THREAD, java_lang_Class::as_klassOop(mirror));
  methodOop m = klass->method_with_idnum(slot);
  if (m == NULL) {
    THROW_MSG_0(vmSymbols::java_lang_InternalError(), "invoke");
  }
  methodHandle method(THREAD, m);

  return invoke(klass, method, receiver, override, ptypes, rtype, args, true, THREAD);
}

再下去就深入到HotSpot VM的内部了,本文就在这里打住吧。有同学有兴趣深究的话以后可以再写一篇讨论native版的实现。

回到Java的一侧。MethodAccessorGenerator长啥样呢?由于代码太长,这里就不完整贴了,有兴趣的可以到OpenJDK 6的Mercurial仓库看:OpenJDK 6 build 17的MethodAccessorGenerator。它的基本工作就是在内存里生成新的专用Java类,并将其加载。就贴这么一个方法:
private static synchronized String generateName(boolean isConstructor,
                                                boolean forSerialization)
{
    if (isConstructor) {
        if (forSerialization) {
            int num = ++serializationConstructorSymnum;
            return "sun/reflect/GeneratedSerializationConstructorAccessor" + num;
        } else {
            int num = ++constructorSymnum;
            return "sun/reflect/GeneratedConstructorAccessor" + num;
        }
    } else {
        int num = ++methodSymnum;
        return "sun/reflect/GeneratedMethodAccessor" + num;
    }
}

去阅读源码的话,可以看到MethodAccessorGenerator是如何一点点把Java版的MethodAccessor实现类生产出来的。也可以看到GeneratedMethodAccessor+数字这种名字是从哪里来的了,就在上面的generateName()方法里。
对本文开头的例子的A.foo(),生成的Java版MethodAccessor大致如下:
package sun.reflect;

public class GeneratedMethodAccessor1 extends MethodAccessorImpl {    
    public GeneratedMethodAccessor1() {
        super();
    }
    
    public Object invoke(Object obj, Object[] args)   
        throws IllegalArgumentException, InvocationTargetException {
        // prepare the target and parameters
        if (obj == null) throw new NullPointerException();
        try {
            A target = (A) obj;
            if (args.length != 1) throw new IllegalArgumentException();
            String arg0 = (String) args[0];
        } catch (ClassCastException e) {
            throw new IllegalArgumentException(e.toString());
        } catch (NullPointerException e) {
            throw new IllegalArgumentException(e.toString());
        }
        // make the invocation
        try {
            target.foo(arg0);
        } catch (Throwable t) {
            throw new InvocationTargetException(t);
        }
    }
}

就反射调用而言,这个invoke()方法非常干净(然而就“正常调用”而言这额外开销还是明显的)。注意到参数数组被拆开了,把每个参数都恢复到原本没有被Object[]包装前的样子,然后对目标方法做正常的invokevirtual调用。由于在生成代码时已经循环遍历过参数类型的数组,生成出来的代码里就不再包含循环了。
当该反射调用成为热点时,它甚至可以被内联到靠近Method.invoke()的一侧,大大降低了反射调用的开销。而native版的反射调用则无法被有效内联,因而调用开销无法随程序的运行而降低。
虽说Sun的JDK这种实现方式使得反射调用方法成本比以前降低了很多,但Method.invoke()本身要用数组包装参数;而且每次调用都必须检查方法的可见性(在Method.invoke()里),也必须检查每个实际参数与形式参数的类型匹配性(在NativeMethodAccessorImpl.invoke0()里或者生成的Java版MethodAccessor.invoke()里);而且Method.invoke()就像是个独木桥一样,各处的反射调用都要挤过去,在调用点上收集到的类型信息就会很乱,影响内联程序的判断,使得Method.invoke()自身难以被内联到调用方。
相比之下JDK 7里新的MethodHandle则更有潜力,在其功能完全实现后能达到比普通反射调用方法更高的性能。在使用MethodHandle来做反射调用时,MethodHandle.invoke()的形式参数与返回值类型都是准确的,所以只需要在链接方法的时候才需要检查类型的匹配性,而不必在每次调用时都检查。而且MethodHandle是不可变值,在创建后其内部状态就不会再改变了;JVM可以利用这个知识而放心的对它做激进优化,例如将实际的调用目标内联到做反射调用的一侧。

到本来Java的安全机制使得不同类之间不是任意信息都可见,但Sun的JDK里开了个口,有一个标记类专门用于开后门:
package sun.reflect;

/** <P> MagicAccessorImpl (named for parity with FieldAccessorImpl and
    others, not because it actually implements an interface) is a
    marker class in the hierarchy. All subclasses of this class are
    "magically" granted access by the VM to otherwise inaccessible
    fields and methods of other classes. It is used to hold the code
    for dynamically-generated FieldAccessorImpl and MethodAccessorImpl
    subclasses. (Use of the word "unsafe" was avoided in this class's
    name to avoid confusion with {@link sun.misc.Unsafe}.) </P>

    <P> The bug fix for 4486457 also necessitated disabling
    verification for this class and all subclasses, as opposed to just
    SerializationConstructorAccessorImpl and subclasses, to avoid
    having to indicate to the VM which of these dynamically-generated
    stub classes were known to be able to pass the verifier. </P>

    <P> Do not change the name of this class without also changing the
    VM's code. </P> */

class MagicAccessorImpl {
}


那个"__JVM_DefineClass__"的来源是这里:
src/share/vm/prims/jvm.cpp
// common code for JVM_DefineClass() and JVM_DefineClassWithSource()
// and JVM_DefineClassWithSourceCond()
static jclass jvm_define_class_common(JNIEnv *env, const char *name,
                                      jobject loader, const jbyte *buf,
                                      jsize len, jobject pd, const char *source,
                                      jboolean verify, TRAPS) {
  if (source == NULL)  source = "__JVM_DefineClass__";


OK,本文就记到这里吧。希望对问答频道提问那位同学有帮助。

Have fun ^_^

============================================================================

P.S. log里的"shared objects file"其实也是个有趣的话题。有机会的话也可以写写。
9
1
分享到:
评论
15 楼 ljh8088 2014-11-24  
最近在尝试反射优化,又涨姿势了。
14 楼 bluesleaf 2011-10-29  
引用
P.S. log里的"shared objects file"其实也是个有趣的话题。有机会的话也可以写写。
求该话题~
13 楼 RednaxelaFX 2011-04-20  
sky_sz 写道
我的项目中出现java.lang.ClassNotFoundException: sun.reflect.GeneratedMethodAccessor27为什么啊?

orz...还有这种事。请问有stack trace么?
12 楼 sky_sz 2011-04-20  
我的项目中出现java.lang.ClassNotFoundException: sun.reflect.GeneratedMethodAccessor27为什么啊?
11 楼 raintung.li 2011-03-29  
具体的反射为什么性能急剧下降,请看http://blog.csdn.net/raintungli/archive/2011/03/29/6286701.aspx
10 楼 buaasss 2010-09-10  
嗯,现在知道了,感谢感谢:)
9 楼 RednaxelaFX 2010-09-06  
buaasss 写道
constructorAccessor.newInstance(initargs);这行代码
继续调用了一些方法,最后进入了MethodAccessorGenerator类的generate方法,又走到了ClassDefiner.defineClass(generatedName,bytes,0,bytes.length,declaringClass.getClassLoader()).newInstance()这一步,这难道不是一个死循环吗?

呵呵呵呵,仔细点阅读代码的话就不会有这个疑问了。
来,看这里:
sun.reflect.ReflectionFactory
// Bootstrapping issue: since we use Class.newInstance() in
// the ConstructorAccessor generation process, we have to
// break the cycle here.

在为ConstructorAccessorImpl的子类的构造器创建constructor accessor的时候,并不像一般情况创建出包着NativeConstructorAccessorImpl的DelegatingConstructorAccessorImpl,而是创建出一个BootstrapConstructorAccessorImpl,后者不会再创建新的constructor accessor,就破掉循环了
8 楼 buaasss 2010-09-06  
先谢谢你的解答!:)   但是我还是没有弄明白。
ClassDefiner.defineClass(generatedName,bytes,0,bytes.length,declaringClass.getClassLoader())返回的是java.lang.Class类型的对象,它的newInstance()方法本质是调用java.lang.reflect.Constructor类中的public T newInstance(Object ... initargs)方法,源代码如下:
public T newInstance(Object ... initargs)
	throws InstantiationException, IllegalAccessException,
               IllegalArgumentException, InvocationTargetException
    {
        if (!override) {
            if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) {
                Class caller = Reflection.getCallerClass(2);
                if (securityCheckCache != caller) {
                    Reflection.ensureMemberAccess(caller, clazz, null, modifiers);
                    securityCheckCache = caller;
                }
            }
        }
	if ((clazz.getModifiers() & Modifier.ENUM) != 0) 
	    throw new IllegalArgumentException("Cannot reflectively create enum objects");
        if (constructorAccessor == null) acquireConstructorAccessor();
        return (T) constructorAccessor.newInstance(initargs);
    }

从中可以看到调用了constructorAccessor.newInstance(initargs)方法;
constructorAccessor是从acquireConstructorAccessor()得到的
private void 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;
            return;
        }
        // Otherwise fabricate one and propagate it up to the root
        tmp = reflectionFactory.newConstructorAccessor(this);
        setConstructorAccessor(tmp);
    }

constructorAccessor.newInstance(initargs);这行代码
继续调用了一些方法,最后进入了MethodAccessorGenerator类的generate方法,又走到了ClassDefiner.defineClass(generatedName,bytes,0,bytes.length,declaringClass.getClassLoader()).newInstance()这一步,这难道不是一个死循环吗?
7 楼 RednaxelaFX 2010-09-02  
buaasss 写道
里面的这段代码
ClassDefiner.defineClass(generatedName,bytes,0,bytes.length,declaringClass.getClassLoader()).newInstance();
又用newInstance()这个方法,这不是会导致死循环?
请问java reflection到底是怎样work的?

结合前面的例子,对A.foo()的反射调用做优化。
generate()方法只是先根据需要造出了名为GeneratedMethodAccessor1的类,然后调用该类的newInstance()等同于new GeneratedMethodAccessor1(),这里是反射调用了一个新的类的构造器,与原本的A.foo()并没有关系。
不知道什么地方看起来会有死循环呢?
6 楼 buaasss 2010-08-29  
private MagicAccessorImpl generate(final Class declaringClass,
  127                                          String name,
  128                                          Class[] parameterTypes,
  129                                          Class   returnType,
  130                                          Class[] checkedExceptions,
  131                                          int modifiers,
  132                                          boolean isConstructor,
  133                                          boolean forSerialization,
  134                                          Class serializationTargetClass)
  135       {
  136           ByteVector vec = ByteVectorFactory.create();
  137           asm = new ClassFileAssembler(vec);
  138           this.declaringClass = declaringClass;
  139           this.parameterTypes = parameterTypes;
  140           this.returnType = returnType;
  141           this.modifiers = modifiers;
  142           this.isConstructor = isConstructor;
  143           this.forSerialization = forSerialization;
  144   
  145           asm.emitMagicAndVersion();
  146   
  147           // Constant pool entries:
  148           // ( * = Boxing information: optional)
  149           // (+  = Shared entries provided by AccessorGenerator)
  150           // (^  = Only present if generating SerializationConstructorAccessor)
  151           //     [UTF-8] [This class's name]
  152           //     [CONSTANT_Class_info] for above
  153           //     [UTF-8] "sun/reflect/{MethodAccessorImpl,ConstructorAccessorImpl,SerializationConstructorAccessorImpl}"
  154           //     [CONSTANT_Class_info] for above
  155           //     [UTF-8] [Target class's name]
  156           //     [CONSTANT_Class_info] for above
  157           // ^   [UTF-8] [Serialization: Class's name in which to invoke constructor]
  158           // ^   [CONSTANT_Class_info] for above
  159           //     [UTF-8] target method or constructor name
  160           //     [UTF-8] target method or constructor signature
  161           //     [CONSTANT_NameAndType_info] for above
  162           //     [CONSTANT_Methodref_info or CONSTANT_InterfaceMethodref_info] for target method
  163           //     [UTF-8] "invoke" or "newInstance"
  164           //     [UTF-8] invoke or newInstance descriptor
  165           //     [UTF-8] descriptor for type of non-primitive parameter 1
  166           //     [CONSTANT_Class_info] for type of non-primitive parameter 1
  167           //     ...
  168           //     [UTF-8] descriptor for type of non-primitive parameter n
  169           //     [CONSTANT_Class_info] for type of non-primitive parameter n
  170           // +   [UTF-8] "java/lang/Exception"
  171           // +   [CONSTANT_Class_info] for above
  172           // +   [UTF-8] "java/lang/ClassCastException"
  173           // +   [CONSTANT_Class_info] for above
  174           // +   [UTF-8] "java/lang/NullPointerException"
  175           // +   [CONSTANT_Class_info] for above
  176           // +   [UTF-8] "java/lang/IllegalArgumentException"
  177           // +   [CONSTANT_Class_info] for above
  178           // +   [UTF-8] "java/lang/InvocationTargetException"
  179           // +   [CONSTANT_Class_info] for above
  180           // +   [UTF-8] "<init>"
  181           // +   [UTF-8] "()V"
  182           // +   [CONSTANT_NameAndType_info] for above
  183           // +   [CONSTANT_Methodref_info] for NullPointerException's constructor
  184           // +   [CONSTANT_Methodref_info] for IllegalArgumentException's constructor
  185           // +   [UTF-8] "(Ljava/lang/String;)V"
  186           // +   [CONSTANT_NameAndType_info] for "<init>(Ljava/lang/String;)V"
  187           // +   [CONSTANT_Methodref_info] for IllegalArgumentException's constructor taking a String
  188           // +   [UTF-8] "(Ljava/lang/Throwable;)V"
  189           // +   [CONSTANT_NameAndType_info] for "<init>(Ljava/lang/Throwable;)V"
  190           // +   [CONSTANT_Methodref_info] for InvocationTargetException's constructor
  191           // +   [CONSTANT_Methodref_info] for "super()"
  192           // +   [UTF-8] "java/lang/Object"
  193           // +   [CONSTANT_Class_info] for above
  194           // +   [UTF-8] "toString"
  195           // +   [UTF-8] "()Ljava/lang/String;"
  196           // +   [CONSTANT_NameAndType_info] for "toString()Ljava/lang/String;"
  197           // +   [CONSTANT_Methodref_info] for Object's toString method
  198           // +   [UTF-8] "Code"
  199           // +   [UTF-8] "Exceptions"
  200           //  *  [UTF-8] "java/lang/Boolean"
  201           //  *  [CONSTANT_Class_info] for above
  202           //  *  [UTF-8] "(Z)V"
  203           //  *  [CONSTANT_NameAndType_info] for above
  204           //  *  [CONSTANT_Methodref_info] for above
  205           //  *  [UTF-8] "booleanValue"
  206           //  *  [UTF-8] "()Z"
  207           //  *  [CONSTANT_NameAndType_info] for above
  208           //  *  [CONSTANT_Methodref_info] for above
  209           //  *  [UTF-8] "java/lang/Byte"
  210           //  *  [CONSTANT_Class_info] for above
  211           //  *  [UTF-8] "(B)V"
  212           //  *  [CONSTANT_NameAndType_info] for above
  213           //  *  [CONSTANT_Methodref_info] for above
  214           //  *  [UTF-8] "byteValue"
  215           //  *  [UTF-8] "()B"
  216           //  *  [CONSTANT_NameAndType_info] for above
  217           //  *  [CONSTANT_Methodref_info] for above
  218           //  *  [UTF-8] "java/lang/Character"
  219           //  *  [CONSTANT_Class_info] for above
  220           //  *  [UTF-8] "(C)V"
  221           //  *  [CONSTANT_NameAndType_info] for above
  222           //  *  [CONSTANT_Methodref_info] for above
  223           //  *  [UTF-8] "charValue"
  224           //  *  [UTF-8] "()C"
  225           //  *  [CONSTANT_NameAndType_info] for above
  226           //  *  [CONSTANT_Methodref_info] for above
  227           //  *  [UTF-8] "java/lang/Double"
  228           //  *  [CONSTANT_Class_info] for above
  229           //  *  [UTF-8] "(D)V"
  230           //  *  [CONSTANT_NameAndType_info] for above
  231           //  *  [CONSTANT_Methodref_info] for above
  232           //  *  [UTF-8] "doubleValue"
  233           //  *  [UTF-8] "()D"
  234           //  *  [CONSTANT_NameAndType_info] for above
  235           //  *  [CONSTANT_Methodref_info] for above
  236           //  *  [UTF-8] "java/lang/Float"
  237           //  *  [CONSTANT_Class_info] for above
  238           //  *  [UTF-8] "(F)V"
  239           //  *  [CONSTANT_NameAndType_info] for above
  240           //  *  [CONSTANT_Methodref_info] for above
  241           //  *  [UTF-8] "floatValue"
  242           //  *  [UTF-8] "()F"
  243           //  *  [CONSTANT_NameAndType_info] for above
  244           //  *  [CONSTANT_Methodref_info] for above
  245           //  *  [UTF-8] "java/lang/Integer"
  246           //  *  [CONSTANT_Class_info] for above
  247           //  *  [UTF-8] "(I)V"
  248           //  *  [CONSTANT_NameAndType_info] for above
  249           //  *  [CONSTANT_Methodref_info] for above
  250           //  *  [UTF-8] "intValue"
  251           //  *  [UTF-8] "()I"
  252           //  *  [CONSTANT_NameAndType_info] for above
  253           //  *  [CONSTANT_Methodref_info] for above
  254           //  *  [UTF-8] "java/lang/Long"
  255           //  *  [CONSTANT_Class_info] for above
  256           //  *  [UTF-8] "(J)V"
  257           //  *  [CONSTANT_NameAndType_info] for above
  258           //  *  [CONSTANT_Methodref_info] for above
  259           //  *  [UTF-8] "longValue"
  260           //  *  [UTF-8] "()J"
  261           //  *  [CONSTANT_NameAndType_info] for above
  262           //  *  [CONSTANT_Methodref_info] for above
  263           //  *  [UTF-8] "java/lang/Short"
  264           //  *  [CONSTANT_Class_info] for above
  265           //  *  [UTF-8] "(S)V"
  266           //  *  [CONSTANT_NameAndType_info] for above
  267           //  *  [CONSTANT_Methodref_info] for above
  268           //  *  [UTF-8] "shortValue"
  269           //  *  [UTF-8] "()S"
  270           //  *  [CONSTANT_NameAndType_info] for above
  271           //  *  [CONSTANT_Methodref_info] for above
  272   
  273           short numCPEntries = NUM_BASE_CPOOL_ENTRIES + NUM_COMMON_CPOOL_ENTRIES;
  274           boolean usesPrimitives = usesPrimitiveTypes();
  275           if (usesPrimitives) {
  276               numCPEntries += NUM_BOXING_CPOOL_ENTRIES;
  277           }
  278           if (forSerialization) {
  279               numCPEntries += NUM_SERIALIZATION_CPOOL_ENTRIES;
  280           }
  281   
  282           // Add in variable-length number of entries to be able to describe
  283           // non-primitive parameter types and checked exceptions.
  284           numCPEntries += (short) (2 * numNonPrimitiveParameterTypes());
  285   
  286           asm.emitShort(add(numCPEntries, S1));
  287   
  288           final String generatedName = generateName(isConstructor, forSerialization);
  289           asm.emitConstantPoolUTF8(generatedName);
  290           asm.emitConstantPoolClass(asm.cpi());
  291           thisClass = asm.cpi();
  292           if (isConstructor) {
  293               if (forSerialization) {
  294                   asm.emitConstantPoolUTF8
  295                       ("sun/reflect/SerializationConstructorAccessorImpl");
  296               } else {
  297                   asm.emitConstantPoolUTF8("sun/reflect/ConstructorAccessorImpl");
  298               }
  299           } else {
  300               asm.emitConstantPoolUTF8("sun/reflect/MethodAccessorImpl");
  301           }
  302           asm.emitConstantPoolClass(asm.cpi());
  303           superClass = asm.cpi();
  304           asm.emitConstantPoolUTF8(getClassName(declaringClass, false));
  305           asm.emitConstantPoolClass(asm.cpi());
  306           targetClass = asm.cpi();
  307           short serializationTargetClassIdx = (short) 0;
  308           if (forSerialization) {
  309               asm.emitConstantPoolUTF8(getClassName(serializationTargetClass, false));
  310               asm.emitConstantPoolClass(asm.cpi());
  311               serializationTargetClassIdx = asm.cpi();
  312           }
  313           asm.emitConstantPoolUTF8(name);
  314           asm.emitConstantPoolUTF8(buildInternalSignature());
  315           asm.emitConstantPoolNameAndType(sub(asm.cpi(), S1), asm.cpi());
  316           if (isInterface()) {
  317               asm.emitConstantPoolInterfaceMethodref(targetClass, asm.cpi());
  318           } else {
  319               if (forSerialization) {
  320                   asm.emitConstantPoolMethodref(serializationTargetClassIdx, asm.cpi());
  321               } else {
  322                   asm.emitConstantPoolMethodref(targetClass, asm.cpi());
  323               }
  324           }
  325           targetMethodRef = asm.cpi();
  326           if (isConstructor) {
  327               asm.emitConstantPoolUTF8("newInstance");
  328           } else {
  329               asm.emitConstantPoolUTF8("invoke");
  330           }
  331           invokeIdx = asm.cpi();
  332           if (isConstructor) {
  333               asm.emitConstantPoolUTF8("([Ljava/lang/Object;)Ljava/lang/Object;");
  334           } else {
  335               asm.emitConstantPoolUTF8
  336                   ("(Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;");
  337           }
  338           invokeDescriptorIdx = asm.cpi();
  339   
  340           // Output class information for non-primitive parameter types
  341           nonPrimitiveParametersBaseIdx = add(asm.cpi(), S2);
  342           for (int i = 0; i < parameterTypes.length; i++) {
  343               Class c = parameterTypes[i];
  344               if (!isPrimitive(c)) {
  345                   asm.emitConstantPoolUTF8(getClassName(c, false));
  346                   asm.emitConstantPoolClass(asm.cpi());
  347               }
  348           }
  349   
  350           // Entries common to FieldAccessor, MethodAccessor and ConstructorAccessor
  351           emitCommonConstantPoolEntries();
  352   
  353           // Boxing entries
  354           if (usesPrimitives) {
  355               emitBoxingContantPoolEntries();
  356           }
  357   
  358           if (asm.cpi() != numCPEntries) {
  359               throw new InternalError("Adjust this code (cpi = " + asm.cpi() +
  360                                       ", numCPEntries = " + numCPEntries + ")");
  361           }
  362   
  363           // Access flags
  364           asm.emitShort(ACC_PUBLIC);
  365   
  366           // This class
  367           asm.emitShort(thisClass);
  368   
  369           // Superclass
  370           asm.emitShort(superClass);
  371   
  372           // Interfaces count and interfaces
  373           asm.emitShort(S0);
  374   
  375           // Fields count and fields
  376           asm.emitShort(S0);
  377   
  378           // Methods count and methods
  379           asm.emitShort(NUM_METHODS);
  380   
  381           emitConstructor();
  382           emitInvoke();
  383   
  384           // Additional attributes (none)
  385           asm.emitShort(S0);
  386   
  387           // Load class
  388           vec.trim();
  389           final byte[] bytes = vec.getData();
  390           // Note: the class loader is the only thing that really matters
  391           // here -- it's important to get the generated code into the
  392           // same namespace as the target class. Since the generated code
  393           // is privileged anyway, the protection domain probably doesn't
  394           // matter.
  395           return (MagicAccessorImpl)
  396               AccessController.doPrivileged(new PrivilegedAction() {
  397                       public Object run() {
  398                           try {
  399                               return ClassDefiner.defineClass
  400                                   (generatedName,
  401                                    bytes,
  402                                    0,
  403                                    bytes.length,
  404                                    declaringClass.getClassLoader()).newInstance();
  405                           } catch (InstantiationException e) {
  406                               throw (InternalError)
  407                                   new InternalError().initCause(e);
  408                           } catch (IllegalAccessException e) {
  409                               throw (InternalError)
  410                                   new InternalError().initCause(e);
  411                           }
  412                       }
  413                   });
  414       }

里面的这段代码
ClassDefiner.defineClass(generatedName,bytes,0,bytes.length,declaringClass.getClassLoader()).newInstance();
又用newInstance()这个方法,这不是会导致死循环?
请问java reflection到底是怎样work的?
5 楼 ZZX19880809 2010-06-21  
虽然看不太懂,但是还是有点收获的,好文章
4 楼 lgdlgd 2009-12-24  
刚想了解一下关反射性能,就找到这了,好文章!
3 楼 iaimstar 2009-12-15  
好吧
。。brs满赛,
2 楼 RednaxelaFX 2009-12-15  
iaimstar 写道
你的头像导致我看了半天一直以为进了ns的blog。。

蓝光满塞,BRS满塞~
1 楼 iaimstar 2009-12-15  
你的头像导致我看了半天一直以为进了ns的blog。。

相关推荐

    使用Java反射实现一个简单的日志记录器.txt

    这段代码实现了一个简单的日志记录器LoggerUtil,其中使用了Java的反射机制。在类的构造方法中,创建了一个Logger对象来记录日志信息。然后定义了两个静态方法logInfo()和logError(),分别用于记录不同级别的日志。...

    log4Net详解(共2讲)

    国讯OA的一个强大之处就是在于它可以让系统在运作的过程中自动触发请求,并且还可以根据前一个请求的实际状况对下一个触发的请求进行智能选择。 6、分支选择流 根据上一步的选择,选择不同的分支进行流程执行。如...

    IronPython应用框架(整合WinAPI/WatiN/SshScp/Dock/dll插件反射等)

    Scripts目录下含多个应用例子,窗体测试(WinAPI)、Web测试(WatiN)、WebAPI测试、LinuxSSH和SCP等。 3)命令行调用支持: IronPythonTest.exe -log logPath ;执行命令后输出日志全路径,默认Log/ -py filePath ;...

    Python应用测试框架(整合WinAPI&WatiN;&Dll;反射&Linux;等)

    Scripts目录下含多个应用例子,窗体测试(WinAPI)、Web测试(WatiN)、WebAPI测试、LinuxSSH和SCP等。 3)命令行调用支持: IronPythonTest.exe -log logPath ;执行命令后输出日志全路径,默认Log/ -py filePath ;...

    廖雪峰 Java 教程.doc

    第一个Java程序 Java代码助手 使用IDE 使用IDE练习插件 Java程序基础 Java程序基本结构 变量和数据类型 整数运算 浮点数运算 布尔运算 字符和字符串 数组类型 流程控制 输入和输出 if判断 switch多重...

    IronPython应用框架AuoPyTest(WinAPI/WatiN/SshScp/Dll插件反射等)

    本工具提供一个python和C#动态支持的框架(内置对象为Ipy_this): (a) Scripts目录下含多个应用例子,窗体测试(WinAPI)、Web测试(WatiN)、WebAPI测试、LinuxSSH和SCP等。 (b) 自动化测试、自动任务均可以通过...

    IronPython应用框架AutoPyTest(WinAPI/WatiN/SshScp/Dll插件反射等)

    本工具提供一个python和C#动态支持的框架(内置对象为Ipy_this): (a) Scripts目录下含多个应用例子,窗体测试(WinAPI)、Web测试(WatiN)、WebAPI测试、LinuxSSH和SCP等。 (b) 自动化测试、自动任务均可以通过...

    5分钟快速看懂ES6中的反射与代理

    它的本质就是一个对象(JS内置的),里边有一些函数可供调用。 为什要叫做反射? 因为它能做的事情,和其他语言(Java、python等)中的反射很相似。其他语言中都叫做反射了,那就共鸣(降低学习成本?) 反射能做...

    asp.net知识库

    通过反射调用類的方法,屬性,字段,索引器(2種方法) ASP.NET: State Server Gems 完整的动态加载/卸载程序集的解决方案 从NUnit中理解.NET自定义属性的应用(转载) 如何在.NET中实现脚本引擎 (CodeDom篇) .NET的插件...

    +Flex+集成到+Java+EE+应用程序的最佳实践(完整源代码)

    资源简介现有的 Java EE 应用 假定我们已经拥有了一个管理雇员...可以通过 ActionScript 编码创建该对象,也可以直接在 MXML 中定义一个 RemoteObject 对象,并列出其所有的方法: 清单 13. 定义 flexServiceRO ...

    将 Flex 集成到 Java EE 应用程序的最佳实践(完整源代码)

    现有的 Java EE 应用 假定我们已经拥有了一个管理雇员信息的...可以通过 ActionScript 编码创建该对象,也可以直接在 MXML 中定义一个 RemoteObject 对象,并列出其所有的方法: 清单 13. 定义 flexServiceRO ...

    C#常用功能类库(20111202最新整理)

    (利用反射(需要制定Dll文件、命名空间)实现实例化、调用) 3个重载方法 三、SR.ShareFunc.RemoteDeskTop 6.SR.ShareFunc.RemoteDeskTop.ucRemoteDeskTop 远程桌面的监控界面控件 原理:利用System.Runtime....

    最牛.Net公共类库,.net 开发类库Cmn

    内含制作的帮助文档,绝对是.Net开发的得力助手! 大致内容包括 DB 数据库操作类 Caches 缓存管理 Cookies Cookies操作类,支持读写...常用类方法 GeneralHelper.cs 开发常用方法,由于多个存在,项目开发新建单独调用

    java代码优化

    1、与log4j有关的性能问题 2、与字符串有关的性能问题 3、与时间有关的性能问题 4、与for循环有关的性能问题 5、与集合类有关的性能...9、与反射调用有关的性能问题 10、缓存使用的性能问题 11、并发锁带来的性能问题

    Spring.net框架

    通过ConfigHandler的解析,我们最终得到一个ConfigInfo实例,Factory就是根据这个实例中所包含的配置信息,利用反射技术对所需对象生成并组装的。SayHelloFactory的代码如下: using System; using System.IO; using...

    grpcox:像邮递员一样,但对于gRPC

    保存已建立的连接,并将其重用于下一次调用/请求(也可以关闭/重新启动连接) 安装 码头工人 docker pull gusaul/grpcox:latest 然后跑 docker run -p 6969:6969 -v {ABSOLUTE_PATH_TO_LOG}/log:/log -d gusaul/...

    aspnet公共类cs文件.rar

    由马丁·米勒http://msdn.microsoft.com/en-us/library/ms996492.aspx提供一个简单的方法打印工作的一个RichTextBox一个帮手(ExRichTextBoxPrintHelper.cs) 显示,隐藏或关闭动画形式。(FormAnimator.cs) 对窗体进行...

    亮剑.NET深入体验与实战精要2

    因pdf的容量过大分4个压缩包打包,还有一个源码另外下载。 《.NET深入体验与实战精要》作者身为从事.NET一线开发的资深开发专家,常年耕耘技术博客,惠及无数.NET新知。此次将长期的思考、感悟,多年的系统开发、...

    亮剑.NET深入体验与实战精要3

    因pdf的容量过大分4个压缩包打包,还有一个源码另外下载。 《.NET深入体验与实战精要》作者身为从事.NET一线开发的资深开发专家,常年耕耘技术博客,惠及无数.NET新知。此次将长期的思考、感悟,多年的系统开发、...

Global site tag (gtag.js) - Google Analytics