Skip to content

Commit 1cf1249

Browse files
authored
Merge pull request #48 from domaframework/chore/change-null-check
Force unwrapping a nullable type into a null-safe object
2 parents a834b35 + 10cb126 commit 1cf1249

File tree

14 files changed

+81
-93
lines changed

14 files changed

+81
-93
lines changed

src/main/kotlin/org/domaframework/doma/intellij/action/dao/GenerateSqlAction.kt

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,16 @@ class GenerateSqlAction : AnAction() {
3737
e.presentation.isEnabledAndVisible = false
3838
currentFile = e.getData(CommonDataKeys.PSI_FILE) ?: return
3939
val editor = e.getData(CommonDataKeys.EDITOR) ?: return
40-
getDaoClass(currentFile!!) ?: return
41-
val element = currentFile!!.findElementAt(editor.caretModel.offset) ?: return
40+
val project = e.project ?: return
41+
val file: PsiFile = currentFile ?: return
42+
if (getDaoClass(file) == null) return
43+
val element = file.findElementAt(editor.caretModel.offset) ?: return
4244
val method = PsiTreeUtil.getParentOfType(element, PsiMethod::class.java) ?: return
4345

44-
val psiDaoMethod = PsiDaoMethod(e.project!!, method)
46+
val psiDaoMethod = PsiDaoMethod(project, method)
4547
e.presentation.isEnabledAndVisible =
4648
psiDaoMethod.isUseSqlFileMethod() &&
47-
isJavaOrKotlinFileType(currentFile!!) &&
49+
isJavaOrKotlinFileType(file) &&
4850
psiDaoMethod.sqlFile == null
4951
}
5052

@@ -61,7 +63,8 @@ class GenerateSqlAction : AnAction() {
6163
)
6264
val project = e.project ?: return
6365
val editor = e.getData(CommonDataKeys.EDITOR) ?: return
64-
val element = currentFile!!.findElementAt(editor.caretModel.offset) ?: return
66+
val file: PsiFile = currentFile ?: return
67+
val element = file.findElementAt(editor.caretModel.offset) ?: return
6568
val method = PsiTreeUtil.getParentOfType(element, PsiMethod::class.java) ?: return
6669
val psiDaoMethod = PsiDaoMethod(project, method)
6770
psiDaoMethod.generateSqlFile()

