Skip to content
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

Merged
merged 8 commits into from
Nov 10, 2023
Prev Previous commit
Next Next commit
feat(render): add introspect method for KCallable
jbl428 committed Nov 8, 2023
commit cccf3fb921aadbb9391f2e24821b5e4ca5c8c8b5
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@ import com.linecorp.kotlinjdsl.SinceJdsl
import com.linecorp.kotlinjdsl.render.AbstractRenderContextElement
import com.linecorp.kotlinjdsl.render.RenderContext
import java.util.concurrent.ConcurrentHashMap
import kotlin.reflect.KCallable
import kotlin.reflect.KClass

/**
@@ -15,7 +16,8 @@ class JpqlRenderIntrospector(
) : AbstractRenderContextElement(Key) {
companion object Key : RenderContext.Key<JpqlRenderIntrospector>

private val tableLookupCache: MutableMap<KClass<*>, JpqlEntityDescription> = ConcurrentHashMap()
private val classTableLookupCache: MutableMap<KClass<*>, JpqlEntityDescription> = ConcurrentHashMap()
private val propertyTableLookupCache: MutableMap<KCallable<*>, JpqlPropertyDescription> = ConcurrentHashMap()
Copy link
Member

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 it entityLookupCache and propertyLookupCache.


/**
* Creates a new introspector by combining this introspector and the introspector.
@@ -38,8 +40,22 @@ class JpqlRenderIntrospector(
return getCachedDescription(clazz)
}

/**
* Introspects the KCallable to get the property information.
*/
@SinceJdsl("3.1.0")
fun introspect(property: KCallable<*>): JpqlPropertyDescription {
return getCachedDescription(property)
}

private fun getCachedDescription(clazz: KClass<*>): JpqlEntityDescription {
return tableLookupCache.computeIfAbsent(clazz) {
return classTableLookupCache.computeIfAbsent(clazz) {
getDescription(it)
}
}

private fun getCachedDescription(property: KCallable<*>): JpqlPropertyDescription {
return propertyTableLookupCache.computeIfAbsent(property) {
getDescription(it)
}
}
@@ -48,4 +64,9 @@ class JpqlRenderIntrospector(
return introspector.introspect(clazz)
?: throw IllegalStateException("There is no description for ${clazz.java.name}")
}

private fun getDescription(property: KCallable<*>): JpqlPropertyDescription {
return introspector.introspect(property)
?: throw IllegalStateException("There is no description for ${property.name}")
}
}
Original file line number Diff line number Diff line change
@@ -18,6 +18,7 @@ import io.mockk.verifySequence
import org.assertj.core.api.WithAssertions
import org.junit.jupiter.api.Test
import org.junit.jupiter.params.ParameterizedTest
import kotlin.reflect.KClass

@JpqlSerializerTest
internal class JpqlEntitySerializerTest : WithAssertions {
@@ -57,7 +58,7 @@ internal class JpqlEntitySerializerTest : WithAssertions {
clause: JpqlRenderClause,
) {
// given
every { introspector.introspect(any()) } returns entityDescription1
every { introspector.introspect(any<KClass<*>>()) } returns entityDescription1

val part = Entities.entity(Book::class, alias1)
val context = TestRenderContext(introspector, statement, clause)
Original file line number Diff line number Diff line change
@@ -19,6 +19,7 @@ import io.mockk.verifySequence
import org.assertj.core.api.WithAssertions
import org.junit.jupiter.api.Test
import org.junit.jupiter.params.ParameterizedTest
import kotlin.reflect.KClass

@JpqlSerializerTest
class JpqlEntityTreatSerializerTest : WithAssertions {
@@ -58,7 +59,7 @@ class JpqlEntityTreatSerializerTest : WithAssertions {
clause: JpqlRenderClause,
) {
// given
every { introspector.introspect(any()) } returns entityDescription1
every { introspector.introspect(any<KClass<*>>()) } returns entityDescription1

val part = Entities.treat(
entity1,
@@ -93,7 +94,7 @@ class JpqlEntityTreatSerializerTest : WithAssertions {
clause: JpqlRenderClause,
) {
// given
every { introspector.introspect(any()) } returns entityDescription1
every { introspector.introspect(any<KClass<*>>()) } returns entityDescription1

val part = Entities.treat(
entity1,
Original file line number Diff line number Diff line change
@@ -15,6 +15,7 @@ import io.mockk.impl.annotations.MockK
import io.mockk.verifySequence
import org.assertj.core.api.WithAssertions
import org.junit.jupiter.api.Test
import kotlin.reflect.KClass

@JpqlSerializerTest
class JpqlPathTreatSerializerTest : WithAssertions {
@@ -47,7 +48,7 @@ class JpqlPathTreatSerializerTest : WithAssertions {
@Test
fun serialize() {
// given
every { introspector.introspect(any()) } returns entityDescription1
every { introspector.introspect(any<KClass<*>>()) } returns entityDescription1

val part = Paths.treat(
path1,
Original file line number Diff line number Diff line change
@@ -13,6 +13,7 @@ import io.mockk.impl.annotations.MockK
import io.mockk.verifySequence
import org.assertj.core.api.WithAssertions
import org.junit.jupiter.api.Test
import kotlin.reflect.KClass

@JpqlSerializerTest
class JpqlValueSerializerTest : WithAssertions {
@@ -59,7 +60,7 @@ class JpqlValueSerializerTest : WithAssertions {
@Test
fun `serialize() draws entity name, when value is KClass`() {
// given
every { introspector.introspect(any()) } returns entityDescription1
every { introspector.introspect(any<KClass<*>>()) } returns entityDescription1

val part = Expressions.value(
Book::class,