-
Notifications
You must be signed in to change notification settings - Fork 3
[#164] fix error found in #164 #165
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
❌ 1 Tests Failed:
View the top 1 failed test(s) by shortest run time
To view more test analytics, go to the Test Analytics Dashboard |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR fixes an error found in issue #164 by enhancing error handling in the import order processor. The main purpose is to provide better diagnostics when the Eclipse JDT parser encounters formatting errors during import rewriting operations.
Key changes:
- Added
Pathparameter toImportOrderProcessor.rewriteImportsIfAny()to track which file is being processed - Introduced
ImportOrderProcessorExceptionto wrap formatting errors with file context - Refactored test utilities to use a
TestResourcerecord that bundles path and contents together
Reviewed Changes
Copilot reviewed 10 out of 10 changed files in this pull request and generated 8 comments.
Show a summary per file
| File | Description |
|---|---|
| cli/src/main/java/io/github/bmarwell/jfmt/imports/ImportOrderProcessorException.java | New exception class to wrap import formatting errors with file path context |
| cli/src/main/java/io/github/bmarwell/jfmt/commands/ImportOrderProcessor.java | Added Path parameter to track file being processed and enhanced error handling with try-catch for IllegalArgumentException |
| cli/src/main/java/io/github/bmarwell/jfmt/commands/AbstractCommand.java | Updated method call to pass javaFile path to ImportOrderProcessor |
| cli/src/test/java/io/github/bmarwell/jfmt/imports/ImportOrderTestUtil.java | Introduced TestResource record to bundle path and contents; refactored loadTestResource to return this record |
| cli/src/test/java/io/github/bmarwell/jfmt/imports/ImportOrderProcessorTestBase.java | Updated to use TestResource record and pass path to ImportOrderProcessor |
| cli/src/test/java/io/github/bmarwell/jfmt/imports/Issue164Test.java | New test case verifying import ordering for the Issue164TestDatasource test resource |
| cli/src/test/java/io/github/bmarwell/jfmt/imports/FallbackMatchesDefaultTest.java | Updated to use TestResource record and pass path to ImportOrderProcessor |
| cli/src/test/java/io/github/bmarwell/jfmt/imports/EmptyConfigurationImportOrderTest.java | Updated to use TestResource record and pass path to ImportOrderProcessor |
| cli/src/test/java/io/github/bmarwell/jfmt/imports/ApacheImportOrderTest.java | Updated to use simplified test resource loading through ImportOrderTestUtil |
| cli/src/test/resources/imports/Issue164TestDatasource.java | Test resource file with mixed imports to validate formatting behavior for issue #164 |
|
|
||
| import java.nio.file.Path; | ||
| import java.util.StringJoiner; | ||
|
|
Copilot
AI
Nov 17, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The exception class lacks documentation. Add a JavaDoc comment describing when this exception is thrown and what it represents.
Example:
/**
* Exception thrown when the import order processor encounters an error
* while rewriting imports in a Java source file.
*/
public class ImportOrderProcessorException extends RuntimeException {| /** | |
| * Exception thrown when the import order processor encounters an error | |
| * while rewriting imports in a Java source file. | |
| * <p> | |
| * This exception provides the path of the file being processed and the underlying cause. | |
| */ |
|
|
||
| public class ImportOrderProcessorException extends RuntimeException { | ||
| private final Path path; | ||
|
|
Copilot
AI
Nov 17, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The constructor lacks documentation. Add a JavaDoc comment describing the parameters.
Example:
/**
* Creates a new import order processor exception.
*
* @param path the path to the Java file being processed
* @param message the error message
* @param cause the underlying cause of the error
*/
public ImportOrderProcessorException(Path path, String message, Throwable cause) {| /** | |
| * Creates a new import order processor exception. | |
| * | |
| * @param path the path to the Java file being processed | |
| * @param message the error message | |
| * @param cause the underlying cause of the error | |
| */ |
| javaFile.toAbsolutePath(), | ||
| importStart, | ||
| importEnd, | ||
| workingDoc.get().substring(importStart, importEnd) |
Copilot
AI
Nov 17, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The substring call workingDoc.get().substring(importStart, importEnd) may throw StringIndexOutOfBoundsException if importEnd exceeds the document length after the workingDoc.replace() call on line 180. This can happen because the document has already been modified before this error handling code runs.
Consider using a safer approach:
int docLength = workingDoc.get().length();
int safeEnd = Math.min(importEnd, docLength);
workingDoc.get().substring(importStart, safeEnd)Or capture the original substring before the replace operation if that's what you intend to show in the error message.
| workingDoc.get().substring(importStart, importEnd) | |
| workingDoc.get().substring(importStart, Math.min(importEnd, workingDoc.get().length())) |
| } catch (IllegalArgumentException jdtEx) { | ||
| String message = String.format( | ||
| Locale.ROOT, | ||
| "Problem formatting file [%s]. Range: [%d-%d]. Rendered old: >>>\n%s\n<<<", | ||
| javaFile.toAbsolutePath(), | ||
| importStart, | ||
| importEnd, | ||
| workingDoc.get().substring(importStart, importEnd) | ||
| ); | ||
|
|
||
| throw new ImportOrderProcessorException(javaFile, message, jdtEx); | ||
| } |
Copilot
AI
Nov 17, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The exception handling for IllegalArgumentException lacks test coverage. Consider adding a test case that triggers this error path to ensure the ImportOrderProcessorException is thrown correctly with the expected error message and context.
This would help verify:
- The exception message format is correct
- The file path is properly included in the exception
- The original
IllegalArgumentExceptionis wrapped as the cause
| record TestResource(Path path, String contents) {} | ||
|
|
Copilot
AI
Nov 17, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The TestResource record lacks documentation. Add a JavaDoc comment explaining its purpose and the meaning of its components.
Example:
/**
* Represents a loaded test resource with its path and contents.
*
* @param path the resource path relative to the classpath
* @param contents the file contents as a string
*/
record TestResource(Path path, String contents) {}| record TestResource(Path path, String contents) {} |
|
|
||
| /** | ||
| * Rewrites the imports in the provided document according to the configured order. | ||
| * If there are no imports, this method is a no-op. |
Copilot
AI
Nov 17, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The JavaDoc for rewriteImportsIfAny method is incomplete. It should document the new javaFile parameter and the new ImportOrderProcessorException that can be thrown.
Suggested update:
/**
* Rewrites the imports in the provided document according to the configured order.
* If there are no imports, this method is a no-op.
*
* @param javaFile
* the path to the Java source file being processed
* @param compilationUnit
* the parsed compilation unit containing the imports
* @param workingDoc
* the document to modify
* @throws BadLocationException
* if the import block cannot be replaced at the calculated position
* @throws ImportOrderProcessorException
* if the import rewrite operation fails
*/| * If there are no imports, this method is a no-op. | |
| * If there are no imports, this method is a no-op. | |
| * | |
| * @param javaFile | |
| * the path to the Java source file being processed | |
| * @param compilationUnit | |
| * the parsed compilation unit containing the imports | |
| * @param workingDoc | |
| * the document to modify | |
| * @throws BadLocationException | |
| * if the import block cannot be replaced at the calculated position | |
| * @throws ImportOrderProcessorException | |
| * if the import rewrite operation fails |
| /** | ||
| * | ||
| * @param javaFile | ||
| * @param compilationUnit | ||
| * @param workingDoc | ||
| * @param rendered | ||
| * the newly rendered import block | ||
| * @throws BadLocationException |
Copilot
AI
Nov 17, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
JavaDoc parameter descriptions are missing. The @param tags should include descriptions for what each parameter represents, not just the parameter name.
Suggested format:
/**
* Replaces the import block in the document.
*
* @param javaFile
* the path to the Java source file being processed
* @param compilationUnit
* the parsed compilation unit containing the imports
* @param workingDoc
* the document being modified
* @param rendered
* the newly rendered import block
* @throws BadLocationException
* if the import block cannot be replaced at the calculated position
*/| /** | |
| * | |
| * @param javaFile | |
| * @param compilationUnit | |
| * @param workingDoc | |
| * @param rendered | |
| * the newly rendered import block | |
| * @throws BadLocationException | |
| /** | |
| * Replaces the import block in the document. | |
| * | |
| * @param javaFile | |
| * the path to the Java source file being processed | |
| * @param compilationUnit | |
| * the parsed compilation unit containing the imports | |
| * @param workingDoc | |
| * the document being modified | |
| * @param rendered | |
| * the newly rendered import block | |
| * @throws BadLocationException | |
| * if the import block cannot be replaced at the calculated position |
|
Blocked by: eclipse-jdt/eclipse.jdt.core#4128 |
No description provided.