|
@@ -1,18 +1,19 @@
|
|
|
package com.gradle.ares
|
|
|
|
|
|
+import org.gradle.api.logging.Logging
|
|
|
import org.objectweb.asm.ClassVisitor
|
|
|
-import org.objectweb.asm.ClassWriter
|
|
|
+import org.objectweb.asm.FieldVisitor
|
|
|
import org.objectweb.asm.Label
|
|
|
import org.objectweb.asm.MethodVisitor
|
|
|
import org.objectweb.asm.Opcodes
|
|
|
-import org.objectweb.asm.commons.GeneratorAdapter
|
|
|
|
|
|
/**
|
|
|
* Created by Glen on 2017/2/8.
|
|
|
*/
|
|
|
|
|
|
-public class MethodLifeCycleVisitor extends GeneratorAdapter {
|
|
|
+public class MethodLifeCycleVisitor extends MethodVisitor {
|
|
|
public final static String LifeClass = "com/uddream/plugin/LifeCycleDelegate";
|
|
|
+ public final static String GetLifeMethod = "\$getLifeCycleAresDelegate";
|
|
|
|
|
|
public final static String[] SuperClass = [
|
|
|
"android/app/Activity",
|
|
@@ -27,7 +28,6 @@ public class MethodLifeCycleVisitor extends GeneratorAdapter {
|
|
|
] as String[]
|
|
|
|
|
|
public final static Agent[] Modify = [
|
|
|
- new Agent("\$getLifeCycleAresDelegate", "()L" + LifeClass + ";"),
|
|
|
new Agent("onCreate", "(Landroid/os/Bundle;)V"),
|
|
|
new Agent("onResume", "()V"),
|
|
|
new Agent("onPause", "()V"),
|
|
@@ -53,32 +53,72 @@ public class MethodLifeCycleVisitor extends GeneratorAdapter {
|
|
|
return null
|
|
|
}
|
|
|
|
|
|
- public final static void addMethods(Set<Agent> agentSet, ClassVisitor cv) {
|
|
|
- cv.visitField(Opcodes.ACC_PUBLIC, "\$lifeCycleAresDelegate", "L" + MethodLifeCycleVisitor.LifeClass + ";", null, null)
|
|
|
- MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PUBLIC, "\$getLifeCycleAresDelegate", "()Lcom/uddream/plugin/LifeCycleDelegate;", null, null)
|
|
|
+ public final
|
|
|
+ static void addMethods(Set<Agent> agentSet, String name, String superName, ClassVisitor cv) {
|
|
|
+ addLifeCycleFieldMethod(cv)
|
|
|
+ for (Agent agent : Modify) {
|
|
|
+ if (!agentSet.contains(agent)) {
|
|
|
+// //method
|
|
|
+// int count = 0;
|
|
|
+// for (def item : agent.desc.substring(1, agent.desc.indexOf(")")).split(";")) {
|
|
|
+// if (!item.equals("")) count++
|
|
|
+// }
|
|
|
+//
|
|
|
+// MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PUBLIC, agent.name, agent.desc, null, null)
|
|
|
+// mv.visitCode()
|
|
|
+// mv.visitVarInsn(Opcodes.ALOAD, 0)
|
|
|
+// mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, name, GetLifeMethod, "()L" + LifeClass + ";", false)
|
|
|
+// for (int i = 1; i <= count; i++) mv.visitVarInsn(Opcodes.ALOAD, i)
|
|
|
+// mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, LifeClass, agent.name, agent.desc, false)
|
|
|
+//
|
|
|
+// mv.visitVarInsn(Opcodes.ALOAD, 0)
|
|
|
+// for (int i = 1; i <= count; i++) mv.visitVarInsn(Opcodes.ALOAD, i)
|
|
|
+// mv.visitMethodInsn(Opcodes.INVOKESPECIAL, superName, agent.name, agent.desc, false)
|
|
|
+// mv.visitInsn(Opcodes.RETURN)
|
|
|
+// mv.visitMaxs(0, 0)
|
|
|
+// mv.visitEnd()
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 注入代理字段及初始化方法
|
|
|
+ * @param cv
|
|
|
+ */
|
|
|
+ private final static void addLifeCycleFieldMethod(ClassVisitor cv) {
|
|
|
+ //desc
|
|
|
+ String fieldName = "\$lifeCycleAresDelegate"
|
|
|
+ String fieldDesc = "L" + LifeClass + ";"
|
|
|
+ //field
|
|
|
+ FieldVisitor fv = cv.visitField(Opcodes.ACC_PUBLIC, fieldName, fieldDesc, null, null)
|
|
|
+ fv.visitEnd()
|
|
|
+ //method
|
|
|
+ MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PUBLIC, GetLifeMethod, "()" + fieldDesc, null, null)
|
|
|
mv.visitCode()
|
|
|
mv.visitVarInsn(Opcodes.ALOAD, 0)
|
|
|
- mv.visitFieldInsn(Opcodes.GETFIELD, "com/uddream/plugin/LifeCycleDelegate", "\$lifeCycleAresDelegate", "Lcom/uddream/plugin/LifeCycleDelegate;")
|
|
|
+ mv.visitFieldInsn(Opcodes.GETFIELD, LifeClass, fieldName, fieldDesc)
|
|
|
Label label = new Label()
|
|
|
mv.visitJumpInsn(Opcodes.IFNONNULL, label)
|
|
|
mv.visitVarInsn(Opcodes.ALOAD, 0)
|
|
|
mv.visitVarInsn(Opcodes.ALOAD, 0)
|
|
|
- mv.visitMethodInsn(Opcodes.INVOKESTATIC, "com/uddream/plugin/LifeCycleDelegate", "create", "(Ljava/lang/Object;)Lcom/uddream/plugin/LifeCycleDelegate;", false)
|
|
|
- mv.visitFieldInsn(Opcodes.PUTFIELD, "com/uddream/plugin/LifeCycleDelegate", "\$lifeCycleAresDelegate", "Lcom/uddream/plugin/LifeCycleDelegate;")
|
|
|
+ mv.visitMethodInsn(Opcodes.INVOKESTATIC, LifeClass, "create", "(Ljava/lang/Object;)" + fieldDesc, false)
|
|
|
+ mv.visitFieldInsn(Opcodes.PUTFIELD, LifeClass, fieldName, fieldDesc)
|
|
|
mv.visitLabel(label)
|
|
|
mv.visitVarInsn(Opcodes.ALOAD, 0)
|
|
|
- mv.visitFieldInsn(Opcodes.GETFIELD, "com/uddream/plugin/LifeCycleDelegate", "\$lifeCycleAresDelegate", "Lcom/uddream/plugin/LifeCycleDelegate;")
|
|
|
+ mv.visitFieldInsn(Opcodes.GETFIELD, LifeClass, fieldName, fieldDesc)
|
|
|
mv.visitInsn(Opcodes.ARETURN)
|
|
|
mv.visitMaxs(0, 0)
|
|
|
mv.visitEnd()
|
|
|
}
|
|
|
|
|
|
private int pCount;
|
|
|
+ private String name;
|
|
|
private Agent agent;
|
|
|
|
|
|
- MethodLifeCycleVisitor(MethodVisitor mv, Agent agent) {
|
|
|
- super(Opcodes.ASM5, mv, agent.access, agent.name, agent.desc)
|
|
|
+ MethodLifeCycleVisitor(MethodVisitor mv, String name, Agent agent) {
|
|
|
+ super(Opcodes.ASM5, mv)
|
|
|
this.agent = agent
|
|
|
+ this.name = name
|
|
|
for (def item : agent.desc.substring(1, agent.desc.indexOf(")")).split(";")) {
|
|
|
if (!item.equals("")) this.pCount++
|
|
|
}
|
|
@@ -87,16 +127,9 @@ public class MethodLifeCycleVisitor extends GeneratorAdapter {
|
|
|
@Override
|
|
|
void visitCode() {
|
|
|
super.visitCode()
|
|
|
- //for (int i = 0; i < pCount; i++) loadArg(i)
|
|
|
- //visitMethodInsn(Opcodes.INVOKESTATIC, LifeClass, agent.name, agent.desc, false)
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void visitInsn(int opcode) {
|
|
|
- if (opcode == Opcodes.RETURN) {
|
|
|
- //for (int i = 0; i < pCount; i++) loadArg(i)
|
|
|
- //visitMethodInsn(Opcodes.INVOKESTATIC, LifeClass, agent.name, agent.desc, false)
|
|
|
- }
|
|
|
- super.visitInsn(opcode);
|
|
|
+ visitVarInsn(Opcodes.ALOAD, 0)
|
|
|
+ visitMethodInsn(Opcodes.INVOKEVIRTUAL, name, GetLifeMethod, "()L" + LifeClass + ";", false)
|
|
|
+ for (int i = 1; i <= pCount; i++) visitVarInsn(Opcodes.ALOAD, i)
|
|
|
+ visitMethodInsn(Opcodes.INVOKEVIRTUAL, LifeClass, agent.name, agent.desc, false)
|
|
|
}
|
|
|
}
|