Skip to content

Commit 7ef54f7

Browse files
committed
Fix return type checks for batch/update with Immutable Entity
1 parent 09168cc commit 7ef54f7

File tree

4 files changed

+59
-10
lines changed

4 files changed

+59
-10
lines changed

src/main/kotlin/org/domaframework/doma/intellij/inspection/dao/processor/returntype/BatchAnnotationReturnTypeCheckProcessor.kt

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import com.intellij.psi.impl.source.PsiClassReferenceType
2121
import org.domaframework.doma.intellij.common.psi.PsiDaoMethod
2222
import org.domaframework.doma.intellij.common.sql.PsiClassTypeUtil
2323
import org.domaframework.doma.intellij.common.validation.result.ValidationResult
24+
import org.domaframework.doma.intellij.extension.getJavaClazz
2425

2526
/**
2627
* Processor for checking the return type of batch annotations.
@@ -39,11 +40,6 @@ class BatchAnnotationReturnTypeCheckProcessor(
3940
*/
4041
override fun checkReturnType(): ValidationResult? {
4142
val methodOtherReturnType = PsiTypes.intType().createArrayType()
42-
if (psiDaoMethod.useSqlAnnotation() || psiDaoMethod.sqlFileOption) {
43-
return generatePsiTypeReturnTypeResult(methodOtherReturnType)
44-
}
45-
46-
// Check if it has an immutable entity parameter
4743
val parameters = method.parameterList.parameters
4844
val immutableEntityParam = parameters.firstOrNull() ?: return null
4945

@@ -52,6 +48,19 @@ class BatchAnnotationReturnTypeCheckProcessor(
5248
val nestPsiType = parameterType.reference.typeParameters.firstOrNull() ?: return null
5349
val nestClass: PsiType = PsiClassTypeUtil.convertOptionalType(nestPsiType, project)
5450

51+
if (psiDaoMethod.useSqlAnnotation() || psiDaoMethod.sqlFileOption) {
52+
nestClass.let { methodParam ->
53+
val paramTypeName = methodParam.canonicalText
54+
project.getJavaClazz(paramTypeName)?.let {
55+
if (isImmutableEntity(paramTypeName)) {
56+
return checkReturnTypeImmutableEntity(nestClass)
57+
}
58+
}
59+
}
60+
return generatePsiTypeReturnTypeResult(methodOtherReturnType)
61+
}
62+
63+
// Check if it has an immutable entity parameter
5564
if (isImmutableEntity(nestClass.canonicalText)) {
5665
return checkReturnTypeImmutableEntity(nestClass)
5766
}

src/main/kotlin/org/domaframework/doma/intellij/inspection/dao/processor/returntype/UpdateAnnotationReturnTypeCheckProcessor.kt

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,18 @@
1515
*/
1616
package org.domaframework.doma.intellij.inspection.dao.processor.returntype
1717

18+
import com.intellij.psi.PsiClass
19+
import com.intellij.psi.PsiClassType
1820
import com.intellij.psi.PsiParameter
1921
import com.intellij.psi.PsiTypes
2022
import org.domaframework.doma.intellij.common.psi.PsiDaoMethod
2123
import org.domaframework.doma.intellij.common.sql.PsiClassTypeUtil
24+
import org.domaframework.doma.intellij.common.util.DomaClassName
2225
import org.domaframework.doma.intellij.common.validation.result.ValidationResult
2326
import org.domaframework.doma.intellij.common.validation.result.ValidationReturnTypeUpdateReturningResult
2427
import org.domaframework.doma.intellij.extension.getJavaClazz
2528
import org.domaframework.doma.intellij.extension.psi.isEntity
29+
import org.domaframework.doma.intellij.extension.psi.psiClassType
2630

2731
/**
2832
* Processor for checking the return type of update-related annotations in DAO methods.
@@ -41,14 +45,23 @@ class UpdateAnnotationReturnTypeCheckProcessor(
4145
*/
4246
override fun checkReturnType(): ValidationResult? {
4347
val methodOtherReturnType = PsiTypes.intType()
48+
val parameters = method.parameterList.parameters
49+
val immutableEntityParam =
50+
parameters.firstOrNull()
51+
?: return generatePsiTypeReturnTypeResult(methodOtherReturnType)
52+
4453
if (psiDaoMethod.useSqlAnnotation() || psiDaoMethod.sqlFileOption) {
54+
immutableEntityParam.let { methodParam ->
55+
val paramTypeName = methodParam.type.canonicalText
56+
project.getJavaClazz(paramTypeName)?.let {
57+
if (isImmutableEntity(paramTypeName)) {
58+
return checkReturnTypeImmutableEntity(immutableEntityParam)
59+
}
60+
}
61+
}
4562
return generatePsiTypeReturnTypeResult(methodOtherReturnType)
4663
}
4764

48-
val parameters = method.parameterList.parameters
49-
val immutableEntityParam =
50-
parameters.firstOrNull() ?: return null
51-
5265
// Check if the method is annotated with @Returning
5366
if (hasReturingOption()) {
5467
return checkReturnTypeWithReturning(immutableEntityParam)
@@ -82,7 +95,7 @@ class UpdateAnnotationReturnTypeCheckProcessor(
8295
PsiClassTypeUtil.convertOptionalType(returnType, project)
8396
val returnTypeClass = project.getJavaClazz(checkReturnType.canonicalText)
8497

85-
return if (returnTypeClass?.isEntity() != true || returnType.canonicalText != paramClass.type.canonicalText) {
98+
return if (!validateReturnType(returnTypeClass, paramClass)) {
8699
ValidationReturnTypeUpdateReturningResult(
87100
paramClass.type.presentableText,
88101
method.nameIdentifier,
@@ -93,6 +106,23 @@ class UpdateAnnotationReturnTypeCheckProcessor(
93106
}
94107
}
95108

109+
private fun validateReturnType(
110+
returnTypeClass: PsiClass?,
111+
paramClass: PsiParameter,
112+
): Boolean {
113+
if (returnTypeClass?.isEntity() != true) return false
114+
115+
if (DomaClassName.OPTIONAL.isTargetClassNameStartsWith(paramClass.type.canonicalText)) {
116+
val optionalType = paramClass.type as? PsiClassType
117+
val optionalParam =
118+
optionalType?.parameters?.firstOrNull()
119+
?: return false
120+
return optionalParam.canonicalText == paramClass.type.canonicalText
121+
}
122+
123+
return returnTypeClass.psiClassType.canonicalText == paramClass.type.canonicalText
124+
}
125+
96126
/**
97127
* Checks the return type when an immutable entity parameter is present.
98128
*

src/test/testData/src/main/java/doma/example/dao/inspection/returntype/BatchReturnTypeTestDao.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ public interface BatchReturnTypeTestDao {
1313
@BatchUpdate
1414
String <error descr="The return type must be \"int[]\"">batchUpdateReturnsString</error>(List<Packet> e);
1515

16+
@BatchUpdate(sqlFile = true)
17+
int[] <error descr="If a method annotated with @BatchUpdate targets immutable entities for insertion, the return type must be BatchResult<Pckt>">batchUpdateImmutableReturnsIntArray</error>(List<Pckt> e);
18+
1619
@BatchDelete
1720
int[] <error descr="If a method annotated with @BatchDelete targets immutable entities for insertion, the return type must be BatchResult<Pckt>">batchDeleteReturnsIntWithImmutable</error>(List<Pckt> e);
1821

src/test/testData/src/main/java/doma/example/dao/inspection/returntype/UpdateReturnTypeTestDao.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package doma.example.dao.inspection.returntype;
22

3+
import java.util.Optional;
34
import org.seasar.doma.*;
45
import org.seasar.doma.jdbc.Result;
56
import doma.example.entity.*;
@@ -16,12 +17,18 @@ public interface UpdateReturnTypeTestDao {
1617
@Delete(returning = @Returning)
1718
Packet deleteReturningEntity(Packet e);
1819

20+
@Delete(returning = @Returning)
21+
Optional<Packet> deleteReturningOptionalEntity(Packet e);
22+
1923
@Delete(returning = @Returning)
2024
int <error descr="When \"returning = @Returning\" is specified, the return type must be \"Packet\" or Optional<Packet>">deleteReturningInt</error>(Packet e);
2125

2226
@Update
2327
Result<Pckt> updateReturnsResultWithImmutable(Pckt e);
2428

29+
@Update(sqlFile = true)
30+
int <error descr="If a method annotated with @Update targets immutable entities for insertion, the return type must be Result<Pckt>">deleteImmutableInt</error>(Pckt e);
31+
2532
@Update
2633
int <error descr="If a method annotated with @Update targets immutable entities for insertion, the return type must be Result<Pckt>">updateReturnsIntWithImmutable</error>(Pckt e);
2734
}

0 commit comments

Comments
 (0)