groostav
10/05/2020, 7:00 PMgetInstance
flow. Im looking at something like this:
inline fun <reified T> Injector.getInstance(): T {
val targetType = T::class.java
if(targetType.typeParameters.isEmpty()){
return getInstance(T::class.java)
}
else {
val targetTypeSpecifically = Key.get(object: TypeLiteral<T>(){})
return getInstance(targetTypeSpecifically)
}
}
I'm wondering what the compiler does with dead-code-elimination of the type that gets created for targetTypeSpecifically
. For the case where the type isnt generic (we take the true branch) Will that create a new Class
entry at runtime or wont it? Whats the best way for me to test that?udalov
groostav
10/06/2020, 8:24 PMgroostav
10/06/2020, 8:26 PMudalov
kotlin.reflect.typeOf
, and either deconstruct the returned KType
as you’d like, or call .javaType
to get a java.lang.reflect.Type
instance representing the typegroostav
10/21/2020, 7:25 PMType
(either kotlin or java) is definately what I want, since it seems to be purely a handle (ie a string --no class-pool involved) and it reifies generics. the Guice guys already support a java.lang type. I am having trouble getting from the typeOf()
operator to a java.lang type. the .javatype
property seems to throw for any argument, and i have to do the replacement of kotlin aliases myself. Very interesting stuff anyways.udalov
java.lang.reflect.Type
is a bit more than a string, it actually references java.lang.Class
objects when needed. For example, for the simplest case when a type = class, you’ll just have an instance of Class
(which inherits from Type
). For generic class types, you’ll get a ParameterizedType
with arguments who are again `Type`s, and so on. Take a look at subclasses of java.lang.reflect.Type
for more info.
If you encountered a case when typeOf<...>().javaType
throws on some type and you can reasonably minimize/isolate it, please report it to our YouTrackgroostav
10/21/2020, 9:47 PMkotlin.collections.List<kotlin.String>
to java.util.List<? extends java.lang.String>
like i expected it togroostav
10/21/2020, 9:51 PMgroostav
10/21/2020, 9:52 PM