Skip to content

Commit c2737c0

Browse files
committed
Refactor DAO method type checks to utilize superclass type retrieval for improved clarity and maintainability
1 parent 7bd4492 commit c2737c0

File tree

4 files changed

+66
-76
lines changed

4 files changed

+66
-76
lines changed

src/main/kotlin/org/domaframework/doma/intellij/inspection/dao/processor/paramtype/SelectParamTypeCheckProcessor.kt

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import org.domaframework.doma.intellij.common.validation.result.ValidationMethod
2626
import org.domaframework.doma.intellij.common.validation.result.ValidationMethodParamsSupportGenericParamResult
2727
import org.domaframework.doma.intellij.common.validation.result.ValidationMethodSelectStrategyParamResult
2828
import org.domaframework.doma.intellij.extension.getJavaClazz
29+
import org.domaframework.doma.intellij.extension.psi.getSuperClassType
2930
import org.domaframework.doma.intellij.extension.psi.isDomain
3031
import org.domaframework.doma.intellij.extension.psi.isEntity
3132
import org.domaframework.doma.intellij.inspection.dao.processor.StrategyParam
@@ -99,19 +100,11 @@ class SelectParamTypeCheckProcessor(
99100
return
100101
}
101102

102-
val functionType = function.type
103103
val identifier = function.nameIdentifier ?: return
104104

105105
// Check if the first parameter of the function is a stream type
106-
val functionClass = project.getJavaClazz(functionType.canonicalText) ?: return
107-
var superCollection: PsiClassType? = functionType as PsiClassType?
108-
while (superCollection != null &&
109-
!DomaClassName.JAVA_FUNCTION.isTargetClassNameStartsWith(superCollection.canonicalText)
110-
) {
111-
superCollection =
112-
functionClass.superTypes
113-
.find { sp -> DomaClassName.JAVA_FUNCTION.isTargetClassNameStartsWith(sp.canonicalText) }
114-
}
106+
val targetType = DomaClassName.JAVA_FUNCTION
107+
var superCollection: PsiClassType? = function.getSuperClassType(targetType)
115108

