3

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"...

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
AD.
  • 389
  • 1
  • 5
  • 20
  • You should also look for bytecode manipulation libraries like [ASM](http://asm.ow2.org/) and [javassist](http://www.csg.ci.i.u-tokyo.ac.jp/~chiba/javassist/) – Ciro Santilli OurBigBook.com Apr 03 '15 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 Jul 13 '15 at 02:27

1 Answers1

4

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.

Johannes Weiss
  • 52,533
  • 16
  • 102
  • 136
  • 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... – AD. Mar 03 '10 at 15:01
  • 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 (http://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. – Johannes Weiss Mar 04 '10 at 11:07