-
Notifications
You must be signed in to change notification settings - Fork 148
itstack demo bytecode 1 01
小傅哥 edited this page Apr 19, 2020
·
1 revision
package org.itstack.demo.javassist;
import javassist.*;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
* 公众号:bugstack虫洞栈
* 博客栈:https://bugstack.cn - 沉淀、分享、成长,让自己和他人都能有所收获!
* 本专栏是小傅哥多年从事一线互联网Java开发的学习历程技术汇总,旨在为大家提供一个清晰详细的学习教程。如果能为您提供帮助,请给予支持(关注、点赞、分享)!
*/
public class GenerateClazzMethod {
public static void main(String[] args) throws IOException, CannotCompileException, NotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
ClassPool pool = ClassPool.getDefault();
// 创建类 classname:创建类路径和名称
CtClass ctClass = pool.makeClass("org.itstack.demo.javassist.HelloWorld");
// 添加方法
CtMethod mainMethod = new CtMethod(CtClass.voidType, "main", new CtClass[]{pool.get(String[].class.getName())}, ctClass);
mainMethod.setModifiers(Modifier.PUBLIC + Modifier.STATIC);
mainMethod.setBody("{System.out.println(\"javassist hi helloworld by 小傅哥(bugstack.cn)\");}");
ctClass.addMethod(mainMethod);
// 创建无参数构造方法
CtConstructor ctConstructor = new CtConstructor(new CtClass[]{}, ctClass);
ctConstructor.setBody("{}");
ctClass.addConstructor(ctConstructor);
// 输出类内容
ctClass.writeFile();
// 测试调用
Class clazz = ctClas.toClass();
Object obj = clazz.newInstance();
Method main = clazz.getDeclaredMethod("main", String[].class);
main.invoke(obj, (Object)new String[1]);
}
}