Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate bytecode and save to .class file?

I have the following weird requirement.

I am given:

  1. A list of some method names.
  2. Names and types of parameters of the above methods.
  3. The functionality of the above methods. This is as follows: For each parameter, the method converts it to string using toString and obtains an array of strings. To this array, the method applies a function foo. The function foo takes as input a String [] type and outputs String. The methods return what foo returns. foo's code is given inside a Java object and is to be accessed as a black-box.

The info in 1. and 2. can be in a text or XML file. For this purpose, we can consider it to be available inside a Java object in whatever way we choose.

The task is to create a .class file (i.e., bytecode) that implements those methods and can be run on the JVM.

I think this assembler library would be one way to do it. Can anyone suggest an easier way?

[EDIT:] I can think of one other way: first generate the .java file and then compile it to get the .class file.

[The context:] I have to do this for several hundreds of methods. I want the shortcut so I can automate my job rather than manually write the code.

like image 247
Jus12 Avatar asked Oct 14 '11 10:10

Jus12


People also ask

Is bytecode a .class file?

The . class file is the byte-code. It is the result of compiling java source code (text) into the intermediate format, byte-code. The byte-code is then interpreted by the JVM and compiled into a language understandable by your CPU.

How do I create a .class file?

To create a new Java class or type, follow these steps: In the Project window, right-click a Java file or folder, and select New > Java Class. Alternatively, select a Java file or folder in the Project window, or click in a Java file in the Code Editor. Then select File > New > Java Class.

How do you generate a byte code?

Byte Code is automatically created in the same directory as . py file, when a module of python is imported for the first time, or when the source is more recent than the current compiled file.

How does the .class file get generated?

class" file is created as a result of successful compilation by the Java compiler from the ". java" file. Each class in the . java file is compiled into a separate class file if the ".


2 Answers

You could generate the required program code in Java syntax and turn it into a class file using the compiler. It's possible to instantiate javac at run time and pass it a byte array instead of the location of the source file. This is probably the easiest for other programmers to maintain.

If you want to generate byte code directly, asm is the library most commonly used.

like image 97
Barend Avatar answered Oct 14 '22 11:10

Barend


Here is a list of Open Source ByteCode Libraries: http://java-source.net/open-source/bytecode-libraries

Have a look at Javassist.

like image 34
cuh Avatar answered Oct 14 '22 11:10

cuh