Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,39 +17,42 @@ class QueryDslProcessor(
override fun process(resolver: Resolver): List<KSAnnotated> {
if (settings.enable) {
QueryModelType.entries.forEach { type ->
resolver.getSymbolsWithAnnotation(type.associatedAnnotation)
.forEach { declaration ->
when {
type == QueryModelType.QUERY_PROJECTION -> {
val errorMessage = "${type.associatedAnnotation} annotation" +
" must be declared on a constructor function or class"
when (declaration) {
is KSFunctionDeclaration -> {
if (!declaration.isConstructor()) error(errorMessage)
val parentDeclaration = declaration.parent as? KSClassDeclaration
?: error(errorMessage)
if (isIncluded(parentDeclaration)) {
typeProcessor.addConstructor(parentDeclaration, declaration)
}
}
is KSClassDeclaration -> {
if (isIncluded(declaration)) {
typeProcessor.addClass(declaration, type)
}
}
else -> error(errorMessage)
}
}
declaration is KSClassDeclaration -> {
if (isIncluded(declaration)) {
typeProcessor.addClass(declaration, type)
}
}
else -> {
error("Annotated element was expected to be class or constructor, instead got ${declaration}")
}
}
}
type.associatedAnnotations.forEach { associatedAnnotation ->
resolver.getSymbolsWithAnnotation(associatedAnnotation)
.forEach { declaration ->
when {
type == QueryModelType.QUERY_PROJECTION -> {
val errorMessage = "$associatedAnnotation annotation" +
" must be declared on a constructor function or class"
when (declaration) {
is KSFunctionDeclaration -> {
if (!declaration.isConstructor()) error(errorMessage)
val parentDeclaration = declaration.parent as? KSClassDeclaration
?: error(errorMessage)
if (isIncluded(parentDeclaration)) {
typeProcessor.addConstructor(parentDeclaration, declaration)
}
}
is KSClassDeclaration -> {
if (isIncluded(declaration)) {
typeProcessor.addClass(declaration, type)
}
}
else -> error(errorMessage)
}
}
declaration is KSClassDeclaration -> {
if (isIncluded(declaration)) {
typeProcessor.addClass(declaration, type)
}
}
else -> {
error("Annotated element was expected to be class or constructor, instead got ${declaration}")
}
}
}
}

}
}
return emptyList()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,26 @@ import jakarta.persistence.Entity
import jakarta.persistence.MappedSuperclass

enum class QueryModelType(
val associatedAnnotation: String
val associatedAnnotations: List<String>
) {
ENTITY(Entity::class.qualifiedName!!),
EMBEDDABLE(Embeddable::class.qualifiedName!!),
SUPERCLASS(MappedSuperclass::class.qualifiedName!!),
QUERY_PROJECTION(QueryProjection::class.qualifiedName!!);
ENTITY(
listOf(
Entity::class.qualifiedName!!,
"com.querydsl.core.annotations.QueryEntity",
"org.springframework.data.mongodb.core.mapping.Document"
)
),
EMBEDDABLE(listOf(Embeddable::class.qualifiedName!!)),
SUPERCLASS(listOf(MappedSuperclass::class.qualifiedName!!)),
QUERY_PROJECTION(listOf(QueryProjection::class.qualifiedName!!));

companion object {
fun autodetect(classDeclaration: KSClassDeclaration): QueryModelType? {
for (annotation in classDeclaration.annotations) {
for (type in QueryModelType.entries) {
if (annotation.isEqualTo(type.associatedAnnotation)) {
return type
}
if (type.associatedAnnotations.any { ann -> annotation.isEqualTo(ann) }) {
return type
}
}
}
return null
Expand Down