-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Chao Zhang
committed
Apr 6, 2021
1 parent
39cc830
commit ca29bed
Showing
5 changed files
with
118 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
distributionBase=GRADLE_USER_HOME | ||
distributionPath=wrapper/dists | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-6.8.3-all.zip | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-7.0-milestone-3-bin.zip | ||
zipStoreBase=GRADLE_USER_HOME | ||
zipStorePath=wrapper/dists |
25 changes: 25 additions & 0 deletions
25
src/main/kotlin/io/github/chao2zhang/LoggingLiveDataClassVisitor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package io.github.chao2zhang | ||
|
||
import org.objectweb.asm.ClassVisitor | ||
import org.objectweb.asm.MethodVisitor | ||
import org.objectweb.asm.Opcodes.ASM9 | ||
|
||
class LoggingLiveDataClassVisitor( | ||
private val classVisitor: ClassVisitor | ||
) : ClassVisitor(ASM9, classVisitor) { | ||
|
||
override fun visitMethod( | ||
access: Int, | ||
name: String?, | ||
descriptor: String?, | ||
signature: String?, | ||
exceptions: Array<out String>? | ||
): MethodVisitor { | ||
val methodVisitor = classVisitor.visitMethod(access, name, descriptor, signature, exceptions) | ||
return if (name == "considerNotify") { | ||
LoggingLiveDataMethodVisitor(methodVisitor) | ||
} else { | ||
methodVisitor | ||
} | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
src/main/kotlin/io/github/chao2zhang/LoggingLiveDataMethodVisitor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package io.github.chao2zhang | ||
|
||
import org.objectweb.asm.MethodVisitor | ||
import org.objectweb.asm.Opcodes | ||
import org.objectweb.asm.Opcodes.ASM9 | ||
|
||
/** | ||
* Add the following code after invoking `observer.onChanged`: | ||
* ``` | ||
* Log.i("LiveData", "considerNotify() called with LiveData = " + this + " Observer = " + observer.mObserver + " Data = " + this.mData); | ||
* ``` | ||
*/ | ||
class LoggingLiveDataMethodVisitor( | ||
private val methodVisitor: MethodVisitor | ||
) : MethodVisitor(ASM9, methodVisitor) { | ||
|
||
override fun visitMethodInsn( | ||
opcode: Int, | ||
owner: String?, | ||
name: String?, | ||
descriptor: String?, | ||
isInterface: Boolean | ||
) { | ||
methodVisitor.visitMethodInsn(opcode, owner, name, descriptor, isInterface) | ||
if (opcode == Opcodes.INVOKEINTERFACE && owner == "androidx/lifecycle/Observer" && name == "onChanged") { | ||
methodVisitor.visitLdcInsn("LiveData") | ||
methodVisitor.visitTypeInsn(Opcodes.NEW, "java/lang/StringBuilder") | ||
methodVisitor.visitInsn(Opcodes.DUP) | ||
methodVisitor.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/StringBuilder", "<init>", "()V", false) | ||
methodVisitor.visitLdcInsn("considerNotify() called with LiveData = ") | ||
methodVisitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/String;)Ljava/lang/StringBuilder;", false) | ||
methodVisitor.visitVarInsn(Opcodes.ALOAD, 0) | ||
methodVisitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/Object;)Ljava/lang/StringBuilder;", false) | ||
methodVisitor.visitLdcInsn(" Observer = ") | ||
methodVisitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/String;)Ljava/lang/StringBuilder;", false) | ||
methodVisitor.visitVarInsn(Opcodes.ALOAD, 1) | ||
methodVisitor.visitFieldInsn(Opcodes.GETFIELD, "androidx/lifecycle/LiveData\$ObserverWrapper", "mObserver", "Landroidx/lifecycle/Observer;") | ||
methodVisitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/Object;)Ljava/lang/StringBuilder;", false) | ||
methodVisitor.visitLdcInsn(" Data = ") | ||
methodVisitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/String;)Ljava/lang/StringBuilder;", false) | ||
methodVisitor.visitVarInsn(Opcodes.ALOAD, 0) | ||
methodVisitor.visitFieldInsn(Opcodes.GETFIELD, "androidx/lifecycle/LiveData", "mData", "Ljava/lang/Object;") | ||
methodVisitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/Object;)Ljava/lang/StringBuilder;", false) | ||
methodVisitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuilder", "toString", "()Ljava/lang/String;", false) | ||
methodVisitor.visitMethodInsn(Opcodes.INVOKESTATIC, "android/util/Log", "i", "(Ljava/lang/String;Ljava/lang/String;)I", false) | ||
methodVisitor.visitInsn(Opcodes.POP) | ||
} | ||
} | ||
|
||
|
||
override fun visitMaxs(maxStack: Int, maxLocals: Int) { | ||
methodVisitor.visitMaxs(3, 2) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters