weiguo 7 سال پیش
والد
کامیت
931541fd29

+ 1 - 1
app/build.gradle

@@ -6,7 +6,7 @@ android {
     buildToolsVersion "23.0.1"
 
     defaultConfig {
-        applicationId "com.uddream.gradle"
+        applicationId "com.uddream.gradle.plugin"
         minSdkVersion 19
         targetSdkVersion 23
         versionCode 1

+ 3 - 1
buildSrc/src/main/groovy/com/gradle/ares/AresTransform.groovy

@@ -55,7 +55,9 @@ public class AresTransform extends ModifyClassTransform {
      * @return
      */
     private boolean modifyClass(File clazz) {
-        if (clazz.absolutePath.replace("\\", "/").endsWith(MethodAgentVisitor.AgentClass + ".class")) return
+        def str = clazz.absolutePath.replace("\\", "/")
+        if (str.endsWith(MethodAgentVisitor.AgentClass + ".class")) return
+        if (str.endsWith(MethodLifeCycleVisitor.LifeClass + ".class")) return
 
         ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS)
         ClassReader cr = new ClassReader(clazz.bytes)

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

@@ -17,11 +17,11 @@ public class ClassAdapter extends ClassVisitor {
     private Logger logger;
 
     private boolean modify;
-    private String name;
+    private String className;
     private String superName;
     private String[] interfaces;
 
-    private Set<Agent> addAgents;
+    private Set<MethodModel> addAgents;
 
     public ClassAdapter(ClassVisitor cv, Project project) {
         super(Opcodes.ASM5, cv)
@@ -32,7 +32,7 @@ public class ClassAdapter extends ClassVisitor {
     @Override
     public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
         super.visit(version, access, name, signature, superName, interfaces)
-        this.name = name
+        this.className = name
         this.superName = superName
         this.interfaces = interfaces
         debug("class:" + name + "_" + superName + "_" + interfaces)
@@ -41,21 +41,21 @@ public class ClassAdapter extends ClassVisitor {
     @Override
     public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
         MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions)
-        def agent = MethodAgentVisitor.getAgent(access, name, desc)
+        def agent = MethodAgentVisitor.getAgent(this.className, this.superName, name, desc)
         if (agent != null) {
             modify = true
             debug("method:" + access + "_" + name + "_" + desc)
             mv = new MethodAgentVisitor(mv, agent)
         }
 
-        agent = MethodLifeCycleVisitor.getAgent(superName, access, name, desc)
+        agent = MethodLifeCycleVisitor.getAgent(this.className, this.superName, name, desc)
         if (agent != null) {
             debug("lifecycle:" + access + "_" + name + "_" + desc)
             if (addAgents == null) addAgents = new HashSet<>();
             addAgents.add(agent)
 
             modify = true
-            mv = new MethodLifeCycleVisitor(mv, this.name, agent)
+            mv = new MethodLifeCycleVisitor(mv, agent)
         }
         return mv
     }
@@ -68,7 +68,7 @@ public class ClassAdapter extends ClassVisitor {
     @Override
     void visitEnd() {
         if (addAgents != null) {
-            MethodLifeCycleVisitor.addMethods(addAgents, this.name, this.superName, this.cv)
+            MethodLifeCycleVisitor.addMethods(addAgents, this.className, this.superName, this.cv)
         }
         super.visitEnd()
     }

+ 10 - 9
buildSrc/src/main/groovy/com/gradle/ares/MethodAgentVisitor.groovy

@@ -10,15 +10,16 @@ import org.objectweb.asm.Opcodes
 public class MethodAgentVisitor extends MethodVisitor {
     public final static String AgentClass = "com/uddream/plugin/Agent";
 
-    public final static Agent[] Modify = [
-            new Agent("onClick", "(Landroid/view/View;)V"),
-            new Agent("onCheckedChanged", "(Landroid/widget/CompoundButton;Z)V")
-    ] as Agent[]
+    public final static MethodModel[] Modify = [
+            new MethodModel("onClick", "(Landroid/view/View;)V"),
+            new MethodModel("onCheckedChanged", "(Landroid/widget/CompoundButton;Z)V")
+    ] as MethodModel[]
 
-    public final static Agent getAgent(int access, String name, String desc) {
-        for (Agent item : Modify) {
+    public static MethodModel getAgent(String className, String superName, String name, String desc) {
+        for (MethodModel item : Modify) {
             if (item.name.equals(name) && item.desc.equals(desc)) {
-                item.access = access
+                item.className = className
+                item.superName = superName
                 return item
             }
         }
@@ -26,9 +27,9 @@ public class MethodAgentVisitor extends MethodVisitor {
     }
 
     private int pCount;
-    private Agent agent;
+    private MethodModel agent;
 
-    MethodAgentVisitor(MethodVisitor mv, Agent agent) {
+    MethodAgentVisitor(MethodVisitor mv, MethodModel agent) {
         super(Opcodes.ASM5, mv)
         this.agent = agent
         for (def item : agent.desc.substring(1, agent.desc.indexOf(")")).split(";")) {

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

@@ -26,23 +26,24 @@ public class MethodLifeCycleVisitor extends MethodVisitor {
             "android/app/DialogFragment"
     ] 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) {
             if (s.equals(superName)) {
-                for (Agent item : Modify) {
+                for (MethodModel item : Modify) {
                     if (item.name.equals(name) && item.desc.equals(desc)) {
-                        item.access = access
+                        item.className = className
+                        item.superName = superName
                         return item
                     }
                 }
@@ -52,10 +53,9 @@ public class MethodLifeCycleVisitor extends MethodVisitor {
         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)) {
                 //method
                 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)
                 mv.visitCode()
                 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)
                 mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, LifeClass, agent.name, agent.desc, false)
 
@@ -84,7 +84,7 @@ public class MethodLifeCycleVisitor extends MethodVisitor {
      * 注入代理字段及初始化方法
      * @param cv
      */
-    private final static void addLifeCycleFieldMethod(String name, ClassVisitor cv) {
+    private static void addLifeCycleFieldMethod(String className, ClassVisitor cv) {
         //desc
         String fieldName = "\$lifeCycleAresDelegate"
         String fieldDesc = "L" + LifeClass + ";"
@@ -95,29 +95,29 @@ 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, name, fieldName, fieldDesc)
+        mv.visitFieldInsn(Opcodes.GETFIELD, className, 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, name, fieldName, fieldDesc)
+        mv.visitFieldInsn(Opcodes.PUTFIELD, className, fieldName, fieldDesc)
         mv.visitLabel(label)
         mv.visitVarInsn(Opcodes.ALOAD, 0)
-        mv.visitFieldInsn(Opcodes.GETFIELD, name, fieldName, fieldDesc)
+        mv.visitFieldInsn(Opcodes.GETFIELD, className, fieldName, fieldDesc)
         mv.visitInsn(Opcodes.ARETURN)
         mv.visitMaxs(0, 0)
         mv.visitEnd()
     }
 
     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)
         this.agent = agent
-        this.name = name
+        this.className = agent.className
         for (def item : agent.desc.substring(1, agent.desc.indexOf(")")).split(";")) {
             if (!item.equals("")) this.pCount++
         }
@@ -127,7 +127,7 @@ public class MethodLifeCycleVisitor extends MethodVisitor {
     void visitCode() {
         super.visitCode()
         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)
         visitMethodInsn(Opcodes.INVOKEVIRTUAL, LifeClass, agent.name, agent.desc, false)
     }

+ 4 - 7
buildSrc/src/main/groovy/com/gradle/ares/Agent.groovy

@@ -4,20 +4,17 @@ package com.gradle.ares;
  * Created by Glen on 2017/2/8.
  */
 
-public class Agent {
-    int access;
+public class MethodModel {
+    String className;
+    String superName;
     String name;
     String desc;
 
-    Agent(String name, String desc) {
+    MethodModel(String name, String desc) {
         this.name = name
         this.desc = desc
     }
 
-    public int getAccess() {
-        return access;
-    }
-
     public String getName() {
         return name;
     }