MyClass.class syntax in Kotlin

Hello, is there any syntax in Kotlin to get class from its static name like in Java: {code} Class<MyClass> clazz = MyClass.class; {code} I couldn't figure out how to do this in Kotlin. Expression like MyClass.class causes compilation error and common MyClass requires class object but there is no defined for Java objects.

2 Likes

I join in asking the question. How?

Try this.

fun testClass() {
  val cls = javaClass<String>() // String.class
  println(cls)
}

Or shortly.
//Java
Class<MyClass> clazz = MyClass.class;
// Kotlin
val clazz = javaClass<MyClass>

Yes, and I find this works to get, for example, an SLF4J logger:

``

  val LOG : Logger? = LoggerFactory.getLogger(javaClass<Account>)

Thanks, it works fine.

As a beginner, I encountered a similar issue, and end up using a style that seems to be better than the style mentioned a year ago.

val log = LoggerFactory.getLogger(javaClass<Account>()) as Logger

fun doSomthing(){
  log.trace(“test”)  //No need to use log?.trace with “as Logger”
}


(edited)
or

val log = LoggerFactory.getLogger(javaClass<Account>())!!

Old post, sorry about the bump but I’m really new to Kotlin (since 1.0 release some days ago) and I am yet struggling with some basic concepts.

This is the syntax I am currently using for SLF4J / Logback:

class FacturaDAO {
    private val log = LoggerFactory.getLogger(FacturaDAO::class.java)
...
3 Likes

Yet another bump for this topic. How does it work in this case? There is no class in SomeFile.kt, only a bunch of functions:

// SomeFile.kt
package some.pkg

fun someFun() = 42

fun main(args: Array<String>) {
    SomeFile::class.java    // does not work
    SomeFileKt::class.java  // does not work
    ::class.java            // does not work
}

Still, I would like to get access to the class file (for a call to getResourceAsStream(...)).

5 Likes

To load a resource, you can take any other class in the file for example, or (if there’s no class) the Java class of an anonymous object:

val resource = object {}.javaClass.getResourceAsStream(...)
1 Like

Thanks Alexander,
I know that I can use any of the existing classes or add a synthetic class. Good to know that there really is now way to achieve that in my scenario, though. I guess that’s just a minor inconvenience for larger projects.

Would it be possible/reasonable to provide access to the SomeFileKt class in Kotlin (as in Java)?

Would it be possible/reasonable to provide access to the SomeFileKt class in Kotlin (as in Java)?

This could be useful. Unfortunately there’s no such class from Kotlin’s point of view, so we’re still unsure how exactly this would work.

It would also throw up issues around inlined functions.

This worked for me on Kotlin 1.1

MyClass::class.java
1 Like

Hi, @udalov. Could you please help me. What about access to classes like SomeFileKt in Kotlin? Is it possible now? Or are you going to provide this access?

1 Like

@KucherenkoIhor No, it’s not possible for file classes yet. I’ve created an issue: https://youtrack.jetbrains.com/issue/KT-21599

2 Likes