Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,6 @@ class PsiDaoMethod(

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

fun isSelectTypeCollect(): Boolean {
val selectAnnotation = DomaAnnotationType.Select.getPsiAnnotation(psiMethod) ?: return false
return daoType.isSelectTypeCollect(selectAnnotation)
}

fun useSqlAnnotation(): Boolean = getSqlAnnotation() != null

private fun setSqlFilePath() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ enum class DomaClassName(
VOID("java.lang.Void"),
RETURNING("org.seasar.doma.Returning"),
REFERENCE("org.seasar.doma.jdbc.Reference"),
SELECT_OPTIONS("org.seasar.doma.jdbc.SelectOptions"),

STRING("java.lang.String"),
OBJECT("java.lang.Object"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ package org.domaframework.doma.intellij.extension.psi
import com.intellij.codeInsight.AnnotationUtil
import com.intellij.psi.PsiAnnotation
import com.intellij.psi.PsiModifierListOwner
import com.intellij.psi.PsiNameValuePair
import com.intellij.psi.PsiReferenceExpression
import com.intellij.psi.util.PsiTreeUtil

enum class DomaAnnotationType(
val fqdn: String,
Expand Down Expand Up @@ -74,19 +71,4 @@ enum class DomaAnnotationType(
} else {
false
}

fun isSelectTypeCollect(element: PsiAnnotation): Boolean {
val strategyPair =
PsiTreeUtil
.getChildrenOfTypeAsList(element.parameterList, PsiNameValuePair::class.java)
.firstOrNull {
it.text.startsWith("strategy")
} ?: return false

return PsiTreeUtil
.getChildOfType(
strategyPair,
PsiReferenceExpression::class.java,
)?.text == "SelectType.COLLECT"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,29 @@ package org.domaframework.doma.intellij.extension.psi

import com.intellij.psi.PsiClassType
import com.intellij.psi.PsiParameter
import org.domaframework.doma.intellij.common.util.DomaClassName

val PsiParameter.isFunctionClazz: Boolean
get() =
(this.typeElement?.type as? PsiClassType)
?.resolve()
?.qualifiedName
?.contains("java.util.function") == true
private val ignoreUsageCheckType =
listOf<DomaClassName>(
DomaClassName.JAVA_FUNCTION,
DomaClassName.BI_FUNCTION,
DomaClassName.SELECT_OPTIONS,
DomaClassName.JAVA_COLLECTOR,
)

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

val PsiParameter.isCollector: Boolean
get() =
(this.typeElement?.type as? PsiClassType)
?.resolve()
?.qualifiedName == "java.util.stream.Collector"
fun PsiParameter.getSuperClassType(superClassType: DomaClassName): PsiClassType? {
val clazzType = this.typeElement?.type as? PsiClassType
var superCollection: PsiClassType? = clazzType
while (superCollection != null &&
!superClassType.isTargetClassNameStartsWith(superCollection.canonicalText)
) {
superCollection =
superCollection.getSuperType(superClassType.className)
}
return superCollection
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ import org.domaframework.doma.intellij.common.dao.getDaoClass
import org.domaframework.doma.intellij.common.isJavaOrKotlinFileType
import org.domaframework.doma.intellij.common.psi.PsiDaoMethod
import org.domaframework.doma.intellij.extension.findFile
import org.domaframework.doma.intellij.extension.psi.isCollector
import org.domaframework.doma.intellij.extension.psi.isFunctionClazz
import org.domaframework.doma.intellij.extension.psi.isSelectOption
import org.domaframework.doma.intellij.extension.psi.isIgnoreUsageCheck
import org.domaframework.doma.intellij.extension.psi.methodParameters

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

val methodParameters =
method.methodParameters
.filter { !it.isFunctionClazz && !it.isSelectOption && !(it.isCollector && psiDaoMethod.isSelectTypeCollect()) }
method.methodParameters.filter { !it.isIgnoreUsageCheck() }
val sqlFileManager =
psiDaoMethod.sqlFile?.let {
method.project.findFile(it)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,14 @@ class DomaUseVariableTest : DomaSqlTest() {
"$testDaoName/collectDoesCauseError.sql",
"$testDaoName/noErrorWhenUsedInFunctionParameters.sql",
"$testDaoName/duplicateForDirectiveDefinitionNames.sql",
"$testDaoName/selectHogeFunction.sql",
"$testDaoName/functionDoesNotCauseError.sql",
"$testDaoName/selectHogeCollector.sql",
)
addOtherJavaFile("collector", "HogeCollector.java")
addOtherJavaFile("function", "HogeFunction.java")
addOtherJavaFile("function", "HogeBiFunction.java")
addOtherJavaFile("option", "HogeSelectOptions.java")
myFixture.enableInspections(UsedDaoMethodParamInspection())
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@
import org.seasar.doma.jdbc.PreparedSql;
import org.seasar.doma.jdbc.SelectOptions;
import org.seasar.doma.SelectType;
import java.util.stream.Stream;
import java.util.function.Function;
import java.util.stream.Collector;
import doma.example.function.*;
import doma.example.collector.*;
import doma.example.option.*;

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

@SqlProcessor
@Sql("SELECT id, name FROM demo")
<R> R biFunctionHogeFunction(HogeBiFunction handler);

@Select
Project selectOptionDoesNotCauseError(Employee <error descr="There are unused parameters in the SQL [employee]">employee</error>,
String searchName,
SelectOptions options);

@Select
Project selectOptionDoesNotCauseError(Employee <error descr="There are unused parameters in the SQL [employee]">employee</error>,String searchName,SelectOptions options);
@Sql("SELECT * FROM project WHERE name = /* searchName */'test'")
Project selectHogeOption(Employee <error descr="There are unused parameters in the SQL [employee]">employee</error>,
String searchName,
HogeSelectOptions options);

@Select(strategy = SelectType.COLLECT)
Project collectDoesNotCauseError(Employee <error descr="There are unused parameters in the SQL [employee]">employee</error>,Integer id,Collector<Project, ?, Project> collector);
Project collectDoesNotCauseError(Employee <error descr="There are unused parameters in the SQL [employee]">employee</error>,
Integer id,
Collector<Project, ?, Project> collector);

@Select(strategy = SelectType.COLLECT)
Project selectHogeCollector(Employee <error descr="There are unused parameters in the SQL [employee]">employee</error>,
Integer id,
HogeCollector collector);

@Select
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>);
Project collectDoesCauseError(Employee <error descr="There are unused parameters in the SQL [employee]">employee</error>,
String searchName,
Collector<Project, ?, Project> collector);

@Select(strategy = SelectType.STREAM)
String functionDoesNotCauseError(Employee <error descr="There are unused parameters in the SQL [employee]">employee</error>,
Integer id,
Function<Stream<Employee>, String> function);

@Select(strategy = SelectType.STREAM)
String selectHogeFunction(Employee <error descr="There are unused parameters in the SQL [employee]">employee</error>,
Integer id,
HogeFunction function);

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

@Select
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>,
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>,
List<Employee> users,
String searchName,
Boolean inForm);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public interface SelectReturnTypeTestDao {

@Select(strategy = SelectType.COLLECT)
@Sql("select * from emp where salary > /* salary */0")
Pckt selectHogeCollect(BigDecimal salary, HogeCollector collector);
Pckt <error descr="The return type must match RESULT of the \"java.util.stream.Collector\" type parameter">selectHogeCollect</error>(BigDecimal salary, HogeCollector collector);

@Select(strategy = SelectType.STREAM)
@Sql("select * from emp where salary > /* salary */0")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package doma.example.function;

import org.seasar.doma.jdbc.Config;
import org.seasar.doma.jdbc.PreparedSql;

import java.io.ObjectInputFilter;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.stream.Stream;

public class HogeBiFunction implements BiFunction<Config, PreparedSql, String> {

@Override
public String apply(Config config, PreparedSql preparedSql) {
return "";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package doma.example.option;

import org.seasar.doma.jdbc.SelectOptions;

public class HogeSelectOptions extends SelectOptions {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
select
p.project_id
, p.project_name
, p.project_number
from project p
where p.project_id = /* id */0
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
select
p.project_id
, p.project_name
, p.project_number
from project p
where p.project_id = /* id */0
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
select
p.project_id
, p.project_name
, p.project_number
from project p
where p.project_id = /* id */0
Loading