使用ASM动态创建接口实现类

使用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]");
  }
}

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值