package com.gradle.ares import org.gradle.api.Project import org.gradle.api.logging.Logger import org.gradle.api.logging.Logging import org.objectweb.asm.ClassVisitor import org.objectweb.asm.FieldVisitor import org.objectweb.asm.MethodVisitor import org.objectweb.asm.Opcodes /** * Created by Glen on 2017/2/8. */ public class ClassAdapter extends ClassVisitor { private Project project; private Logger logger; private boolean modify; private String className; private String superName; private String[] interfaces; private boolean isAdAgent; private boolean isModifyField; private Set addLifecycleSet; public ClassAdapter(ClassVisitor cv, Project project) { super(Opcodes.ASM5, cv) this.project = project this.logger = Logging.getLogger(ClassAdapter.class) } @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.className = name this.superName = superName this.interfaces = interfaces //初始化 this.isAdAgent = !MethodAgentVisitor.AgentClass.equals(name) this.isModifyField = MethodAgentVisitor.AgentClass.equals(name) for (String su : MethodLifeCycleVisitor.SuperClass) { if (su.equals(superName)) { addLifecycleSet = new HashSet<>() } } debug("class:" + this.className + "_" + this.superName + "_" + this.isAdAgent + "_" + this.isModifyField + "_" + (this.addLifecycleSet != null)) } @Override public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) { MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions) if (isAdAgent) { 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) } } if (addLifecycleSet != null) { def agent = MethodLifeCycleVisitor.getAgent(this.className, this.superName, name, desc) if (agent != null) { modify = true debug("lifecycle:" + access + "_" + name + "_" + desc) mv = new MethodLifeCycleVisitor(mv, agent) addLifecycleSet.add(agent) } } return mv } @Override FieldVisitor visitField(int access, String name, String desc, String signature, Object value) { if (isModifyField) { if (MethodAgentVisitor.AgentPackageField.equals(name)) { modify = true debug("field:" + access + "_" + name + "_" + desc) return super.visitField(access, name, desc, signature, getPackageName()) } if (MethodAgentVisitor.AgentPluginFileld.equals(name)) { modify = true debug("field:" + access + "_" + name + "_" + desc) return super.visitField(access, name, desc, signature, true) } } return super.visitField(access, name, desc, signature, value) } @Override void visitEnd() { if (addLifecycleSet != null) { MethodLifeCycleVisitor.addMethods(addLifecycleSet, this.className, this.superName, this.cv) } super.visitEnd() } public boolean getModify() { return modify } private String getPackageName() { String root = this.project.buildDir.getParentFile().absolutePath String xml = root + "/src/main/" + "AndroidManifest.xml" def file = new File(xml); def lines = file.readLines() for (line in lines) { line = line.replace(" ", "") if (line.contains("package")) { line = line.replace("package=\"", "") String packageName = line.substring(0, line.indexOf("\"")) return packageName } } return null } private void debug(def msg) { this.logger.lifecycle(":" + this.project.name + ":" + msg) } }