Skip to content

Issue #484 - Optimize the KLoggerNameResolver for jvm #485

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,41 +1,28 @@
package io.github.oshai.kotlinlogging.internal

import java.lang.reflect.Modifier
import kotlin.reflect.KClass

/** Resolves name of java classes */
internal actual object KLoggerNameResolver {

/** get class name for function by the package of the function */
internal actual fun name(func: () -> Unit): String {
val name = func.javaClass.name
val slicedName =
when {
name.contains("Kt$") -> name.substringBefore("Kt$")
name.contains("$") -> name.substringBefore("$")
else -> name
}
return slicedName
return name(func::class)
}

internal fun name(clazz: KClass<*>): String {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this method should be private

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@oshai historically this method was used in the tests, therefore it's not possible to make it private without rewriting tests structure. https://github.com/oshai/kotlin-logging/blob/master/src/javaMain/kotlin/io/github/oshai/kotlinlogging/internal/KLoggerNameResolver.kt#L21

I would prefer to keep it internal for now and if the changes from #481 will be accepted, then signature of the method will be changed from

internal actual fun name(func: () -> Unit): String

to

internal actual fun name(ref: Any): String {

and then we can update the tests to use instances of the classes, rather then KClass

return clazz.java.name.toCleanClassName()
}

/** get class name for java class (that usually represents kotlin class) */
internal fun <T : Any> name(forClass: Class<T>): String = unwrapCompanionClass(forClass).name
private val classNameEndings = listOf("Kt$", "$")

/** unwrap companion class to enclosing class given a Java Class */
private fun <T : Any> unwrapCompanionClass(clazz: Class<T>): Class<*> {
return clazz.enclosingClass?.let { enclosingClass ->
try {
enclosingClass.declaredFields
.find { field ->
field.name == clazz.simpleName &&
Modifier.isStatic(field.modifiers) &&
field.type == clazz
}
?.run { enclosingClass }
} catch (se: SecurityException) {
// The security manager isn't properly set up, so it won't be possible
// to search for the target declared field.
null
private fun String.toCleanClassName(): String {
classNameEndings.forEach { ending ->
val indexOfEnding = this.indexOf(ending)
if (indexOfEnding != -1) {
return this.substring(0, indexOfEnding)
}
} ?: clazz
}
return this
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.github.oshai.kotlinlogging.internal

import java.util.stream.Stream
import kotlin.reflect.KClass
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.TestInstance
import org.junit.jupiter.params.ParameterizedTest
Expand All @@ -12,43 +13,37 @@ class KLoggerNameResolverTest {

@ParameterizedTest
@MethodSource("testNames")
fun testNames(expectedName: String, clazz: Class<*>) {
fun testNames(expectedName: String, clazz: KClass<*>) {
assertEquals(expectedName, KLoggerNameResolver.name(clazz))
}

private fun testNames(): Stream<Arguments> =
Stream.of(
Arguments.of("io.github.oshai.kotlinlogging.internal.BaseClass", BaseClass::class.java),
Arguments.of("io.github.oshai.kotlinlogging.internal.ChildClass", ChildClass::class.java),
Arguments.of(
"io.github.oshai.kotlinlogging.internal.BaseClass",
BaseClass.Companion::class.java,
),
Arguments.of("io.github.oshai.kotlinlogging.internal.BaseClass", BaseClass::class),
Arguments.of("io.github.oshai.kotlinlogging.internal.ChildClass", ChildClass::class),
Arguments.of("io.github.oshai.kotlinlogging.internal.BaseClass", BaseClass.Companion::class),
Arguments.of(
"io.github.oshai.kotlinlogging.internal.ChildClass",
ChildClass.Companion::class.java,
ChildClass.Companion::class,
),
Arguments.of("io.github.oshai.kotlinlogging.internal.Singleton", Singleton::class.java),
Arguments.of("io.github.oshai.kotlinlogging.internal.MyInterface", MyInterface::class.java),
Arguments.of("java.lang.Object", Any().javaClass),
Arguments.of("io.github.oshai.kotlinlogging.internal.Singleton", Singleton::class),
Arguments.of("io.github.oshai.kotlinlogging.internal.MyInterface", MyInterface::class),
Arguments.of("java.lang.Object", Any()::class),
Arguments.of(
"io.github.oshai.kotlinlogging.internal.KLoggerNameResolverTest\$testNames$1",
object {}.javaClass,
"io.github.oshai.kotlinlogging.internal.KLoggerNameResolverTest",
object {}::class,
),
Arguments.of("io.github.oshai.kotlinlogging.internal.BaseClass", BaseClass.InnerClass::class),
Arguments.of(
"io.github.oshai.kotlinlogging.internal.BaseClass\$InnerClass\$Obj",
BaseClass.InnerClass.Obj::class.java,
),
Arguments.of(
"io.github.oshai.kotlinlogging.internal.BaseClass\$InnerClass\$Obj",
BaseClass.InnerClass.Obj.javaClass,
"io.github.oshai.kotlinlogging.internal.BaseClass",
BaseClass.InnerClass.Obj::class,
),
Arguments.of(
"io.github.oshai.kotlinlogging.internal.BaseClass\$InnerClass",
BaseClass.InnerClass.CmpObj::class.java,
"io.github.oshai.kotlinlogging.internal.BaseClass",
BaseClass.InnerClass.CmpObj::class,
),
Arguments.of("io.github.oshai.kotlinlogging.internal.Foo\$Bar", Foo.Bar::class.java),
Arguments.of("io.github.oshai.kotlinlogging.internal.Foo\$Bar2", Foo.Bar3.javaClass),
Arguments.of("io.github.oshai.kotlinlogging.internal.Foo", Foo.Bar::class),
Arguments.of("io.github.oshai.kotlinlogging.internal.Foo", Foo.Bar3::class),
Arguments.of(
"io.github.oshai.kotlinlogging.internal.PrivateCompanion",
PrivateCompanion().companionClass,
Expand Down Expand Up @@ -88,7 +83,7 @@ class Foo {
}

class PrivateCompanion {
val companionClass: Class<*> = Companion::class.java
val companionClass: KClass<*> = Companion::class

private companion object
}
Loading