Java字节码实现Aop(续二)

以下代码需要asm-5.0.2.jar和asm-commons-5.0.2.jar两个包。

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 startlabel = new Label();
								visitLabel(startlabel);
								visitLineNumber(13, startlabel);
								
								
								visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/System", "currentTimeMillis", "()J", false);
								visitVarInsn(Opcodes.LSTORE, 2);
								Label ctmlabel = new Label();
								visitLabel(ctmlabel);
								visitLineNumber(14, ctmlabel);
								
								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 sdlabel = new Label();
								visitLabel(sdlabel);
								visitLineNumber(16, sdlabel);
								
								
								Label endlabel = new Label();
								visitLabel(endlabel);

visitLocalVariable("this", "Lcom/shihuan/field/AccountField;", null, startlabel, endlabel, 0);
								
								visitLocalVariable("starttime", "J", null, ctmlabel, endlabel, 2);
								visitLocalVariable("startdate", "Ljava/util/Date;", null, sdlabel, endlabel, 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代码在[email protected]的网盘的原创作品里。



----------------------------------------------------------------------------------

下面是笔者改Oracle的代码例子:
package oracle.jdbc.driver;

public class OracleMyEnd {

	public static void getMySqlInfo(OraclePreparedStatement opsObj, java.util.Date startdate){
		System.out.println(startdate);
		System.out.println("ShiHuan Is Inner --->> " + opsObj.sqlObject.originalSql);
	}
	
}


package oracle.jdbc.driver;

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 OracleGeneratorAsm {

	public static void main(String[] args) throws IOException {
		ClassReader cr = new ClassReader("oracle.jdbc.driver.OraclePreparedStatement");
		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 startlabel = new Label();
								visitLabel(startlabel);
								visitLineNumber(3362, startlabel);
								
								
								visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/System", "currentTimeMillis", "()J", false);
								visitVarInsn(Opcodes.LSTORE, 5);
								Label ctmlabel = new Label();
								visitLabel(ctmlabel);
								visitLineNumber(3363, ctmlabel);
								
								visitTypeInsn(Opcodes.NEW, "java/util/Date");
								visitInsn(Opcodes.DUP);
								visitVarInsn(Opcodes.LLOAD, 5);
								visitMethodInsn(Opcodes.INVOKESPECIAL, "java/util/Date", "<init>", "(J)V", false);
								visitVarInsn(Opcodes.ASTORE, 7);
								Label sdlabel = new Label();
								visitLabel(sdlabel);
								visitLineNumber(3364, sdlabel);
								
								
								Label endlabel = new Label();
								visitLabel(endlabel);
								
								visitLocalVariable("this", "Loracle/jdbc/driver/OraclePreparedStatement;", null, startlabel, endlabel, 0);
								visitLocalVariable("starttime", "J", null, ctmlabel, endlabel, 5);
								visitLocalVariable("startdate", "Ljava/util/Date;", null, sdlabel, endlabel, 7);
								
								
								super.visitCode();
								
							}
							
							
							public void visitInsn(int opcode) {
								if ((opcode >= Opcodes.IRETURN && opcode <= Opcodes.RETURN) || opcode == Opcodes.ATHROW) {
									visitVarInsn(Opcodes.ALOAD, 0);
									visitVarInsn(Opcodes.ALOAD, 7);
									visitMethodInsn(
											Opcodes.INVOKESTATIC,
											"oracle/jdbc/driver/OracleMyEnd",
											"getMySqlInfo",
											"(Loracle/jdbc/driver/OraclePreparedStatement;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:/ojdbc6_b/oracle/jdbc/driver/OraclePreparedStatement.class");
		fos.write(code);
		fos.close();
	}

}

你可能感兴趣的:(java,ASM)