Some methods contain a Compiled Code?

前端 未结 2 1864
被撕碎了的回忆
被撕碎了的回忆 2021-01-07 01:46

From going to the source of some classes I saw some methods contain a compiled code, they are looks like this before expanding:

public void someMethod(){

相关标签:
2条回答
  • 2021-01-07 02:03

    That is Java(or another JVM-compatible language, as per comment) as well.

    When you are given a jar file (such as rt.jar with the JDK/JRE), it is already compiled. High-level code such as:

    HashMap<Foo, Bar> baz=Quuz.getInstance(); //This is obviously fake
    

    is turned into low-level bytecode that is not human-readable.

    This bytecode contains metadata such as method argument types, and information needed for loading these classes and methods. It is needed for IDEs to offer syntax completion for libraries(such as jars you add to project classpaths) and more importantly for loading and using methods of the classes you get from these libraries.

    The "code" you see here is a summary, and a type of code called bytecode. Line by line, instructions like ldc seen in your example operate on small portions of a stack that powers the JVM at a time. This bytecode is fairly difficult to read and program in by hand, so your compiler will generate it for you.

    All of this (except for some native methods) is implemented in a JVM-compatible language.

    Depending on the library and its restrictions, as well as your IDE's functionality, it may be possible to get the full source code in another package and "attach" it so your IDE shows the source instead of the machine-generated summary, or to decompile it back to Java source code with the same functionality1.

    1 The Java compiler generally outputs bytecode that is fairly easy to convert back to source code with the same function (not true of other compiled languages such as C/C++). It is possible to obfuscate code using a tool such as Proguard, which replaces identifiers with nonsense ones, and can additionally convert certain operations to highly unclear ones with the same result when executed.

    0 讨论(0)
  • 2021-01-07 02:07

    Compiled code just means that the IDE doesn't have the source available to it (but the code is written in Java). The sun.* classes typically don't make their source available because they have a non-public API. This shouldn't be confused with native code, which can be written in C/C++ and called through JNI/JNA.

    0 讨论(0)
提交回复
热议问题