Skip to content

Commit 09168cc

Browse files
authored
Merge pull request #266 from domaframework/feature/dao-method-inspection-return-type-select
Implement Return Type Checking for Select and Function Annotations
2 parents d3e3c0f + 3649cfb commit 09168cc

29 files changed

+809
-101
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright Doma Tools Authors
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+
* https://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+
package org.domaframework.doma.intellij.common.validation.result
17+
18+
import com.intellij.codeInspection.ProblemsHolder
19+
import com.intellij.openapi.util.TextRange
20+
import com.intellij.psi.PsiElement
21+
import org.domaframework.doma.intellij.bundle.MessageBundle
22+
import org.domaframework.doma.intellij.common.psi.PsiParentClass
23+
24+
class ValidationMethodInvalidReturnTypeResult(
25+
override val identify: PsiElement?,
26+
override val shortName: String = "",
27+
private val returnTypeName: String,
28+
) : ValidationResult(identify, null, shortName) {
29+
override fun setHighlight(
30+
highlightRange: TextRange,
31+
identify: PsiElement,
32+
holder: ProblemsHolder,
33+
parent: PsiParentClass?,
34+
) {
35+
val project = identify.project
36+
holder.registerProblem(
37+
identify,
38+
MessageBundle.message("inspection.invalid.dao.returnType.invalid", returnTypeName),
39+
problemHighlightType(project, shortName),
40+
highlightRange,
41+
)
42+
}
43+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import com.intellij.psi.PsiElement
2121
import org.domaframework.doma.intellij.bundle.MessageBundle
2222
import org.domaframework.doma.intellij.common.psi.PsiParentClass
2323

24-
class ValidationMethodProcedureParamsSupportGenericParamResult(
24+
class ValidationMethodParamsSupportGenericParamResult(
2525
override val identify: PsiElement?,
2626
override val shortName: String = "",
2727
private val paramTypeName: String,

src/main/kotlin/org/domaframework/doma/intellij/common/validation/result/ValidationMethodSelectStrategyParamResult.kt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ import org.domaframework.doma.intellij.common.psi.PsiParentClass
2424
class ValidationMethodSelectStrategyParamResult(
2525
override val identify: PsiElement?,
2626
override val shortName: String = "",
27-
private val selectTypeName: String,
28-
private val requireClassName: String,
27+
private val genericTypeName: String,
28+
private val parentParamTypeName: String,
2929
) : ValidationResult(identify, null, shortName) {
3030
override fun setHighlight(
3131
highlightRange: TextRange,
@@ -36,7 +36,11 @@ class ValidationMethodSelectStrategyParamResult(
3636
val project = identify.project
3737
holder.registerProblem(
3838
identify,
39-
MessageBundle.message("inspection.invalid.dao.select.param.strategy.require.type", selectTypeName, requireClassName),
39+
MessageBundle.message(
40+
"inspection.invalid.dao.select.param.strategy.require.type",
41+
genericTypeName,
42+
parentParamTypeName,
43+
),
4044
problemHighlightType(project, shortName),
4145
highlightRange,
4246
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright Doma Tools Authors
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+
* https://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+
package org.domaframework.doma.intellij.common.validation.result
17+
18+
import com.intellij.codeInspection.ProblemsHolder
19+
import com.intellij.openapi.util.TextRange
20+
import com.intellij.psi.PsiElement
21+
import org.domaframework.doma.intellij.bundle.MessageBundle
22+
import org.domaframework.doma.intellij.common.psi.PsiParentClass
23+
24+
class ValidationMethodSelectStrategyReturnTypeResult(
25+
override val identify: PsiElement?,
26+
override val shortName: String = "",
27+
private val resultType: String,
28+
) : ValidationResult(identify, null, shortName) {
29+
override fun setHighlight(
30+
highlightRange: TextRange,
31+
identify: PsiElement,
32+
holder: ProblemsHolder,
33+
parent: PsiParentClass?,
34+
) {
35+
val project = identify.project
36+
holder.registerProblem(
37+
identify,
38+
MessageBundle.message(
39+
"inspection.invalid.dao.select.returnType.strategy",
40+
resultType,
41+
),
42+
problemHighlightType(project, shortName),
43+
highlightRange,
44+
)
45+
}
46+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright Doma Tools Authors
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+
* https://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+
package org.domaframework.doma.intellij.inspection.dao.processor
17+
18+
import org.domaframework.doma.intellij.common.util.DomaClassName
19+
20+
class StrategyParam(
21+
val fieldName: String = "",
22+
parentClassName: String?,
23+
) {
24+
private val isSelectType: Boolean = parentClassName == DomaClassName.SELECT_TYPE.className
25+
26+
fun isStream(): Boolean = fieldName == "STREAM" && isSelectType
27+
28+
fun isCollect(): Boolean = fieldName == "COLLECT" && isSelectType
29+
}

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@ package org.domaframework.doma.intellij.inspection.dao.processor
1818
import com.intellij.psi.PsiAnnotation
1919
import com.intellij.psi.PsiClassType
2020
import com.intellij.psi.PsiField
21+
import com.intellij.psi.PsiParameter
2122
import com.intellij.psi.PsiReferenceExpression
2223
import com.intellij.psi.PsiType
2324
import org.domaframework.doma.intellij.common.psi.PsiDaoMethod
2425
import org.domaframework.doma.intellij.common.psi.PsiTypeChecker
2526
import org.domaframework.doma.intellij.common.util.DomaClassName
2627
import org.domaframework.doma.intellij.extension.getJavaClazz
28+
import org.domaframework.doma.intellij.extension.psi.getSuperType
2729
import org.domaframework.doma.intellij.extension.psi.isDomain
2830

2931
abstract class TypeCheckerProcessor(
@@ -34,6 +36,11 @@ abstract class TypeCheckerProcessor(
3436

3537
protected fun getAnnotation(fqName: String): PsiAnnotation? = method.annotations.find { it.qualifiedName == fqName }
3638

39+
protected fun getMethodParamTargetType(typeName: String): PsiParameter? =
40+
method.parameterList.parameters.find { param ->
41+
(param.type as? PsiClassType)?.getSuperType(typeName) != null
42+
}
43+
3744
protected fun getDaoAnnotationOption(
3845
psiAnnotation: PsiAnnotation,
3946
findOptionName: String,
@@ -90,6 +97,6 @@ abstract class TypeCheckerProcessor(
9097
DomaClassName.STRING.className,
9198
DomaClassName.OBJECT.className,
9299
).replace(" ", "")
93-
return mapClassName != mapExpectedType
100+
return mapClassName == mapExpectedType
94101
}
95102
}

src/main/kotlin/org/domaframework/doma/intellij/inspection/dao/processor/cheker/ProcedureFunctionInOutParamAnnotationTypeChecker.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ import com.intellij.psi.PsiElement
2222
import com.intellij.psi.PsiType
2323
import org.domaframework.doma.intellij.common.psi.PsiDaoMethod
2424
import org.domaframework.doma.intellij.common.util.DomaClassName
25+
import org.domaframework.doma.intellij.common.validation.result.ValidationMethodParamsSupportGenericParamResult
2526
import org.domaframework.doma.intellij.common.validation.result.ValidationMethodProcedureParamTypeResult
26-
import org.domaframework.doma.intellij.common.validation.result.ValidationMethodProcedureParamsSupportGenericParamResult
2727

2828
class ProcedureFunctionInOutParamAnnotationTypeChecker(
2929
private val annotationType: ProcedureFunctionParamAnnotationType,
@@ -49,7 +49,7 @@ class ProcedureFunctionInOutParamAnnotationTypeChecker(
4949
// Check if the parameter type is a valid reference type with generic parameters
5050
val referenceParamType = (paramType as? PsiClassType)?.parameters?.firstOrNull()
5151
if (referenceParamType == null) {
52-
ValidationMethodProcedureParamsSupportGenericParamResult(
52+
ValidationMethodParamsSupportGenericParamResult(
5353
identifier,
5454
shortName,
5555
"Unknown",
@@ -59,7 +59,7 @@ class ProcedureFunctionInOutParamAnnotationTypeChecker(
5959
}
6060

6161
if (checkParamType(referenceParamType)) return
62-
ValidationMethodProcedureParamsSupportGenericParamResult(
62+
ValidationMethodParamsSupportGenericParamResult(
6363
identifier,
6464
shortName,
6565
referenceParamType.canonicalText,

src/main/kotlin/org/domaframework/doma/intellij/inspection/dao/processor/cheker/ProcedureFunctionResultSetParamAnnotationTypeChecker.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ import com.intellij.psi.PsiType
2323
import org.domaframework.doma.intellij.common.psi.PsiDaoMethod
2424
import org.domaframework.doma.intellij.common.psi.PsiTypeChecker
2525
import org.domaframework.doma.intellij.common.util.DomaClassName
26+
import org.domaframework.doma.intellij.common.validation.result.ValidationMethodParamsSupportGenericParamResult
2627
import org.domaframework.doma.intellij.common.validation.result.ValidationMethodProcedureParamTypeResult
27-
import org.domaframework.doma.intellij.common.validation.result.ValidationMethodProcedureParamsSupportGenericParamResult
2828
import org.domaframework.doma.intellij.extension.getJavaClazz
2929
import org.domaframework.doma.intellij.extension.psi.isDomain
3030
import org.domaframework.doma.intellij.extension.psi.isEntity
@@ -77,7 +77,7 @@ class ProcedureFunctionResultSetParamAnnotationTypeChecker(
7777
// Check if the parameter type is a valid List type with generic parameters
7878
val listParamType = (paramType as? PsiClassType)?.parameters?.firstOrNull()
7979
if (listParamType == null) {
80-
ValidationMethodProcedureParamsSupportGenericParamResult(
80+
ValidationMethodParamsSupportGenericParamResult(
8181
identifier,
8282
shortName,
8383
"Unknown",
@@ -88,14 +88,14 @@ class ProcedureFunctionResultSetParamAnnotationTypeChecker(
8888

8989
val listCanonicalText = listParamType.canonicalText
9090
val result =
91-
ValidationMethodProcedureParamsSupportGenericParamResult(
91+
ValidationMethodParamsSupportGenericParamResult(
9292
identifier,
9393
shortName,
9494
listCanonicalText,
9595
annotationType.requireType,
9696
)
9797
if (DomaClassName.MAP.isTargetClassNameStartsWith(listCanonicalText)) {
98-
if (checkMapType(listCanonicalText)) result.highlightElement(holder)
98+
if (!checkMapType(listCanonicalText)) result.highlightElement(holder)
9999
return
100100
}
101101

0 commit comments

Comments
 (0)