-
Notifications
You must be signed in to change notification settings - Fork 92
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
Add support for java getter #518
Changes from 6 commits
0167496
9034133
8f63036
c9ba4d4
cccf3fb
4e145d2
3a2fbc7
9de2e4a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.linecorp.kotlinjdsl.render.jpql.introspector | ||
|
||
import com.linecorp.kotlinjdsl.SinceJdsl | ||
|
||
/** | ||
* Interface to represent the property information. | ||
*/ | ||
@SinceJdsl("3.1.0") | ||
interface JpqlPropertyDescription { | ||
@SinceJdsl("3.1.0") | ||
val name: String | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,11 +3,15 @@ package com.linecorp.kotlinjdsl.render.jpql.introspector.impl | |
import com.linecorp.kotlinjdsl.Internal | ||
import com.linecorp.kotlinjdsl.render.jpql.introspector.JpqlEntityDescription | ||
import com.linecorp.kotlinjdsl.render.jpql.introspector.JpqlIntrospector | ||
import com.linecorp.kotlinjdsl.render.jpql.introspector.JpqlPropertyDescription | ||
import kotlin.reflect.KCallable | ||
import kotlin.reflect.KClass | ||
import kotlin.reflect.KFunction1 | ||
import kotlin.reflect.KProperty1 | ||
import kotlin.reflect.full.findAnnotations | ||
|
||
/** | ||
* Introspector that introspects KClass using [javax.persistence.Entity]. | ||
* Introspector that introspects KClass and KCallable using [javax.persistence.Entity]. | ||
*/ | ||
@Internal | ||
class JavaxJpqlIntrospector : JpqlIntrospector { | ||
|
@@ -20,6 +24,21 @@ class JavaxJpqlIntrospector : JpqlIntrospector { | |
null | ||
} | ||
} | ||
|
||
override fun introspect(property: KCallable<*>): JpqlPropertyDescription? { | ||
return when (property) { | ||
is KProperty1<*, *> -> JpqlProperty(property.name) | ||
is KFunction1<*, *> -> JpqlProperty(resolvePropertyName(property)) | ||
else -> null | ||
} | ||
} | ||
|
||
private fun resolvePropertyName(getter: KFunction1<*, *>): String = | ||
if (getter.name.startsWith("is")) { | ||
getter.name | ||
} else { | ||
getter.name.removePrefix("get").replaceFirstChar { it.lowercase() } | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suppose the Property Introspector can be separated from
class JavaxJpqlEntityIntrospector : JpqlIntrospector {
override fun introspect(type: KClass<*>): JpqlEntityDescription? {
// ...
}
override fun introspect(property: KCallable<*>): JpqlPropertyDescription? {
return null
}
}
class JakartaJpqlEntityIntrospector : JpqlIntrospector {
override fun introspect(type: KClass<*>): JpqlEntityDescription? {
// ...
}
override fun introspect(property: KCallable<*>): JpqlPropertyDescription? {
return null
}
}
class KotlinStyleJpqlPropertyIntrospector : JpqlIntrospector {
override fun introspect(type: KClass<*>): JpqlEntityDescription? {
return null
}
override fun introspect(property: KCallable<*>): JpqlPropertyDescription? {
return when (property) {
is KProperty1<*, *> -> JpqlProperty(property.name)
is KFunction1<*, *> -> JpqlProperty(resolvePropertyName(property))
else -> null
}
}
// ...
}
private class DefaultModule : JpqlRenderModule {
override fun setupModule(context: JpqlRenderModule.SetupContext) {
// ...
context.appendIntrospector(KotlinStyleJpqlPropertyIntrospector())
// ...
}
} I think also you could create two abstract classes, |
||
} | ||
|
||
private data class JavaxEntity( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.linecorp.kotlinjdsl.render.jpql.introspector.impl | ||
|
||
import com.linecorp.kotlinjdsl.render.jpql.introspector.JpqlPropertyDescription | ||
|
||
internal data class JpqlProperty( | ||
override val name: String, | ||
) : JpqlPropertyDescription |
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
table
prefix is a misnomer on my part - I named it table because I was planning to have native queries at that time, but now that it's JPQL-only, I think it's okay to name itentityLookupCache
andpropertyLookupCache
.