Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Catch java.lang.NoSuchFieldError when looking for WHEN_ENTRY_GUARD in kotlin version 2.0.1 #2857

Merged
merged 1 commit into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all 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,7 +103,7 @@ internal class RuleExecutionContext private constructor(
} else {
executeRuleWithoutAutocorrectApproveHandlerOnNodeRecursively(node, rule, autocorrectHandler, emitAndApprove)
}
} catch (e: Exception) {
} catch (e: Throwable) {
if (autocorrectHandler is NoneAutocorrectHandler) {
val (line, col) = positionInTextLocator(node.startOffset)
throw RuleExecutionException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ public class TrailingCommaOnDeclarationSiteRule :
val trailingCommaNode = prevLeaf?.findPreviousTrailingCommaNodeOrNull()
val trailingCommaState =
when {
hasWhenGuard() -> {
hasWhenEntryGuard() -> {
// The compiler won't allow any comma in the when-entry in case it contains a guard clause
TrailingCommaState.NOT_EXISTS
}
Expand Down Expand Up @@ -444,7 +444,16 @@ public class TrailingCommaOnDeclarationSiteRule :
return codeLeaf?.takeIf { it.elementType == COMMA }
}

private fun ASTNode.hasWhenGuard() = elementType == WHEN_ENTRY && children().any { it.elementType == WHEN_ENTRY_GUARD }
private fun ASTNode.hasWhenEntryGuard() = elementType == WHEN_ENTRY && hasWhenEntryGuardKotlin21()

private fun ASTNode.hasWhenEntryGuardKotlin21(): Boolean =
// TODO: Remove try-catch wrapping once kotlin version is upgraded to 2.1 or above
try {
children().any { it.elementType == WHEN_ENTRY_GUARD }
} catch (e: NoSuchFieldError) {
// Prior to Kotlin 2.1 the WHEN_ENTRY_GUARD can not be retrieved successfully.
false
}

private fun containsLineBreakInLeavesRange(
from: PsiElement,
Expand Down