weiguo vor 7 Jahren
Ursprung
Commit
1cea678df9

+ 0 - 1
app/src/main/java/com/uddream/plugin/MainActivity.java

@@ -32,7 +32,6 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
         fab.setOnClickListener(new Test());
         fab.setOnClickListener(this);
         new CheckBox(this).setOnCheckedChangeListener(this);
-
     }
 
     @Override

+ 2 - 2
buildSrc/src/main/groovy/com/gradle/ares/ClassAdapter.groovy

@@ -55,7 +55,7 @@ public class ClassAdapter extends ClassVisitor {
             addAgents.add(agent)
 
             modify = true
-            mv = new MethodLifeCycleVisitor(mv, name, agent)
+            mv = new MethodLifeCycleVisitor(mv, this.name, agent)
         }
         return mv
     }
@@ -68,7 +68,7 @@ public class ClassAdapter extends ClassVisitor {
     @Override
     void visitEnd() {
         if (addAgents != null) {
-            MethodLifeCycleVisitor.addMethods(addAgents, name, superName, this.cv)
+            MethodLifeCycleVisitor.addMethods(addAgents, this.name, this.superName, this.cv)
         }
         super.visitEnd()
     }

+ 25 - 26
buildSrc/src/main/groovy/com/gradle/ares/MethodLifeCycleVisitor.groovy

@@ -1,6 +1,5 @@
 package com.gradle.ares
 
-import org.gradle.api.logging.Logging
 import org.objectweb.asm.ClassVisitor
 import org.objectweb.asm.FieldVisitor
 import org.objectweb.asm.Label
@@ -55,28 +54,28 @@ public class MethodLifeCycleVisitor extends MethodVisitor {
 
     public final
     static void addMethods(Set<Agent> agentSet, String name, String superName, ClassVisitor cv) {
-        addLifeCycleFieldMethod(cv)
+        addLifeCycleFieldMethod(name, 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()
+                //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()
             }
         }
     }
@@ -85,7 +84,7 @@ public class MethodLifeCycleVisitor extends MethodVisitor {
      * 注入代理字段及初始化方法
      * @param cv
      */
-    private final static void addLifeCycleFieldMethod(ClassVisitor cv) {
+    private final static void addLifeCycleFieldMethod(String name, ClassVisitor cv) {
         //desc
         String fieldName = "\$lifeCycleAresDelegate"
         String fieldDesc = "L" + LifeClass + ";"
@@ -96,23 +95,23 @@ public class MethodLifeCycleVisitor extends MethodVisitor {
         MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PUBLIC, GetLifeMethod, "()" + fieldDesc, null, null)
         mv.visitCode()
         mv.visitVarInsn(Opcodes.ALOAD, 0)
-        mv.visitFieldInsn(Opcodes.GETFIELD, LifeClass, fieldName, fieldDesc)
+        mv.visitFieldInsn(Opcodes.GETFIELD, name, 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, LifeClass, "create", "(Ljava/lang/Object;)" + fieldDesc, false)
-        mv.visitFieldInsn(Opcodes.PUTFIELD, LifeClass, fieldName, fieldDesc)
+        mv.visitFieldInsn(Opcodes.PUTFIELD, name, fieldName, fieldDesc)
         mv.visitLabel(label)
         mv.visitVarInsn(Opcodes.ALOAD, 0)
-        mv.visitFieldInsn(Opcodes.GETFIELD, LifeClass, fieldName, fieldDesc)
+        mv.visitFieldInsn(Opcodes.GETFIELD, name, fieldName, fieldDesc)
         mv.visitInsn(Opcodes.ARETURN)
         mv.visitMaxs(0, 0)
         mv.visitEnd()
     }
 
     private int pCount;
-    private String name;
+    private String name;//class name
     private Agent agent;
 
     MethodLifeCycleVisitor(MethodVisitor mv, String name, Agent agent) {