Skip to content

Commit 87a9762

Browse files
committed
fixes #132 Projections are not allowed on type arguments of functions and properties in generated code
1 parent cf06ccd commit 87a9762

File tree

12 files changed

+332
-75
lines changed

12 files changed

+332
-75
lines changed

kmongo-annotation-processor/src/main/kotlin/org/litote/kmongo/KMongoAnnotationProcessor.kt

Lines changed: 44 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -369,32 +369,56 @@ class KMongoAnnotationProcessor : KGenerator() {
369369
else if (annotatedMap) enclosedValueMapPackage(type)
370370
else env.elementUtils.getPackageOf(returnType).qualifiedName.toString()
371371

372-
fun propertyClass(start: Boolean): TypeName {
372+
fun propertyClass(start: Boolean, withGeneric: Boolean = true): TypeName {
373373
val sourceClass = if (start) sourceClassName else TypeVariableName("T")
374374
return if (annotated) {
375375
ClassName(
376376
packageOfReturnType,
377377
generatedClassProperty(type, annotatedCollection, annotatedMap)
378-
).parameterizedBy(
379-
*listOfNotNull(sourceClass, mapKeyClass(type, annotatedMap)).toTypedArray()
380-
)
378+
).run {
379+
if (withGeneric) {
380+
parameterizedBy(
381+
*listOfNotNull(sourceClass, mapKeyClass(type, annotatedMap)).toTypedArray()
382+
)
383+
} else {
384+
this
385+
}
386+
}
381387
} else {
382388
if (collection) {
383-
KCollectionSimplePropertyPath::class.asClassName().parameterizedBy(
384-
sourceClass,
385-
e.typeArgument()!!.copy(nullable = true)
386-
)
389+
KCollectionSimplePropertyPath::class.asClassName().run {
390+
if (withGeneric) {
391+
parameterizedBy(
392+
sourceClass,
393+
e.typeArgument()!!.copy(nullable = true)
394+
)
395+
} else {
396+
this
397+
}
398+
}
387399
} else if (map) {
388-
KMapSimplePropertyPath::class.asClassName().parameterizedBy(
389-
sourceClass,
390-
e.typeArgument()!!.copy(nullable = true),
391-
e.typeArgument(1)!!.copy(nullable = true)
392-
)
400+
KMapSimplePropertyPath::class.asClassName().run {
401+
if (withGeneric) {
402+
parameterizedBy(
403+
sourceClass,
404+
e.typeArgument()!!.copy(nullable = true),
405+
e.typeArgument(1)!!.copy(nullable = true)
406+
)
407+
} else {
408+
this
409+
}
410+
}
393411
} else {
394-
(if (start) KProperty1::class.asClassName() else KPropertyPath::class.asClassName()).parameterizedBy(
395-
sourceClass,
396-
propertyType.copy(nullable = true)
397-
)
412+
(if (start) KProperty1::class.asClassName() else KPropertyPath::class.asClassName()).run {
413+
if (withGeneric) {
414+
parameterizedBy(
415+
sourceClass,
416+
propertyType.copy(nullable = true)
417+
)
418+
} else {
419+
this
420+
}
421+
}
398422
}
399423
}
400424
}
@@ -493,7 +517,7 @@ class KMongoAnnotationProcessor : KGenerator() {
493517
if (annotated) {
494518
addCode(
495519
"return %T(null,%L)",
496-
propertyClass(true),
520+
propertyClass(true, false),
497521
propertyReference(true, propertyDef)
498522
)
499523
} else {
@@ -504,9 +528,8 @@ class KMongoAnnotationProcessor : KGenerator() {
504528
.build()
505529
)
506530

507-
val classPropertyClass: TypeName = propertyClass(false)
508531
val property = PropertySpec
509-
.builder(generatedFieldName(e), classPropertyClass)
532+
.builder(generatedFieldName(e), propertyClass(false))
510533
.mutable(false)
511534
.getter(
512535
FunSpec.getterBuilder().apply {
@@ -515,7 +538,7 @@ class KMongoAnnotationProcessor : KGenerator() {
515538
if (annotated) {
516539
generatedClassProperty(type, annotatedCollection, annotatedMap)
517540
} else {
518-
classPropertyClass
541+
propertyClass(false, false)
519542
},
520543
if (annotated || collection || map) propertyReference(false) else propertyDef
521544
)
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright (C) 2017/2019 Litote
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.litote.kmongo.issue
18+
19+
import org.litote.kmongo.Data
20+
import java.math.BigDecimal
21+
import java.time.LocalDate
22+
import java.time.Month
23+
import java.time.Year
24+
25+
/**
26+
*
27+
*/
28+
interface RecordCollection1 {
29+
val _id : Long
30+
val year : Year
31+
val month : Month
32+
val records : List<Record1>
33+
}
34+
35+
interface Record1 {
36+
val date : LocalDate
37+
val type : String
38+
val hoursSubmitted : BigDecimal
39+
val desc : String?
40+
val report : RecordCollection1
41+
}
42+
43+
interface Employee1 {
44+
val _id : Long
45+
val records : List<RecordCollection1>
46+
}
47+
48+
@Data
49+
data class RecordCollectionImpl1(
50+
override val _id : Long,
51+
override val year : Year,
52+
override val month : Month
53+
) : RecordCollection1 {
54+
override lateinit var records: List<Record1>
55+
}
56+
57+
@Data
58+
data class EmployeeAggregate1(override val _id: Long): Employee1 {
59+
override lateinit var records : List<RecordCollection1>
60+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package org.litote.kmongo.issue
2+
3+
import kotlin.Long
4+
import kotlin.String
5+
import kotlin.Suppress
6+
import kotlin.collections.Collection
7+
import kotlin.collections.List
8+
import kotlin.collections.Map
9+
import kotlin.reflect.KProperty1
10+
import org.litote.kmongo.property.KCollectionPropertyPath
11+
import org.litote.kmongo.property.KCollectionSimplePropertyPath
12+
import org.litote.kmongo.property.KMapPropertyPath
13+
import org.litote.kmongo.property.KPropertyPath
14+
15+
private val __Records: KProperty1<EmployeeAggregate1, List<out RecordCollection1?>?>
16+
get() = EmployeeAggregate1::records
17+
private val ___id: KProperty1<EmployeeAggregate1, Long?>
18+
get() = EmployeeAggregate1::_id
19+
class EmployeeAggregate1_<T>(previous: KPropertyPath<T, *>?, property: KProperty1<*,
20+
EmployeeAggregate1?>) : KPropertyPath<T, EmployeeAggregate1?>(previous,property) {
21+
val records: KCollectionSimplePropertyPath<T, out RecordCollection1??>
22+
get() = KCollectionSimplePropertyPath(this,EmployeeAggregate1::records)
23+
24+
val _id: KPropertyPath<T, Long?>
25+
get() = KPropertyPath(this,___id)
26+
27+
companion object {
28+
val Records: KCollectionSimplePropertyPath<EmployeeAggregate1, out RecordCollection1??>
29+
get() = KCollectionSimplePropertyPath(null, __Records)
30+
val _id: KProperty1<EmployeeAggregate1, Long?>
31+
get() = ___id}
32+
}
33+
34+
class EmployeeAggregate1_Col<T>(previous: KPropertyPath<T, *>?, property: KProperty1<*,
35+
Collection<EmployeeAggregate1>?>) : KCollectionPropertyPath<T, EmployeeAggregate1?,
36+
EmployeeAggregate1_<T>>(previous,property) {
37+
val records: KCollectionSimplePropertyPath<T, out RecordCollection1??>
38+
get() = KCollectionSimplePropertyPath(this,EmployeeAggregate1::records)
39+
40+
val _id: KPropertyPath<T, Long?>
41+
get() = KPropertyPath(this,___id)
42+
43+
@Suppress("UNCHECKED_CAST")
44+
override fun memberWithAdditionalPath(additionalPath: String): EmployeeAggregate1_<T> =
45+
EmployeeAggregate1_(this, customProperty(this, additionalPath))}
46+
47+
class EmployeeAggregate1_Map<T, K>(previous: KPropertyPath<T, *>?, property: KProperty1<*, Map<K,
48+
EmployeeAggregate1>?>) : KMapPropertyPath<T, K, EmployeeAggregate1?,
49+
EmployeeAggregate1_<T>>(previous,property) {
50+
val records: KCollectionSimplePropertyPath<T, out RecordCollection1??>
51+
get() = KCollectionSimplePropertyPath(this,EmployeeAggregate1::records)
52+
53+
val _id: KPropertyPath<T, Long?>
54+
get() = KPropertyPath(this,___id)
55+
56+
@Suppress("UNCHECKED_CAST")
57+
override fun memberWithAdditionalPath(additionalPath: String): EmployeeAggregate1_<T> =
58+
EmployeeAggregate1_(this, customProperty(this, additionalPath))}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package org.litote.kmongo.issue
2+
3+
import kotlin.String
4+
import kotlin.Suppress
5+
import kotlin.collections.Collection
6+
import kotlin.collections.Map
7+
import kotlin.reflect.KProperty1
8+
import org.litote.kmongo.property.KCollectionPropertyPath
9+
import org.litote.kmongo.property.KMapPropertyPath
10+
import org.litote.kmongo.property.KPropertyPath
11+
12+
open class Issue130ParentGeneric_<T>(previous: KPropertyPath<T, *>?, property: KProperty1<*,
13+
Issue130ParentGeneric<*>?>) : KPropertyPath<T, Issue130ParentGeneric<*>?>(previous,property)
14+
{
15+
companion object
16+
}
17+
18+
open class Issue130ParentGeneric_Col<T>(previous: KPropertyPath<T, *>?, property: KProperty1<*,
19+
Collection<Issue130ParentGeneric<*>>?>) : KCollectionPropertyPath<T,
20+
Issue130ParentGeneric<*>?, Issue130ParentGeneric_<T>>(previous,property) {
21+
@Suppress("UNCHECKED_CAST")
22+
override fun memberWithAdditionalPath(additionalPath: String): Issue130ParentGeneric_<T> =
23+
Issue130ParentGeneric_(this, customProperty(this, additionalPath))}
24+
25+
open class Issue130ParentGeneric_Map<T, K>(previous: KPropertyPath<T, *>?, property: KProperty1<*,
26+
Map<K, Issue130ParentGeneric<*>>?>) : KMapPropertyPath<T, K, Issue130ParentGeneric<*>?,
27+
Issue130ParentGeneric_<T>>(previous,property) {
28+
@Suppress("UNCHECKED_CAST")
29+
override fun memberWithAdditionalPath(additionalPath: String): Issue130ParentGeneric_<T> =
30+
Issue130ParentGeneric_(this, customProperty(this, additionalPath))}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package org.litote.kmongo.issue
2+
3+
import java.time.Month
4+
import java.time.Year
5+
import kotlin.Long
6+
import kotlin.String
7+
import kotlin.Suppress
8+
import kotlin.collections.Collection
9+
import kotlin.collections.List
10+
import kotlin.collections.Map
11+
import kotlin.reflect.KProperty1
12+
import org.litote.kmongo.property.KCollectionPropertyPath
13+
import org.litote.kmongo.property.KCollectionSimplePropertyPath
14+
import org.litote.kmongo.property.KMapPropertyPath
15+
import org.litote.kmongo.property.KPropertyPath
16+
17+
private val __Records: KProperty1<RecordCollectionImpl1, List<out Record1?>?>
18+
get() = RecordCollectionImpl1::records
19+
private val ___id: KProperty1<RecordCollectionImpl1, Long?>
20+
get() = RecordCollectionImpl1::_id
21+
private val __Year: KProperty1<RecordCollectionImpl1, Year?>
22+
get() = RecordCollectionImpl1::year
23+
private val __Month: KProperty1<RecordCollectionImpl1, Month?>
24+
get() = RecordCollectionImpl1::month
25+
class RecordCollectionImpl1_<T>(previous: KPropertyPath<T, *>?, property: KProperty1<*,
26+
RecordCollectionImpl1?>) : KPropertyPath<T, RecordCollectionImpl1?>(previous,property) {
27+
val records: KCollectionSimplePropertyPath<T, out Record1??>
28+
get() = KCollectionSimplePropertyPath(this,RecordCollectionImpl1::records)
29+
30+
val _id: KPropertyPath<T, Long?>
31+
get() = KPropertyPath(this,___id)
32+
33+
val year: KPropertyPath<T, Year?>
34+
get() = KPropertyPath(this,__Year)
35+
36+
val month: KPropertyPath<T, Month?>
37+
get() = KPropertyPath(this,__Month)
38+
39+
companion object {
40+
val Records: KCollectionSimplePropertyPath<RecordCollectionImpl1, out Record1??>
41+
get() = KCollectionSimplePropertyPath(null, __Records)
42+
val _id: KProperty1<RecordCollectionImpl1, Long?>
43+
get() = ___id
44+
val Year: KProperty1<RecordCollectionImpl1, Year?>
45+
get() = __Year
46+
val Month: KProperty1<RecordCollectionImpl1, Month?>
47+
get() = __Month}
48+
}
49+
50+
class RecordCollectionImpl1_Col<T>(previous: KPropertyPath<T, *>?, property: KProperty1<*,
51+
Collection<RecordCollectionImpl1>?>) : KCollectionPropertyPath<T, RecordCollectionImpl1?,
52+
RecordCollectionImpl1_<T>>(previous,property) {
53+
val records: KCollectionSimplePropertyPath<T, out Record1??>
54+
get() = KCollectionSimplePropertyPath(this,RecordCollectionImpl1::records)
55+
56+
val _id: KPropertyPath<T, Long?>
57+
get() = KPropertyPath(this,___id)
58+
59+
val year: KPropertyPath<T, Year?>
60+
get() = KPropertyPath(this,__Year)
61+
62+
val month: KPropertyPath<T, Month?>
63+
get() = KPropertyPath(this,__Month)
64+
65+
@Suppress("UNCHECKED_CAST")
66+
override fun memberWithAdditionalPath(additionalPath: String): RecordCollectionImpl1_<T> =
67+
RecordCollectionImpl1_(this, customProperty(this, additionalPath))}
68+
69+
class RecordCollectionImpl1_Map<T, K>(previous: KPropertyPath<T, *>?, property: KProperty1<*, Map<K,
70+
RecordCollectionImpl1>?>) : KMapPropertyPath<T, K, RecordCollectionImpl1?,
71+
RecordCollectionImpl1_<T>>(previous,property) {
72+
val records: KCollectionSimplePropertyPath<T, out Record1??>
73+
get() = KCollectionSimplePropertyPath(this,RecordCollectionImpl1::records)
74+
75+
val _id: KPropertyPath<T, Long?>
76+
get() = KPropertyPath(this,___id)
77+
78+
val year: KPropertyPath<T, Year?>
79+
get() = KPropertyPath(this,__Year)
80+
81+
val month: KPropertyPath<T, Month?>
82+
get() = KPropertyPath(this,__Month)
83+
84+
@Suppress("UNCHECKED_CAST")
85+
override fun memberWithAdditionalPath(additionalPath: String): RecordCollectionImpl1_<T> =
86+
RecordCollectionImpl1_(this, customProperty(this, additionalPath))}

kmongo-annotation-processor/target/generated-sources/kapt/test/org/litote/kmongo/model/InternalDataClass_.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ private val __L: KProperty1<InternalDataClass, Long?>
1515
internal class InternalDataClass_<T>(previous: KPropertyPath<T, *>?, property: KProperty1<*,
1616
InternalDataClass?>) : KPropertyPath<T, InternalDataClass?>(previous,property) {
1717
val l: KPropertyPath<T, Long?>
18-
get() = KPropertyPath<T, Long?>(this,__L)
18+
get() = KPropertyPath(this,__L)
1919

2020
companion object {
2121
val L: KProperty1<InternalDataClass, Long?>
@@ -26,7 +26,7 @@ internal class InternalDataClass_Col<T>(previous: KPropertyPath<T, *>?, property
2626
Collection<InternalDataClass>?>) : KCollectionPropertyPath<T, InternalDataClass?,
2727
InternalDataClass_<T>>(previous,property) {
2828
val l: KPropertyPath<T, Long?>
29-
get() = KPropertyPath<T, Long?>(this,__L)
29+
get() = KPropertyPath(this,__L)
3030

3131
@Suppress("UNCHECKED_CAST")
3232
override fun memberWithAdditionalPath(additionalPath: String): InternalDataClass_<T> =
@@ -36,7 +36,7 @@ internal class InternalDataClass_Map<T, K>(previous: KPropertyPath<T, *>?, prope
3636
Map<K, InternalDataClass>?>) : KMapPropertyPath<T, K, InternalDataClass?,
3737
InternalDataClass_<T>>(previous,property) {
3838
val l: KPropertyPath<T, Long?>
39-
get() = KPropertyPath<T, Long?>(this,__L)
39+
get() = KPropertyPath(this,__L)
4040

4141
@Suppress("UNCHECKED_CAST")
4242
override fun memberWithAdditionalPath(additionalPath: String): InternalDataClass_<T> =

kmongo-annotation-processor/target/generated-sources/kapt/test/org/litote/kmongo/model/NotAnnotatedData_.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ private val __Test: KProperty1<NotAnnotatedData, String?>
1414
open class NotAnnotatedData_<T>(previous: KPropertyPath<T, *>?, property: KProperty1<*,
1515
NotAnnotatedData?>) : KPropertyPath<T, NotAnnotatedData?>(previous,property) {
1616
val test: KPropertyPath<T, String?>
17-
get() = KPropertyPath<T, String?>(this,__Test)
17+
get() = KPropertyPath(this,__Test)
1818

1919
companion object {
2020
val Test: KProperty1<NotAnnotatedData, String?>
@@ -25,7 +25,7 @@ open class NotAnnotatedData_Col<T>(previous: KPropertyPath<T, *>?, property: KPr
2525
Collection<NotAnnotatedData>?>) : KCollectionPropertyPath<T, NotAnnotatedData?,
2626
NotAnnotatedData_<T>>(previous,property) {
2727
val test: KPropertyPath<T, String?>
28-
get() = KPropertyPath<T, String?>(this,__Test)
28+
get() = KPropertyPath(this,__Test)
2929

3030
@Suppress("UNCHECKED_CAST")
3131
override fun memberWithAdditionalPath(additionalPath: String): NotAnnotatedData_<T> =
@@ -35,7 +35,7 @@ open class NotAnnotatedData_Map<T, K>(previous: KPropertyPath<T, *>?, property:
3535
NotAnnotatedData>?>) : KMapPropertyPath<T, K, NotAnnotatedData?,
3636
NotAnnotatedData_<T>>(previous,property) {
3737
val test: KPropertyPath<T, String?>
38-
get() = KPropertyPath<T, String?>(this,__Test)
38+
get() = KPropertyPath(this,__Test)
3939

4040
@Suppress("UNCHECKED_CAST")
4141
override fun memberWithAdditionalPath(additionalPath: String): NotAnnotatedData_<T> =

0 commit comments

Comments
 (0)