Skip to content

Commit 342d3f2

Browse files
authored
Merge pull request #230 from domaframework/chore/split-inspection
Refactor: Split inspection logic
2 parents 7728471 + 97dfe8d commit 342d3f2

28 files changed

+584
-240
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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.inspector
17+
18+
import com.intellij.codeHighlighting.HighlightDisplayLevel
19+
import com.intellij.codeInspection.AbstractBaseJavaLocalInspectionTool
20+
import com.intellij.codeInspection.ProblemsHolder
21+
import com.intellij.psi.PsiElementVisitor
22+
import org.domaframework.doma.intellij.inspection.dao.visitor.DaoMethodVariableInspectionVisitor
23+
24+
/**
25+
* Check if DAO method arguments are used in the corresponding SQL file
26+
*/
27+
class DaoMethodVariableInspection : AbstractBaseJavaLocalInspectionTool() {
28+
override fun getDisplayName(): String = "Check usage of DAO method arguments in corresponding SQL file."
29+
30+
override fun getShortName(): String = "org.domaframework.doma.intellij.variablechecker"
31+
32+
override fun getGroupDisplayName(): String = "DomaTools"
33+
34+
override fun isEnabledByDefault(): Boolean = true
35+
36+
override fun getDefaultLevel(): HighlightDisplayLevel = HighlightDisplayLevel.ERROR
37+
38+
override fun buildVisitor(
39+
holder: ProblemsHolder,
40+
isOnTheFly: Boolean,
41+
): PsiElementVisitor = DaoMethodVariableInspectionVisitor(holder)
42+
}

src/main/kotlin/org/domaframework/doma/intellij/inspection/dao/inspector/DaoMethodVariableInspector.kt

Lines changed: 0 additions & 157 deletions
This file was deleted.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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.inspector
17+
18+
import com.intellij.codeHighlighting.HighlightDisplayLevel
19+
import com.intellij.codeInspection.AbstractBaseJavaLocalInspectionTool
20+
import com.intellij.codeInspection.ProblemsHolder
21+
import com.intellij.psi.PsiElementVisitor
22+
import org.domaframework.doma.intellij.inspection.dao.visitor.SqlFileExistInspectionVisitor
23+
24+
/**
25+
* Check for existence of SQL file
26+
*/
27+
class SqlFileExistInspection : AbstractBaseJavaLocalInspectionTool() {
28+
override fun getDisplayName(): String = "Ensure the existence of SQL files for DAO methods."
29+
30+
override fun getShortName(): String = "org.domaframework.doma.intellij.existsqlchecker"
31+
32+
override fun getGroupDisplayName(): String = "DomaTools"
33+
34+
override fun isEnabledByDefault(): Boolean = true
35+
36+
override fun getDefaultLevel(): HighlightDisplayLevel = HighlightDisplayLevel.ERROR
37+
38+
override fun buildVisitor(
39+
holder: ProblemsHolder,
40+
isOnTheFly: Boolean,
41+
): PsiElementVisitor = SqlFileExistInspectionVisitor(holder)
42+
}

src/main/kotlin/org/domaframework/doma/intellij/inspection/dao/provider/DaoMethodVariableProvider.kt

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

1818
import com.intellij.codeInspection.InspectionToolProvider
1919
import com.intellij.codeInspection.LocalInspectionTool
20-
import org.domaframework.doma.intellij.inspection.dao.inspector.DaoMethodVariableInspector
20+
import org.domaframework.doma.intellij.inspection.dao.inspector.DaoMethodVariableInspection
2121

2222
class DaoMethodVariableProvider : InspectionToolProvider {
2323
override fun getInspectionClasses(): Array<Class<out LocalInspectionTool>> =
2424
arrayOf(
25-
DaoMethodVariableInspector::class.java,
25+
DaoMethodVariableInspection::class.java,
2626
)
2727
}

src/main/kotlin/org/domaframework/doma/intellij/inspection/dao/provider/SqlFileExistProvider.kt

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

