|
@@ -21,7 +21,9 @@ public class ClassAdapter extends ClassVisitor {
|
|
|
private String superName;
|
|
|
private String[] interfaces;
|
|
|
|
|
|
- private Set<MethodModel> addAgents;
|
|
|
+ private boolean isAdAgent;
|
|
|
+ private boolean isModifyField;
|
|
|
+ private Set<MethodModel> addLifecycleSet;
|
|
|
|
|
|
public ClassAdapter(ClassVisitor cv, Project project) {
|
|
|
super(Opcodes.ASM5, cv)
|
|
@@ -35,48 +37,89 @@ public class ClassAdapter extends ClassVisitor {
|
|
|
this.className = name
|
|
|
this.superName = superName
|
|
|
this.interfaces = interfaces
|
|
|
- debug("class:" + name + "_" + superName + "_" + 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)
|
|
|
- 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(this.className, this.superName, name, desc)
|
|
|
- if (agent != null) {
|
|
|
- debug("lifecycle:" + access + "_" + name + "_" + desc)
|
|
|
- if (addAgents == null) addAgents = new HashSet<>();
|
|
|
- addAgents.add(agent)
|
|
|
+ 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)
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- modify = true
|
|
|
- mv = new MethodLifeCycleVisitor(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 (addAgents != null) {
|
|
|
- MethodLifeCycleVisitor.addMethods(addAgents, this.className, this.superName, this.cv)
|
|
|
+ if (addLifecycleSet != null) {
|
|
|
+ MethodLifeCycleVisitor.addMethods(addLifecycleSet, this.className, this.superName, this.cv)
|
|
|
}
|
|
|
super.visitEnd()
|
|
|
}
|
|
|
|
|
|
- boolean getModify() {
|
|
|
+ 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)
|
|
|
}
|