ADFA-3119 | Fix crash when generating setters and getters#1041
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 Walkthrough
WalkthroughCentralized error handling in GenerateSettersAndGettersAction via a new private helper and added try/catch around editor insert/format operations; EditHelper.insertAtEndOfClass now validates Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 3
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In
`@lsp/java/src/main/java/com/itsaky/androidide/lsp/java/actions/generators/GenerateSettersAndGettersAction.kt`:
- Around line 71-76: Always call log.error with the Throwable regardless of
whether a Context is available, and only execute the UI toast when Context is
non-null; update showErrorMessage so it unconditionally logs the error via
log.error("Unable to generate setters and getters", error) and wraps the
ThreadUtils.runOnUiThread { flashError(...) } block inside a conditional that
checks context != null (or change callers to call showErrorMessage with a
nullable Context and have it handle the null case). Apply the same change to the
other usages mentioned (the other call sites around
GenerateSettersAndGettersAction where showErrorMessage is skipped) so
diagnostics are always logged while toasts remain conditional.
- Line 141: The catch block in GenerateSettersAndGettersAction (inside the
method that builds/patches getters/setters) is too broad; change the handler
from catch (e: Exception) to catch (e: StringIndexOutOfBoundsException) so only
the reported string index error is caught, and preserve the existing
null-check/logging behavior (i.e., keep the same body that checks e?.message or
logs the exception). Ensure other exceptions continue to propagate (remove or
avoid a broad fallback catch) so unrelated failures are not masked.
In `@lsp/java/src/main/java/com/itsaky/androidide/lsp/java/utils/EditHelper.java`:
- Around line 179-187: The current code in EditHelper returns new Position(0,0)
when end < 0 which causes silent insertion at file start; change the early
return to signal failure instead (e.g., return null or Optional.empty) from the
method in EditHelper (the method that computes Position from the class-end 'end'
value) and update all call sites listed (CreateMissingMethod.java,
ImplementAbstractMethods.java, OverrideSuperclassMethodsAction.kt,
GenerateRecordConstructor.java) to check for that failure and abort the edit
generation (do not create an edit if the Position is absent). Ensure method
signature and all callers are updated consistently so malformed class-end
metadata results in no edit rather than insertion at (0,0).
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: de08502b-e425-4611-909d-89bc1301bb38
📒 Files selected for processing (2)
lsp/java/src/main/java/com/itsaky/androidide/lsp/java/actions/generators/GenerateSettersAndGettersAction.ktlsp/java/src/main/java/com/itsaky/androidide/lsp/java/utils/EditHelper.java
b3f374f to
4b37265
Compare
Add safe coordinate bounds in EditHelper and try-catch fallback with UI error message.
4b37265 to
81bf22b
Compare
* fix: prevent StringIndexOutOfBoundsException in setter/getter generator Add safe coordinate bounds in EditHelper and try-catch fallback with UI error message. * refactor: refine error handling for setter/getter generator
Description
Fixed a
StringIndexOutOfBoundsExceptionthat caused the application to crash when generating setters and getters on files with specific formatting. Added bounds checking inEditHelper.javato ensure calculated line and column indices are never negative. Additionally, wrapped the editor text insertion inGenerateSettersAndGettersAction.ktwith a try-catch block to gracefully handle any remaining insertion errors and display a user-friendly UI error message.Details
Math.max(0, ...)ininsertAtEndOfClassto prevent negative column or line values.showErrorMessagefunction to handle UI error flashing consistently.editor.text.insertoperation in a try-catch block to prevent runtime crashes.Before fix
Before.fix.mov
After fix
After.fix.mov
Ticket
ADFA-3119
Observation
The root cause of the crash was that the AST end position could yield
0or1for the column number depending on the file's formatting or missing closing braces. This resulted in a negative index when theEditHelperblindly subtracted values to find the insertion point. The UI fallback now ensures the user is informed if the code structure prevents generation.