Can i write bytecode inside a method of a class so that the compiler bypasses that part since it is already compiled. Something similar to writing assembly programs in C language using "asm"...
-
You should also look for bytecode manipulation libraries like ASM and javassistCiro Santilli OurBigBook.com– Ciro Santilli OurBigBook.com2015-04-03 16:52:18 +00:00Commented Apr 3, 2015 at 16:52
-
Just curious, why would you want to do this? Your Java bytecode isn't ran on the target processor directly, it will either be interpreted or recompiled into native machine code.Jonathan S. Fisher– Jonathan S. Fisher2015-07-13 02:27:15 +00:00Commented Jul 13, 2015 at 2:27
Add a comment
|
1 Answer
I think you mean Java. If that's the case:
Short answer: no.
Long answer:
There is nothing like asm { ... } in Java. But you could (not very clever in most situations) write a .class file (or have bytecode in textual representation and then assemble it in Java to a .class file) from Java and dynamically load and execute it.
2 Comments
AD.
You said... "(or have bytecode in textual representation and then assemble it in Java to a .class file)"...can you give more inputs on this...
Johannes Weiss
Sure, you store the textual bytecode representation (as
javap -c SomeJavaClass outputs it) as a String in your Java source. While your program is running, you can assemble that code(e.g. with Jasmin (jasmin.sourceforge.net)) which gives you a .class file. That class can be loaded dynamically with the class loader. Finally, you can execute anything from that class.