-
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 24, 2021
1 parent
12cc37f
commit 8c3d1a0
Showing
13 changed files
with
334 additions
and
104 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
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,2 +1,2 @@ | ||
rootProject.name = 'LoggingLiveData' | ||
rootProject.name = 'LiveDataDebugger' | ||
|
52 changes: 52 additions & 0 deletions
52
src/main/kotlin/io/github/chao2zhang/ConsiderNotifyMethodVisitor.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,52 @@ | ||
package io.github.chao2zhang | ||
|
||
import org.objectweb.asm.MethodVisitor | ||
import org.objectweb.asm.Opcodes.* | ||
|
||
/** | ||
* Add the following code after invoking `observer.onChanged`: | ||
* ``` | ||
* Log.i("LiveData", "considerNotify() called with LiveData = " + this + " Observer = " + observer.mObserver + " Data = " + this.mData); | ||
* ``` | ||
*/ | ||
class ConsiderNotifyMethodVisitor( | ||
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 == INVOKEINTERFACE && owner == "androidx/lifecycle/Observer" && name == "onChanged") { | ||
methodVisitor.visitLdcInsn("LiveData") | ||
methodVisitor.visitTypeInsn(NEW, "java/lang/StringBuilder") | ||
methodVisitor.visitInsn(DUP) | ||
methodVisitor.visitMethodInsn(INVOKESPECIAL, "java/lang/StringBuilder", "<init>", "()V", false) | ||
methodVisitor.visitLdcInsn("considerNotify() called with LiveData = ") | ||
methodVisitor.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/String;)Ljava/lang/StringBuilder;", false) | ||
methodVisitor.visitVarInsn(ALOAD, 0) | ||
methodVisitor.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/Object;)Ljava/lang/StringBuilder;", false) | ||
methodVisitor.visitLdcInsn(" Observer = ") | ||
methodVisitor.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/String;)Ljava/lang/StringBuilder;", false) | ||
methodVisitor.visitVarInsn(ALOAD, 1) | ||
methodVisitor.visitFieldInsn(GETFIELD, "androidx/lifecycle/LiveData\$ObserverWrapper", "mObserver", "Landroidx/lifecycle/Observer;") | ||
methodVisitor.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/Object;)Ljava/lang/StringBuilder;", false) | ||
methodVisitor.visitLdcInsn(" Data = ") | ||
methodVisitor.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/String;)Ljava/lang/StringBuilder;", false) | ||
methodVisitor.visitVarInsn(ALOAD, 0) | ||
methodVisitor.visitFieldInsn(GETFIELD, "androidx/lifecycle/LiveData", "mData", "Ljava/lang/Object;") | ||
methodVisitor.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/Object;)Ljava/lang/StringBuilder;", false) | ||
methodVisitor.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "toString", "()Ljava/lang/String;", false) | ||
methodVisitor.visitMethodInsn(INVOKESTATIC, "android/util/Log", "i", "(Ljava/lang/String;Ljava/lang/String;)I", false) | ||
methodVisitor.visitInsn(POP) | ||
} | ||
} | ||
|
||
override fun visitMaxs(maxStack: Int, maxLocals: Int) { | ||
methodVisitor.visitMaxs(3, 2) | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
src/main/kotlin/io/github/chao2zhang/LiveDataClassVisitor.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,45 @@ | ||
package io.github.chao2zhang | ||
|
||
import org.objectweb.asm.ClassVisitor | ||
import org.objectweb.asm.MethodVisitor | ||
import org.objectweb.asm.Opcodes.* | ||
|
||
|
||
class LiveDataClassVisitor( | ||
private val classVisitor: ClassVisitor | ||
) : ClassVisitor(ASM9, classVisitor) { | ||
|
||
override fun visit( | ||
version: Int, | ||
access: Int, | ||
name: String?, | ||
signature: String?, | ||
superName: String?, | ||
interfaces: Array<out String>? | ||
) { | ||
super.visit(version, access, name, signature, superName, interfaces) | ||
visitLogValueMethod(classVisitor.visitMethod( | ||
ACC_PRIVATE, | ||
"logValue", | ||
"(Ljava/lang/String;Ljava/lang/Object;)V", | ||
"(Ljava/lang/String;TT;)V", | ||
null | ||
)) | ||
} | ||
|
||
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 when (name) { | ||
"considerNotify" -> ConsiderNotifyMethodVisitor(methodVisitor) | ||
"postValue" -> PostValueMethodVisitor(methodVisitor) | ||
"setValue" -> SetValueMethodVisitor(methodVisitor) | ||
else -> methodVisitor | ||
} | ||
} | ||
} |
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
143 changes: 143 additions & 0 deletions
143
src/main/kotlin/io/github/chao2zhang/LogValueMethodVisitor.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,143 @@ | ||
package io.github.chao2zhang | ||
|
||
import org.objectweb.asm.Label | ||
import org.objectweb.asm.MethodVisitor | ||
import org.objectweb.asm.Opcodes | ||
|
||
/** | ||
* private void logValue(String methodName, T value) { | ||
* StackTraceElement[] traces = Thread.currentThread().getStackTrace(); | ||
* if (traces.length > 3) { | ||
* Log.i("LiveData", methodName + "() called with LiveData = " + this + " Value = " + value + " Caller = " + traces[3].toString()); | ||
* } | ||
* } | ||
*/ | ||
fun visitLogValueMethod(methodVisitor: MethodVisitor) { | ||
methodVisitor.visitCode() | ||
val label0 = Label() | ||
methodVisitor.visitLabel(label0) | ||
methodVisitor.visitLineNumber(302, label0) | ||
methodVisitor.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Thread", "currentThread", "()Ljava/lang/Thread;", false) | ||
methodVisitor.visitMethodInsn( | ||
Opcodes.INVOKEVIRTUAL, | ||
"java/lang/Thread", | ||
"getStackTrace", | ||
"()[Ljava/lang/StackTraceElement;", | ||
false | ||
) | ||
methodVisitor.visitVarInsn(Opcodes.ASTORE, 3) | ||
val label1 = Label() | ||
methodVisitor.visitLabel(label1) | ||
methodVisitor.visitLineNumber(303, label1) | ||
methodVisitor.visitVarInsn(Opcodes.ALOAD, 3) | ||
methodVisitor.visitInsn(Opcodes.ARRAYLENGTH) | ||
methodVisitor.visitInsn(Opcodes.ICONST_5) | ||
val label2 = Label() | ||
methodVisitor.visitJumpInsn(Opcodes.IF_ICMPLE, label2) | ||
val label3 = Label() | ||
methodVisitor.visitLabel(label3) | ||
methodVisitor.visitLineNumber(304, label3) | ||
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.visitVarInsn(Opcodes.ALOAD, 1) | ||
methodVisitor.visitMethodInsn( | ||
Opcodes.INVOKEVIRTUAL, | ||
"java/lang/StringBuilder", | ||
"append", | ||
"(Ljava/lang/String;)Ljava/lang/StringBuilder;", | ||
false | ||
) | ||
methodVisitor.visitLdcInsn("() 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(" Value = ") | ||
methodVisitor.visitMethodInsn( | ||
Opcodes.INVOKEVIRTUAL, | ||
"java/lang/StringBuilder", | ||
"append", | ||
"(Ljava/lang/String;)Ljava/lang/StringBuilder;", | ||
false | ||
) | ||
methodVisitor.visitVarInsn(Opcodes.ALOAD, 2) | ||
methodVisitor.visitMethodInsn( | ||
Opcodes.INVOKEVIRTUAL, | ||
"java/lang/StringBuilder", | ||
"append", | ||
"(Ljava/lang/Object;)Ljava/lang/StringBuilder;", | ||
false | ||
) | ||
methodVisitor.visitLdcInsn(" Caller = ") | ||
methodVisitor.visitMethodInsn( | ||
Opcodes.INVOKEVIRTUAL, | ||
"java/lang/StringBuilder", | ||
"append", | ||
"(Ljava/lang/String;)Ljava/lang/StringBuilder;", | ||
false | ||
) | ||
methodVisitor.visitVarInsn(Opcodes.ALOAD, 3) | ||
methodVisitor.visitInsn(Opcodes.ICONST_5) | ||
methodVisitor.visitInsn(Opcodes.AALOAD) | ||
methodVisitor.visitMethodInsn( | ||
Opcodes.INVOKEVIRTUAL, | ||
"java/lang/StackTraceElement", | ||
"toString", | ||
"()Ljava/lang/String;", | ||
false | ||
) | ||
methodVisitor.visitMethodInsn( | ||
Opcodes.INVOKEVIRTUAL, | ||
"java/lang/StringBuilder", | ||
"append", | ||
"(Ljava/lang/String;)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) | ||
methodVisitor.visitLabel(label2) | ||
methodVisitor.visitLineNumber(306, label2) | ||
methodVisitor.visitFrame(Opcodes.F_APPEND, 1, arrayOf<Any>("[Ljava/lang/StackTraceElement;"), 0, null) | ||
methodVisitor.visitInsn(Opcodes.RETURN) | ||
val label4 = Label() | ||
methodVisitor.visitLabel(label4) | ||
methodVisitor.visitLocalVariable( | ||
"this", | ||
"Landroidx/lifecycle/LiveData;", | ||
"Landroidx/lifecycle/LiveData<TT;>;", | ||
label0, | ||
label4, | ||
0 | ||
) | ||
methodVisitor.visitLocalVariable("methodName", "Ljava/lang/String;", null, label0, label4, 1) | ||
methodVisitor.visitLocalVariable("value", "Ljava/lang/Object;", "TT;", label0, label4, 2) | ||
methodVisitor.visitLocalVariable("traces", "[Ljava/lang/StackTraceElement;", null, label1, label4, 3) | ||
methodVisitor.visitMaxs(4, 4) | ||
methodVisitor.visitEnd() | ||
} |
25 changes: 0 additions & 25 deletions
25
src/main/kotlin/io/github/chao2zhang/LoggingLiveDataClassVisitor.kt
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.