I use following initialization:
val entityClass = javaClass<Class<T>>() var entity = entityClass.newInstance().newInstance()
but it's wrong and causes IllegalAccessException
on java.lang.Class.newInstance(Class.java:1208)
In Kotlin, we cannot create an instance of an abstract class. Abstract classes can only be implemented by another class which should be abstract in nature. In order to use an abstract class, we need to create another class and inherit the abstract class.
To construct an instance of a generic type GetType(String) method overload with a string describing the type, and by calling the GetGenericTypeDefinition method on the constructed type Dictionary\<String, Example> ( Dictionary(Of String, Example) in Visual Basic).
When we want to assign the generic type to any of its super type, then we need to use “out” keyword, and when we want to assign the generic type to any of its sub-type, then we need to use “in” keyword. In the following example, we will use “out” keyword. Similarly, you can try using “in” keyword.
Kotlin generic extension function example As extension function allows to add methods to class without inherit a class or any design pattern. In this example, we add a method printValue()to ArrayList class of generic type. This method is called form stringList. printValue() and floatList.
If you let IntelliJ add explicit type information, you see that entityClass
is actually of type Class<Class<String>>
. I'm not sure if that's what you want. In line 2 you are first creating an instance of Class<T>
and then one of T
but that's not possible anyway, because the generic information about T
is lost at runtime. Apart from that you can't instantiate class objects directly.
One possible solution would be to add a parameter of type Class<T>
to your function or class and use it to instantiate objects like this.
fun <T> foo(entityClass: Class<T>) { var entity: T = entityClass.newInstance() } fun test() { foo(Object::class.java) }
But there's actually a more elegant solution without the use of reflection. Define a parameter of method type () -> T
and use constructor references. Here's my related question about constructor references and here's the code:
fun <T> foo2(factory: () -> T) { var entity: T = factory() } fun test() { foo2(::Object) }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With