Java运行时动态编译

/**
 * Alipay.com Inc.
 * Copyright (c) 2004-2014 All Rights Reserved.
 */
package cannon.framework.util;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import cannon.framework.util.compile.DynamicEngine;

/**
 *
 * @author jialong.fjl
 * @version $Id: CompileTest.java, v 0.1 2014年6月17日 上午11:03:25 jialong.fjl Exp $
 */
public class CompileTest {
    public static void main(String[] args) throws Exception{
        DynamicEngine engine = DynamicEngine.getInstance();
        InputStream is = CompileTest.class.getClassLoader().getResourceAsStream("cannon/framework/util/HelloWorldImpl.txt");
        
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String line = null;
        StringBuilder code = new StringBuilder();
        while((line=br.readLine())!=null){
            code.append(line);
            code.append('\n');
        }
        
        is.close();
        
        HelloWorld helloworld = (HelloWorld) engine.javaCodeToObject("HelloWorldImpl", code.toString());
        helloworld.invoke("动态编译技术");
    }
}

 

 

/**
 * Alipay.com Inc.
 * Copyright (c) 2004-2014 All Rights Reserved.
 */
package cannon.framework.util;

/**
 *
 * @author jialong.fjl
 * @version $Id: HelloWorld.java, v 0.1 2014年6月17日 上午11:04:57 jialong.fjl Exp $
 */
public interface HelloWorld {
    void invoke(String message);
}

 

 

 

import cannon.framework.util.HelloWorld;

public class HelloWorldImpl implements HelloWorld {
    
    public void invoke(String message) {
        System.out.println("invoke : " + message);
    }

}

 

你可能感兴趣的:(java)