-
-
Notifications
You must be signed in to change notification settings - Fork 123
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
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
41 changes: 14 additions & 27 deletions
41
src/javaMain/kotlin/io/github/oshai/kotlinlogging/internal/KLoggerNameResolver.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
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 | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
to
and then we can update the tests to use instances of the classes, rather then KClass