How can you describe `MyType[].class` in kotlin ? ...
# java-to-kotlin-refactoring
d
How can you describe
MyType[].class
in kotlin ?
MyType::class.java.arrayType()
does not seem to be it, I get [UNRESOLVED_REFERENCE] back. Code snippet looks like:
Copy code
class MyType<T> {
  val targetClass: Class<T>
  val isArrayType: Boolean
  constructor(targetClass: Class<T>, isArrayType: Boolean = false) {
    this.targetClass = targetClass
    this.isArrayType = isArrayType
  }
  fun toTypeForJackson(): Class<*> {
    return if(isArrayType) targetClass.arrayType() else targetClass   /// <<< ERROR HERE
    // tried also: targetClass.arrayType()::class.java
  }
}
Java code that compiles:
Copy code
public class MyUtil {
  public static <T> Class<?> foo(Class<T> targetClass, boolean isArrayType) {
    if(isArrayType)
      return targetClass.arrayType();
    else
      return targetClass;
  }
}
Just testing it works as expected, maybe I make a Java library project with this util method to resolve ? UPDATE: Doh! so Class#arrayType() came with JDK12 and project targets JDK11 as it is a library project, while runtime testing is JDK21. Solution
java.lang.reflect.Array.newInstance(targetClass, 0)::class.java