-
Notifications
You must be signed in to change notification settings - Fork 55
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
Fix: add proper backwards-compatible Kotlin support #42
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 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
This file contains 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
118 changes: 118 additions & 0 deletions
118
...a-common/src/main/java/com/infobip/spring/data/common/PreferredConstructorDiscoverer.java
This file contains 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 |
---|---|---|
@@ -0,0 +1,118 @@ | ||
package com.infobip.spring.data.common; | ||
|
||
import kotlin.jvm.JvmClassMappingKt; | ||
import kotlin.reflect.KFunction; | ||
import kotlin.reflect.full.KClasses; | ||
import kotlin.reflect.jvm.ReflectJvmMapping; | ||
import org.springframework.core.DefaultParameterNameDiscoverer; | ||
import org.springframework.core.ParameterNameDiscoverer; | ||
import org.springframework.data.annotation.PersistenceConstructor; | ||
import org.springframework.data.mapping.PersistentEntity; | ||
import org.springframework.data.mapping.PersistentProperty; | ||
import org.springframework.data.mapping.PreferredConstructor; | ||
import org.springframework.data.util.ClassTypeInformation; | ||
import org.springframework.data.util.KotlinReflectionUtils; | ||
import org.springframework.data.util.TypeInformation; | ||
import org.springframework.lang.Nullable; | ||
|
||
import java.lang.annotation.Annotation; | ||
import java.lang.reflect.Constructor; | ||
import java.util.*; | ||
|
||
/** | ||
* Utility class to find the preferred constructor which is compatible with both Spring Data JDBC and QueryDSL. | ||
*/ | ||
interface PreferredConstructorDiscoverer { | ||
|
||
@Nullable | ||
static <T, P extends PersistentProperty<P>> PreferredConstructor<T, P> discover(Class<T> type) { | ||
return Discoverers.findDiscoverer(type) | ||
.discover(ClassTypeInformation.from(type), null); | ||
} | ||
|
||
enum Discoverers { | ||
|
||
DEFAULT { | ||
|
||
@Nullable | ||
@Override | ||
<T, P extends PersistentProperty<P>> PreferredConstructor<T, P> discover( | ||
TypeInformation<T> type, @Nullable PersistentEntity<T, P> entity) { | ||
|
||
return Arrays.stream(type.getType().getDeclaredConstructors()) | ||
.filter(it -> !it.isSynthetic()) | ||
.filter(it -> it.isAnnotationPresent(PersistenceConstructor.class)) | ||
.map(it -> buildPreferredConstructor(it, type, entity)) | ||
.findFirst() | ||
.orElseGet(() -> Arrays.stream(type.getType().getDeclaredConstructors()) | ||
.filter(it -> !it.isSynthetic()) | ||
.max(Comparator.comparingInt(Constructor::getParameterCount)) | ||
.map(it -> buildPreferredConstructor(it, type, entity)) | ||
.orElse(null)); | ||
} | ||
}, | ||
|
||
KOTLIN { | ||
|
||
@Nullable | ||
@Override | ||
<T, P extends PersistentProperty<P>> PreferredConstructor<T, P> discover( | ||
TypeInformation<T> type, @Nullable PersistentEntity<T, P> entity) { | ||
|
||
return Arrays.stream(type.getType().getDeclaredConstructors()) | ||
.filter(it -> !it.isSynthetic()) | ||
.filter(it -> it.isAnnotationPresent(PersistenceConstructor.class)) | ||
.map(it -> buildPreferredConstructor(it, type, entity)) | ||
.findFirst() | ||
.orElseGet(() -> { | ||
KFunction<T> primaryConstructor = KClasses | ||
.getPrimaryConstructor(JvmClassMappingKt.getKotlinClass(type.getType())); | ||
|
||
if (primaryConstructor == null) { | ||
return DEFAULT.discover(type, entity); | ||
} | ||
|
||
Constructor<T> javaConstructor = ReflectJvmMapping.getJavaConstructor(primaryConstructor); | ||
|
||
return javaConstructor != null ? buildPreferredConstructor(javaConstructor, type, entity) : null; | ||
}); | ||
} | ||
}; | ||
|
||
private static final ParameterNameDiscoverer PARAMETER_NAME_DISCOVERER = new DefaultParameterNameDiscoverer(); | ||
|
||
private static Discoverers findDiscoverer(Class<?> type) { | ||
return KotlinReflectionUtils.isSupportedKotlinClass(type) ? KOTLIN : DEFAULT; | ||
} | ||
|
||
@Nullable | ||
abstract <T, P extends PersistentProperty<P>> PreferredConstructor<T, P> discover(TypeInformation<T> type, | ||
@Nullable PersistentEntity<T, P> entity); | ||
|
||
@SuppressWarnings({ "unchecked", "rawtypes" }) | ||
private static <T, P extends PersistentProperty<P>> PreferredConstructor<T, P> buildPreferredConstructor( | ||
Constructor<?> constructor, TypeInformation<T> typeInformation, @Nullable PersistentEntity<T, P> entity) { | ||
|
||
if (constructor.getParameterCount() == 0) { | ||
return new PreferredConstructor<>((Constructor<T>) constructor); | ||
} | ||
|
||
List<TypeInformation<?>> parameterTypes = typeInformation.getParameterTypes(constructor); | ||
String[] parameterNames = PARAMETER_NAME_DISCOVERER.getParameterNames(constructor); | ||
|
||
PreferredConstructor.Parameter<Object, P>[] parameters = new PreferredConstructor.Parameter[parameterTypes.size()]; | ||
Annotation[][] parameterAnnotations = constructor.getParameterAnnotations(); | ||
|
||
for (int i = 0; i < parameterTypes.size(); i++) { | ||
|
||
String name = parameterNames == null || parameterNames.length <= i ? null : parameterNames[i]; | ||
TypeInformation<?> type = parameterTypes.get(i); | ||
Annotation[] annotations = parameterAnnotations[i]; | ||
|
||
parameters[i] = new PreferredConstructor.Parameter(name, type, annotations, entity); | ||
} | ||
|
||
return new PreferredConstructor<>((Constructor<T>) constructor, parameters); | ||
} | ||
} | ||
} |
This file contains 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
68 changes: 68 additions & 0 deletions
68
...ng-data-common/src/test/kotlin/com/infobip/spring/data/common/PreferredConstructorTest.kt
This file contains 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 |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package com.infobip.spring.data.common | ||
|
||
import org.assertj.core.api.BDDAssertions.then | ||
import org.junit.jupiter.api.Test | ||
import org.springframework.data.annotation.Id | ||
import org.springframework.data.annotation.PersistenceConstructor | ||
import org.springframework.data.mapping.PersistentProperty | ||
|
||
class PreferredConstructorTest { | ||
|
||
@Test | ||
internal fun `it should prefer persistence constructors`() { | ||
val preferredConstructor = PreferredConstructorDiscoverer | ||
.discover<EntityWithPersistenceConstructor, PersistentProperty<*>>(EntityWithPersistenceConstructor::class.java) | ||
|
||
then(preferredConstructor.parameters).hasSize(3) | ||
then(preferredConstructor.parameters.map { it.name }).containsExactly("id", "firstName", "lastName") | ||
} | ||
|
||
@Test | ||
internal fun `if no persistence constructor, it should take primary constructor`() { | ||
val preferredConstructor = PreferredConstructorDiscoverer | ||
.discover<DataClass, PersistentProperty<*>>(DataClass::class.java) | ||
|
||
then(preferredConstructor.parameters).hasSize(3) | ||
then(preferredConstructor.parameters.map { it.name }).containsExactly("id", "firstName", "lastName") | ||
} | ||
|
||
@Test | ||
internal fun `if no primary constructor, it should take constructor with most arguments`() { | ||
val preferredConstructor = PreferredConstructorDiscoverer | ||
.discover<EntityWithoutPersistenceConstructor, PersistentProperty<*>>(EntityWithoutPersistenceConstructor::class.java) | ||
|
||
then(preferredConstructor.parameters).hasSize(3) | ||
then(preferredConstructor.parameters.map { it.name }).containsExactly("id", "firstName", "lastName") | ||
} | ||
} | ||
|
||
data class DataClass( | ||
@Id private val id: String, | ||
val firstName: String, | ||
val lastName: String | ||
) | ||
|
||
data class EntityWithPersistenceConstructor( | ||
@Id private val id: String, | ||
private val nameInformation: NameInformation | ||
) { | ||
@PersistenceConstructor | ||
constructor(id: String, firstName: String, lastName: String): this(id, NameInformation(firstName, lastName)) | ||
} | ||
|
||
class EntityWithoutPersistenceConstructor { | ||
val id: String | ||
val nameInformation: NameInformation | ||
|
||
constructor() { | ||
this.id = "_" | ||
this.nameInformation = NameInformation("_", "_") | ||
} | ||
|
||
constructor(id: String, firstName: String, lastName: String) { | ||
this.id = id | ||
this.nameInformation = NameInformation(firstName, lastName) | ||
} | ||
} | ||
|
||
data class NameInformation(val firstName: String, val lastName: String) |
This file contains 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
This file contains 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
This file contains 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
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.
PS I suspect that this can't be optional due to the fact that kotlin discovery code path is invoked every time, even in non kotlin environments?
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.
In Spring Data Common it's optional, I think because we first invoke
KotlinReflectionUtils. isSupportedKotlinClass
from Spring, which - I think - doesn't rely on kotkin-reflect itself.