-
Notifications
You must be signed in to change notification settings - Fork 174
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
Refactor KVI #512
Refactor KVI #512
Conversation
return | ||
} | ||
|
||
throw IllegalAccessException("Cannot access to function or companion object instance, target: $callable") |
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.
The error message here is a summary of what was originally individual content.
Should I follow the existing content and give a more specific message for each one?
Also, I couldn't think of any good error message content, so I would appreciate any suggestions.
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'm not sure what you mean by individual content
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 don't remember exactly what I was thinking when I left this comment because it's been so long since I created the PR...
I think I was probably worrying about the following two points.
- Whether people who read this error message will be able to respond appropriately
- Should I indicate specifically which of
constructor
,factory function
, orcompanion object
was inaccessible?
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.
Ahh, yes, if that's possible I think it could prove to be quite helpful
return | ||
} | ||
|
||
throw IllegalAccessException("Cannot access to function or companion object instance, target: $callable") |
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'm not sure what you mean by individual content
} | ||
} | ||
else -> throw IllegalStateException("Expected a constructor or method to create a Kotlin object, instead found ${_withArgsCreator.annotated.javaClass.name}") | ||
} // we cannot reflect this method so do the default Java-ish behavior |
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.
Won't the else -> throw
prevent any further processing? Perhaps it should be else -> null
?
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.
The reason is that the following process has been ported as is.
jackson-module-kotlin/src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinValueInstantiator.kt
Lines 40 to 48 in 6023113
val callable = when (_withArgsCreator) { | |
is AnnotatedConstructor -> cache.kotlinFromJava(_withArgsCreator.annotated as Constructor<Any>) | |
is AnnotatedMethod -> cache.kotlinFromJava(_withArgsCreator.annotated as Method) | |
else -> throw IllegalStateException("Expected a constructor or method to create a Kotlin object, instead found ${_withArgsCreator.annotated.javaClass.name}") | |
} ?: return super.createFromObjectWith( | |
ctxt, | |
props, | |
buffer | |
) // we cannot reflect this method so do the default Java-ish behavior |
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.
Got it. For posterity the null half of that ?:
was moved to KotlinValueInstantiator:31
src/main/kotlin/com/fasterxml/jackson/module/kotlin/ReflectionCache.kt
Outdated
Show resolved
Hide resolved
src/main/kotlin/com/fasterxml/jackson/module/kotlin/ReflectionCache.kt
Outdated
Show resolved
Hide resolved
@@ -157,23 +120,19 @@ internal class KotlinValueInstantiator( | |||
numCallableParameters++ |
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.
Existing code, but I think this would be a bit clearer as numCallableParameters + 1
, since we don't need to increment the value in place. That'll also allow numCallableParameters
to be declared as a val
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 was planning to fix this when I introduce ArgumentBucket
in the next PR.
https://github.com/FasterXML/jackson-module-kotlin/pull/439/files#diff-4fffadfbb02911c033bc87266a27253e0410d773ee8067b6f6786c440ac5306fR1
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.
Sounds good.
init { | ||
callable.isAccessible = true | ||
} |
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.
Does this override the above since this init
block is lower? Is that the correct behavior?
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.
Are you saying that it is difficult to know which comes first, initializing the property or rewriting isAccessible
?
If I believe Intellij IDEA
's point, it should work correctly, but if I value readability, should I rewrite it as follows?(Rewriting the accessible property was surprisingly expensive, so I've included a change to rewrite it only when necessary.)
override val accessible: Boolean = run {
val initialAccessible = callable.isAccessible
if (!initialAccessible) callable.isAccessible = true
initialAccessible
}
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.
Understood, I was just a bit unsure whether that rewrite was intended 👍
…Cache.kt Co-authored-by: Drew Stephens <[email protected]>
…Cache.kt Co-authored-by: Drew Stephens <[email protected]>
This PR is a partial realization of the first step of the 3-step change I suggested in my comment #439.
Changes from existing
Many of the reflection operations performed by the
KotlinValueInstantiator
are now cached.This is expected to speed up the process since the reflection process is omitted.
The results of the benchmark comparison are as follows.
From this result, we can see that it is actually faster.
before
after
Changes from #439
The class that was originally named
Instantiator
has been changed toValueCreator
.Because the name
Instantiator
seemed to be confusing with the keyword derived fromJackson
.Also, the content that was originally defined as just an
interface
has been changed to asealed class
.This makes it easier to standardize the process and improve the handling of conditional expressions.
Others
I don't think it's necessary to make it
ExperimentalDeserializationBackend
, since many of the changes are based on existing content.Therefore, I have not incorporated the relevant changes.
Also, to keep the size of the PR small, I have not yet refactored the
KVI.createFromObjectWith
function, such as splitting it.I plan to do that after this PR is merged.