Skip to content
Merged
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 @@ -21,6 +21,7 @@ import com.intellij.psi.impl.source.PsiClassReferenceType
import org.domaframework.doma.intellij.common.psi.PsiDaoMethod
import org.domaframework.doma.intellij.common.sql.PsiClassTypeUtil
import org.domaframework.doma.intellij.common.validation.result.ValidationResult
import org.domaframework.doma.intellij.extension.getJavaClazz

/**
* Processor for checking the return type of batch annotations.
Expand All @@ -39,11 +40,6 @@ class BatchAnnotationReturnTypeCheckProcessor(
*/
override fun checkReturnType(): ValidationResult? {
val methodOtherReturnType = PsiTypes.intType().createArrayType()
if (psiDaoMethod.useSqlAnnotation() || psiDaoMethod.sqlFileOption) {
return generatePsiTypeReturnTypeResult(methodOtherReturnType)
}

// Check if it has an immutable entity parameter
val parameters = method.parameterList.parameters
val immutableEntityParam = parameters.firstOrNull() ?: return null

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

if (psiDaoMethod.useSqlAnnotation() || psiDaoMethod.sqlFileOption) {
nestClass.let { methodParam ->
val paramTypeName = methodParam.canonicalText
project.getJavaClazz(paramTypeName)?.let {
if (isImmutableEntity(paramTypeName)) {
return checkReturnTypeImmutableEntity(nestClass)
}
}
}
return generatePsiTypeReturnTypeResult(methodOtherReturnType)
}

// Check if it has an immutable entity parameter
if (isImmutableEntity(nestClass.canonicalText)) {
return checkReturnTypeImmutableEntity(nestClass)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,17 @@
*/
package org.domaframework.doma.intellij.inspection.dao.processor.returntype

import com.intellij.psi.PsiClass
import com.intellij.psi.PsiParameter
import com.intellij.psi.PsiTypes
import org.domaframework.doma.intellij.common.psi.PsiDaoMethod
import org.domaframework.doma.intellij.common.sql.PsiClassTypeUtil
import org.domaframework.doma.intellij.common.util.DomaClassName
import org.domaframework.doma.intellij.common.validation.result.ValidationResult
import org.domaframework.doma.intellij.common.validation.result.ValidationReturnTypeUpdateReturningResult
import org.domaframework.doma.intellij.extension.getJavaClazz
import org.domaframework.doma.intellij.extension.psi.isEntity
import org.domaframework.doma.intellij.extension.psi.psiClassType

/**
* Processor for checking the return type of update-related annotations in DAO methods.
Expand All @@ -41,14 +44,23 @@ class UpdateAnnotationReturnTypeCheckProcessor(
*/
override fun checkReturnType(): ValidationResult? {
val methodOtherReturnType = PsiTypes.intType()
val parameters = method.parameterList.parameters
val immutableEntityParam =
parameters.firstOrNull()
?: return generatePsiTypeReturnTypeResult(methodOtherReturnType)

if (psiDaoMethod.useSqlAnnotation() || psiDaoMethod.sqlFileOption) {
immutableEntityParam.let { methodParam ->
val paramTypeName = methodParam.type.canonicalText
project.getJavaClazz(paramTypeName)?.let {
if (isImmutableEntity(paramTypeName)) {
return checkReturnTypeImmutableEntity(immutableEntityParam)
}
}
}
return generatePsiTypeReturnTypeResult(methodOtherReturnType)
}

val parameters = method.parameterList.parameters
val immutableEntityParam =
parameters.firstOrNull() ?: return null

// Check if the method is annotated with @Returning
if (hasReturingOption()) {
return checkReturnTypeWithReturning(immutableEntityParam)
Expand Down Expand Up @@ -82,7 +94,7 @@ class UpdateAnnotationReturnTypeCheckProcessor(
PsiClassTypeUtil.convertOptionalType(returnType, project)
val returnTypeClass = project.getJavaClazz(checkReturnType.canonicalText)

return if (returnTypeClass?.isEntity() != true || returnType.canonicalText != paramClass.type.canonicalText) {
return if (!validateReturnType(returnTypeClass, paramClass)) {
ValidationReturnTypeUpdateReturningResult(
paramClass.type.presentableText,
method.nameIdentifier,
Expand All @@ -93,6 +105,23 @@ class UpdateAnnotationReturnTypeCheckProcessor(
}
}

private fun validateReturnType(
returnTypeClass: PsiClass?,
paramClass: PsiParameter,
): Boolean {
if (returnTypeClass?.isEntity() != true) return false

if (DomaClassName.OPTIONAL.isTargetClassNameStartsWith(returnTypeClass.psiClassType.canonicalText)) {
val optionalType = returnTypeClass.psiClassType
val optionalParam =
optionalType.parameters.firstOrNull()
?: return false
return optionalParam.canonicalText == paramClass.type.canonicalText
}

return returnTypeClass.psiClassType.canonicalText == paramClass.type.canonicalText
}

/**
* Checks the return type when an immutable entity parameter is present.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ public interface BatchReturnTypeTestDao {
@BatchUpdate
String <error descr="The return type must be \"int[]\"">batchUpdateReturnsString</error>(List<Packet> e);

@BatchUpdate(sqlFile = true)
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);

@BatchDelete
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);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package doma.example.dao.inspection.returntype;

import java.util.Optional;
import org.seasar.doma.*;
import org.seasar.doma.jdbc.Result;
import doma.example.entity.*;
Expand All @@ -16,12 +17,18 @@ public interface UpdateReturnTypeTestDao {
@Delete(returning = @Returning)
Packet deleteReturningEntity(Packet e);

@Delete(returning = @Returning)
Optional<Packet> deleteReturningOptionalEntity(Packet e);

@Delete(returning = @Returning)
int <error descr="When \"returning = @Returning\" is specified, the return type must be \"Packet\" or Optional<Packet>">deleteReturningInt</error>(Packet e);

@Update
Result<Pckt> updateReturnsResultWithImmutable(Pckt e);

@Update(sqlFile = true)
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);

@Update
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);
}
Expand Down
Loading