Skip to content

Commit 4351341

Browse files
authored
Merge pull request #29 from csense-oss/2.1.2
2.1.2
2 parents 51a29bd + 05a4d90 commit 4351341

File tree

10 files changed

+112
-48
lines changed

10 files changed

+112
-48
lines changed

build.gradle.kts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
plugins {
22
//https://github.com/JetBrains/gradle-intellij-plugin
3-
id("org.jetbrains.intellij") version "1.17.2"
3+
id("org.jetbrains.intellij") version "1.17.3"
44
//https://github.com/JetBrains/kotlin
55
kotlin("jvm") version "1.9.23"
66
//https://jeremylong.github.io/DependencyCheck/
7-
id("org.owasp.dependencycheck") version "9.0.10"
7+
id("org.owasp.dependencycheck") version "9.1.0"
88
}
99

1010
val javaVersion = "11"
1111

1212
group = "csense-idea"
13-
version = "2.1.1"
13+
version = "2.1.2"
1414

1515
intellij {
1616
updateSinceUntilBuild.set(false)
@@ -35,7 +35,7 @@ dependencies {
3535
//https://github.com/csense-oss/csense-kotlin-annotations
3636
implementation("csense.kotlin:csense-kotlin-annotations-jvm:0.0.63")
3737
//https://github.com/csense-oss/idea-kotlin-shared-base
38-
implementation("csense.idea.base:csense-idea-base:0.1.62")
38+
implementation("csense.idea.base:csense-idea-base:0.1.63")
3939
//https://github.com/Kotlin/kotlinx.serialization
4040
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.0")
4141
//https://github.com/Kotlin/kotlinx.coroutines
@@ -49,9 +49,11 @@ dependencies {
4949
tasks.getByName<org.jetbrains.intellij.tasks.PatchPluginXmlTask>("patchPluginXml") {
5050
changeNotes.set(
5151
"""
52+
Fixes
5253
<ul>
53-
<li>Fixed https://github.com/csense-oss/idea-kotlin-checked-exceptions/issues/18</li>
54-
<li>Fixed https://github.com/csense-oss/idea-kotlin-checked-exceptions/issues/25</li>
54+
<li> Fixes a lot of issues with respect to throw expressions (also causes a thrown exception to be interpreted as Exception https://github.com/csense-oss/idea-kotlin-checked-exceptions/issues/26 )</li>
55+
<li> QuickFixes (CSense detects Throws annotation only if lambda provides callsInPlace contract https://github.com/csense-oss/idea-kotlin-checked-exceptions/issues/27) </li>
56+
<li> Method references are now resolved (method references are not marked https://github.com/csense-oss/idea-kotlin-checked-exceptions/issues/28) </li>
5557
</ul>
5658
"""
5759
)

changelog.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
# 2.1.2
2+
- Fixes a lot of issues with respect to throw expressions (also causes a thrown exception to be interpreted as Exception https://github.com/csense-oss/idea-kotlin-checked-exceptions/issues/26 )
3+
- QuickFixes (CSense detects Throws annotation only if lambda provides callsInPlace contract https://github.com/csense-oss/idea-kotlin-checked-exceptions/issues/27)
4+
- Method references are now resolved (method references are not marked https://github.com/csense-oss/idea-kotlin-checked-exceptions/issues/28)
5+
16
# 2.1.1
27
- Fixed https://github.com/csense-oss/idea-kotlin-checked-exceptions/issues/18
38
- Fixed https://github.com/csense-oss/idea-kotlin-checked-exceptions/issues/25
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package csense.idea.kotlin.checked.exceptions.bll
2+
3+
import csense.idea.base.bll.psiWrapper.`class`.*
4+
import csense.idea.base.bll.psiWrapper.function.operations.*
5+
import org.jetbrains.kotlin.psi.*
6+
7+
fun KtCallExpression.throwsTypesForSettings(): List<KtPsiClass> {
8+
val forFunction: List<KtPsiClass> = resolveMainReferenceAsFunction()
9+
?.throwsTypesForSettings()
10+
?: listOf()
11+
12+
return forFunction + resolveAllThrowingValueArguments()
13+
}
14+
15+
fun KtCallExpression.resolveAllThrowingValueArguments(): List<KtPsiClass> {
16+
return valueArguments.flatMap { it: KtValueArgument? ->
17+
val argExpression: KtExpression = it?.getArgumentExpression() ?: return@flatMap emptyList()
18+
when (argExpression) {
19+
is KtCallableReferenceExpression -> argExpression.throwsTypesForSettings()
20+
is KtCallExpression -> argExpression.throwsTypesForSettings()
21+
else -> emptyList()
22+
}
23+
}
24+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package csense.idea.kotlin.checked.exceptions.bll
2+
3+
import csense.idea.base.bll.psiWrapper.`class`.*
4+
import csense.idea.base.bll.psiWrapper.function.operations.*
5+
import org.jetbrains.kotlin.psi.*
6+
7+
fun KtCallableReferenceExpression.throwsTypesForSettings(): List<KtPsiClass> {
8+
return resolveKtPsiFunctionOrNull()?.throwsTypesForSettings() ?: emptyList()
9+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package csense.idea.kotlin.checked.exceptions.csense
2+
3+
import csense.kotlin.extensions.collections.*
4+
5+
6+
public inline fun <Item, U> Collection<Item>.selectLastOrNull(mappingPredicate: (Item) -> U?): U? {
7+
forEachBackwards { item: Item ->
8+
mappingPredicate(item)?.let { mapped: U ->
9+
return@selectLastOrNull mapped
10+
}
11+
}
12+
return null
13+
}

src/main/kotlin/csense/idea/kotlin/checked/exceptions/lineMarkers/CheckedExceptionLineMarkerProvider.kt

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,15 @@ import com.intellij.codeInsight.navigation.*
55
import com.intellij.openapi.util.*
66
import com.intellij.psi.*
77
import com.intellij.psi.impl.source.tree.*
8-
import com.intellij.psi.tree.*
98
import csense.idea.base.bll.ast.*
109
import csense.idea.base.bll.kotlin.*
1110
import csense.idea.base.bll.linemarkers.*
1211
import csense.idea.base.bll.psiWrapper.`class`.*
1312
import csense.idea.base.bll.psiWrapper.`class`.operations.*
14-
import csense.idea.base.bll.psiWrapper.function.operations.*
1513
import csense.idea.kotlin.checked.exceptions.bll.*
16-
import csense.idea.kotlin.checked.exceptions.builtin.operations.*
17-
import csense.idea.kotlin.checked.exceptions.inspections.*
18-
import csense.idea.kotlin.checked.exceptions.settings.*
1914
import csense.kotlin.extensions.*
2015
import csense.kotlin.extensions.collections.*
2116
import org.intellij.lang.annotations.*
22-
import org.jetbrains.kotlin.lexer.*
2317
import org.jetbrains.kotlin.psi.*
2418
import javax.swing.*
2519

@@ -76,17 +70,14 @@ class CheckedExceptionLineMarkerProvider : SafeRelatedItemLineMarkerProvider() {
7670
leaf.parent.parent.invokeIsInstance { call: KtCallExpression ->
7771
onCallExpression(element = call, leafPsiElement = leaf, result = result)
7872
}
79-
8073
}
8174

82-
8375
private fun onCallExpression(
8476
element: KtCallExpression,
8577
leafPsiElement: LeafPsiElement,
8678
result: MutableCollection<in RelatedItemLineMarkerInfo<*>>
8779
) {
88-
val throwsNonEmpty: List<KtPsiClass> = element.resolveMainReferenceAsFunction()
89-
?.throwsTypesForSettings()
80+
val throwsNonEmpty: List<KtPsiClass> = element.throwsTypesForSettings()
9081
.nullOnEmpty()
9182
?: return
9283

@@ -100,6 +91,7 @@ class CheckedExceptionLineMarkerProvider : SafeRelatedItemLineMarkerProvider() {
10091
leafPsiElement: LeafPsiElement,
10192
typesOfExceptions: List<KtPsiClass>
10293
): RelatedItemLineMarkerInfo<PsiElement> {
94+
val exceptionIcon: Icon by lazy { IconLoader.getIcon("/icons/throws.svg", Companion::class.java) }
10395

10496
val typesString: String = typesOfExceptions.coloredFqNameString(
10597
cssColor = IconThemeColor,
@@ -120,9 +112,7 @@ class CheckedExceptionLineMarkerProvider : SafeRelatedItemLineMarkerProvider() {
120112
}
121113

122114

123-
124115
companion object {
125-
val exceptionIcon: Icon by lazy { IconLoader.getIcon("/icons/throws.svg", Companion::class.java) }
126116
const val IconThemeColor = "#DB5860"
127117
}
128-
}
118+
}

src/main/kotlin/csense/idea/kotlin/checked/exceptions/lineMarkers/ThrowsExceptionLineMarkerProvider.kt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import com.intellij.codeInsight.navigation.*
55
import com.intellij.openapi.util.*
66
import com.intellij.psi.*
77
import com.intellij.psi.impl.source.tree.*
8-
import csense.idea.base.bll.annotator.*
98
import csense.idea.base.bll.kotlin.*
109
import csense.idea.base.bll.linemarkers.*
1110
import csense.idea.base.bll.psiWrapper.`class`.*
@@ -52,6 +51,10 @@ class ThrowsExceptionLineMarkerProvider : AbstractSafeRelatedItemLineMarkerProvi
5251
type: String,
5352
isRuntimeException: Boolean
5453
): RelatedItemLineMarkerInfo<PsiElement> {
54+
55+
val exceptionIcon: Icon = IconLoader.getIcon("/icons/exception.svg", Companion::class.java)
56+
57+
5558
@Language("html")
5659
val runtimeExceptionText: String = when (isRuntimeException) {
5760
true -> "(subtype of <i style=\"color:$iconColorTheme\">RuntimeException</i>)"
@@ -69,8 +72,7 @@ class ThrowsExceptionLineMarkerProvider : AbstractSafeRelatedItemLineMarkerProvi
6972
}
7073

7174
companion object {
72-
val exceptionIcon: Icon = IconLoader.getIcon("/icons/exception.svg", Companion::class.java)
73-
const val iconColorTheme = "#EDA200"
75+
const val iconColorTheme: String = "#EDA200"
7476
}
7577

76-
}
78+
}

src/main/kotlin/csense/idea/kotlin/checked/exceptions/quickfixes/selectors/KtCallExpressionQuickFixSelector.kt

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ package csense.idea.kotlin.checked.exceptions.quickfixes.selectors
33
import com.intellij.codeInspection.*
44
import csense.idea.base.bll.kotlin.*
55
import csense.idea.base.bll.psiWrapper.`class`.*
6+
import csense.idea.kotlin.checked.exceptions.csense.*
67
import csense.idea.kotlin.checked.exceptions.quickfixes.add.*
78
import csense.idea.kotlin.checked.exceptions.quickfixes.wrap.*
89
import csense.idea.kotlin.checked.exceptions.repo.*
910
import csense.idea.kotlin.checked.exceptions.visitors.*
11+
import csense.kotlin.extensions.*
1012
import org.jetbrains.kotlin.psi.*
1113

1214
class KtCallExpressionQuickFixSelector(
@@ -30,38 +32,38 @@ class KtCallExpressionQuickFixSelector(
3032
result: MutableList<LocalQuickFix>
3133
) {
3234
appendQuickFixesForExpression(state, result)
33-
if (state.containingLambdas.isNotEmpty()) {
34-
appendQuickFixesForContainingLambdas(state, result)
35-
} else {
36-
AddThrowsTypeToSelector.tryAddThrowsTypesAnnotations(
37-
parent = state.parentScope,
38-
result = result, uncaughtExceptions = uncaughtExceptions,
39-
kotlinThrowable = kotlinThrowable
40-
)
35+
36+
val firstBlockingLambda: LambdaArgumentLookup? = state.containingLambdas.selectLastOrNull { it: KtLambdaExpression ->
37+
val lookup: LambdaArgumentLookup = it.toLamdaArgumentLookup() ?: return@selectLastOrNull null
38+
val isBlocking: Boolean = !callThoughRepo.isLambdaCallThough(lookup)
39+
return@selectLastOrNull isBlocking.map(ifTrue = lookup, ifFalse = null)
40+
}
41+
42+
if (firstBlockingLambda != null) {
43+
appendQuickFixesForContainingLambdas(result, firstBlockingLambda)
44+
return
4145
}
46+
47+
AddThrowsTypeToSelector.tryAddThrowsTypesAnnotations(
48+
parent = state.parentScope,
49+
result = result, uncaughtExceptions = uncaughtExceptions,
50+
kotlinThrowable = kotlinThrowable
51+
)
4252
}
4353

4454
private fun appendQuickFixesForContainingLambdas(
45-
state: IncrementalExceptionCheckerState,
46-
result: MutableList<LocalQuickFix>
55+
result: MutableList<LocalQuickFix>,
56+
blockingLambda: LambdaArgumentLookup
4757
) {
48-
val lambda: LambdaArgumentLookup = getLambdaFromStateOrNull(state) ?: return
49-
50-
if (!ignoreRepo.isLambdaIgnoreExceptions(lambda)) {
51-
result += AddLambdaToIgnoreQuickFix(lambda)
58+
if (!ignoreRepo.isLambdaIgnoreExceptions(blockingLambda)) {
59+
result += AddLambdaToIgnoreQuickFix(blockingLambda)
5260
}
53-
if (!callThoughRepo.isLambdaCallThough(lambda)) {
54-
result += AddLambdaToCallthoughQuickFix(lambda)
61+
if(!callThoughRepo.isLambdaCallThough(blockingLambda)) {
62+
result += AddLambdaToCallthoughQuickFix(blockingLambda)
5563
}
5664
}
5765

58-
private fun getLambdaFromStateOrNull(
59-
state: IncrementalExceptionCheckerState
60-
): LambdaArgumentLookup? {
61-
return state.containingLambdas.lastOrNull()?.toLamdaArgumentLookup()
62-
}
63-
64-
private fun appendQuickFixesForExpression(
66+
private fun appendQuickFixesForExpression(
6567
state: IncrementalExceptionCheckerState,
6668
result: MutableList<LocalQuickFix>
6769
) {

src/main/kotlin/csense/idea/kotlin/checked/exceptions/visitors/IncrementalExceptionCheckerVisitor.kt

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,24 @@ class IncrementalExceptionCheckerVisitor(
262262
}
263263
}
264264

265+
override fun visitCallableReferenceExpression(
266+
expression: KtCallableReferenceExpression,
267+
state: IncrementalExceptionCheckerState?
268+
): Void? {
269+
val currentState: IncrementalExceptionCheckerState = state.newStateByAppending()
270+
271+
val thrownExceptions: List<KtPsiClass> = expression
272+
.resolveKtPsiFunctionOrNull()
273+
?.throwsTypesForCallBySettingsOrEmpty() ?: listOf()
274+
275+
findIssuesAndReport(
276+
expression = expression,
277+
currentState = currentState,
278+
potentialExceptions = thrownExceptions
279+
)
280+
return super.visitCallableReferenceExpression(expression, state)
281+
}
282+
265283
private fun IncrementalExceptionCheckerState?.newStateByAppending(
266284
newCaptures: List<KtPsiClass> = listOf(),
267285
newThrows: List<KtPsiClass> = listOf(),
@@ -290,7 +308,7 @@ data class IncrementalExceptionCheckerState(
290308
val parentScope: KtElement? = null
291309
) {
292310
companion object {
293-
val empty = IncrementalExceptionCheckerState(
311+
val empty: IncrementalExceptionCheckerState = IncrementalExceptionCheckerState(
294312
captures = emptyList(),
295313
throwsTypes = emptyList(),
296314
containingLambdas = emptyList(),

todo.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
- handle "*arrayOf(exception::class, exception2::class)" ....
66
- come up with something for init that can throw...
77
- SAM function(s) might not be working
8-
- if (ignore, callthough) does not exists?
98
- improve documentation parsing / handling
109
- improve the "source" target when finding e.g.
1110
- settings for the colors etc. of the hints.

0 commit comments

Comments
 (0)