Skip to content
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

Fix incorrect return value in StaticImportFavoritesCompletionInvoker. #970

Merged
merged 1 commit into from
Dec 4, 2023

Conversation

rgrunber
Copy link
Contributor

@rgrunber rgrunber commented Dec 4, 2023

  • Helper method createNewCompilationUnit should always return an array.

@robstryker adopted the latest 4.31-I-builds at eclipse-jdtls/eclipse.jdt.ls#2947, and they uncovered a regression in the organize imports logic : https://ci.eclipse.org/ls/job/jdt-ls-pr/4816/ when triggered in a module file.

java.lang.NullPointerException: Cannot load from object array because "res" is null
	at org.eclipse.jdt.internal.corext.util.StaticImportFavoritesCompletionInvoker.getStaticImportFavorites(StaticImportFavoritesCompletionInvoker.java:54)
	at org.eclipse.jdt.core.manipulation.OrganizeImportsOperation.addStaticImports(OrganizeImportsOperation.java:722)
	at org.eclipse.jdt.core.manipulation.OrganizeImportsOperation.createTextEdit(OrganizeImportsOperation.java:621)
	at org.eclipse.jdt.ls.core.internal.handlers.OrganizeImportsHandler.organizeImports(OrganizeImportsHandler.java:98)
	at org.eclipse.jdt.ls.core.internal.handlers.OrganizeImportsHandler$1.addEdits(OrganizeImportsHandler.java:142)
	at org.eclipse.jdt.core.manipulation.CUCorrectionProposalCore.createTextChange(CUCorrectionProposalCore.java:188)
	at org.eclipse.jdt.core.manipulation.CUCorrectionProposalCore.createChange(CUCorrectionProposalCore.java:195)
	at org.eclipse.jdt.core.manipulation.ChangeCorrectionProposalCore.getChange(ChangeCorrectionProposalCore.java:152)
	at org.eclipse.jdt.core.manipulation.CUCorrectionProposalCore.getTextChange(CUCorrectionProposalCore.java:206)

Introduced by #927 (in 4.31-I-builds).

Before:

StringBuilder dummyCU= new StringBuilder();
String packName= cu.getParent().getElementName();
IType type= cu.findPrimaryType();
if (type == null)
return new String[0];

After:

StringBuilder dummyCU= new StringBuilder();
String packName= fCu.getParent().getElementName();
IType type= fCu.findPrimaryType();
if (type == null)
return null;

The fix is straightforward.

Update: I've added a testcase. Given that the PR that introduced the regression passed when merged, it's safe to say no tests are triggering "Organize Imports" in module files, so I've added the one we use in JDT-LS.

- Helper method createNewCompilationUnit should always return an array.

Signed-off-by: Roland Grunberg <[email protected]>
@rgrunber rgrunber force-pushed the static-import-method-issue branch from 3181cb0 to bd29ac8 Compare December 4, 2023 21:22
@robstryker
Copy link
Contributor

Patch looks clean to me. Original PR seems harder to parse but the fix looks obvious with clear and concise solution.

@rgrunber rgrunber added this to the 4.31 M1 milestone Dec 4, 2023
@rgrunber rgrunber merged commit d0421bf into eclipse-jdt:master Dec 4, 2023
7 checks passed
@rgrunber rgrunber deleted the static-import-method-issue branch December 4, 2023 22:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants