ClassAdapter.groovy 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package com.gradle.ares
  2. import org.gradle.api.Project
  3. import org.gradle.api.logging.Logger
  4. import org.gradle.api.logging.Logging
  5. import org.objectweb.asm.ClassVisitor
  6. import org.objectweb.asm.FieldVisitor
  7. import org.objectweb.asm.MethodVisitor
  8. import org.objectweb.asm.Opcodes
  9. /**
  10. * Created by Glen on 2017/2/8.
  11. */
  12. public class ClassAdapter extends ClassVisitor {
  13. private Project project;
  14. private Logger logger;
  15. private boolean modify;
  16. private String className;
  17. private String superName;
  18. private String[] interfaces;
  19. private boolean isAdAgent;
  20. private boolean isModifyField;
  21. private Set<MethodModel> addLifecycleSet;
  22. public ClassAdapter(ClassVisitor cv, Project project) {
  23. super(Opcodes.ASM5, cv)
  24. this.project = project
  25. this.logger = Logging.getLogger(ClassAdapter.class)
  26. }
  27. @Override
  28. public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
  29. super.visit(version, access, name, signature, superName, interfaces)
  30. this.className = name
  31. this.superName = superName
  32. this.interfaces = interfaces
  33. //初始化
  34. this.isAdAgent = !MethodAgentVisitor.AgentClass.equals(name)
  35. this.isModifyField = MethodAgentVisitor.AgentClass.equals(name)
  36. for (String su : MethodLifeCycleVisitor.SuperClass) {
  37. if (su.equals(superName)) {
  38. addLifecycleSet = new HashSet<>()
  39. }
  40. }
  41. debug("class:" + this.className + "_" + this.superName + "_" + this.isAdAgent + "_" + this.isModifyField + "_" + (this.addLifecycleSet != null))
  42. }
  43. @Override
  44. public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
  45. MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions)
  46. if (isAdAgent) {
  47. def agent = MethodAgentVisitor.getAgent(this.className, this.superName, name, desc)
  48. if (agent != null) {
  49. modify = true
  50. debug("method:" + access + "_" + name + "_" + desc)
  51. mv = new MethodAgentVisitor(mv, agent)
  52. }
  53. }
  54. if (addLifecycleSet != null) {
  55. def agent = MethodLifeCycleVisitor.getAgent(this.className, this.superName, name, desc)
  56. if (agent != null) {
  57. modify = true
  58. debug("lifecycle:" + access + "_" + name + "_" + desc)
  59. mv = new MethodLifeCycleVisitor(mv, agent)
  60. addLifecycleSet.add(agent)
  61. }
  62. }
  63. return mv
  64. }
  65. @Override
  66. FieldVisitor visitField(int access, String name, String desc, String signature, Object value) {
  67. if (isModifyField) {
  68. if (MethodAgentVisitor.AgentPackageField.equals(name)) {
  69. modify = true
  70. debug("field:" + access + "_" + name + "_" + desc)
  71. return super.visitField(access, name, desc, signature, getPackageName())
  72. }
  73. if (MethodAgentVisitor.AgentPluginFileld.equals(name)) {
  74. modify = true
  75. debug("field:" + access + "_" + name + "_" + desc)
  76. return super.visitField(access, name, desc, signature, true)
  77. }
  78. }
  79. return super.visitField(access, name, desc, signature, value)
  80. }
  81. @Override
  82. void visitEnd() {
  83. if (addLifecycleSet != null) {
  84. MethodLifeCycleVisitor.addMethods(addLifecycleSet, this.className, this.superName, this.cv)
  85. }
  86. super.visitEnd()
  87. }
  88. public boolean getModify() {
  89. return modify
  90. }
  91. private String getPackageName() {
  92. String root = this.project.buildDir.getParentFile().absolutePath
  93. String xml = root + "/src/main/" + "AndroidManifest.xml"
  94. def file = new File(xml);
  95. def lines = file.readLines()
  96. for (line in lines) {
  97. line = line.replace(" ", "")
  98. if (line.contains("package")) {
  99. line = line.replace("package=\"", "")
  100. String packageName = line.substring(0, line.indexOf("\""))
  101. return packageName
  102. }
  103. }
  104. return null
  105. }
  106. private void debug(def msg) {
  107. this.logger.lifecycle(":" + this.project.name + ":" + msg)
  108. }
  109. }