java中可以通过类的class属性得到该类的字节码文件对象?
ringa_lee
ringa_lee 2017-04-18 10:50:41
0
2
546


Class c1 = Student.class;
可是无论是Student类还是它继承的Object类都没有class属性呀?

ringa_lee
ringa_lee

ringa_lee

reply all(2)
巴扎黑

Thanks for the invitation.

First of all, correct an error. class is a keyword, not a static member in the Student class. <class-name>.class is a specific syntax in Java.

Secondly, to get the bytecode file of this class, you must go to the file system to find it, which is nothing more than methods like Student.class.getResource/getResourceAsStream, which is quite troublesome. It is recommended to look at bytecode open source projects such as asm and cglib.

Finally, bytecode is a relatively complex technology and should be used as a last resort, such as getting the Annotation in method parameters (refer to Spring Web MVC). Some technologies that seem to be related to bytecode can actually be bypassed. For example, interface-based AOP can be implemented using Proxy instead of bytecode.

大家讲道理

One theory I checked is that this is not a keyword, but a literal value. You can try typing "Student.class" directly in the IDE, and it will remind you that you need to assign this value to a variable. If we find new ones later, we will add them. As for class loading, you can refer to my blog Customized Class Loader - Reading from .class and .jar

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!