Skip to content

Commit 35142f9

Browse files
authored
Merge pull request #58 from domaframework/fix/check-selecttype-collect
Fix: Exclude java.stream.Collector type arguments from code inspection
2 parents b2e9d68 + 032ba9b commit 35142f9

File tree

8 files changed

+58
-1
lines changed

8 files changed

+58
-1
lines changed

src/main/kotlin/org/domaframework/doma/intellij/common/psi/PsiDaoMethod.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@ class PsiDaoMethod(
9292

9393
private fun getSqlAnnotation(): PsiAnnotation? = DomaAnnotationType.Sql.getPsiAnnotation(psiMethod)
9494

95+
fun isSelectTypeCollect(): Boolean {
96+
val selectAnnotation = DomaAnnotationType.Select.getPsiAnnotation(psiMethod) ?: return false
97+
return daoType.isSelectTypeCollect(selectAnnotation)
98+
}
99+
95100
fun useSqlAnnotation(): Boolean = getSqlAnnotation() != null
96101

97102
private fun setSqlFilePath() {

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ package org.domaframework.doma.intellij.extension.psi
1818
import com.intellij.codeInsight.AnnotationUtil
1919
import com.intellij.psi.PsiAnnotation
2020
import com.intellij.psi.PsiModifierListOwner
21+
import com.intellij.psi.PsiNameValuePair
22+
import com.intellij.psi.PsiReferenceExpression
23+
import com.intellij.psi.util.PsiTreeUtil
24+
import kotlin.jvm.java
2125

2226
enum class DomaAnnotationType(
2327
val fqdn: String,
@@ -59,4 +63,19 @@ enum class DomaAnnotationType(
5963
true -> AnnotationUtil.getBooleanAttributeValue(element, "sqlFile") == true
6064
false -> false
6165
}
66+
67+
fun isSelectTypeCollect(element: PsiAnnotation): Boolean {
68+
val strategyPair =
69+
PsiTreeUtil
70+
.getChildrenOfTypeAsList(element.parameterList, PsiNameValuePair::class.java)
71+
.firstOrNull {
72+
it.text.startsWith("strategy")
73+
} ?: return false
74+
75+
return PsiTreeUtil
76+
.getChildOfType(
77+
strategyPair,
78+
PsiReferenceExpression::class.java,
79+
)?.text == "SelectType.COLLECT"
80+
}
6281
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,9 @@ val PsiParameter.isSelectOption: Boolean
4848
(this.typeElement?.type as? PsiClassReferenceType)
4949
?.resolve()
5050
?.qualifiedName == "org.seasar.doma.jdbc.SelectOptions"
51+
52+
val PsiParameter.isCollector: Boolean
53+
get() =
54+
(this.typeElement?.type as? PsiClassReferenceType)
55+
?.resolve()
56+
?.qualifiedName == "java.util.stream.Collector"

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import org.domaframework.doma.intellij.common.dao.getDaoClass
3232
import org.domaframework.doma.intellij.common.isJavaOrKotlinFileType
3333
import org.domaframework.doma.intellij.common.psi.PsiDaoMethod
3434
import org.domaframework.doma.intellij.extension.findFile
35+
import org.domaframework.doma.intellij.extension.psi.isCollector
3536
import org.domaframework.doma.intellij.extension.psi.isFunctionClazz
3637
import org.domaframework.doma.intellij.extension.psi.isSelectOption
3738
import org.domaframework.doma.intellij.extension.psi.methodParameters
@@ -66,7 +67,7 @@ class DaoMethodVariableInspector : AbstractBaseJavaLocalInspectionTool() {
6667

6768
val methodParameters =
6869
method.methodParameters
69-
.filter { !it.isFunctionClazz && !it.isSelectOption }
70+
.filter { !it.isFunctionClazz && !it.isSelectOption && !(it.isCollector && psiDaoMethod.isSelectTypeCollect()) }
7071
val sqlFileManager =
7172
psiDaoMethod.sqlFile?.let {
7273
method.project.findFile(it)

src/test/kotlin/org/domaframework/doma/intellij/inspection/dao/DomaUseVariableTest.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ class DomaUseVariableTest : DomaSqlTest() {
3232
addSqlFile(
3333
"$testDaoName/biFunctionDoesNotCauseError.sql",
3434
"$testDaoName/selectOptionDoesNotCauseError.sql",
35+
"$testDaoName/collectDoesNotCauseError.sql",
36+
"$testDaoName/collectDoesCauseError.sql",
3537
)
3638
myFixture.enableInspections(DaoMethodVariableInspector())
3739
}

src/test/testData/src/main/java/doma/example/dao/DaoMethodVariableInspectionTestDao.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
import org.seasar.doma.jdbc.Config;
1717
import org.seasar.doma.jdbc.PreparedSql;
1818
import org.seasar.doma.jdbc.SelectOptions;
19+
import org.seasar.doma.SelectType;
20+
import java.util.stream.Collector;
1921

2022
import java.util.List;
2123
import java.util.function.BiFunction;
@@ -39,4 +41,11 @@ interface DaoMethodVariableInspectionTestDao {
3941
@Select
4042
Project selectOptionDoesNotCauseError(Employee <error descr="There are unused parameters in the SQL [employee]">employee</error>,String searchName,SelectOptions options);
4143

44+
@Select(strategy = SelectType.COLLECT)
45+
Project collectDoesNotCauseError(Employee <error descr="There are unused parameters in the SQL [employee]">employee</error>,Integer id,Collector<Project, ?, Project> collector);
46+
47+
@Select
48+
Project collectDoesCauseError(Employee <error descr="There are unused parameters in the SQL [employee]">employee</error>,String searchName,Collector<Project, ?, Project> <error descr="There are unused parameters in the SQL [collector]">collector</error>);
49+
50+
4251
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
-- Using Dao arguments other than SelectOption as bind variables
2+
select
3+
p.project_id
4+
, p.project_name
5+
, p.project_number
6+
from project p
7+
where p.project_id = /* id */0
8+
and p. project_name = /* searchName */'search'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
-- Using Dao arguments other than SelectOption as bind variables
2+
select
3+
p.project_id
4+
, p.project_name
5+
, p.project_number
6+
from project p
7+
where p.project_id = /* id */0

0 commit comments

Comments
 (0)