src/main/kotlin/org/domaframework/doma/intellij/action/dao/JumpToSQLFromDaoAction.kt

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,15 @@ class JumpToSQLFromDaoAction : AnAction() {
3434
override fun update(e: AnActionEvent) {
3535
e.presentation.isEnabledAndVisible = false
3636
currentFile = e.getData(CommonDataKeys.PSI_FILE) ?: return
37+
val file: PsiFile = currentFile ?: return
3738

38-
if (!isJavaOrKotlinFileType(currentFile!!)) return
39-
getDaoClass(currentFile!!) ?: return
39+
if (!isJavaOrKotlinFileType(file) || getDaoClass(file) == null) return
4040

4141
val editor = e.getData(CommonDataKeys.EDITOR) ?: return
42-
val element = currentFile!!.findElementAt(editor.caretModel.offset) ?: return
42+
val element = file.findElementAt(editor.caretModel.offset) ?: return
43+
val project = e.project ?: return
4344
val method = PsiTreeUtil.getParentOfType(element, PsiMethod::class.java) ?: return
44-
val psiDaoMethod = PsiDaoMethod(e.project!!, method)
45+
val psiDaoMethod = PsiDaoMethod(project, method)
4546

4647
e.presentation.isEnabledAndVisible =
4748
psiDaoMethod.isUseSqlFileMethod() &&
@@ -60,10 +61,9 @@ class JumpToSQLFromDaoAction : AnAction() {
6061
startTime,
6162
)
6263
val editor = e.getData(CommonDataKeys.EDITOR) ?: return
63-
val element = currentFile!!.findElementAt(editor.caretModel.offset) ?: return
64+
val element = currentFile?.findElementAt(editor.caretModel.offset) ?: return
6465
val method = PsiTreeUtil.getParentOfType(element, PsiMethod::class.java) ?: return
6566
val project = e.project ?: return
66-
6767
val psiDaoMethod = PsiDaoMethod(project, method)
6868

6969
PluginLoggerUtil.countLoggingByAction(
@@ -72,6 +72,7 @@ class JumpToSQLFromDaoAction : AnAction() {
7272
inputEvent,
7373
startTime,
7474
)
75-
jumpSqlFromDao(project, psiDaoMethod.sqlFile!!)
75+
val sqlFile = psiDaoMethod.sqlFile ?: return
76+
jumpSqlFromDao(project, sqlFile)
7677
}
7778
}

src/main/kotlin/org/domaframework/doma/intellij/action/sql/JumpToDaoFromSQLAction.kt

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,10 @@ class JumpToDaoFromSQLAction : AnAction() {
3535
override fun update(e: AnActionEvent) {
3636
e.presentation.isEnabledAndVisible = false
3737
currentFile = e.getData(CommonDataKeys.PSI_FILE) ?: return
38-
findDaoMethod(currentFile!!) ?: return
38+
val file = currentFile ?: return
39+
if (findDaoMethod(file) == null) return
3940
e.presentation.isEnabledAndVisible =
40-
isSupportFileType(currentFile!!)
41+
isSupportFileType(file)
4142
}
4243

4344
override fun getActionUpdateThread(): ActionUpdateThread = ActionUpdateThread.BGT
@@ -51,11 +52,12 @@ class JumpToDaoFromSQLAction : AnAction() {
5152
inputEvent,
5253
startTime,
5354
)
54-
55+
val file = currentFile ?: return
5556
val project = e.project ?: return
56-
val daoFile = findDaoFile(project, currentFile!!) ?: return
57+
val daoFile = findDaoFile(project, file) ?: return
5758

58-
jumpToDaoMethod(project, currentFile?.virtualFile?.nameWithoutExtension!!, daoFile)
59+
val nameWithoutExtension = file.virtualFile?.nameWithoutExtension ?: return
60+
jumpToDaoMethod(project, nameWithoutExtension, daoFile)
5961
PluginLoggerUtil.countLoggingByAction(
6062
this::class.java.simpleName,
6163
"JumpToDao",

src/main/kotlin/org/domaframework/doma/intellij/action/sql/JumpToDeclarationFromSqlAction.kt

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import com.intellij.psi.util.PsiTypesUtil
3131
import com.intellij.psi.util.elementType
3232
import org.domaframework.doma.intellij.common.PluginLoggerUtil
3333
import org.domaframework.doma.intellij.common.dao.findDaoMethod
34+
import org.domaframework.doma.intellij.common.dao.getDaoClass
3435
import org.domaframework.doma.intellij.common.isJavaOrKotlinFileType
3536
import org.domaframework.doma.intellij.common.psi.PsiParentClass
3637
import org.domaframework.doma.intellij.common.psi.PsiStaticElement
@@ -58,31 +59,35 @@ class JumpToDeclarationFromSqlAction : AnAction() {
5859
val caretOffset = editor.caretModel.primaryCaret.selectionStart
5960

6061
currentFile = e.getData(CommonDataKeys.PSI_FILE) ?: return
61-
currentFile?.let { element = it.findElementAt(caretOffset) ?: return }
62-
if (element == null) return
62+
element = currentFile?.findElementAt(caretOffset) ?: return
6363

64+
var file: PsiFile = currentFile ?: return
6465
val project = element?.project ?: return
65-
if (isJavaOrKotlinFileType(currentFile ?: return)) {
66+
if (isJavaOrKotlinFileType(file) && getDaoClass(file) != null) {
6667
val injectedLanguageManager =
6768
InjectedLanguageManager.getInstance(project)
68-
val literal = PsiTreeUtil.getParentOfType(element, PsiLiteralExpression::class.java)
69+
val literal = PsiTreeUtil.getParentOfType(element, PsiLiteralExpression::class.java) ?: return
6970
currentFile =
7071
injectedLanguageManager
71-
.getInjectedPsiFiles(literal!!)
72+
.getInjectedPsiFiles(literal)
7273
?.firstOrNull()
7374
?.first as? PsiFile
74-
element = currentFile?.findElementAt(countInjectionOffset(literal, caretOffset))
75+
?: return
76+
file = currentFile ?: return
77+
element = file.findElementAt(countInjectionOffset(literal, caretOffset)) ?: return
7578
}
76-
findDaoMethod(currentFile ?: return) ?: return
79+
if (findDaoMethod(file) == null) return
7780

78-
val staticDirection = element?.parent
79-
val staticDirective = getStaticDirective(staticDirection, element!!.text)
81+
val elm = element ?: return
82+
val elementText = elm.text ?: ""
83+
val staticDirection = elm.parent
84+
val staticDirective = getStaticDirective(staticDirection, elementText)
8085
if (staticDirective != null) {
8186
e.presentation.isEnabledAndVisible = true
8287
return
8388
}
8489

85-
val targetElement = getBlockCommentElements(element ?: return)
90+
val targetElement = getBlockCommentElements(elm)
8691
if (targetElement.isNotEmpty()) e.presentation.isEnabledAndVisible = true
8792
}
8893

@@ -114,9 +119,13 @@ class JumpToDeclarationFromSqlAction : AnAction() {
114119
override fun getActionUpdateThread(): ActionUpdateThread = ActionUpdateThread.BGT
115120

116121
override fun actionPerformed(e: AnActionEvent) {
122+
val elm = element ?: return
123+
val elementText = elm.text ?: ""
124+
val file = currentFile ?: return
125+
117126
val startTime = System.nanoTime()
118-
val staticDirection = element?.parent
119-
val staticDirective = getStaticDirective(staticDirection, element!!.text)
127+
val staticDirection = elm.parent
128+
val staticDirective = getStaticDirective(staticDirection, elementText)
120129
if (staticDirective != null) {
121130
BindVariableElement(staticDirective).jumpToEntity()
122131
PluginLoggerUtil.countLoggingByAction(
@@ -127,20 +136,20 @@ class JumpToDeclarationFromSqlAction : AnAction() {
127136
)
128137
return
129138
}
130-
if (element == null) return
139+
131140
// TODO Since the update also checks whether the action is to be executed,
132141
// delete it if it is unnecessary.
133-
if (isNotBindVariable(element!!)) return
142+
if (isNotBindVariable(elm)) return
134143

135-
val targetElement = getBlockCommentElements(element!!)
144+
val targetElement = getBlockCommentElements(elm)
136145
if (targetElement.isEmpty()) return
137146

138-
val daoMethod = currentFile?.let { file -> findDaoMethod(file) } ?: return
139-
when (element!!.textOffset) {
147+
val daoMethod = findDaoMethod(file) ?: return
148+
when (elm.textOffset) {
140149
targetElement.first().textOffset ->
141150
jumpToDaoMethodParameter(
142151
daoMethod,
143-
element!!,
152+
elm,
144153
logActionFileType,
145154
e,
146155
startTime,
@@ -155,10 +164,10 @@ class JumpToDeclarationFromSqlAction : AnAction() {
155164
elementName: String,
156165
): PsiElement? {
157166
if (staticDirection == null) return null
158-
167+
val file: PsiFile = currentFile ?: return null
159168
// Jump to class definition
160169
if (staticDirection is SqlElClass) {
161-
val psiStaticElement = PsiStaticElement(staticDirection.text, currentFile!!)
170+
val psiStaticElement = PsiStaticElement(staticDirection.text, file)
162171
return psiStaticElement.getRefClazz()
163172
}
164173

@@ -167,12 +176,14 @@ class JumpToDeclarationFromSqlAction : AnAction() {
167176
if (staticDirection is SqlElStaticFieldAccessExpr ||
168177
staticAccessParent is SqlElStaticFieldAccessExpr
169178
) {
179+
val firstChildText =
180+
staticAccessParent.children
181+
.firstOrNull()
182+
?.text ?: ""
170183
val psiStaticElement =
171184
PsiStaticElement(
172-
staticAccessParent.children
173-
.firstOrNull()
174-
?.text ?: "",
175-
currentFile!!,
185+
firstChildText,
186+
file,
176187
)
177188
val javaClazz = psiStaticElement.getRefClazz() ?: return null
178189
val psiParentClass = PsiParentClass(PsiTypesUtil.getClassType(javaClazz))
@@ -300,9 +311,9 @@ class JumpToDeclarationFromSqlAction : AnAction() {
300311
parentClass
301312
.findMethod(elm.text)
302313
?.let {
303-
if (it.returnType == null) return null
314+
val returnType = it.returnType ?: return null
304315
val bindVal =
305-
getBindVariableIfLastIndex(index, it.returnType!!, it.originalElement)
316+
getBindVariableIfLastIndex(index, returnType, it.originalElement)
306317
if (bindVal != null) return bindVal
307318
}
308319
if (!isExistProperty) return null

src/main/kotlin/org/domaframework/doma/intellij/common/dao/JumpActionFunctions.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ private fun getJavaFunctionOffset(
6363
targetMethodName: String,
6464
) {
6565
try {
66-
PsiNavigateUtil.navigate(findUseSqlDaoMethod(file, targetMethodName)!!)
66+
val dapMethod = findUseSqlDaoMethod(file, targetMethodName) ?: return
67+
PsiNavigateUtil.navigate(dapMethod)
6768
} catch (e: Exception) {
6869
rethrow(e)
6970
}

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ import java.io.IOException
4646
* Class that handles Dao method information
4747
*/
4848
class PsiDaoMethod(
49-
val psiProject: Project,
49+
private val psiProject: Project,
5050
val psiMethod: PsiMethod,
5151
) {
5252
private val isTest = false
@@ -132,7 +132,7 @@ class PsiDaoMethod(
132132
psiFileFactory
133133
.createFileFromText(
134134
"tempFile",
135-
Language.findLanguageByID(SqlLanguage.INSTANCE.id)!!,
135+
Language.findLanguageByID(SqlLanguage.INSTANCE.id) ?: return,
136136
valueText,
137137
).virtualFile
138138
}
@@ -154,11 +154,14 @@ class PsiDaoMethod(
154154
throw IncorrectOperationException(e)
155155
}
156156

157+
val virtualFile =
158+
VfsUtil.findRelativeFile(rootDir, *parenDirPathSpirit)
159+
?: return@runWriteCommandAction
157160
val sqlOutputDirPath =
158161
PsiManager
159162
.getInstance(psiProject)
160-
.findDirectory(VfsUtil.findRelativeFile(rootDir, *parenDirPathSpirit)!!)
161-
val sqlVirtualFile = sqlOutputDirPath!!.createFile(sqlFileName).virtualFile
163+
.findDirectory(virtualFile) ?: return@runWriteCommandAction
164+
val sqlVirtualFile = sqlOutputDirPath.createFile(sqlFileName).virtualFile
162165
FileEditorManager
163166
.getInstance(psiProject)
164167
.openFile(sqlVirtualFile, true)

src/main/kotlin/org/domaframework/doma/intellij/contributor/sql/provider/SqlParameterCompletionProvider.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,8 @@ class SqlParameterCompletionProvider : CompletionProvider<CompletionParameters>(
112112
it !is PsiErrorElement
113113
}
114114
if (!pos.isNotWhiteSpace() && !isRightFactor(prevElm)) return
115-
116-
val blockElements = getAccessElementTextBlocks(parameters.originalPosition!!)
115+
val originalPosition = parameters.originalPosition ?: return
116+
val blockElements = getAccessElementTextBlocks(originalPosition)
117117
generateCompletionList(
118118
blockElements,
119119
originalFile,
@@ -146,7 +146,7 @@ class SqlParameterCompletionProvider : CompletionProvider<CompletionParameters>(
146146
prevElm?.elementType == SqlTypes.EL_ASTERISK ||
147147
prevElm?.elementType == SqlTypes.EL_SLASH ||
148148
prevElm?.elementType == SqlTypes.EL_PERCENT ||
149-
prevElm?.isNotWhiteSpace()!!
149+
prevElm?.isNotWhiteSpace() == true
150150
)
151151

152152
private fun getAccessElementTextBlocks(targetElement: PsiElement): List<PsiElement> {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ enum class DomaAnnotationType(
4040

4141
fun isRequireSqlTemplate(): Boolean = this == Select || this == Script || this == SqlProcessor
4242

43-
fun useSqlFileOption(): Boolean =
43+
private fun useSqlFileOption(): Boolean =
4444
this == Insert ||
4545
this == Update ||
4646
this == Delete ||

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

Lines changed: 0 additions & 33 deletions
This file was deleted.

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ fun PsiParameter.getIterableClazz(annotationType: DomaAnnotationType): PsiParent
2929
if (immediate.name == "List" && annotationType.isBatchAnnotation()) {
3030
val listType =
3131
(this.type as PsiClassReferenceType).parameters.firstOrNull()
32-
return PsiParentClass(listType!!)
32+
?: return PsiParentClass(this.type)
33+
return PsiParentClass(listType)
3334
}
3435
}
3536
return PsiParentClass(this.type)

0 commit comments

Comments
 (0)