使用ASM动态生成一个接口的实现类,接口如下:
public interface ISayHello {
public void MethodA();
public void MethodB();
public void Abs();
}
具体实现如下:
public class InterfaceHandler extends ClassLoader implements Opcodes {
public static Object MakeClass(Class<?> clazz) throws Exception {
String name = clazz.getSimpleName();
String className = name + "$imp";
String Iter = clazz.getName().replaceAll("\\.", "/");
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
cw.visit(V1_5, ACC_PUBLIC + ACC_SUPER, className, null,
"java/lang/Object", new String[] { Iter });
// 空构造
MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null,
null);
mv.visitVarInsn(ALOAD, 0);
mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
mv.visitInsn(RETURN);
mv.visitMaxs(1, 1);
mv.visitEnd();
// 实现接口中所有方法
Method[] methods = clazz.getMethods();
for (Method method : methods) {
MakeMethod(cw, method.getName(), className);
}
cw.visitEnd();
//写入文件
byte[] code = cw.toByteArray();
FileOutputStream fos = new FileOutputStream("d:/com/" + className + ".class");
fos.write(code);
fos.close();
//从文件加载类
InterfaceHandler loader = new InterfaceHandler();
Class<?> exampleClass = loader.defineClass(className, code, 0,
code.length);
Object obj = exampleClass.getConstructor().newInstance();
return obj;
}
private static void MakeMethod(ClassWriter cw, String MethodName,
String className) {
MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, MethodName, "()V", null, null);
//mv.visitCode();
//Label l0 = new Label();
//mv.visitLabel(l0);
//mv.visitLineNumber(8, l0);
mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out",
"Ljava/io/PrintStream;");
mv.visitLdcInsn("调用方法 [" + MethodName + "]");
mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println",
"(Ljava/lang/String;)V");
//Label l1 = new Label();
//mv.visitLabel(l1);
//mv.visitLineNumber(9, l1);
mv.visitInsn(RETURN);
//Label l2 = new Label();
//mv.visitLabel(l2);
//mv.visitLocalVariable("this", "L" + className + ";", null, l0, l2, 0);
mv.visitMaxs(2, 1);
mv.visitEnd();
}
public static void main(final String args[]) throws Exception {
ISayHello iSayHello = (ISayHello) MakeClass(ISayHello.class);
iSayHello.MethodA();
iSayHello.MethodB();
iSayHello.Abs();
}
}
注意,使用ASM访问属性和方法的时候,会返回一个Visitor对象,如属性为FieldVisitor,方法为MethodVisitor。
使用反编译工具查看生成的字节码文件内容如下:
public class ISayHello$imp
implements ISayHello
{
public void MethodA()
{
System.out.println("调用方法 [MethodA]");
}
public void MethodB()
{
System.out.println("调用方法 [MethodB]");
}
public void Abs()
{
System.out.println("调用方法 [Abs]");
}
}
使用ASM生成接口实现类
在java的许多框架里面,都能找到ASM的身影,比如AOP编程就可以利visitMethod对指定方法就行拦截,做前置后置增强,还有比如常用的插件Lombok就是利用ASM添加的setter getter方法。MyBatis的Mapper接口实现是通过动态代理实现,现在可以使用ASM动态创建实现了字节码来实现。
1、定义接口
定义StudentMapper接口
package org.example.cn.mapper;
import java.util.List;
public interface StudentMapper {
List<String> findStudentList();
}
2、生成实现类
使用ASM生成实现类StudentMapperImpl
ClassWriter classWriter = new ClassWriter(0);
/// 参数列表第3个参数表示继承的父类,第4个参数表示实现的接口
classWriter.visit(61, ACC_PUBLIC | ACC_SUPER, "org/example/cn/mapper/StudentMapperImpl", null, "java/lang/Object", new String[]{"org/example/cn/mapper/StudentMapper"});
classWriter.visitSource("StudentMapperImpl.java", null);
/// 定义构造器
MethodVisitor methodVisitor = classWriter.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
methodVisitor.visitCode();
Label initLabel0 = new Label();
methodVisitor.visitLabel(initLabel0);
methodVisitor.visitVarInsn(ALOAD, 0);
methodVisitor.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
methodVisitor.visitInsn(RETURN);
Label initLabel1 = new Label();
methodVisitor.visitLabel(initLabel1);
/// 局部变量表,槽位的0处放的是this变量
methodVisitor.visitLocalVariable("this", "Lorg/example/cn/mapper/StudentMapperImpl;", null, initLabel0, initLabel1, 0);
methodVisitor.visitMaxs(1, 1);
methodVisitor.visitEnd();
/// 重写findStudentList方法
methodVisitor = classWriter.visitMethod(ACC_PUBLIC, "findStudentList", "()Ljava/util/List;", "()Ljava/util/List<Ljava/lang/String;>;", null);
methodVisitor.visitCode();
Label label0 = new Label();
methodVisitor.visitLabel(label0);
/// new ArrayList()
methodVisitor.visitTypeInsn(NEW, "java/util/ArrayList");
methodVisitor.visitInsn(DUP);
methodVisitor.visitMethodInsn(INVOKESPECIAL, "java/util/ArrayList", "<init>", "()V");
methodVisitor.visitVarInsn(ASTORE, 1);
Label label1 = new Label();
methodVisitor.visitLabel(label1);
/// 压栈
methodVisitor.visitVarInsn(ALOAD, 1);
/// 赋值
methodVisitor.visitLdcInsn("\u9648\u7476");
///调用add方法
methodVisitor.visitMethodInsn(INVOKEINTERFACE, "java/util/List", "add", "(Ljava/lang/Object;)Z");
/// 出栈
methodVisitor.visitInsn(POP);
Label label2 = new Label();
methodVisitor.visitLabel(label2);
methodVisitor.visitVarInsn(ALOAD, 1);
methodVisitor.visitLdcInsn("\u674e\u73b0");
methodVisitor.visitMethodInsn(INVOKEINTERFACE, "java/util/List", "add", "(Ljava/lang/Object;)Z");
methodVisitor.visitInsn(POP);
Label label3 = new Label();
methodVisitor.visitLabel(label3);
methodVisitor.visitLineNumber(11, label3);
methodVisitor.visitVarInsn(ALOAD, 1);
methodVisitor.visitLdcInsn("\u91d1\u6668");
methodVisitor.visitMethodInsn(INVOKEINTERFACE, "java/util/List", "add", "(Ljava/lang/Object;)Z");
methodVisitor.visitInsn(POP);
Label label4 = new Label();
methodVisitor.visitLabel(label4);
methodVisitor.visitLineNumber(12, label4);
methodVisitor.visitVarInsn(ALOAD, 1);
methodVisitor.visitInsn(ARETURN);
Label label5 = new Label();
methodVisitor.visitLabel(label5);
methodVisitor.visitLocalVariable("this", "Lorg/example/cn/mapper/StudentMapperImpl;", null, label0, label5, 0);
methodVisitor.visitLocalVariable("list", "Ljava/util/List;", "Ljava/util/List<Ljava/lang/String;>;", label1, label5, 1);
methodVisitor.visitMaxs(2, 2);
methodVisitor.visitEnd();
classWriter.visitEnd();
byte[] bytes = classWriter.toByteArray();
字节码java源码
public class StudentMapperImpl implements StudentMapper {
public List<String> findStudentList() {
List<String> list = new ArrayList();
list.add("陈瑶");
list.add("李现");
list.add("金晨");
return list;
}
}
3、加载实现类
自定义类加载器加载StudentMapperImpl字节码
public class MyClassLoader extends ClassLoader{
/// 类名全路径
private final String className;
/// 字节码
private final byte[] bytes;
public MyClassLoader( String className,byte[] bytes){
this.className = className;
this.bytes = bytes;
}
@Override
protected Class<?> findClass(String name) throws ClassNotFoundException {
return defineClass(className,bytes,0,bytes.length);
}
}
加载类
String className = "org.example.cn.mapper.StudentMapperImpl";
MyClassLoader myClassLoader = new MyClassLoader(className, bytes);
Class<?> clz = myClassLoader.loadClass(className);
4、调用实现类
◆ 方法1
利用构造器直接反射创建实例
/// Class<?> aClass = Class.forName("org.example.cn.mapper.StudentMapperImpl");
StudentMapper studentMapper = (StudentMapper) clz.getConstructor().newInstance();
List<String> studentList = studentMapper.findStudentList();
System.out.println("studentList---"+studentList);
方法2
动态代理实例化接口
StudentMapper studentMapper = (StudentMapper) Proxy.newProxyInstance(myClassLoader, new Class[]{StudentMapper.class}, (proxy, method, args1) -> method.invoke(clz.getConstructor().newInstance(), args1));
List<String> studentList = studentMapper.findStudentList();
System.out.println("studentList---"+studentList);
运行结果