How to generate bytecode and save to .class file?

陌路散爱 提交于 2019-12-10 03:05:59

问题


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.


回答1:


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.




回答2:


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

Have a look at Javassist.




回答3:


I saw you answer to my comment, but it is still not clear for me why you want to generate the code to be then packaged in a jar, just type it :)

Now if you want a typesafe api with all method having the same behaviour you could provide a dynamic proxy for a given interface (this leaves you with the question on how to generate the interface :)

Here is an example where all the calls to all the 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));

}   

}


来源:https://stackoverflow.com/questions/7766291/how-to-generate-bytecode-and-save-to-class-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!