Skip to content

Commit 7bd4492

Browse files
authored
Merge pull request #273 from domaframework/fix/usage-check-to-dao-method-param-sub-type
Enhance the usage checks of DAO method parameters for subtypes.
2 parents b7b4f98 + 7686c12 commit 7bd4492

File tree

12 files changed

+115
-48
lines changed

12 files changed

+115
-48
lines changed

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

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

104104
private fun getSqlAnnotation(): PsiAnnotation? = DomaAnnotationType.Sql.getPsiAnnotation(psiMethod)
105105

106-
fun isSelectTypeCollect(): Boolean {
107-
val selectAnnotation = DomaAnnotationType.Select.getPsiAnnotation(psiMethod) ?: return false
108-
return daoType.isSelectTypeCollect(selectAnnotation)
109-
}
110-
111106
fun useSqlAnnotation(): Boolean = getSqlAnnotation() != null
112107

113108
private fun setSqlFilePath() {

src/main/kotlin/org/domaframework/doma/intellij/common/util/DomaClassName.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ enum class DomaClassName(
3838
VOID("java.lang.Void"),
3939
RETURNING("org.seasar.doma.Returning"),
4040
REFERENCE("org.seasar.doma.jdbc.Reference"),
41+
SELECT_OPTIONS("org.seasar.doma.jdbc.SelectOptions"),
4142

4243
STRING("java.lang.String"),
4344
OBJECT("java.lang.Object"),

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

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@ 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
2421

2522
enum class DomaAnnotationType(
2623
val fqdn: String,
@@ -74,19 +71,4 @@ enum class DomaAnnotationType(
7471
} else {
7572
false
7673
}
77-
78-
fun isSelectTypeCollect(element: PsiAnnotation): Boolean {
79-
val strategyPair =
80-
PsiTreeUtil
81-
.getChildrenOfTypeAsList(element.parameterList, PsiNameValuePair::class.java)
82-
.firstOrNull {
83-
it.text.startsWith("strategy")
84-
} ?: return false
85-
86-
return PsiTreeUtil
87-
.getChildOfType(
88-
strategyPair,
89-
PsiReferenceExpression::class.java,
90-
)?.text == "SelectType.COLLECT"
91-
}
9274
}

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

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,29 @@ package org.domaframework.doma.intellij.extension.psi
1717

1818
import com.intellij.psi.PsiClassType
1919
import com.intellij.psi.PsiParameter
20+
import org.domaframework.doma.intellij.common.util.DomaClassName
2021

21-
val PsiParameter.isFunctionClazz: Boolean
22-
get() =
23-
(this.typeElement?.type as? PsiClassType)
24-
?.resolve()
25-
?.qualifiedName
26-
?.contains("java.util.function") == true
22+
private val ignoreUsageCheckType =
23+
listOf<DomaClassName>(
24+
DomaClassName.JAVA_FUNCTION,
25+
DomaClassName.BI_FUNCTION,
26+
DomaClassName.SELECT_OPTIONS,
27+
DomaClassName.JAVA_COLLECTOR,
28+
)
2729

28-
val PsiParameter.isSelectOption: Boolean
29-
get() =
30-
(this.typeElement?.type as? PsiClassType)
31-
?.resolve()
32-
?.qualifiedName == "org.seasar.doma.jdbc.SelectOptions"
30+
fun PsiParameter.isIgnoreUsageCheck(): Boolean =
31+
ignoreUsageCheckType.any { type ->
32+
getSuperClassType(type) != null
33+
}
3334

34-
val PsiParameter.isCollector: Boolean
35-
get() =
36-
(this.typeElement?.type as? PsiClassType)
37-
?.resolve()
38-
?.qualifiedName == "java.util.stream.Collector"
35+
fun PsiParameter.getSuperClassType(superClassType: DomaClassName): PsiClassType? {
36+
val clazzType = this.typeElement?.type as? PsiClassType
37+
var superCollection: PsiClassType? = clazzType
38+
while (superCollection != null &&
39+
!superClassType.isTargetClassNameStartsWith(superCollection.canonicalText)
40+
) {
41+
superCollection =
42+
superCollection.getSuperType(superClassType.className)
43+
}
44+
return superCollection
45+
}

src/main/kotlin/org/domaframework/doma/intellij/inspection/dao/visitor/UsedDaoMethodParamInspectionVisitor.kt

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,7 @@ import org.domaframework.doma.intellij.common.dao.getDaoClass
2727
import org.domaframework.doma.intellij.common.isJavaOrKotlinFileType
2828
import org.domaframework.doma.intellij.common.psi.PsiDaoMethod
2929
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
30+
import org.domaframework.doma.intellij.extension.psi.isIgnoreUsageCheck
3331
import org.domaframework.doma.intellij.extension.psi.methodParameters
3432

3533
class UsedDaoMethodParamInspectionVisitor(
@@ -44,8 +42,7 @@ class UsedDaoMethodParamInspectionVisitor(
4442
if (!psiDaoMethod.useSqlAnnotation() && !psiDaoMethod.isUseSqlFileMethod()) return
4543

4644
val methodParameters =
47-
method.methodParameters
48-
.filter { !it.isFunctionClazz && !it.isSelectOption && !(it.isCollector && psiDaoMethod.isSelectTypeCollect()) }
45+
method.methodParameters.filter { !it.isIgnoreUsageCheck() }
4946
val sqlFileManager =
5047
psiDaoMethod.sqlFile?.let {
5148
method.project.findFile(it)

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,14 @@ class DomaUseVariableTest : DomaSqlTest() {
3636
"$testDaoName/collectDoesCauseError.sql",
3737
"$testDaoName/noErrorWhenUsedInFunctionParameters.sql",
3838
"$testDaoName/duplicateForDirectiveDefinitionNames.sql",
39+
"$testDaoName/selectHogeFunction.sql",
40+
"$testDaoName/functionDoesNotCauseError.sql",
41+
"$testDaoName/selectHogeCollector.sql",
3942
)
43+
addOtherJavaFile("collector", "HogeCollector.java")
44+
addOtherJavaFile("function", "HogeFunction.java")
45+
addOtherJavaFile("function", "HogeBiFunction.java")
46+
addOtherJavaFile("option", "HogeSelectOptions.java")
4047
myFixture.enableInspections(UsedDaoMethodParamInspection())
4148
}
4249

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

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@
1717
import org.seasar.doma.jdbc.PreparedSql;
1818
import org.seasar.doma.jdbc.SelectOptions;
1919
import org.seasar.doma.SelectType;
20+
import java.util.stream.Stream;
21+
import java.util.function.Function;
2022
import java.util.stream.Collector;
23+
import doma.example.function.*;
24+
import doma.example.collector.*;
25+
import doma.example.option.*;
2126

2227
import java.util.List;
2328
import java.util.function.BiFunction;
@@ -38,20 +43,52 @@ interface DaoMethodVariableInspectionTestDao {
3843
@SqlProcessor
3944
<R> R biFunctionDoesNotCauseError(Integer id, BiFunction<Config, PreparedSql, R> handler);
4045

46+
@SqlProcessor
47+
@Sql("SELECT id, name FROM demo")
48+
<R> R biFunctionHogeFunction(HogeBiFunction handler);
49+
50+
@Select
51+
Project selectOptionDoesNotCauseError(Employee <error descr="There are unused parameters in the SQL [employee]">employee</error>,
52+
String searchName,
53+
SelectOptions options);
54+
4155
@Select
42-
Project selectOptionDoesNotCauseError(Employee <error descr="There are unused parameters in the SQL [employee]">employee</error>,String searchName,SelectOptions options);
56+
@Sql("SELECT * FROM project WHERE name = /* searchName */'test'")
57+
Project selectHogeOption(Employee <error descr="There are unused parameters in the SQL [employee]">employee</error>,
58+
String searchName,
59+
HogeSelectOptions options);
4360

4461
@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);
62+
Project collectDoesNotCauseError(Employee <error descr="There are unused parameters in the SQL [employee]">employee</error>,
63+
Integer id,
64+
Collector<Project, ?, Project> collector);
65+
66+
@Select(strategy = SelectType.COLLECT)
67+
Project selectHogeCollector(Employee <error descr="There are unused parameters in the SQL [employee]">employee</error>,
68+
Integer id,
69+
HogeCollector collector);
4670

4771
@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>);
72+
Project collectDoesCauseError(Employee <error descr="There are unused parameters in the SQL [employee]">employee</error>,
73+
String searchName,
74+
Collector<Project, ?, Project> collector);
75+
76+
@Select(strategy = SelectType.STREAM)
77+
String functionDoesNotCauseError(Employee <error descr="There are unused parameters in the SQL [employee]">employee</error>,
78+
Integer id,
79+
Function<Stream<Employee>, String> function);
80+
81+
@Select(strategy = SelectType.STREAM)
82+
String selectHogeFunction(Employee <error descr="There are unused parameters in the SQL [employee]">employee</error>,
83+
Integer id,
84+
HogeFunction function);
4985

5086
@Select
5187
Project noErrorWhenUsedInFunctionParameters(Employee employee, Integer count);
5288

5389
@Select
54-
Employee duplicateForDirectiveDefinitionNames(Employee <error descr="An element name that is a duplicate of an element name defined in SQL is used">member</error>, Integer <error descr="There are unused parameters in the SQL [count]">count</error>,
90+
Employee duplicateForDirectiveDefinitionNames(Employee <error descr="An element name that is a duplicate of an element name defined in SQL is used">member</error>,
91+
Integer <error descr="There are unused parameters in the SQL [count]">count</error>,
5592
List<Employee> users,
5693
String searchName,
5794
Boolean inForm);
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package doma.example.function;
2+
3+
import org.seasar.doma.jdbc.Config;
4+
import org.seasar.doma.jdbc.PreparedSql;
5+
6+
import java.io.ObjectInputFilter;
7+
import java.util.function.BiFunction;
8+
import java.util.function.Function;
9+
import java.util.stream.Stream;
10+
11+
public class HogeBiFunction implements BiFunction<Config, PreparedSql, String> {
12+
13+
@Override
14+
public String apply(Config config, PreparedSql preparedSql) {
15+
return "";
16+
}
17+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package doma.example.option;
2+
3+
import org.seasar.doma.jdbc.SelectOptions;
4+
5+
public class HogeSelectOptions extends SelectOptions {
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
select
2+
p.project_id
3+
, p.project_name
4+
, p.project_number
5+
from project p
6+
where p.project_id = /* id */0

0 commit comments

Comments
 (0)