Skip to content

Commit f349fdf

Browse files
authored
Merge pull request #270 from domaframework/fix/update-annotation-return-typpe-check
Fix Return Type Check
2 parents 4f9aa57 + edcd651 commit f349fdf

File tree

4 files changed

+58
-10
lines changed

4 files changed

+58
-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: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,17 @@
1515
*/
1616
package org.domaframework.doma.intellij.inspection.dao.processor.returntype
1717

18+
import com.intellij.psi.PsiClass
1819
import com.intellij.psi.PsiParameter
1920
import com.intellij.psi.PsiTypes
2021
import org.domaframework.doma.intellij.common.psi.PsiDaoMethod
2122
import org.domaframework.doma.intellij.common.sql.PsiClassTypeUtil
23+
import org.domaframework.doma.intellij.common.util.DomaClassName
2224
import org.domaframework.doma.intellij.common.validation.result.ValidationResult
2325
import org.domaframework.doma.intellij.common.validation.result.ValidationReturnTypeUpdateReturningResult
2426
import org.domaframework.doma.intellij.extension.getJavaClazz
2527
import org.domaframework.doma.intellij.extension.psi.isEntity
28+
import org.domaframework.doma.intellij.extension.psi.psiClassType
2629

2730
/**
2831
* Processor for checking the return type of update-related annotations in DAO methods.
@@ -41,14 +44,23 @@ class UpdateAnnotationReturnTypeCheckProcessor(
4144
*/
4245
override fun checkReturnType(): ValidationResult? {
4346
val methodOtherReturnType = PsiTypes.intType()
47+
val parameters = method.parameterList.parameters
48+
val immutableEntityParam =
49+
parameters.firstOrNull()
50+
?: return generatePsiTypeReturnTypeResult(methodOtherReturnType)
51+
4452
if (psiDaoMethod.useSqlAnnotation() || psiDaoMethod.sqlFileOption) {
53+
immutableEntityParam.let { methodParam ->
54+
val paramTypeName = methodParam.type.canonicalText
55+
project.getJavaClazz(paramTypeName)?.let {
56+
if (isImmutableEntity(paramTypeName)) {
57+
return checkReturnTypeImmutableEntity(immutableEntityParam)
58+
}
59+
}
60+
}
4561
return generatePsiTypeReturnTypeResult(methodOtherReturnType)
4662
}
4763

48-
val parameters = method.parameterList.parameters
49-
val immutableEntityParam =
50-
parameters.firstOrNull() ?: return null
51-
5264
// Check if the method is annotated with @Returning
5365
if (hasReturingOption()) {
5466
return checkReturnTypeWithReturning(immutableEntityParam)
@@ -82,7 +94,7 @@ class UpdateAnnotationReturnTypeCheckProcessor(
8294
PsiClassTypeUtil.convertOptionalType(returnType, project)
8395
val returnTypeClass = project.getJavaClazz(checkReturnType.canonicalText)
8496

85-
return if (returnTypeClass?.isEntity() != true || returnType.canonicalText != paramClass.type.canonicalText) {
97+
return if (!validateReturnType(returnTypeClass, paramClass)) {
8698
ValidationReturnTypeUpdateReturningResult(
8799
paramClass.type.presentableText,
88100
method.nameIdentifier,
@@ -93,6 +105,23 @@ class UpdateAnnotationReturnTypeCheckProcessor(
93105
}
94106
}
95107

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

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)