|
@@ -26,23 +26,24 @@ public class MethodLifeCycleVisitor extends MethodVisitor {
|
|
"android/app/DialogFragment"
|
|
"android/app/DialogFragment"
|
|
] as String[]
|
|
] as String[]
|
|
|
|
|
|
- public final static Agent[] Modify = [
|
|
|
|
- new Agent("onCreate", "(Landroid/os/Bundle;)V"),
|
|
|
|
- new Agent("onResume", "()V"),
|
|
|
|
- new Agent("onPause", "()V"),
|
|
|
|
- new Agent("onDestroy", "()V"),
|
|
|
|
- new Agent("onSaveInstanceState", "(Landroid/os/Bundle;)V"),
|
|
|
|
- new Agent("onRestoreInstanceState", "(Landroid/os/Bundle;)V"),
|
|
|
|
- new Agent("setUserVisibleHint", "(Z)V"),
|
|
|
|
- new Agent("onHiddenChanged", "(Z)V")
|
|
|
|
- ] as Agent[]
|
|
|
|
|
|
+ public final static MethodModel[] Modify = [
|
|
|
|
+ new MethodModel("onCreate", "(Landroid/os/Bundle;)V"),
|
|
|
|
+ new MethodModel("onResume", "()V"),
|
|
|
|
+ new MethodModel("onPause", "()V"),
|
|
|
|
+ new MethodModel("onDestroy", "()V"),
|
|
|
|
+ new MethodModel("onSaveInstanceState", "(Landroid/os/Bundle;)V"),
|
|
|
|
+ new MethodModel("onRestoreInstanceState", "(Landroid/os/Bundle;)V"),
|
|
|
|
+ new MethodModel("setUserVisibleHint", "(Z)V"),
|
|
|
|
+ new MethodModel("onHiddenChanged", "(Z)V")
|
|
|
|
+ ] as MethodModel[]
|
|
|
|
|
|
- public final static Agent getAgent(String superName, int access, String name, String desc) {
|
|
|
|
|
|
+ public static MethodModel getAgent(String className, String superName, String name, String desc) {
|
|
for (String s : SuperClass) {
|
|
for (String s : SuperClass) {
|
|
if (s.equals(superName)) {
|
|
if (s.equals(superName)) {
|
|
- for (Agent item : Modify) {
|
|
|
|
|
|
+ for (MethodModel item : Modify) {
|
|
if (item.name.equals(name) && item.desc.equals(desc)) {
|
|
if (item.name.equals(name) && item.desc.equals(desc)) {
|
|
- item.access = access
|
|
|
|
|
|
+ item.className = className
|
|
|
|
+ item.superName = superName
|
|
return item
|
|
return item
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -52,10 +53,9 @@ public class MethodLifeCycleVisitor extends MethodVisitor {
|
|
return null
|
|
return null
|
|
}
|
|
}
|
|
|
|
|
|
- public final
|
|
|
|
- static void addMethods(Set<Agent> agentSet, String name, String superName, ClassVisitor cv) {
|
|
|
|
- addLifeCycleFieldMethod(name, cv)
|
|
|
|
- for (Agent agent : Modify) {
|
|
|
|
|
|
+ public static void addMethods(Set<MethodModel> agentSet, String className, String superName, ClassVisitor cv) {
|
|
|
|
+ addLifeCycleFieldMethod(className, cv)
|
|
|
|
+ for (MethodModel agent : Modify) {
|
|
if (!agentSet.contains(agent)) {
|
|
if (!agentSet.contains(agent)) {
|
|
//method
|
|
//method
|
|
int count = 0;
|
|
int count = 0;
|
|
@@ -66,7 +66,7 @@ public class MethodLifeCycleVisitor extends MethodVisitor {
|
|
MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PUBLIC, agent.name, agent.desc, null, null)
|
|
MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PUBLIC, agent.name, agent.desc, null, null)
|
|
mv.visitCode()
|
|
mv.visitCode()
|
|
mv.visitVarInsn(Opcodes.ALOAD, 0)
|
|
mv.visitVarInsn(Opcodes.ALOAD, 0)
|
|
- mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, name, GetLifeMethod, "()L" + LifeClass + ";", false)
|
|
|
|
|
|
+ mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, className, GetLifeMethod, "()L" + LifeClass + ";", false)
|
|
for (int i = 1; i <= count; i++) mv.visitVarInsn(Opcodes.ALOAD, i)
|
|
for (int i = 1; i <= count; i++) mv.visitVarInsn(Opcodes.ALOAD, i)
|
|
mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, LifeClass, agent.name, agent.desc, false)
|
|
mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, LifeClass, agent.name, agent.desc, false)
|
|
|
|
|
|
@@ -84,7 +84,7 @@ public class MethodLifeCycleVisitor extends MethodVisitor {
|
|
* 注入代理字段及初始化方法
|
|
* 注入代理字段及初始化方法
|
|
* @param cv
|
|
* @param cv
|
|
*/
|
|
*/
|
|
- private final static void addLifeCycleFieldMethod(String name, ClassVisitor cv) {
|
|
|
|
|
|
+ private static void addLifeCycleFieldMethod(String className, ClassVisitor cv) {
|
|
//desc
|
|
//desc
|
|
String fieldName = "\$lifeCycleAresDelegate"
|
|
String fieldName = "\$lifeCycleAresDelegate"
|
|
String fieldDesc = "L" + LifeClass + ";"
|
|
String fieldDesc = "L" + LifeClass + ";"
|
|
@@ -95,29 +95,29 @@ public class MethodLifeCycleVisitor extends MethodVisitor {
|
|
MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PUBLIC, GetLifeMethod, "()" + fieldDesc, null, null)
|
|
MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PUBLIC, GetLifeMethod, "()" + fieldDesc, null, null)
|
|
mv.visitCode()
|
|
mv.visitCode()
|
|
mv.visitVarInsn(Opcodes.ALOAD, 0)
|
|
mv.visitVarInsn(Opcodes.ALOAD, 0)
|
|
- mv.visitFieldInsn(Opcodes.GETFIELD, name, fieldName, fieldDesc)
|
|
|
|
|
|
+ mv.visitFieldInsn(Opcodes.GETFIELD, className, fieldName, fieldDesc)
|
|
Label label = new Label()
|
|
Label label = new Label()
|
|
mv.visitJumpInsn(Opcodes.IFNONNULL, label)
|
|
mv.visitJumpInsn(Opcodes.IFNONNULL, label)
|
|
mv.visitVarInsn(Opcodes.ALOAD, 0)
|
|
mv.visitVarInsn(Opcodes.ALOAD, 0)
|
|
mv.visitVarInsn(Opcodes.ALOAD, 0)
|
|
mv.visitVarInsn(Opcodes.ALOAD, 0)
|
|
mv.visitMethodInsn(Opcodes.INVOKESTATIC, LifeClass, "create", "(Ljava/lang/Object;)" + fieldDesc, false)
|
|
mv.visitMethodInsn(Opcodes.INVOKESTATIC, LifeClass, "create", "(Ljava/lang/Object;)" + fieldDesc, false)
|
|
- mv.visitFieldInsn(Opcodes.PUTFIELD, name, fieldName, fieldDesc)
|
|
|
|
|
|
+ mv.visitFieldInsn(Opcodes.PUTFIELD, className, fieldName, fieldDesc)
|
|
mv.visitLabel(label)
|
|
mv.visitLabel(label)
|
|
mv.visitVarInsn(Opcodes.ALOAD, 0)
|
|
mv.visitVarInsn(Opcodes.ALOAD, 0)
|
|
- mv.visitFieldInsn(Opcodes.GETFIELD, name, fieldName, fieldDesc)
|
|
|
|
|
|
+ mv.visitFieldInsn(Opcodes.GETFIELD, className, fieldName, fieldDesc)
|
|
mv.visitInsn(Opcodes.ARETURN)
|
|
mv.visitInsn(Opcodes.ARETURN)
|
|
mv.visitMaxs(0, 0)
|
|
mv.visitMaxs(0, 0)
|
|
mv.visitEnd()
|
|
mv.visitEnd()
|
|
}
|
|
}
|
|
|
|
|
|
private int pCount;
|
|
private int pCount;
|
|
- private String name;//class name
|
|
|
|
- private Agent agent;
|
|
|
|
|
|
+ private String className;
|
|
|
|
+ private MethodModel agent;
|
|
|
|
|
|
- MethodLifeCycleVisitor(MethodVisitor mv, String name, Agent agent) {
|
|
|
|
|
|
+ MethodLifeCycleVisitor(MethodVisitor mv, MethodModel agent) {
|
|
super(Opcodes.ASM5, mv)
|
|
super(Opcodes.ASM5, mv)
|
|
this.agent = agent
|
|
this.agent = agent
|
|
- this.name = name
|
|
|
|
|
|
+ this.className = agent.className
|
|
for (def item : agent.desc.substring(1, agent.desc.indexOf(")")).split(";")) {
|
|
for (def item : agent.desc.substring(1, agent.desc.indexOf(")")).split(";")) {
|
|
if (!item.equals("")) this.pCount++
|
|
if (!item.equals("")) this.pCount++
|
|
}
|
|
}
|
|
@@ -127,7 +127,7 @@ public class MethodLifeCycleVisitor extends MethodVisitor {
|
|
void visitCode() {
|
|
void visitCode() {
|
|
super.visitCode()
|
|
super.visitCode()
|
|
visitVarInsn(Opcodes.ALOAD, 0)
|
|
visitVarInsn(Opcodes.ALOAD, 0)
|
|
- visitMethodInsn(Opcodes.INVOKEVIRTUAL, name, GetLifeMethod, "()L" + LifeClass + ";", false)
|
|
|
|
|
|
+ visitMethodInsn(Opcodes.INVOKEVIRTUAL, this.className, GetLifeMethod, "()L" + LifeClass + ";", false)
|
|
for (int i = 1; i <= pCount; i++) visitVarInsn(Opcodes.ALOAD, i)
|
|
for (int i = 1; i <= pCount; i++) visitVarInsn(Opcodes.ALOAD, i)
|
|
visitMethodInsn(Opcodes.INVOKEVIRTUAL, LifeClass, agent.name, agent.desc, false)
|
|
visitMethodInsn(Opcodes.INVOKEVIRTUAL, LifeClass, agent.name, agent.desc, false)
|
|
}
|
|
}
|