Skip to content

Commit

Permalink
fix ReflectResolver
Browse files Browse the repository at this point in the history
  • Loading branch information
way-zer committed Jan 30, 2024
1 parent 371abbd commit e27909a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 15 deletions.
6 changes: 5 additions & 1 deletion src/main/kotlin/cf/wayzer/contentsTweaker/CTNode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ class CTNode private constructor() : ExtendableClass<CTExtInfo>() {
fun collectAll(): CTNode {
if (collected) return this
collected = true
resolvers.forEach { it.collectChild(this) }
for (resolver in resolvers) {
kotlin.runCatching { resolver.collectChild(this) }.onFailure { e ->
throw Exception("Fail to collectChild in $resolver", e)
}
}
get<Modifiable<Any?>>()?.let { modifiable ->
getOrCreate("=").apply {
+Modifier { json ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,21 @@ object ReflectResolver : ContentsTweaker.NodeCollector {

fun extend(node: CTNode, objInfo: CTNode.ObjInfo<*>, filter: (Field) -> Boolean = { Modifier.isPublic(it.modifiers) }) {
val obj = objInfo.obj ?: return
runCatching { JsonIO.json.getFields(objInfo.type) }.getOrNull()
?.filter { filter(it.value.field) }
?.forEach { entry ->
val meta = entry.value
node.getOrCreate(entry.key).apply {
var cls = meta.field.type
if (cls.isAnonymousClass) cls = cls.superclass
+CTNode.ObjInfo<Any?>(meta.field.get(obj), cls, meta.elementType, meta.keyType)
+object : CTNode.Modifiable<Any?>(this) {
override val currentValue: Any? get() = meta.field.get(obj)
override fun setValue0(value: Any?) {
meta.field.set(obj, value)
}
val fields = runCatching { JsonIO.json.getFields(objInfo.type) }.getOrNull() ?: return
for (entry in fields) {
if (!filter(entry.value.field)) continue
val meta = entry.value
node.getOrCreate(entry.key).apply {
var cls = meta.field.type
if (cls.isAnonymousClass) cls = cls.superclass
+CTNode.ObjInfo<Any?>(meta.field.get(obj), cls, meta.elementType, meta.keyType)
+object : CTNode.Modifiable<Any?>(this) {
override val currentValue: Any? get() = meta.field.get(obj)
override fun setValue0(value: Any?) {
meta.field.set(obj, value)
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ abstract class ExtendableClass<Ext : Any> {
inline fun <reified T : Ext> get(): T? {
if (this is T) return this
return mixins.filterIsInstance<T>().let {
require(it.size <= 1) { "More than one ${T::class.java} mixin" }
require(it.size <= 1) { "More than one ${T::class.java} mixin: $it" }
it.firstOrNull()
}
}
Expand Down

0 comments on commit e27909a

Please sign in to comment.