MyClass.class syntax in Kotlin


#1

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

I join in asking the question. How?


#3

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>


#4

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

``

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


#5

Thanks, it works fine.


#6

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>())!!

#7

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)
...

#8

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(...)).


Is it possible to reference MainKt file?
#9

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(...)

#10

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)?


#11

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.


#12

It would also throw up issues around inlined functions.


#13

This worked for me on Kotlin 1.1

MyClass::class.java

#14

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?


#15

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