Home  >  Q&A  >  body text

java中可以通过类的class属性得到该类的字节码文件对象?


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

ringa_leeringa_lee2743 days ago549

reply all(2)I'll reply

  • 巴扎黑

    巴扎黑2017-04-18 10:52:41

    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.

    reply
    0
  • 大家讲道理

    大家讲道理2017-04-18 10:52:41

    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

    reply
    0
  • Cancelreply