Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compile Java class with missing code parts

Tags:

java

I'm looking for some ideas on how to compile Java code with some other pieces of code missing (method calls). I am fully aware that javac will not allow you to compile Java files if cannot find all dependencies. But maybe there is some way how to bypass it, something like force compile.
My bytecode knowledge is not so good but I think some method invoke is just full package definition of class and method name with parameters. So if compiler just puts this data to class file and assume in running process dependency will be available (if not simple NoSuchMethodExp).

Only workaround so far I found is to create empty missing class files with empty methods to "cheat" compiler. Works perfectly but there should be easier way :)
Any ideas?

like image 203
JIV Avatar asked Nov 19 '25 22:11

JIV


1 Answers

Use Interfaces.

Create the interfaces that have the methods you need. At runtime, inject (Spring, Guice, etc.) or generate (cglib ...) classes that implement the interface.

like image 128
rfeak Avatar answered Nov 21 '25 10:11

rfeak