.yass.yass.60.0.1.source-code.Reflect.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of yass Show documentation
Show all versions of yass Show documentation
Yet Another Service Solution
package ch.softappeal.yass
import sun.misc.Unsafe
import java.lang.reflect.Field
import java.lang.reflect.Modifier
fun ownFields(type: Class<*>): List {
val fields = mutableListOf()
for (field in type.declaredFields) {
val modifiers = field.modifiers
if (!Modifier.isStatic(modifiers) && !Modifier.isTransient(modifiers)) {
fields.add(field)
if (!Modifier.isPublic(modifiers) || Modifier.isFinal(modifiers)) field.isAccessible = true
}
}
fields.sortBy { it.name }
return fields
}
fun allFields(type: Class<*>): List {
val fields = mutableListOf()
var t: Class<*>? = type
while ((t !== null) && (t !== Throwable::class.java)) {
fields.addAll(ownFields(t))
t = t.superclass
}
return fields
}
private val Unsafe = {
val field = Unsafe::class.java.getDeclaredField("theUnsafe")
field.isAccessible = true
field.get(null) as Unsafe
}()
val AllocatorFactory: (type: Class<*>) -> (() -> Any) =
{ type -> { Unsafe.allocateInstance(type) } }
© 2015 - 2025 Weber Informatics LLC | Privacy Policy