All Downloads are FREE. Search and download functionalities are using the official Maven repository.

.yass.yass.60.0.1.source-code.Reflect.kt Maven / Gradle / Ivy

There is a newer version: 66.0.0
Show newest version
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