Skip to content

Commit b1cd203

Browse files
committed
add: 为aop添加行数(假的行数)和源文件名称, 解决debug时eclipse弹出警告
1 parent aca6f96 commit b1cd203

4 files changed

Lines changed: 132 additions & 121 deletions

File tree

Lines changed: 74 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,74 @@
1-
package org.nutz.aop.asm;
2-
3-
import java.lang.reflect.Method;
4-
5-
import org.nutz.repo.org.objectweb.asm.Label;
6-
import org.nutz.repo.org.objectweb.asm.MethodVisitor;
7-
import org.nutz.repo.org.objectweb.asm.Type;
8-
9-
/**
10-
*
11-
* @author wendal(wendal1985@gmail.com)
12-
*
13-
*/
14-
class AopInvokeAdpter extends AopMethodAdapter {
15-
16-
Method[] methodArray;
17-
18-
AopInvokeAdpter( Method[] methodArray,
19-
MethodVisitor mv,
20-
int access,
21-
String methodName,
22-
String desc,
23-
int methodIndex,
24-
String myName,
25-
String enhancedSuperName) {
26-
super(mv, access, methodName, desc, methodIndex, myName, enhancedSuperName);
27-
this.methodArray = methodArray;
28-
}
29-
30-
void visitCode() {
31-
mv.visitCode();
32-
33-
for (int i = 0; i < methodArray.length; i++) {
34-
Method method = methodArray[i];
35-
mv.visitVarInsn(ILOAD, 1);
36-
visitX(i);
37-
Label l0 = new Label();
38-
mv.visitJumpInsn(IF_ICMPNE, l0);
39-
mv.visitVarInsn(ALOAD, 0);
40-
Type[] args = Type.getArgumentTypes(method);
41-
for (int j = 0; j < args.length; j++) {
42-
mv.visitVarInsn(ALOAD, 2);
43-
visitX(j);
44-
mv.visitInsn(AALOAD);
45-
returnType = args[j];
46-
AsmHelper.checkCast(returnType,mv);
47-
}
48-
mv.visitMethodInsn( INVOKESPECIAL,
49-
enhancedSuperName,
50-
method.getName(),
51-
Type.getMethodDescriptor(method));
52-
{
53-
returnType = Type.getReturnType(method);
54-
if (returnType.equals(Type.VOID_TYPE))
55-
mv.visitInsn(ACONST_NULL);
56-
else if (returnType.getOpcode(IRETURN) != ARETURN)
57-
AsmHelper.packagePrivateData(returnType,mv);
58-
mv.visitInsn(ARETURN);
59-
}
60-
mv.visitLabel(l0);
61-
}
62-
63-
mv.visitInsn(ACONST_NULL);
64-
mv.visitInsn(ARETURN);
65-
mv.visitMaxs(4, 3);
66-
mv.visitEnd();
67-
}
68-
69-
}
1+
package org.nutz.aop.asm;
2+
3+
import java.lang.reflect.Method;
4+
5+
import org.nutz.repo.org.objectweb.asm.Label;
6+
import org.nutz.repo.org.objectweb.asm.MethodVisitor;
7+
import org.nutz.repo.org.objectweb.asm.Type;
8+
9+
/**
10+
*
11+
* @author wendal(wendal1985@gmail.com)
12+
*
13+
*/
14+
class AopInvokeAdpter extends AopMethodAdapter {
15+
16+
Method[] methodArray;
17+
18+
AopInvokeAdpter( Method[] methodArray,
19+
MethodVisitor mv,
20+
int access,
21+
String methodName,
22+
String desc,
23+
int methodIndex,
24+
String myName,
25+
String enhancedSuperName) {
26+
super(mv, access, methodName, desc, methodIndex, myName, enhancedSuperName);
27+
this.methodArray = methodArray;
28+
}
29+
30+
void visitCode() {
31+
mv.visitCode();
32+
33+
for (int i = 0; i < methodArray.length; i++) {
34+
// start of fuck linenumber
35+
Label tmp = new Label();
36+
mv.visitLabel(tmp);
37+
mv.visitLineNumber(1, tmp);
38+
// end of Linenumber
39+
Method method = methodArray[i];
40+
mv.visitVarInsn(ILOAD, 1);
41+
visitX(i);
42+
Label l0 = new Label();
43+
mv.visitJumpInsn(IF_ICMPNE, l0);
44+
mv.visitVarInsn(ALOAD, 0);
45+
Type[] args = Type.getArgumentTypes(method);
46+
for (int j = 0; j < args.length; j++) {
47+
mv.visitVarInsn(ALOAD, 2);
48+
visitX(j);
49+
mv.visitInsn(AALOAD);
50+
returnType = args[j];
51+
AsmHelper.checkCast(returnType,mv);
52+
}
53+
mv.visitMethodInsn( INVOKESPECIAL,
54+
enhancedSuperName,
55+
method.getName(),
56+
Type.getMethodDescriptor(method));
57+
{
58+
returnType = Type.getReturnType(method);
59+
if (returnType.equals(Type.VOID_TYPE))
60+
mv.visitInsn(ACONST_NULL);
61+
else if (returnType.getOpcode(IRETURN) != ARETURN)
62+
AsmHelper.packagePrivateData(returnType,mv);
63+
mv.visitInsn(ARETURN);
64+
}
65+
mv.visitLabel(l0);
66+
}
67+
68+
mv.visitInsn(ACONST_NULL);
69+
mv.visitInsn(ARETURN);
70+
mv.visitMaxs(4, 3);
71+
mv.visitEnd();
72+
}
73+
74+
}
Lines changed: 38 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,39 @@
1-
package org.nutz.aop.asm;
2-
3-
import static org.nutz.repo.org.objectweb.asm.Opcodes.ALOAD;
4-
import static org.nutz.repo.org.objectweb.asm.Opcodes.INVOKESPECIAL;
5-
import static org.nutz.repo.org.objectweb.asm.Opcodes.RETURN;
6-
7-
import org.nutz.repo.org.objectweb.asm.MethodVisitor;
8-
9-
/**
10-
* @author wendal(wendal1985@gmail.com)
11-
*/
12-
class ChangeToChildConstructorMethodAdapter extends NormalMethodAdapter {
13-
14-
private String superClassName;
15-
16-
ChangeToChildConstructorMethodAdapter( MethodVisitor mv,
17-
String desc,
18-
int access,
19-
String superClassName) {
20-
super(mv, desc, access);
21-
this.superClassName = superClassName;
22-
}
23-
24-
void visitCode() {
25-
mv.visitCode();
26-
mv.visitVarInsn(ALOAD, 0);
27-
loadArgs();
28-
mv.visitMethodInsn(INVOKESPECIAL, superClassName, "<init>", desc);
29-
mv.visitInsn(RETURN);
30-
mv.visitMaxs(2, 2);
31-
mv.visitEnd();
32-
}
1+
package org.nutz.aop.asm;
2+
3+
import static org.nutz.repo.org.objectweb.asm.Opcodes.ALOAD;
4+
import static org.nutz.repo.org.objectweb.asm.Opcodes.INVOKESPECIAL;
5+
import static org.nutz.repo.org.objectweb.asm.Opcodes.RETURN;
6+
7+
import org.nutz.repo.org.objectweb.asm.Label;
8+
import org.nutz.repo.org.objectweb.asm.MethodVisitor;
9+
10+
/**
11+
* @author wendal(wendal1985@gmail.com)
12+
*/
13+
class ChangeToChildConstructorMethodAdapter extends NormalMethodAdapter {
14+
15+
private String superClassName;
16+
17+
ChangeToChildConstructorMethodAdapter( MethodVisitor mv,
18+
String desc,
19+
int access,
20+
String superClassName) {
21+
super(mv, desc, access);
22+
this.superClassName = superClassName;
23+
}
24+
25+
void visitCode() {
26+
mv.visitCode();
27+
// start of fuck linenumber
28+
Label tmp = new Label();
29+
mv.visitLabel(tmp);
30+
mv.visitLineNumber(1, tmp);
31+
// end of Linenumber
32+
mv.visitVarInsn(ALOAD, 0);
33+
loadArgs();
34+
mv.visitMethodInsn(INVOKESPECIAL, superClassName, "<init>", desc);
35+
mv.visitInsn(RETURN);
36+
mv.visitMaxs(2, 2);
37+
mv.visitEnd();
38+
}
3339
}

src/org/nutz/repo/org/objectweb/asm/MethodVisitor.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -369,17 +369,17 @@ public interface MethodVisitor {
369369
// Label end,
370370
// int index);
371371

372-
// /**
373-
// * Visits a line number declaration.
374-
// *
375-
// * @param line a line number. This number refers to the source file from
376-
// * which the class was compiled.
377-
// * @param start the first instruction corresponding to this line number.
378-
// * @throws IllegalArgumentException if <tt>start</tt> has not already been
379-
// * visited by this visitor (by the {@link #visitLabel visitLabel}
380-
// * method).
381-
// */
382-
// void visitLineNumber(int line, Label start);
372+
/**
373+
* Visits a line number declaration.
374+
*
375+
* @param line a line number. This number refers to the source file from
376+
* which the class was compiled.
377+
* @param start the first instruction corresponding to this line number.
378+
* @throws IllegalArgumentException if <tt>start</tt> has not already been
379+
* visited by this visitor (by the {@link #visitLabel visitLabel}
380+
* method).
381+
*/
382+
void visitLineNumber(int line, Label start);
383383

384384
/**
385385
* Visits the maximum stack size and the maximum number of local variables

src/org/nutz/repo/org/objectweb/asm/MethodWriter.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1204,15 +1204,15 @@ public void visitTryCatchBlock(
12041204
// }
12051205
// }
12061206
// }
1207-
//
1208-
// public void visitLineNumber(final int line, final Label start) {
1209-
// if (lineNumber == null) {
1210-
// lineNumber = new ByteVector();
1211-
// }
1212-
// ++lineNumberCount;
1213-
// lineNumber.putShort(start.position);
1214-
// lineNumber.putShort(line);
1215-
// }
1207+
1208+
public void visitLineNumber(final int line, final Label start) {
1209+
if (lineNumber == null) {
1210+
lineNumber = new ByteVector();
1211+
}
1212+
++lineNumberCount;
1213+
lineNumber.putShort(start.position);
1214+
lineNumber.putShort(line);
1215+
}
12161216

12171217
public void visitMaxs(final int maxStack, final int maxLocals) {
12181218
if (ClassReader.FRAMES && compute == FRAMES) {

0 commit comments

Comments
 (0)