Smallville7123
04/10/2019, 9:47 AMclass A {
inner class B {
inner class C {
var empty: Int = 0
}
var empty: Int = 0
var a: MutableList<C>
init {
a = mutableListOf(C())
}
}
var a: MutableList<B>
init {
a = mutableListOf(B())
}
/**
* test variable
*/
var empty: Int = 0
}
fun getDeclaringUpperLevelClassObject(objectA: Any?): Any? {
if (objectA == null) {
return null
}
val cls = objectA.javaClass ?: return objectA
val outerCls = cls.enclosingClass
?: // this is top-level class
return objectA
// get outer class object
var outerObj: Any? = null
try {
val fields = cls.declaredFields
for (field in fields) {
if (field != null && field.type === outerCls
&& field.name != null && field.name.startsWith("this$")
) {
/*
`field.isAccessible = true` does not work with a Security Manager
java.security.AccessControlException: access denied
("java.lang.reflect.ReflectPermission" "suppressAccessChecks")
*/
field.isAccessible = true
outerObj = field.get(objectA)
break
}
}
} catch (e: Exception) {
e.printStackTrace()
}
return outerObj
}
/**
* returns a [MutableList] of classes parenting the current class
*
* the top most class is always the last index
*
* the last class is always the first index
*
* for example: up(f[0].a[0].a[0]) = 0(f[0].a[0].a[0]) 1(f[0].a[0]) 2(f[0])
*/
fun up(a: Any, m: MutableList<Any> = mutableListOf(), debug: Boolean = false): MutableList<Any> {
m.add(a)
if (debug) println("get upper class of ${a.javaClass.name}")
val upperC = getDeclaringUpperLevelClassObject(a)
if (upperC == null) abort("upperC is null o.o")
if (a.equals(upperC)) return m
else {
return up(upperC, m)
}
}
/**
* use as follows: `instanceChain(`[up]`(f[0]))`
*/
fun instanceChain(chain: MutableList<Any>, index: Int = chain.lastIndex, debug: Boolean = false): Any {
return if (index == 0) {
chain[index]
} else {
if (debug) println("chain[$index] = " + chain[index])
val outer = chain[index]
val toRun = Class.forName(chain[index].javaClass.name + "$" + chain[index - 1].javaClass.simpleName)
val ctor = toRun.getDeclaredConstructor(chain[index]::class.java)
val lowerCInstance = ctor.newInstance(outer)
if (debug) println("lowerCInstance = " + lowerCInstance!!::class.java)
if (index == 1) lowerCInstance
else instanceChain(chain, index - 1)
}
}
fun Test() {
val f = mutableListOf<A>()
f.add(A()) // this is required, i do not know how to do accomplish this in the init block
println("f[0] = ${instanceChain(up(f[0]))}")
println("f[0].a[0] = ${instanceChain(up(f[0].a[0]))}")
println("f[0].a[0].a[0] = ${instanceChain(up(f[0].a[0].a[0]))}")
abort()
}
serebit
04/10/2019, 3:42 PMSmallville7123
04/10/2019, 5:08 PMSmallville7123
04/10/2019, 5:10 PMserebit
04/10/2019, 5:10 PMSmallville7123
04/10/2019, 5:11 PMserebit
04/10/2019, 5:12 PMSmallville7123
04/10/2019, 5:12 PMSmallville7123
04/10/2019, 5:13 PMserebit
04/10/2019, 5:14 PMSmallville7123
04/10/2019, 5:14 PMserebit
04/10/2019, 5:14 PMSmallville7123
04/10/2019, 5:30 PMExpression in a class literal has a nullable type 'E', use !! to make the type non-nullable
no matter what i do, in kotlin playgroundSmallville7123
04/10/2019, 5:49 PM::class.isInner
refuses to be called, with Exception in thread "main" java.lang.IllegalStateException: No BuiltInsLoader implementation was found. Please ensure that the META-INF/services/ is not stripped from your application and that the Java virtual machine is not running under a security manager
Smallville7123
04/10/2019, 5:51 PMmain
i get Exception in thread "main" java.security.AccessControlException: Access control exception due to security reasons in web playground:
access denied ("java.lang.RuntimePermission" "accessDeclaredMembers")
https://pl.kotl.in/SkzV6ssK4Smallville7123
04/10/2019, 5:52 PMfun main(args: Array<String>) {
class A() {}
println(A::class.isInner)
}
serebit
04/10/2019, 6:01 PMSmallville7123
04/10/2019, 6:07 PMfield.isAccessible = true
is not allowed either in web playground