kennyyi
08/11/2018, 5:58 AMfun foo(clazz: Class<*>) {
// 1. How to know clazz is collection?
// 2. How to know T of Collection<T> is String?
}
fun myFunc() {
val mySet: HashSet<String> = HashSet()
foo(mySet::class.java)
}
diesieben07
08/12/2018, 4:30 PMHashSet<String>
and HashSet<Foo>
are represented by the same (erased) class HashSet
.
You can do some clever tricks with subclasses and the reified
keyword, but your example is not possible.kennyyi
08/13/2018, 6:42 AMannotation class ElementType(val element: KClass<*>)
...
@ElementType(String::class)
class Names: HashSet<String>()
...
foo() {
...
val elementType = clazz.annotations.find{ it is ElementType }
elementType?.let {
val elementClass = (elementType as ElementType).element
...
}
...
}