Skip to content

Commit 9ad2717

Browse files
committed
leaner reflection
1 parent 9dfbbb3 commit 9ad2717

File tree

6 files changed

+36
-62
lines changed
  • AutomaticAdvancedSettingsExpander/src/main/java/de/binarynoise/automaticadvancedsettingsexpander
  • BetterVerboseWiFiLogging/src/main/java/de/binarynoise/betterVerboseWiFiLogging
  • FreeNotifications/src/main/java/de/binarynoise/freeNotifications
  • MuteSlf4jWarnings/src/main/java/de/binarynoise/MuteSlf4jWarnings
  • PersistentForegroundServiceNotifications/src/main/java/de/binarynoise/persistentForegroundServiceNotifications
  • reflection/src/main/java/de/binarynoise/reflection

6 files changed

+36
-62
lines changed

AutomaticAdvancedSettingsExpander/src/main/java/de/binarynoise/automaticadvancedsettingsexpander/Hook.kt

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import de.robv.android.xposed.IXposedHookLoadPackage
77
import de.robv.android.xposed.XC_MethodHook
88
import de.robv.android.xposed.XposedHelpers
99
import de.robv.android.xposed.callbacks.XC_LoadPackage
10+
import de.robv.android.xposed.XC_MethodHook as MethodHook
1011

1112
const val TAG = "AutomaticSettingsExpand"
1213

@@ -20,11 +21,10 @@ class Hook : IXposedHookLoadPackage {
2021

2122
val classes = arrayOf("PreferenceGroup")
2223

23-
val hook = object : XC_MethodHook() {
24+
val hook = object : MethodHook() {
2425
override fun beforeHookedMethod(param: MethodHookParam) {
25-
var expandedCount by param.args(0)
26-
Log.d(TAG, "expandedCount would have been set to $expandedCount, but setting to Int.MAX_VALUE instead")
27-
expandedCount = Int.MAX_VALUE
26+
Log.d(TAG, "expandedCount would have been set to ${param.args[0]}, but setting to Int.MAX_VALUE instead")
27+
param.args[0] = Int.MAX_VALUE
2828
}
2929
}
3030

@@ -44,18 +44,4 @@ class Hook : IXposedHookLoadPackage {
4444
}
4545
}
4646
}
47-
48-
operator fun <T> Array<T>.invoke(index: Int): ArrayDelegate<T> {
49-
return ArrayDelegate(this, index)
50-
}
51-
52-
class ArrayDelegate<T>(val array: Array<T>, val index: Int) : ReadWriteProperty<Any?, T> {
53-
override fun getValue(thisRef: Any?, property: KProperty<*>): T {
54-
return array[index]
55-
}
56-
57-
override fun setValue(thisRef: Any?, property: KProperty<*>, value: T) {
58-
array[index] = value
59-
}
60-
}
6147
}

BetterVerboseWiFiLogging/src/main/java/de/binarynoise/betterVerboseWiFiLogging/Wrapper.kt

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@
22

33
package de.binarynoise.betterVerboseWiFiLogging
44

5-
import java.lang.reflect.Method
65
import android.annotation.SuppressLint
76
import android.net.wifi.ScanResult
87
import android.net.wifi.WifiInfo
98
import android.os.Build
109
import de.binarynoise.betterVerboseWiFiLogging.WifiEntry.mWifiInfoField
1110
import de.binarynoise.betterVerboseWiFiLogging.Wrapper.classLoader
12-
import de.binarynoise.reflection.field
13-
import de.binarynoise.reflection.method
11+
import de.binarynoise.reflection.findDeclaredField
12+
import de.binarynoise.reflection.findDeclaredMethod
1413

1514
const val wifitrackerlib = "com.android.wifitrackerlib"
1615

