KClass questions

1. Is there any way of getting a `javaClass` from a `KClass`? Obviously, doing `mykclass.javaClass` will give the `Class` of the `KClass`, not of the referenced class (I hope that makes sense :D) 2. Can I get annotations from a kclass reference or from a java class without having to do `SpecificClass.javaClass.getAnnotation()`? I guess this is tied to the first question 3. Is there any way of creating instances of a Kotlin class using Reflection? Right now I'm using the `::ClassName` "trick" (I don't see documentation about it) but if I need a constructor with arguments, I don't see a way of doing that. 4. If I use KClass on my methods arguments, will people on the Java side be able to call the methods and pass a `Class` instead, or will they need to do some kind of transformation to use my methods? I know annotations will convert KClass to Class, but there is no explicit mention of any other case in the docs that I can see...

Thanks :slight_smile:

1. `mykclass.java`. "java" is an extension property in package kotlin.reflect.jvm 2, 3. Currently not possible, work in progress. 4. No, KClass is a separate type unrelated to Class and conversion only happens in annotation arguments. To get a KClass by a Class elsewhere: `jclass.kotlin`. "kotlin" is also an extension property in package kotlin.reflect.jvm, so you can call it as `getKotlin()` from Java.

Thank you!

Is there a way of adding some sort of extension method, so I can do MyClass::jclass to get the java class? (instead of doing `javaClass<MyClass>). It’s just a bit nicer, but I understand if it’s not possible.

Than you again!

It's not possible, the intended way uses the "java" extension property I mentioned above: `MyClass::class.java`. We are in fact considering deprecating the "javaClass" function soon and removing it eventually.

Ohh, I see.

Ok, I’ll start replacing my existing uses of javaClass by MyClass::class.java.

Hmm, I've replaed all `javaClass<MyClass>` with `MyClass::class.java` and I'm getting:

Exception in thread “main” java.lang.NoSuchFieldError: $kotlinClass

Is this a bug in Kotlin?

Yes, it's a known problem: KT-8206. Please don't replace javaClass usages yet, there are still some issues with "::class.java" which need to be resolved before we deprecate "javaClass".

I see, I'll watch that bug, thanks!

Related question... shouldn't this print a line for the extension property? It prints nothing.

class Foo(val num: Int)


fun Foo.bar(): Int { return 1 }
val Foo.baz: Int
  get() { return 2 }


object Main {
  platformStatic fun main(args: Array<String>) {
  val kc = Foo::class
  for (prop in kc.extensionProperties) {
           println(“prop: $prop”)
  }
  }
}

No, KClass.extensionProperties returns extension properties declared in the given class. The property you're looking for is declared in the package. Unfortunately there's no API currently to list properties in the package, however you can use a simple property reference for that: Foo::baz

Oh, I see. I thought it was a list of extension properties extending Foo, but it's not.  ... which makes sense since extensions to a type will be different in different static scopes.

thanks,
Rob

I'm having some trouble with KClass and `.java` to get the Java class from it. If I have "val c : KClass<MyClass>" then I can do "c.java" without problems. But if my declaration is "val c : KClass<out MyClass>" then there is no ".java", or at least IDEA doesn't seem to see it. Is this a bug or there is no way of getting the .java from an "<out X>" declaration?

Works for me. Please create an issue at youtrack.jetbrains.com with the sample code that's not compiling.