Java Compiler API 代码生成 和 执行

124 篇文章 0 订阅
package com.xiuye.test;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.util.Map;

import org.junit.Test;

import com.xiuye.util.cls.XClassLoader;
import com.xiuye.util.cls.XType;
import com.xiuye.util.code.XCompiler;
import com.xiuye.util.code.gen.ClassInfo;
import com.xiuye.util.log.XLog;

public class TestCompiler {
	
	@Test
	public void testGenerateClass() throws MalformedURLException, ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException {
		ClassInfo dc = new ClassInfo("com.xy.app", "ABC");
		dc.addImportPackage("com.xiuye.test.TestCompiler");
		dc.addConstructor("System.out.println(\"Hello World!Constructor!\");"
				+ "System.out.println(a);"
				+ "System.out.println(b);"
				+ "","int","a","int","b");
		dc.addField("int", "lang");
		dc.addField("int", "lang2", "100");
		dc.addMethod("int", "f1", "return a>b?a:b;", "int", "a", "int", "b");
//		dc.addMethod("int", "f2", null, "int", "a", "int", "b");
		dc.addMethod("int", "f3", "TestCompiler test = new TestCompiler();"
				+ "try{"
				+ "test.testGenerateClass();"
				+ "}catch(Exception e1){"
				+ "e1.printStackTrace();"
				+ "}"
				+ "return 100;", "int", "a", "int", "b");
		XLog.ln(dc);
		XLog.ln(XType.firstUpperCase("abcdefg"));
		XLog.ln(XType.firstLowerCase("ABCDEFG"));
		
		XLog.ln(dc.getFullName());
		
		Map<String,String> code = XType.map();
		code.put(dc.getFullName(),dc.toString());
		XCompiler.compileCode(code);
		XClassLoader cl = XType.createClassLoader();
		
		Class<?> clazz = cl.load(dc.getFullName());
		XLog.ln(clazz);
		Method m = clazz.getMethod("getLang2");
		Constructor<?> con = clazz.getConstructor(int.class,int.class);
		Object obj = con.newInstance(7,88);
		int a = (int) m.invoke(obj);
		XLog.ln(a);
		Method f1 = clazz.getMethod("f1", int.class,int.class);
		XLog.ln(f1.invoke(obj, 5,8));
		
		//下面的调用会无限的循环,无限的套娃!!!
		//好好看看 f3 和 外部类的 调用关系!!!
		Method f3 = clazz.getMethod("f3", int.class,int.class);
		XLog.ln(f3.invoke(obj, 98,88));
		
	}

	public static void main(String[] args) {

	}

}

 

 

package com.xy.app;
import com.xiuye.test.TestCompiler;

public  class ABC
{
	private  int lang;
	private  int lang2 = 100;

	public   ABC(int a,int b){System.out.println("Hello World!Constructor!");System.out.println(a);System.out.println(b);}
	public  void setLang2(int lang2){this.lang2=lang2;}
	public  int getLang2(){return this.lang2;}
	public  int f1(int a,int b){return a>b?a:b;}
	public  int f3(int a,int b){TestCompiler test = new TestCompiler();try{test.testGenerateClass();}catch(Exception e1){e1.printStackTrace();}return 100;}

}
 	[ line:38 | com.xiuye.test.TestCompiler | TestCompiler.java ]
Abcdefg 	[ line:39 | com.xiuye.test.TestCompiler | TestCompiler.java ]
aBCDEFG 	[ line:40 | com.xiuye.test.TestCompiler | TestCompiler.java ]
com.xy.app.ABC 	[ line:42 | com.xiuye.test.TestCompiler | TestCompiler.java ]
class com.xy.app.ABC 	[ line:50 | com.xiuye.test.TestCompiler | TestCompiler.java ]
Hello World!Constructor!
7
88
100 	[ line:55 | com.xiuye.test.TestCompiler | TestCompiler.java ]
8 	[ line:57 | com.xiuye.test.TestCompiler | TestCompiler.java ]
package com.xy.app;
import com.xiuye.test.TestCompiler;

public  class ABC
{
	private  int lang;
	private  int lang2 = 100;

	public   ABC(int a,int b){System.out.println("Hello World!Constructor!");System.out.println(a);System.out.println(b);}
	public  void setLang2(int lang2){this.lang2=lang2;}
	public  int getLang2(){return this.lang2;}
	public  int f1(int a,int b){return a>b?a:b;}
	public  int f3(int a,int b){TestCompiler test = new TestCompiler();try{test.testGenerateClass();}catch(Exception e1){e1.printStackTrace();}return 100;}

}
 	[ line:38 | com.xiuye.test.TestCompiler | TestCompiler.java ]
Abcdefg 	[ line:39 | com.xiuye.test.TestCompiler | TestCompiler.java ]
aBCDEFG 	[ line:40 | com.xiuye.test.TestCompiler | TestCompiler.java ]
com.xy.app.ABC 	[ line:42 | com.xiuye.test.TestCompiler | TestCompiler.java ]
class com.xy.app.ABC 	[ line:50 | com.xiuye.test.TestCompiler | TestCompiler.java ]
Hello World!Constructor!
7
88
100 	[ line:55 | com.xiuye.test.TestCompiler | TestCompiler.java ]
8 	[ line:57 | com.xiuye.test.TestCompiler | TestCompiler.java ]
package com.xy.app;
import com.xiuye.test.TestCompiler;

public  class ABC
{
	private  int lang;
	private  int lang2 = 100;

	public   ABC(int a,int b){System.out.println("Hello World!Constructor!");System.out.println(a);System.out.println(b);}
	public  void setLang2(int lang2){this.lang2=lang2;}
	public  int getLang2(){return this.lang2;}
	public  int f1(int a,int b){return a>b?a:b;}
	public  int f3(int a,int b){TestCompiler test = new TestCompiler();try{test.testGenerateClass();}catch(Exception e1){e1.printStackTrace();}return 100;}

}
 	[ line:38 | com.xiuye.test.TestCompiler | TestCompiler.java ]
Abcdefg 	[ line:39 | com.xiuye.test.TestCompiler | TestCompiler.java ]
aBCDEFG 	[ line:40 | com.xiuye.test.TestCompiler | TestCompiler.java ]
com.xy.app.ABC 	[ line:42 | com.xiuye.test.TestCompiler | TestCompiler.java ]
class com.xy.app.ABC 	[ line:50 | com.xiuye.test.TestCompiler | TestCompiler.java ]
Hello World!Constructor!
7
88
100 	[ line:55 | com.xiuye.test.TestCompiler | TestCompiler.java ]
8 	[ line:57 | com.xiuye.test.TestCompiler | TestCompiler.java ]

............

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值