Skip to content

Commit 551bc52

Browse files
committed
Refactor annotation option parameter check to extract array values into a separate function
1 parent 33c4118 commit 551bc52

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

src/main/kotlin/org/domaframework/doma/intellij/inspection/dao/processor/option/DaoAnnotationOptionParameterCheckProcessor.kt

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ package org.domaframework.doma.intellij.inspection.dao.processor.option
1818
import com.intellij.codeInspection.ProblemsHolder
1919
import com.intellij.openapi.project.Project
2020
import com.intellij.psi.PsiAnnotation
21+
import com.intellij.psi.PsiAnnotationMemberValue
2122
import com.intellij.psi.PsiClass
2223
import com.intellij.psi.PsiClassType
24+
import com.intellij.psi.PsiElement
2325
import com.intellij.psi.PsiField
2426
import com.intellij.psi.PsiLiteralExpression
2527
import com.intellij.psi.PsiPrimitiveType
@@ -98,15 +100,9 @@ class DaoAnnotationOptionParameterCheckProcessor(
98100
annotation.parameterList.attributes
99101
.find { it.name == optionName }
100102
?.value
103+
?: return
101104

102-
val arrayValues =
103-
if (expression is PsiLiteralExpression) {
104-
listOf(expression)
105-
} else {
106-
expression
107-
?.children
108-
?.filter { it is PsiLiteralExpression } ?: return
109-
}
105+
val arrayValues = extractArrayValues(expression)
110106
if (arrayValues.isEmpty()) return
111107

112108
val project = method.project
@@ -121,6 +117,8 @@ class DaoAnnotationOptionParameterCheckProcessor(
121117
searchParamClass
122118
?.fields
123119
?.find { property -> isOptionTargetProperty(property, field, project) }
120+
// Given that the first `searchParamType` is assumed to contain the type of Entity class,
121+
// checking the index for a primitive type is unnecessary.
124122
if (searchParamType is PsiPrimitiveType) {
125123
// This is a primitive/basic type but there are more fields after it
126124
ValidationAnnotationOptionPrimitiveFieldResult(
@@ -164,6 +162,15 @@ class DaoAnnotationOptionParameterCheckProcessor(
164162
}
165163
}
166164

165+
private fun extractArrayValues(expression: PsiAnnotationMemberValue): List<PsiElement> =
166+
if (expression is PsiLiteralExpression) {
167+
listOf(expression)
168+
} else {
169+
expression
170+
.children
171+
.filter { it is PsiLiteralExpression }
172+
}
173+
167174
private fun getTargetOptionProperties(paramClass: PsiClass?) =
168175
paramClass?.fields?.filter { isOptionTargetProperty(it, it.name, project) }?.joinToString(", ") { it.name.substringAfter(":") }
169176
?: "No fields found"

0 commit comments

Comments
 (0)