116109
val functionFirstParam = superCollection?.parameters?.firstOrNull()
117110
if (functionFirstParam == null ||
@@ -157,19 +150,10 @@ class SelectParamTypeCheckProcessor(
157150
return
158151
}
159152

160-
val collectionType = collection.type
161153
val identifier = collection.nameIdentifier ?: return
162154

163-
val collectionClass = project.getJavaClazz(collectionType.canonicalText) ?: return
164-
var superCollection: PsiClassType? = collection.type as? PsiClassType
165-
while (superCollection != null &&
166-
!DomaClassName.JAVA_COLLECTOR.isTargetClassNameStartsWith(superCollection.canonicalText)
167-
) {
168-
superCollection =
169-
collectionClass.superTypes
170-
.find { sp -> DomaClassName.JAVA_COLLECTOR.isTargetClassNameStartsWith(sp.canonicalText) }
171-
}
172-
155+
val targetType = DomaClassName.JAVA_COLLECTOR
156+
var superCollection: PsiClassType? = collection.getSuperClassType(targetType)
173157
val collectorTargetParam = superCollection?.parameters?.firstOrNull()
174158
if (collectorTargetParam == null) {
175159
generateTargetTypeResult(identifier, "Unknown", collector).highlightElement(holder)

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

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import org.domaframework.doma.intellij.common.validation.result.ValidationReturn
2626
import org.domaframework.doma.intellij.extension.getJavaClazz
2727
import org.domaframework.doma.intellij.extension.psi.DomaAnnotationType
2828
import org.domaframework.doma.intellij.extension.psi.getClassAnnotation
29+
import org.domaframework.doma.intellij.extension.psi.getSuperClassType
2930
import org.domaframework.doma.intellij.inspection.dao.processor.TypeCheckerProcessor
3031

3132
abstract class ReturnTypeCheckerProcessor(
@@ -39,7 +40,8 @@ abstract class ReturnTypeCheckerProcessor(
3940

4041
protected fun isImmutableEntity(canonicalText: String): Boolean {
4142
val returnTypeClass = method.project.getJavaClazz(canonicalText)
42-
val entity = returnTypeClass?.getClassAnnotation(DomaClassName.ENTITY.className) ?: return false
43+
val entity =
44+
returnTypeClass?.getClassAnnotation(DomaClassName.ENTITY.className) ?: return false
4345
return entity.let { entity ->
4446
AnnotationUtil.getBooleanAttributeValue(entity, "immutable") == true
4547
} == true ||
@@ -84,4 +86,33 @@ abstract class ReturnTypeCheckerProcessor(
8486
null
8587
}
8688
}
89+
90+
/**
91+
* Retrieve parameters with a specified type from the method parameters, and obtain the element type at the specified index.
92+
* @param targetType The target type to check against the method parameters.
93+
* @param resultIndex The index of the element type to retrieve from the target type's parameters.
94+
* @return [ValidationResult] if the parameter type is invalid, otherwise null.
95+
*/
96+
protected open fun checkParamTypeResult(
97+
targetType: DomaClassName,
98+
resultIndex: Int,
99+
): ValidationResult? = null
100+
101+
/**
102+
* Retrieve the method parameter type that matches the target type.
103+
* @param targetType The target type to check against the method parameters.
104+
* @param resultIndex The index of the element type to retrieve from the target type's parameters.
105+
* @return The method parameter type if found, otherwise null.
106+
*/
107+
protected fun getMethodParamTargetArgByIndex(
108+
targetType: DomaClassName,
109+
resultIndex: Int,
110+
): PsiType? {
111+
val methodParameter = getMethodParamTargetType(targetType.className) ?: return null
112+
val targetParamClassType = methodParameter.getSuperClassType(targetType)
113+
val targetClassTypeParams = targetParamClassType?.parameters ?: return null
114+
if (targetClassTypeParams.size < resultIndex + 1) return null
115+
116+
return targetClassTypeParams[resultIndex]
117+
}
87118
}

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

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -113,35 +113,28 @@ class SelectReturnTypeCheckProcessor(
113113
}
114114

115115
private fun checkStream(): ValidationResult? {
116-
val function =
117-
getMethodParamTargetType(DomaClassName.JAVA_FUNCTION.className) ?: return null
118-
119-
val functionClassType = (function.type as? PsiClassType)
120-
val functionParams = functionClassType?.parameters ?: return null
121-
if (functionParams.size < 2) return null
122-
val functionResultParam = functionParams[1] ?: return null
123-
124-
if (functionResultParam == method.returnType) return null
125-
return ValidationMethodSelectStrategyReturnTypeResult(
126-
method.nameIdentifier,
127-
shortName,
128-
DomaClassName.JAVA_FUNCTION.className,
129-
)
116+
val targetType = DomaClassName.JAVA_FUNCTION
117+
return checkParamTypeResult(targetType, 1)
130118
}
131119

132120
private fun checkCollect(): ValidationResult? {
133-
val collection = getMethodParamTargetType(DomaClassName.JAVA_COLLECTOR.className) ?: return null
134-
val collectorParamClassType = (collection.type as? PsiClassType)
135-
val collectorParams = collectorParamClassType?.parameters ?: return null
136-
if (collectorParams.size < 3) return null
121+
val targetType = DomaClassName.JAVA_COLLECTOR
122+
return checkParamTypeResult(targetType, 2)
123+
}
137124

138-
val collectorTargetParam = collectorParams[2]
139-
if (collectorTargetParam == method.returnType) return null
125+
override fun checkParamTypeResult(
126+
targetType: DomaClassName,
127+
resultIndex: Int,
128+
): ValidationResult? {
129+
val resultParam =
130+
getMethodParamTargetArgByIndex(targetType, resultIndex)
131+
?: return null
132+
if (resultParam == method.returnType) return null
140133

141134
return ValidationMethodSelectStrategyReturnTypeResult(
142135
method.nameIdentifier,
143136
shortName,
144-
DomaClassName.JAVA_COLLECTOR.className,
137+
targetType.className,
145138
)
146139
}
147140
}

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

Lines changed: 15 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ package org.domaframework.doma.intellij.inspection.dao.processor.returntype
1717

1818
import com.intellij.psi.PsiType
1919
import com.intellij.psi.PsiTypes
20-
import com.intellij.psi.impl.source.PsiClassReferenceType
2120
import org.domaframework.doma.intellij.common.psi.PsiDaoMethod
2221
import org.domaframework.doma.intellij.common.sql.PsiClassTypeUtil
2322
import org.domaframework.doma.intellij.common.util.DomaClassName
@@ -34,59 +33,42 @@ class SqlProcessorReturnTypeCheckProcessor(
3433
psiDaoMethod: PsiDaoMethod,
3534
private val shortName: String,
3635
) : ReturnTypeCheckerProcessor(psiDaoMethod, shortName) {
37-
private val biFunctionClassName = DomaClassName.BI_FUNCTION.className
36+
private val biFunctionClass = DomaClassName.BI_FUNCTION
3837

3938
/**
4039
* Checks the return type of the DAO method.
4140
*
4241
* @return [ValidationResult] if the return type is invalid, otherwise null.
4342
*/
44-
override fun checkReturnType(): ValidationResult? {
45-
val parameters = method.parameterList.parameters
46-
val biFunctionParam =
47-
parameters.firstOrNull { param ->
48-
param.type.canonicalText.startsWith(biFunctionClassName)
49-
} ?: return null
50-
val convertOptional = PsiClassTypeUtil.convertOptionalType(biFunctionParam.type, project)
51-
val parameterType = convertOptional as PsiClassReferenceType
52-
return checkReturnTypeBiFunctionParam(parameterType)
53-
}
43+
override fun checkReturnType(): ValidationResult? = checkParamTypeResult(biFunctionClass, 2)
5444

55-
/**
56-
* Checks the return type when a BiFunction parameter is present.
57-
*
58-
* @param parameterType The BiFunction parameter type to check.
59-
* @return [ValidationResult] if the return type is invalid, otherwise null.
60-
*/
61-
private fun checkReturnTypeBiFunctionParam(parameterType: PsiClassReferenceType): ValidationResult? {
62-
if (!parameterType.canonicalText.startsWith(biFunctionClassName)) return null
45+
override fun checkParamTypeResult(
46+
targetType: DomaClassName,
47+
resultIndex: Int,
48+
): ValidationResult? {
49+
val resultParam =
50+
getMethodParamTargetArgByIndex(targetType, resultIndex)
51+
?: return null
52+
val optionalNestClass: PsiType = PsiClassTypeUtil.convertOptionalType(resultParam, project)
6353

64-
val parameterTypeParams = parameterType.reference.typeParameters
65-
if (parameterTypeParams.size < 3) return null
66-
67-
val nestPsiType = parameterType.reference.typeParameters[2]
68-
if (nestPsiType == null) return null
69-
70-
val nestClass: PsiType = PsiClassTypeUtil.convertOptionalType(nestPsiType, project)
71-
72-
if (nestClass.canonicalText != DomaClassName.VOID.className) {
54+
if (optionalNestClass.canonicalText != DomaClassName.VOID.className) {
7355
val methodReturnType = method.returnType
7456
val returnTypeCheckResult =
75-
(nestClass.canonicalText == "?" && methodReturnType?.canonicalText == "R") ||
76-
methodReturnType?.canonicalText == nestClass.canonicalText
57+
(optionalNestClass.canonicalText == "?" && methodReturnType?.canonicalText == "R") ||
58+
methodReturnType?.canonicalText == optionalNestClass.canonicalText
7759

7860
return if (!returnTypeCheckResult) {
7961
ValidationSqlProcessorReturnResult(
8062
returnType?.canonicalText ?: "void",
81-
nestClass.canonicalText,
63+
optionalNestClass.canonicalText,
8264
method.nameIdentifier,
8365
shortName,
8466
)
8567
} else {
8668
null
8769
}
8870
}
89-
// If the return type is not void, return a validation result
71+
9072
val methodOtherReturnType = PsiTypes.voidType()
9173
return generatePsiTypeReturnTypeResult(methodOtherReturnType)
9274
}

0 commit comments

Comments
 (0)