@@ -203,7 +202,7 @@ val channelMap = mapOf(
203202
object WifiEntry {
204203
val wifiEntryClass: Class<*> = classLoader.loadClass("$wifitrackerlib.WifiEntry")
205204

206-
val getWifiInfoDescription: Method by method(wifiEntryClass)
205+
val getWifiInfoDescription = wifiEntryClass.findDeclaredMethod("getWifiInfoDescription")
207206

208207
private const val CONNECTED_STATE_CONNECTED = 2
209208
fun newGetWifiInfoDescription(wifiEntry: Any): String {
@@ -228,19 +227,19 @@ object WifiEntry {
228227
}
229228
}
230229

231-
val getNetworkCapabilityDescription by method(wifiEntryClass)
232-
val getNetworkSelectionDescription by method(wifiEntryClass)
230+
val getNetworkCapabilityDescription = wifiEntryClass.findDeclaredMethod("getNetworkCapabilityDescription")
231+
val getNetworkSelectionDescription = wifiEntryClass.findDeclaredMethod("getNetworkSelectionDescription")
233232

234-
val getScanResultDescription by method(wifiEntryClass) // -> StandardWifiEntry
233+
val getScanResultDescription = wifiEntryClass.findDeclaredMethod("getScanResultDescription") // -> StandardWifiEntry
235234

236-
val getConnectedState by method(wifiEntryClass)
235+
val getConnectedState = wifiEntryClass.findDeclaredMethod("getConnectedState")
237236

238-
val mWifiInfoField by field(wifiEntryClass)
237+
val mWifiInfoField = wifiEntryClass.findDeclaredField("mWifiInfo")
239238
}
240239

241240
object StandardWifiEntry {
242241
private val standardWifiEntryClass: Class<*> = classLoader.loadClass("$wifitrackerlib.StandardWifiEntry")
243-
val getScanResultDescription by method(WifiEntry.wifiEntryClass) // -> StandardWifiEntry
242+
val getScanResultDescription = WifiEntry.wifiEntryClass.findDeclaredMethod("getScanResultDescription") // -> StandardWifiEntry
244243

245244
fun newGetScanResultDescription(wifiEntry: Any): String {
246245
@Suppress("UNCHECKED_CAST") val mTargetScanResults = mTargetScanResultsField[wifiEntry] as ArrayList<ScanResult>
@@ -274,5 +273,5 @@ object StandardWifiEntry {
274273
}
275274
}
276275

277-
private val mTargetScanResultsField by field(standardWifiEntryClass)
276+
private val mTargetScanResultsField = standardWifiEntryClass.findDeclaredField("mTargetScanResults")
278277
}

FreeNotifications/src/main/java/de/binarynoise/freeNotifications/Hook.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package de.binarynoise.freeNotifications
22

33
import android.annotation.SuppressLint
44
import android.app.NotificationChannel
5-
import de.binarynoise.reflection.field
5+
import de.binarynoise.reflection.findDeclaredField
66
import de.robv.android.xposed.IXposedHookLoadPackage
77
import de.robv.android.xposed.XposedBridge
88
import de.robv.android.xposed.XposedHelpers
@@ -15,7 +15,7 @@ class Hook : IXposedHookLoadPackage {
1515
override fun handleLoadPackage(lpparam: XC_LoadPackage.LoadPackageParam) {
1616
val cls = NotificationChannel::class.java
1717

18-
val mBlockableSystem by field(cls)
18+
val mBlockableSystem = cls.findDeclaredField("mBlockableSystem")
1919

2020
val setToTrue = object : MethodHook() {
2121
override fun beforeHookedMethod(param: MethodHookParam) {

MuteSlf4jWarnings/src/main/java/de/binarynoise/MuteSlf4jWarnings/Hook.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package de.binarynoise.muteSlf4jWarnings
22

33
import android.annotation.SuppressLint
4-
import de.binarynoise.reflection.method
4+
import de.binarynoise.reflection.findDeclaredMethod
55
import de.robv.android.xposed.IXposedHookLoadPackage
66
import de.robv.android.xposed.XposedBridge
77
import de.robv.android.xposed.callbacks.XC_LoadPackage
@@ -13,8 +13,8 @@ class Hook : IXposedHookLoadPackage {
1313
override fun handleLoadPackage(lpparam: XC_LoadPackage.LoadPackageParam) {
1414
val errorPrintStreamClass = lpparam.classLoader.loadClass("com.android.internal.os.AndroidPrintStream")
1515

16-
val println by method(errorPrintStreamClass, String::class.java)
17-
val print by method(errorPrintStreamClass, String::class.java)
16+
val println = errorPrintStreamClass.findDeclaredMethod("println", String::class.java)
17+
val print = errorPrintStreamClass.findDeclaredMethod("print", String::class.java)
1818

1919
val hook = object : MethodHook() {
2020
override fun beforeHookedMethod(param: MethodHookParam) {

PersistentForegroundServiceNotifications/src/main/java/de/binarynoise/persistentForegroundServiceNotifications/Hook.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import android.app.Notification.FLAG_ONLY_ALERT_ONCE
1515
import android.app.Notification.FLAG_SHOW_LIGHTS
1616
import android.service.notification.StatusBarNotification
1717
import de.binarynoise.logger.Logger.log
18-
import de.binarynoise.reflection.method
18+
import de.binarynoise.reflection.findDeclaredMethod
1919
import de.robv.android.xposed.IXposedHookLoadPackage
2020
import de.robv.android.xposed.XposedBridge
2121
import de.robv.android.xposed.XposedHelpers
@@ -29,7 +29,7 @@ class Hook : IXposedHookLoadPackage {
2929

3030
override fun handleLoadPackage(lpparam: XC_LoadPackage.LoadPackageParam) = try {
3131
val statusBarNotificationClass = StatusBarNotification::class.java
32-
val isNonDismissableMethod by method(statusBarNotificationClass)
32+
val isNonDismissableMethod = statusBarNotificationClass.findDeclaredMethod("isNonDismissable")
3333

3434
XposedBridge.hookMethod(isNonDismissableMethod, object : MethodHook() {
3535
override fun beforeHookedMethod(param: MethodHookParam) {

reflection/src/main/java/de/binarynoise/reflection/Reflection.kt

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,39 +5,28 @@ package de.binarynoise.reflection
55
import java.lang.reflect.AccessibleObject
66
import java.lang.reflect.Field
77
import java.lang.reflect.Method
8+
import kotlin.properties.ReadWriteProperty
89
import kotlin.reflect.KProperty
910

10-
class field(private val cls: Class<*>) {
11-
operator fun getValue(thisRef: Nothing?, property: KProperty<*>): Field {
12-
return cls.getDeclaredField(property.name.removeSuffix("Field")).makeAccessible()
11+
fun <T> Class<T>.findDeclaredMethod(name: String, vararg params: Class<*>): Method {
12+
var c: Class<*>? = this
13+
while (c != null) {
14+
c.declaredMethods.filter { it.name == name }
15+
.firstOrNull() { params.isEmpty() || (it.parameterTypes.map { t -> t.name }) == (params.map { p -> p.name }) }
16+
?.let { return it.makeAccessible() }
17+
c = c.superclass
1318
}
1419

15-
operator fun getValue(thisRef: Any?, property: KProperty<*>): Field {
16-
return cls.getDeclaredField(property.name.removeSuffix("Field")).makeAccessible()
17-
}
20+
throw NoSuchMethodException("$name(${params.joinToString { it.simpleName }})")
1821
}
1922

20-
class method(private val cls: Class<*>, private vararg val params: Class<*>) {
21-
operator fun getValue(thisRef: Nothing?, property: KProperty<*>): Method {
22-
return getMethod(property)
23-
}
24-
25-
operator fun getValue(thisRef: Any?, property: KProperty<*>): Method {
26-
return getMethod(property)
27-
}
28-
29-
private fun getMethod(property: KProperty<*>): Method {
30-
val methods = mutableListOf<Method>()
31-
var c: Class<*>? = cls
32-
while (c != null) {
33-
methods += c.declaredMethods
34-
c = c.superclass
35-
}
36-
37-
return methods.filter { it.name == property.name.removeSuffix("Method") }
38-
.first { params.isEmpty() || (it.parameterTypes.map { t -> t.name }) == (params.map { p -> p.name }) }
39-
.makeAccessible()
23+
fun <T> Class<T>.findDeclaredField(name: String): Field {
24+
var c: Class<*>? = this
25+
while (c != null) {
26+
c.declaredFields.filter { it.name == name }.firstOrNull()?.let { return it.makeAccessible() }
27+
c = c.superclass
4028
}
29+
throw NoSuchFieldException(name)
4130
}
4231

4332
fun <T : AccessibleObject> T.makeAccessible(): T = apply { isAccessible = true }

0 commit comments

Comments
 (0)