Skip to content

Commit 868a1e2

Browse files
committed
Implement factory parameter type checks and update validation messages
1 parent f349fdf commit 868a1e2

18 files changed

+214
-32
lines changed
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@ 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 ValidationMethodParamEntityResult(
24+
class ValidationMethodParamTypeResult(
2525
override val identify: PsiElement?,
2626
override val shortName: String = "",
27+
private val typeName: String,
2728
) : ValidationResult(identify, null, shortName) {
2829
override fun setHighlight(
2930
highlightRange: TextRange,
@@ -34,7 +35,7 @@ class ValidationMethodParamEntityResult(
3435
val project = identify.project
3536
holder.registerProblem(
3637
identify,
37-
MessageBundle.message("inspection.invalid.dao.params.entity"),
38+
MessageBundle.message("inspection.invalid.dao.params.type", typeName),
3839
problemHighlightType(project, shortName),
3940
highlightRange,
4041
)

src/main/kotlin/org/domaframework/doma/intellij/extension/psi/DomaAnnotationType.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ enum class DomaAnnotationType(
3838
Insert("org.seasar.doma.Insert"),
3939
Update("org.seasar.doma.Update"),
4040
Delete("org.seasar.doma.Delete"),
41+
ArrayFactory("org.seasar.doma.ArrayFactory"),
42+
BlobFactory("org.seasar.doma.BlobFactory"),
43+
ClobFactory("org.seasar.doma.ClobFactory"),
44+
NClobFactory("org.seasar.doma.NClobFactory"),
45+
SQLXMLFactory("org.seasar.doma.SQLXMLFactory"),
4146
Sql("org.seasar.doma.Sql"),
4247
Unknown("Unknown"),
4348
;
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
enum class FactoryAnnotationType(
19+
val fqdn: String,
20+
val returnType: String,
21+
val paramCount: Int = 0,
22+
) {
23+
ArrayFactory("org.seasar.doma.ArrayFactory", "java.sql.Array", 1),
24+
BlobFactory("org.seasar.doma.BlobFactory", "java.sql.Blob"),
25+
ClobFactory("org.seasar.doma.ClobFactory", "java.sql.Clob"),
26+
NClobFactory("org.seasar.doma.NClobFactory", "java.sql.NClob"),
27+
SQLXMLFactory("org.seasar.doma.SQLXMLFactory", "java.sql.SQLXML"),
28+
;
29+
30+
fun matchFactoryAnnotation(fqName: String): Boolean = this.fqdn == fqName
31+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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.paramtype
17+
18+
import com.intellij.codeInspection.ProblemsHolder
19+
import com.intellij.psi.PsiArrayType
20+
import org.domaframework.doma.intellij.common.psi.PsiDaoMethod
21+
import org.domaframework.doma.intellij.common.validation.result.ValidationMethodParamTypeResult
22+
import org.domaframework.doma.intellij.common.validation.result.ValidationMethodParamsCountResult
23+
import org.domaframework.doma.intellij.common.validation.result.ValidationMethodParamsEmptyResult
24+
import org.domaframework.doma.intellij.inspection.dao.processor.FactoryAnnotationType
25+
26+
class FactoryParamTypeCheckProcessor(
27+
private val psiDaoMethod: PsiDaoMethod,
28+
private val shortName: String,
29+
) : ParamTypeCheckProcessor(psiDaoMethod, shortName) {
30+
override fun checkParams(holder: ProblemsHolder) {
31+
val daoType = psiDaoMethod.daoType
32+
val factoryAnnotationType =
33+
FactoryAnnotationType.entries.find { annotation ->
34+
annotation.matchFactoryAnnotation(daoType.fqdn)
35+
} ?: return
36+
37+
val params = method.parameterList.parameters
38+
if (factoryAnnotationType.paramCount != params.size) {
39+
val paramCountResult =
40+
when (factoryAnnotationType) {
41+
FactoryAnnotationType.ArrayFactory ->
42+
ValidationMethodParamsCountResult(
43+
method.nameIdentifier,
44+
shortName,
45+
)
46+
47+
else ->
48+
ValidationMethodParamsEmptyResult(
49+
method.nameIdentifier,
50+
shortName,
51+
)
52+
}
53+
paramCountResult.highlightElement(holder)
54+
return
55+
}
56+
57+
if (factoryAnnotationType == FactoryAnnotationType.ArrayFactory) {
58+
val arrayParam = params.firstOrNull() ?: return
59+
if (arrayParam.type !is PsiArrayType) {
60+
ValidationMethodParamTypeResult(
61+
arrayParam.nameIdentifier ?: return,
62+
shortName,
63+
"an array type",
64+
).highlightElement(holder)
65+
}
66+
}
67+
}
68+
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ package org.domaframework.doma.intellij.inspection.dao.processor.paramtype
1717

1818
import com.intellij.codeInspection.ProblemsHolder
1919
import org.domaframework.doma.intellij.common.psi.PsiDaoMethod
20-
import org.domaframework.doma.intellij.common.validation.result.ValidationMethodParamEntityResult
20+
import org.domaframework.doma.intellij.common.validation.result.ValidationMethodParamTypeResult
2121
import org.domaframework.doma.intellij.extension.getJavaClazz
2222
import org.domaframework.doma.intellij.extension.psi.isEntity
2323

@@ -61,9 +61,10 @@ class UpdateParamTypeCheckProcessor(
6161
val paramClass = project.getJavaClazz(param?.type?.canonicalText ?: "")
6262
val identifier = param?.nameIdentifier ?: return
6363
if (paramClass == null || !paramClass.isEntity()) {
64-
ValidationMethodParamEntityResult(
64+
ValidationMethodParamTypeResult(
6565
identifier,
6666
shortName,
67+
"an entity",
6768
).highlightElement(holder)
6869
}
6970
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import org.domaframework.doma.intellij.extension.getJavaClazz
2929
* @property psiDaoMethod The target DAO method info to be checked.
3030
* @property shortName The short name of inspection to check.
3131
*/
32-
class BatchAnnotationReturnTypeCheckProcessor(
32+
class BatchReturnTypeCheckProcessor(
3333
private val psiDaoMethod: PsiDaoMethod,
3434
private val shortName: String,
3535
) : ReturnTypeCheckerProcessor(psiDaoMethod, shortName) {
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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.returntype
17+
18+
import org.domaframework.doma.intellij.common.psi.PsiDaoMethod
19+
import org.domaframework.doma.intellij.common.validation.result.ValidationResult
20+
import org.domaframework.doma.intellij.common.validation.result.ValidationReturnTypeResult
21+
import org.domaframework.doma.intellij.inspection.dao.processor.FactoryAnnotationType
22+
23+
/**
24+
* Processor for checking the return type of SqlProcessor annotation.
25+
*
26+
* @param psiDaoMethod The target DAO method info to be checked.
27+
* @param shortName The short name of the inspection to check.
28+
*/
29+
class FactoryReturnTypeCheckProcessor(
30+
private val psiDaoMethod: PsiDaoMethod,
31+
private val shortName: String,
32+
) : ReturnTypeCheckerProcessor(psiDaoMethod, shortName) {
33+
override fun checkReturnType(): ValidationResult? {
34+
val daoType = psiDaoMethod.daoType
35+
val factoryAnnotationType =
36+
FactoryAnnotationType.entries.find { annotation ->
37+
annotation.matchFactoryAnnotation(daoType.fqdn)
38+
} ?: return null
39+
40+
val returnTypeResult =
41+
ValidationReturnTypeResult(
42+
method.nameIdentifier,
43+
shortName,
44+
factoryAnnotationType.returnType,
45+
)
46+
47+
val returnType = method.returnType
48+
return if (returnType?.canonicalText != factoryAnnotationType.returnType) {
49+
returnTypeResult
50+
} else {
51+
null
52+
}
53+
}
54+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import org.domaframework.doma.intellij.extension.getJavaClazz
2727
import org.domaframework.doma.intellij.extension.psi.isDomain
2828
import org.domaframework.doma.intellij.extension.psi.isEntity
2929

30-
class FunctionAnnotationReturnTypeCheckProcessor(
30+
class FunctionReturnTypeCheckProcessor(
3131
psiDaoMethod: PsiDaoMethod,
3232
private val shortName: String,
3333
) : ReturnTypeCheckerProcessor(psiDaoMethod, shortName) {
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import org.domaframework.doma.intellij.extension.psi.DomaAnnotationType
3131
* @property psiDaoMethod The target DAO method info to be checked.
3232
* @property shortName The short name of the inspection to check.
3333
*/
34-
class MultiInsertAnnotationReturnTypeCheckProcessor(
34+
class MultiInsertReturnTypeCheckProcessor(
3535
private val psiDaoMethod: PsiDaoMethod,
3636
private val shortName: String,
3737
) : ReturnTypeCheckerProcessor(psiDaoMethod, shortName) {
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import com.intellij.psi.PsiTypes
1919
import org.domaframework.doma.intellij.common.psi.PsiDaoMethod
2020
import org.domaframework.doma.intellij.common.validation.result.ValidationResult
2121

22-
class ScriptAnnotationReturnTypeCheckProcessor(
22+
class ProcedureReturnTypeCheckProcessor(
2323
psiDaoMethod: PsiDaoMethod,
2424
shortName: String,
2525
) : ReturnTypeCheckerProcessor(psiDaoMethod, shortName) {

0 commit comments

Comments
 (0)