Reputation: 5055
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(){
Compiled Code
}
When expanding the method you can see the codes inside it are written as a comment.
See one of the method from sun.awt.shell.ShellFolder
:
public boolean isFileSystem() {
// <editor-fold defaultstate="collapsed" desc="Compiled Code">
/* 0: aload_0
* 1: invokevirtual #362 // Method getPath:()Ljava/lang/String;
* 4: ldc #7 // String ShellFolder
* 6: invokevirtual #342 // Method java/lang/String.startsWith:(Ljava/lang/String;)
/........
*/
// </editor-fold>
}
I am interested to know more about those methods and what language is used.
Is it possible to write my own compiled code?
Upvotes: 2
Views: 2383
Reputation: 41271
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.
Upvotes: 10
Reputation: 57192
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.
Upvotes: 2