Skip to content

Commit 37e307b

Browse files
authored
KSP QueryEntity annotation support (#1345)
QueryEntity annotation support
1 parent 0a92cdd commit 37e307b

File tree

2 files changed

+50
-41
lines changed

2 files changed

+50
-41
lines changed

querydsl-tooling/querydsl-ksp-codegen/src/main/kotlin/com/querydsl/ksp/codegen/QueryDslProcessor.kt

Lines changed: 36 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -17,39 +17,42 @@ class QueryDslProcessor(
1717
override fun process(resolver: Resolver): List<KSAnnotated> {
1818
if (settings.enable) {
1919
QueryModelType.entries.forEach { type ->
20-
resolver.getSymbolsWithAnnotation(type.associatedAnnotation)
21-
.forEach { declaration ->
22-
when {
23-
type == QueryModelType.QUERY_PROJECTION -> {
24-
val errorMessage = "${type.associatedAnnotation} annotation" +
25-
" must be declared on a constructor function or class"
26-
when (declaration) {
27-
is KSFunctionDeclaration -> {
28-
if (!declaration.isConstructor()) error(errorMessage)
29-
val parentDeclaration = declaration.parent as? KSClassDeclaration
30-
?: error(errorMessage)
31-
if (isIncluded(parentDeclaration)) {
32-
typeProcessor.addConstructor(parentDeclaration, declaration)
33-
}
34-
}
35-
is KSClassDeclaration -> {
36-
if (isIncluded(declaration)) {
37-
typeProcessor.addClass(declaration, type)
38-
}
39-
}
40-
else -> error(errorMessage)
41-
}
42-
}
43-
declaration is KSClassDeclaration -> {
44-
if (isIncluded(declaration)) {
45-
typeProcessor.addClass(declaration, type)
46-
}
47-
}
48-
else -> {
49-
error("Annotated element was expected to be class or constructor, instead got ${declaration}")
50-
}
51-
}
52-
}
20+
type.associatedAnnotations.forEach { associatedAnnotation ->
21+
resolver.getSymbolsWithAnnotation(associatedAnnotation)
22+
.forEach { declaration ->
23+
when {
24+
type == QueryModelType.QUERY_PROJECTION -> {
25+
val errorMessage = "$associatedAnnotation annotation" +
26+
" must be declared on a constructor function or class"
27+
when (declaration) {
28+
is KSFunctionDeclaration -> {
29+
if (!declaration.isConstructor()) error(errorMessage)
30+
val parentDeclaration = declaration.parent as? KSClassDeclaration
31+
?: error(errorMessage)
32+
if (isIncluded(parentDeclaration)) {
33+
typeProcessor.addConstructor(parentDeclaration, declaration)
34+
}
35+
}
36+
is KSClassDeclaration -> {
37+
if (isIncluded(declaration)) {
38+
typeProcessor.addClass(declaration, type)
39+
}
40+
}
41+
else -> error(errorMessage)
42+
}
43+
}
44+
declaration is KSClassDeclaration -> {
45+
if (isIncluded(declaration)) {
46+
typeProcessor.addClass(declaration, type)
47+
}
48+
}
49+
else -> {
50+
error("Annotated element was expected to be class or constructor, instead got ${declaration}")
51+
}
52+
}
53+
}
54+
}
55+
5356
}
5457
}
5558
return emptyList()

querydsl-tooling/querydsl-ksp-codegen/src/main/kotlin/com/querydsl/ksp/codegen/QueryModelType.kt

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,26 @@ import jakarta.persistence.Entity
99
import jakarta.persistence.MappedSuperclass
1010

1111
enum class QueryModelType(
12-
val associatedAnnotation: String
12+
val associatedAnnotations: List<String>
1313
) {
14-
ENTITY(Entity::class.qualifiedName!!),
15-
EMBEDDABLE(Embeddable::class.qualifiedName!!),
16-
SUPERCLASS(MappedSuperclass::class.qualifiedName!!),
17-
QUERY_PROJECTION(QueryProjection::class.qualifiedName!!);
14+
ENTITY(
15+
listOf(
16+
Entity::class.qualifiedName!!,
17+
"com.querydsl.core.annotations.QueryEntity",
18+
"org.springframework.data.mongodb.core.mapping.Document"
19+
)
20+
),
21+
EMBEDDABLE(listOf(Embeddable::class.qualifiedName!!)),
22+
SUPERCLASS(listOf(MappedSuperclass::class.qualifiedName!!)),
23+
QUERY_PROJECTION(listOf(QueryProjection::class.qualifiedName!!));
1824

1925
companion object {
2026
fun autodetect(classDeclaration: KSClassDeclaration): QueryModelType? {
2127
for (annotation in classDeclaration.annotations) {
2228
for (type in QueryModelType.entries) {
23-
if (annotation.isEqualTo(type.associatedAnnotation)) {
24-
return type
25-
}
29+
if (type.associatedAnnotations.any { ann -> annotation.isEqualTo(ann) }) {
30+
return type
31+
}
2632
}
2733
}
2834
return null

0 commit comments

Comments
 (0)