Skip to content

Commit

Permalink
fix #182 - change a reflection method so that it works on private fields
Browse files Browse the repository at this point in the history
  • Loading branch information
ilya-yamschikov authored and oshai committed Jul 13, 2021
1 parent bd0c8c3 commit f125269
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/jvmMain/kotlin/mu/internal/KLoggerNameResolver.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ internal object KLoggerNameResolver {
inline private fun <T : Any> unwrapCompanionClass(clazz: Class<T>): Class<*> {
if (clazz.enclosingClass != null) {
try {
val field = clazz.enclosingClass.getField(clazz.simpleName)
val field = clazz.enclosingClass.getDeclaredField(clazz.simpleName)
if (Modifier.isStatic(field.modifiers) && field.type == clazz) {
// && field.get(null) === obj
// the above might be safer but problematic with initialization order
Expand Down
6 changes: 6 additions & 0 deletions src/jvmTest/kotlin/mu/internal/KLoggerNameResolverTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class KLoggerNameResolverTest {
)
assertEquals("mu.internal.Foo\$Bar", KLoggerNameResolver.name(Foo.Bar::class.java))
assertEquals("mu.internal.Foo\$Bar2", KLoggerNameResolver.name(Foo.Bar3.javaClass))
assertEquals("mu.internal.PrivateCompanion", KLoggerNameResolver.name(PrivateCompanion().companionClass))
}
}

Expand Down Expand Up @@ -66,3 +67,8 @@ class Foo {
val Bar3 = Foo().z
}
}

class PrivateCompanion {
val companionClass: Class<*> = Companion::class.java
private companion object
}

0 comments on commit f125269

Please sign in to comment.