Java字节码实现Aop(续二)_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > Java字节码实现Aop(续二)

Java字节码实现Aop(续二)

 2014/5/29 18:50:02  shihuan830619  程序员俱乐部  我要评论(1)
  • 摘要:以下代码需要asm-5.0.2.jar和asm-commons-5.0.2.jar两个包。packagecom.shihuan.field;publicclassAccountField{Stringsql="select*fromtables";publicvoidoperation(){System.out.println("shihuanoperation...");}intexecuteInternal(){System.out.println
  • 标签:实现 Java
以下代码需要asm-5.0.2.jar和asm-commons-5.0.2.jar两个包。

class="java" name="code">
package com.shihuan.field;

public class AccountField {

	String sql = "select * from tables";
	
	public void operation() {
        System.out.println("shihuan operation...");
    }
	
	int executeInternal() {
		
		System.out.println("shihuan executeInternal...");
		return 1;
	}
	
}


package com.shihuan.field;

public class TestAccountField {

	public static void main(String[] args) {
		AccountField t = new AccountField();
		t.operation();
		t.executeInternal();
	}

}


package com.shihuan.field;

public class SecurityCheckerField {
	
	public static void checkCode(AccountField afObj, java.util.Date startdate) {
		System.out.println("shihuan starttime --->> " + startdate);
		
		long endtime = System.currentTimeMillis();
		java.util.Date enddate = new java.util.Date(endtime);
		System.out.println("shihuan endtime --->> " + enddate);
		System.out.println(afObj.sql);
	}
	
}


package com.shihuan.field;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.Label;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;

public class GeneratorAsmField {

	public static void main(String[] args) throws IOException {
		ClassReader cr = new ClassReader("com.shihuan.field.AccountField");
		ClassWriter cw = new ClassWriter(cr, ClassWriter.COMPUTE_MAXS);
		ClassVisitor cv = new ClassVisitor(Opcodes.ASM5, cw) {
			
			public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
				MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions); // 先得到原始的方法
				MethodVisitor newMethod = mv;

				if (mv != null) {
					if ("executeInternal".equals(name)) { // 此处的executeInternal即为需要修改的方法,修改方法內容
						// 访问需要修改的方法
						newMethod = new MethodVisitor(Opcodes.ASM5, mv) {

							public void visitCode() {
								
								Label l0 = new Label();
								mv.visitLabel(l0);
								mv.visitLineNumber(12, l0);
								visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/System", "currentTimeMillis", "()J", false);
								visitVarInsn(Opcodes.LSTORE, 2);
								
								Label l1 = new Label();
								mv.visitLabel(l1);
								mv.visitLineNumber(13, l1);
								visitTypeInsn(Opcodes.NEW, "java/util/Date");
								visitInsn(Opcodes.DUP);
								visitVarInsn(Opcodes.LLOAD, 2);
								visitMethodInsn(Opcodes.INVOKESPECIAL, "java/util/Date", "<init>", "(J)V", false);
								visitVarInsn(Opcodes.ASTORE, 4);
								
								Label l2 = new Label();
								mv.visitLabel(l2);
								
								visitLocalVariable("starttime", "J", null, l0, l2, 2);
								visitLocalVariable("startdate", "Ljava/util/Date;", null, l1, l2, 4);
								
								super.visitCode();
							}
							
							public void visitInsn(int opcode) {
								if ((opcode >= Opcodes.IRETURN && opcode <= Opcodes.RETURN) || opcode == Opcodes.ATHROW) {
									visitVarInsn(Opcodes.ALOAD, 0);
									visitVarInsn(Opcodes.ALOAD, 4);
									visitMethodInsn(
											Opcodes.INVOKESTATIC,
											"com/shihuan/field/SecurityCheckerField",
											"checkCode",
											"(Lcom/shihuan/field/AccountField;Ljava/util/Date;)V",
											false);
								}
								super.visitInsn(opcode);
							}
							
						};

					}
				}
				return newMethod;
			}

		};
		cr.accept(cv, ClassReader.SKIP_DEBUG);

		byte[] code = cw.toByteArray();
		OutputStream fos = new FileOutputStream("D:/myworkspace/JavaAsm/bin/com/shihuan/field/AccountField.class");
		fos.write(code);
		fos.close();
	}

}



【注】:本例中涉及到org.objectweb.asm.Label类和visitTypeInsn、visitInsn、visitVarInsn、visitLocalVariable等函数的参数意义问题,如果有想深入学习Asm5.0.2的朋友,请联系笔者,笔者学习Asm5.0.2的Java代码在shihuan83019@163.com的网盘的原创作品里。
上一篇: 在线教育开线下体验店 沪江网首吃螃蟹 下一篇: 没有下一篇了!
    网友 2014/6/3 20:34:57 发表

    转载请注明出处,谢谢!

发表评论
用户名: 匿名