Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I verify linking between compiled Java code?

Generally speaking one set of code (the client code) links against another (the API code). Java linking is typically verified between .java & .class at compilation time or between .class & .class at runtime. However in the latter case verification is as and when bad references are encountered (i.e. it is lazy).

Is there a way to force verification of all links between the client code and the API code at once with compiled code? The purpose would be to verify that the client code will work with the given version of the API - even though it has been compiled against another.

(Of course one way would be to decompile and recompile against the API, but is there a more direct method?)

like image 237
mike g Avatar asked Jul 28 '10 18:07

mike g


People also ask

Who is responsible for verifying the program in Java?

After the class loader in the JVM loads the byte code of . class file to the machine the Bytecode is first checked for validity by the verifier and this process is called as verification.

What happens when Java code is compiled?

Java source code is compiled into bytecode when we use the javac compiler. The bytecode gets saved on the disk with the file extension . class . When the program is to be run, the bytecode is converted, using the just-in-time (JIT) compiler.

Does Java Do linking?

Java doesn't do linking the way C does. The principle unit is the class definition. A lot of the matching of a class reference to its definition happens at runtime. So you could compile a class against one version of a library, but provide another version at runtime.

Which is more suitable for verifying the byte codes in Java?

Explanation: JIT optimizes bytecode to machine specific language code by compiling similar bytecodes at the same time.


1 Answers

Force verification of links is difficult, due to the nature of the language and the implementation of the JVM.

I believe the rationale for this question is to prevent linkage errors at runtime, and the intention to do so is very much valid. However, when one looks at the cause of linkage errors, from the point of view of the JVM, then it is more or less difficult to peform verification forcefully without any performance impact.

Linkage errors are thrown at runtime, when the JVM executes bytecode corresponding to the method invocation instructions. It is usually at this point in that the deferred final pass of the JVM's bytecode verifier is kicked in, which could result in linkage errors. Needless to say, this is expensive from the point of view of performance.

(It is my assumption that) Most projects (including commercial ones) therefore avoid forced verification, and instead rely on build and dependency management systems to avoid the pain. More commentary in this SO question; the answer of choosing OSGi framework might help.

like image 197
Vineet Reynolds Avatar answered Oct 15 '22 21:10

Vineet Reynolds