How to generate bytecode and save .class file?

I have the following strange requirement.

I have been given:

  • A list of method names.
  • Names and types of parameters of the above methods.
  • The functionality of the above methods. It looks like this: For each parameter, the method converts it to a string using toString and gets an array of strings. The method applies the foo function to this array. The foo function accepts String [] as input and outputs String . Methods return what foo returns. foo code is inside the Java object and should be accessible as a black box.

The information in 1. and 2. can be in a text or XML file. To this end, we can consider it as accessible inside a Java object in any way.

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

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

[EDIT:] I can think of something else: first generate a .java file and then compile it to get a .class file.

[Context:] I have to do this several hundred methods. I want a shortcut so that I can automate my work, rather than manually write code.

+7
source share
3 answers

You can generate the required program code in Java syntax and turn it into a class file using the compiler. At run time, you can create a javac instance and pass it a byte array instead of the location of the source file. This is perhaps the easiest for other programmers.

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

+7
source

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

Take a look at the Javassist .

+5
source

I saw how you answered my comment, but it’s still not clear to me why you want to generate the code, which will then be packaged in a jar, just enter it :)

Now, if you want typeafe api with the whole method to have the same behavior, you could provide a dynamic proxy for this interface (this leaves you with a question about how to generate the interface :)

Here is an example where all calls to the entire MyInterface method will be handled by the invoke method (just add methods to the interface to test it) ...

 package test; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; public class Test { interface MyInterface { String methodOne(String s); String methodTwo(String s, Integer i); } static MyInterface proxy = (MyInterface) Proxy.newProxyInstance( MyInterface.class.getClassLoader(), new Class[] { MyInterface.class }, new InvocationHandler() { public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { StringBuilder result = new StringBuilder(); for (Object arg : args) { result.append(arg.toString()); } return result.toString(); } }); public static void main(String[] args) { System.out.println(proxy.methodOne("hello")); System.out.println(proxy.methodTwo("world", 5)); } } 
+1
source

All Articles