1818
import com.intellij.codeInspection.InspectionToolProvider
1919
import com.intellij.codeInspection.LocalInspectionTool
20-
import org.domaframework.doma.intellij.inspection.dao.inspector.SqlFileExistInspector
20+
import org.domaframework.doma.intellij.inspection.dao.inspector.SqlFileExistInspection
2121

2222
class SqlFileExistProvider : InspectionToolProvider {
2323
override fun getInspectionClasses(): Array<Class<out LocalInspectionTool>> =
2424
arrayOf(
25-
SqlFileExistInspector::class.java,
25+
SqlFileExistInspection::class.java,
2626
)
2727
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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.visitor
17+
18+
import com.intellij.codeInspection.ProblemHighlightType
19+
import com.intellij.codeInspection.ProblemsHolder
20+
import com.intellij.psi.JavaElementVisitor
21+
import com.intellij.psi.PsiFile
22+
import com.intellij.psi.PsiMethod
23+
import com.intellij.psi.PsiParameter
24+
import com.intellij.psi.impl.source.PsiParameterImpl
25+
import org.domaframework.doma.intellij.bundle.MessageBundle
26+
import org.domaframework.doma.intellij.common.dao.getDaoClass
27+
import org.domaframework.doma.intellij.common.isJavaOrKotlinFileType
28+
import org.domaframework.doma.intellij.common.psi.PsiDaoMethod
29+
import org.domaframework.doma.intellij.extension.findFile
30+
import org.domaframework.doma.intellij.extension.psi.isCollector
31+
import org.domaframework.doma.intellij.extension.psi.isFunctionClazz
32+
import org.domaframework.doma.intellij.extension.psi.isSelectOption
33+
import org.domaframework.doma.intellij.extension.psi.methodParameters
34+
35+
class DaoMethodVariableInspectionVisitor(
36+
private val holder: ProblemsHolder,
37+
) : JavaElementVisitor() {
38+
override fun visitMethod(method: PsiMethod) {
39+
super.visitMethod(method)
40+
val file = method.containingFile
41+
if (!isJavaOrKotlinFileType(file) || getDaoClass(file) == null) return
42+
43+
val psiDaoMethod = PsiDaoMethod(method.project, method)
44+
if (!psiDaoMethod.useSqlAnnotation() && !psiDaoMethod.isUseSqlFileMethod()) return
45+
46+
val methodParameters =
47+
method.methodParameters
48+
.filter { !it.isFunctionClazz && !it.isSelectOption && !(it.isCollector && psiDaoMethod.isSelectTypeCollect()) }
49+
val sqlFileManager =
50+
psiDaoMethod.sqlFile?.let {
51+
method.project.findFile(it)
52+
} ?: return
53+
54+
val params = methodParameters.toList()
55+
val result = findElementsInSqlFile(sqlFileManager, params)
56+
params.forEach { param ->
57+
if (!result.elements.contains(param)) {
58+
val message =
59+
if (result.deplicateForItemElements.contains(param)) {
60+
MessageBundle.message("inspection.invalid.dao.duplicate")
61+
} else {
62+
MessageBundle.message(
63+
"inspection.invalid.dao.paramUse",
64+
param.name,
65+
)
66+
}
67+
holder.registerProblem(
68+
(param.originalElement as PsiParameterImpl).nameIdentifier,
69+
message,
70+
ProblemHighlightType.ERROR,
71+
)
72+
}
73+
}
74+
}
75+
76+
private data class DaoMethodVariableVisitorResult(
77+
val elements: List<PsiParameter>,
78+
val deplicateForItemElements: List<PsiParameter>,
79+
)
80+
81+
private fun findElementsInSqlFile(
82+
sqlFile: PsiFile,
83+
args: List<PsiParameter>,
84+
): DaoMethodVariableVisitorResult {
85+
val elements = mutableListOf<PsiParameter>()
86+
val deplicateForItemElements = mutableListOf<PsiParameter>()
87+
88+
sqlFile.accept(DaoMethodVariableSqlVisitor(args, elements, deplicateForItemElements))
89+
val result = DaoMethodVariableVisitorResult(elements, deplicateForItemElements)
90+
return result
91+
}
92+
}

0 commit comments

Comments
 (0)