# How to get KClass of @file:JvmName

**URL:** <https://discuss.kotlinlang.org/t/how-to-get-kclass-of-file-jvmname/5644>\
**Category:** Language Design\
**Created:** [December 2, 2017, 3:15pm UTC](https://discuss.kotlinlang.org/t/how-to-get-kclass-of-file-jvmname/5644 "2017-12-02T15:15:08Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![KucherenkoIhor](https://avatars.discourse-cdn.com/v4/letter/k/13edae/32.png) [@KucherenkoIhor](https://discuss.kotlinlang.org/u/KucherenkoIhor)\
**Post date:** [December 2, 2017, 3:15pm UTC](https://discuss.kotlinlang.org/t/how-to-get-kclass-of-file-jvmname/5644/1 "2017-12-02T15:15:08Z")

</div>

@file:JvmName(“EncryptionModule”)  
@file:Module

package com.dfdfdfd.di.module

```
@Provides
@Singleton
fun getEncryptionService(context: Context): EncryptionService {
    return EncryptionServiceDefault(context)
}

```

I wrote in this way to retrieve class with static functions.

@Singleton  
@Component(modules = [  
ApplicationModule::class,  
EncryptionModule::class, //error Unresolved reference EncryptionModule  
DataModule::class])

Please, help me. How to retrieve const instance of KClass in this case?

---

<div class="post-metadata">

**Author:** ![TWiStErRob](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/twisterrob/32/4111_2.png) [@TWiStErRob](https://discuss.kotlinlang.org/u/TWiStErRob)\
**Post date:** [January 20, 2018, 10:40am UTC](https://discuss.kotlinlang.org/t/how-to-get-kclass-of-file-jvmname/5644/2 "2018-01-20T10:40:44Z")

</div>

Did you figure this out?

---

<div class="post-metadata">

**Author:** ![pdvrieze](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/pdvrieze/32/1882_2.png) [@pdvrieze](https://discuss.kotlinlang.org/u/pdvrieze)\
**Post date:** [January 23, 2018, 2:31pm UTC](https://discuss.kotlinlang.org/t/how-to-get-kclass-of-file-jvmname/5644/3 "2018-01-23T14:31:56Z")

</div>

At Kotlin language level top level functions are independent of the file they are declared in. They belong directly to their package. `@JvmName` annotations do not change this, they merely change details of the compilation result not the validity of your Kotlin (unless you use it to avoid platform issues on uncompilable Kotlin). On the JVM a class is needed that contains these static members. It still isn’t possible to create instances of them. Instead you may be thinking about an object rather than a toplevel.

---

<div class="post-metadata">

**Author:** ![KucherenkoIhor](https://avatars.discourse-cdn.com/v4/letter/k/13edae/32.png) [@KucherenkoIhor](https://discuss.kotlinlang.org/u/KucherenkoIhor)\
**Post date:** [February 7, 2018, 8:54pm UTC](https://discuss.kotlinlang.org/t/how-to-get-kclass-of-file-jvmname/5644/4 "2018-02-07T20:54:35Z")

</div>

No

---

<div class="post-metadata">

**Author:** ![baltiyskiy](https://avatars.discourse-cdn.com/v4/letter/b/85e7bf/32.png) [@baltiyskiy](https://discuss.kotlinlang.org/u/baltiyskiy)\
**Post date:** [October 22, 2018, 4:47pm UTC](https://discuss.kotlinlang.org/t/how-to-get-kclass-of-file-jvmname/5644/5 "2018-10-22T16:47:56Z")

</div>

> [@pdvrieze](#):
>
> On the JVM a class is needed that contains these static members.

Yeah, that’s what the OP asked about — how to get hold of the j.l.Class reference to the container class?

---

<div class="post-metadata">

**Author:** ![pdvrieze](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/pdvrieze/32/1882_2.png) [@pdvrieze](https://discuss.kotlinlang.org/u/pdvrieze)\
**Post date:** [October 23, 2018, 9:57am UTC](https://discuss.kotlinlang.org/t/how-to-get-kclass-of-file-jvmname/5644/6 "2018-10-23T09:57:49Z")

</div>

The easiest way would be through one of the properties or methods in the file. They (at least on Java level) have an owner class that you can get through there (and you can create a `KClass` of a `j.l.Class`). However, the class file is (on the Kotlin side) merely an implementation detail. It does not exist in the language model, as such there is no way to directly reference it (or indirectly without going into JVM implementation details).

---

<div class="post-metadata">

**Author:** ![baltiyskiy](https://avatars.discourse-cdn.com/v4/letter/b/85e7bf/32.png) [@baltiyskiy](https://discuss.kotlinlang.org/u/baltiyskiy)\
**Post date:** [October 23, 2018, 11:40am UTC](https://discuss.kotlinlang.org/t/how-to-get-kclass-of-file-jvmname/5644/7 "2018-10-23T11:40:36Z")

</div>

> [@pdvrieze](#):
>
> The easiest way would be through one of the properties or methods in the file.

I can’t do it right now, can I?  
Suppose I need something like `val log = LoggerFactory.getLogger(clazz)`. If `log` is top-level, how do I take the `clazz`? I can’t use `log` itself (other than wrapping it into lazy, which is an abomination), so I need to declare a dummy some top-level val… but its `::class` only gives us its KClass, not the one of its owner.

Looks like for the time being, I need to wrap such things in an object.

---

<div class="post-metadata">

**Author:** ![pdvrieze](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/pdvrieze/32/1882_2.png) [@pdvrieze](https://discuss.kotlinlang.org/u/pdvrieze)\
**Post date:** [October 23, 2018, 1:26pm UTC](https://discuss.kotlinlang.org/t/how-to-get-kclass-of-file-jvmname/5644/8 "2018-10-23T13:26:30Z")

</div>

You’ll have to use the reflection library in combination with delegation. Using that you can use: [`KProperty.javaGetter`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.reflect/-k-property0/index.html) in the `getValue` or `provideDelegate` operators. `javaGetter` will give you a Java method. A method has an owner and you know the Java class. At the same time you are now tightly bound to implementation details of Kotlin toplevel properties.

---

<div class="post-metadata">

**Author:** ![baltiyskiy](https://avatars.discourse-cdn.com/v4/letter/b/85e7bf/32.png) [@baltiyskiy](https://discuss.kotlinlang.org/u/baltiyskiy)\
**Post date:** [October 30, 2018, 10:02am UTC](https://discuss.kotlinlang.org/t/how-to-get-kclass-of-file-jvmname/5644/9 "2018-10-30T10:02:37Z")

</div>

Thanks! I ended up doing this:

```auto
// Usage: `val log by TopLevelLoggerCreator`
object TopLevelLoggerCreator {
  operator fun getValue(thisRef: Any?, property: KProperty<*>): Logger {
    val loggerClass = property.javaGetter!!.declaringClass
    return LoggerFactory.getLogger(loggerClass)
  }
}

```

… but I ran into an issue when trying to use the code that calls this delegate in the tests: `property.javaGetter` was failing with internal reflection error. I tried to reproduce it in a separate project, but couldn’t. Anyway, in general this should work, so thanks again!
