Java 7语言规范很早就说:
“本规范不详细描述反射。”
我只是想知道:反射是如何在Java中实现的?
我不是在问它是如何使用的,而且我知道可能没有我想要的具体答案,但是任何信息都会非常感谢。
我在Stackoverflow上发现了这一点,类似于C#:How is reflection implemented in C#?的问题
发布于 2013-12-09 02:19:27
任何反射活动的主要入口点都是Class类。从中可以得到Method、Field、Class、Constructor和Annotation实例。
如果您查看源代码,您将注意到,要检索上面的任何内容,Java必须进行native调用。例如,
private native Field[] getDeclaredFields0(boolean publicOnly);
private native Method[] getDeclaredMethods0(boolean publicOnly);
private native Constructor<T>[] getDeclaredConstructors0(boolean publicOnly);
private native Class<?>[] getDeclaredClasses0();
private native byte[] getRawAnnotations(); // get converted to Annotation instances later实现是在本机C代码(和/或C++)中完成的。每个JDK可能不同,但如果源代码可用,并且您有耐心,您可以查找它。有关OpenJDK源代码的详细信息可以找到in this question。
https://stackoverflow.com/questions/20461851
复制相似问题