Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to obtain the bytecode for a class at runtime?

In Java, is there a way (at runtime) to obtain the bytecode which defined a particular class?

Put another way, is there a way to obtain the byte[] array passed to ClassLoader.defineClass(String name, byte[] b, int off, int len) when a particular class was loaded? I see that this method is declared final, so creating a custom ClassLoader to intercept class definitions seems out of the question.

In the past, I have used the class's ClassLoader to obtain the bytecode via the getResourceAsStream(String) method, but I would prefer a more canonical solution.

like image 311
Adam Paynter Avatar asked Apr 29 '10 12:04

Adam Paynter


People also ask

How do I read a .class file bytecode?

Using javap. The Java command-line comes with the javap tool that displays information about the fields, constructors, and methods of a class file. Based on the options used, it can disassemble a class and show the instructions that comprise the Java bytecode.

Does .class contain bytecode?

Yes, a Java class file is a file containing Java bytecode that can be executed on the Java Virtual Machine (JVM).

How can we get byte code in Java?

How is Byte Code generated? Compiler converts the source code or the Java program into the Byte Code(or machine code), and secondly, the Interpreter executes the byte code on the system. The Interpreter can also be called JVM(Java Virtual Machine).


1 Answers

Here is a description how to implement an agent

like image 65
stacker Avatar answered Oct 25 '22 03:10

stacker