-
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add FileReader/FileWriter instrumentation (#241)
- Loading branch information
Showing
17 changed files
with
360 additions
and
31 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -73,6 +73,5 @@ sentry { | |
|
||
tracingInstrumentation { | ||
forceInstrumentDependencies.set(true) | ||
debug.set(true) | ||
} | ||
} |
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
66 changes: 66 additions & 0 deletions
66
examples/android-room/src/main/java/io/sentry/android/roomsample/util/Filesystem.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,66 @@ | ||
package io.sentry.android.roomsample.util | ||
|
||
import android.content.Context as AndroidContext | ||
import java.io.File | ||
import java.io.FileReader | ||
import java.io.FileWriter | ||
import java.io.Serializable | ||
|
||
sealed class Filesystem : Serializable { | ||
|
||
abstract fun read(context: AndroidContext, filename: String): String | ||
|
||
abstract fun write(context: AndroidContext, filename: String, text: String) | ||
|
||
companion object { | ||
fun from(which: Int) = when (which) { | ||
0 -> IOStream | ||
1 -> ReaderWriter | ||
2 -> Context | ||
else -> error("Unknown File API") | ||
} | ||
} | ||
|
||
object ReaderWriter : Filesystem() { | ||
|
||
override fun read(context: AndroidContext, filename: String): String { | ||
val file = File(context.filesDir, filename) | ||
return if (file.exists()) FileReader(file).use { it.readText() } else "" | ||
} | ||
|
||
override fun write(context: AndroidContext, filename: String, text: String) { | ||
val file = File(context.filesDir, filename) | ||
if (!file.exists()) { | ||
file.createNewFile() | ||
} | ||
FileWriter(file).use { it.write(text) } | ||
} | ||
} | ||
|
||
object IOStream : Filesystem() { | ||
override fun read(context: AndroidContext, filename: String): String { | ||
val file = File(context.filesDir, filename) | ||
return if (file.exists()) file.readText() else "" | ||
} | ||
|
||
override fun write(context: AndroidContext, filename: String, text: String) { | ||
val file = File(context.filesDir, filename) | ||
if (!file.exists()) { | ||
file.createNewFile() | ||
} | ||
file.writeText(text) | ||
} | ||
} | ||
|
||
object Context : Filesystem() { | ||
override fun read(context: AndroidContext, filename: String): String { | ||
val fis = context.openFileInput(filename) | ||
return fis.bufferedReader().readText() | ||
} | ||
|
||
override fun write(context: AndroidContext, filename: String, text: String) { | ||
val fos = context.openFileOutput(filename, AndroidContext.MODE_PRIVATE) | ||
fos.bufferedWriter().write(text) | ||
} | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
...n-build/src/main/kotlin/io/sentry/android/gradle/instrumentation/ChainedInstrumentable.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,43 @@ | ||
@file:Suppress("UnstableApiUsage") | ||
|
||
package io.sentry.android.gradle.instrumentation | ||
|
||
import com.android.build.api.instrumentation.ClassContext | ||
import java.util.LinkedList | ||
import org.objectweb.asm.ClassVisitor | ||
|
||
class ChainedInstrumentable( | ||
private val instrumentables: List<ClassInstrumentable> = emptyList() | ||
) : ClassInstrumentable { | ||
|
||
override fun getVisitor( | ||
instrumentableContext: ClassContext, | ||
apiVersion: Int, | ||
originalVisitor: ClassVisitor, | ||
parameters: SpanAddingClassVisitorFactory.SpanAddingParameters | ||
): ClassVisitor { | ||
// build a chain of visitors in order they are provdied | ||
val queue = LinkedList(instrumentables) | ||
var prevVisitor = originalVisitor | ||
var visitor: ClassVisitor? = null | ||
while (queue.isNotEmpty()) { | ||
val instrumentable = queue.poll() | ||
|
||
visitor = if (instrumentable.isInstrumentable(instrumentableContext)) { | ||
instrumentable.getVisitor( | ||
instrumentableContext, | ||
apiVersion, | ||
prevVisitor, | ||
parameters | ||
) | ||
} else { | ||
prevVisitor | ||
} | ||
prevVisitor = visitor | ||
} | ||
return visitor ?: originalVisitor | ||
} | ||
|
||
override fun isInstrumentable(data: ClassContext): Boolean = | ||
instrumentables.any { it.isInstrumentable(data) } | ||
} |
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
30 changes: 30 additions & 0 deletions
30
...src/main/kotlin/io/sentry/android/gradle/instrumentation/remap/RemappingInstrumentable.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,30 @@ | ||
@file:Suppress("UnstableApiUsage") | ||
|
||
package io.sentry.android.gradle.instrumentation.remap | ||
|
||
import com.android.build.api.instrumentation.ClassContext | ||
import io.sentry.android.gradle.instrumentation.ClassInstrumentable | ||
import io.sentry.android.gradle.instrumentation.SpanAddingClassVisitorFactory | ||
import io.sentry.android.gradle.instrumentation.util.isSentryClass | ||
import org.objectweb.asm.ClassVisitor | ||
import org.objectweb.asm.commons.ClassRemapper | ||
import org.objectweb.asm.commons.SimpleRemapper | ||
|
||
class RemappingInstrumentable : ClassInstrumentable { | ||
companion object { | ||
private val mapping = mapOf( | ||
"java/io/FileReader" to "io/sentry/instrumentation/file/SentryFileReader", | ||
"java/io/FileWriter" to "io/sentry/instrumentation/file/SentryFileWriter" | ||
) | ||
} | ||
|
||
override fun getVisitor( | ||
instrumentableContext: ClassContext, | ||
apiVersion: Int, | ||
originalVisitor: ClassVisitor, | ||
parameters: SpanAddingClassVisitorFactory.SpanAddingParameters | ||
): ClassVisitor = | ||
ClassRemapper(originalVisitor, SimpleRemapper(mapping)) | ||
|
||
override fun isInstrumentable(data: ClassContext): Boolean = !data.isSentryClass() | ||
} |
14 changes: 14 additions & 0 deletions
14
...d/src/main/kotlin/io/sentry/android/gradle/instrumentation/util/SentryPackageNameUtils.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,14 @@ | ||
@file:Suppress("UnstableApiUsage") | ||
|
||
package io.sentry.android.gradle.instrumentation.util | ||
|
||
import com.android.build.api.instrumentation.ClassContext | ||
|
||
fun ClassContext.isSentryClass(): Boolean = | ||
when { | ||
currentClassData.className.startsWith("io.sentry") && | ||
!currentClassData.className.startsWith("io.sentry.android.roomsample") && | ||
!currentClassData.className.startsWith("io.sentry.samples") && | ||
!currentClassData.className.startsWith("io.sentry.mobile") -> true | ||
else -> false | ||
} |
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
5 changes: 5 additions & 0 deletions
5
plugin-build/src/test/kotlin/androidx/sqlite/db/SupportSQLiteOpenHelper.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,5 @@ | ||
package androidx.sqlite.db | ||
|
||
interface SupportSQLiteOpenHelper { | ||
abstract class Callback | ||
} |
Oops, something went wrong.