Using reflection is it possible to call a private ...
# announcements
a
Using reflection is it possible to call a private constructor for a class? My attempt to do this results in exception about the modifier being private which makes sense but figured there is some way around this
e
Copy code
import kotlin.reflect.jvm.isAccessible
class Foo private constructor()
val constructor = Foo::class.constructors.single()
constructor.isAccessible = true
println(constructor.call())
(whether this is allowed or not is up to the SecurityManager installed in your ClassLoader)
1
a
thanks, that seemed to work I was just missing that isAccessible part
v
And then there is always Objenesis which can instantiate classes without calling their constructor.
376 Views