-
Notifications
You must be signed in to change notification settings - Fork 32
Session bean class must not declare a finalize() method Diagnostic and Quickfix #1628
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
Open
venmanyarun
wants to merge
2
commits into
OpenLiberty:lsp4jakarta-0.2.7-release-branch
Choose a base branch
from
venmanyarun:session_ejb_finalize_diag_quickfix
base: lsp4jakarta-0.2.7-release-branch
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
...enliberty/tools/intellij/lsp4jakarta/lsp4ij/codeAction/proposal/RemoveMethodProposal.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| /******************************************************************************* | ||
| * Copyright (c) 2026 IBM Corporation and others. | ||
| * | ||
| * This program and the accompanying materials are made available under the | ||
| * terms of the Eclipse Public License v. 2.0 which is available at | ||
| * http://www.eclipse.org/legal/epl-2.0. | ||
| * | ||
| * SPDX-License-Identifier: EPL-2.0 | ||
| * | ||
| * Contributors: | ||
| * IBM Corporation - initial API and implementation | ||
| *******************************************************************************/ | ||
|
|
||
| package io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.codeAction.proposal; | ||
|
|
||
| import com.intellij.psi.PsiElement; | ||
| import com.intellij.psi.PsiFile; | ||
| import com.intellij.psi.PsiModifierListOwner; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| /** | ||
| * Code action proposal for removing methods from a class. | ||
| */ | ||
| public class RemoveMethodProposal extends RemoveElementsProposal { | ||
|
|
||
| /** | ||
| * Constructor for RemoveMethodProposal. | ||
| * | ||
| * @param label The label for this proposal | ||
| * @param sourceCU The source compilation unit | ||
| * @param invocationNode The invocation node | ||
| * @param binding The modifier list owner (the class) | ||
| * @param relevance The relevance score | ||
| * @param methodsToRemove List of methods to remove | ||
| */ | ||
| public RemoveMethodProposal( | ||
| String label, | ||
| PsiFile sourceCU, | ||
| PsiFile invocationNode, | ||
| PsiModifierListOwner binding, | ||
| int relevance, | ||
| List<? extends PsiElement> methodsToRemove) { | ||
| super(label, sourceCU, invocationNode, binding, relevance, methodsToRemove); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
...va/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/ejb/RemoveFinalizeMethodQuickFix.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| /******************************************************************************* | ||
| * Copyright (c) 2026 IBM Corporation and others. | ||
| * | ||
| * This program and the accompanying materials are made available under the | ||
| * terms of the Eclipse Public License v. 2.0 which is available at | ||
| * http://www.eclipse.org/legal/epl-2.0. | ||
| * | ||
| * SPDX-License-Identifier: EPL-2.0 | ||
| * | ||
| * Contributors: | ||
| * IBM Corporation - initial API and implementation | ||
| *******************************************************************************/ | ||
|
|
||
| package io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.ejb; | ||
|
|
||
| import com.intellij.psi.PsiClass; | ||
| import com.intellij.psi.PsiElement; | ||
| import com.intellij.psi.PsiMethod; | ||
| import com.intellij.psi.util.PsiTreeUtil; | ||
| import io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.JDTUtils; | ||
| import io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.Messages; | ||
| import io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.codeAction.proposal.RemoveMethodProposal; | ||
|
|
||
| import static io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.ejb.EjbConstants.FINALIZE_METHOD_NAME; | ||
| import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.java.codeaction.IJavaCodeActionParticipant; | ||
| import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.java.codeaction.JavaCodeActionContext; | ||
| import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.java.codeaction.JavaCodeActionResolveContext; | ||
| import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.java.corrections.proposal.ChangeCorrectionProposal; | ||
| import io.openliberty.tools.intellij.util.ExceptionUtil; | ||
| import org.eclipse.lsp4j.CodeAction; | ||
| import org.eclipse.lsp4j.Diagnostic; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import java.util.logging.Logger; | ||
|
|
||
| /** | ||
| * QuickFix for removing the finalize() method from session beans. | ||
| */ | ||
| public class RemoveFinalizeMethodQuickFix implements IJavaCodeActionParticipant { | ||
|
|
||
| private static final String NAME = Messages.getMessage("RemoveFinalizeMethod"); | ||
| private static final Logger LOGGER = Logger.getLogger(RemoveFinalizeMethodQuickFix.class.getName()); | ||
|
|
||
| @Override | ||
| public String getParticipantId() { | ||
| return RemoveFinalizeMethodQuickFix.class.getName(); | ||
| } | ||
|
|
||
| @Override | ||
| public List<? extends CodeAction> getCodeActions(JavaCodeActionContext context, Diagnostic diagnostic) { | ||
| List<CodeAction> codeActions = new ArrayList<>(); | ||
| final PsiElement node = context.getCoveredNode(); | ||
| final PsiMethod parentMethod = PsiTreeUtil.getParentOfType(node, PsiMethod.class); | ||
|
|
||
| if (parentMethod != null && FINALIZE_METHOD_NAME.equals(parentMethod.getName()) && | ||
| parentMethod.getParameterList().getParametersCount() == 0) { | ||
| codeActions.add(JDTUtils.createCodeAction(context, diagnostic, NAME, getParticipantId())); | ||
| } | ||
| return codeActions; | ||
| } | ||
|
|
||
| @Override | ||
| public CodeAction resolveCodeAction(JavaCodeActionResolveContext context) { | ||
| final CodeAction toResolve = context.getUnresolved(); | ||
| final PsiElement node = context.getCoveredNode(); | ||
| final PsiClass parentType = PsiTreeUtil.getParentOfType(node, PsiClass.class); | ||
| final PsiMethod parentMethod = PsiTreeUtil.getParentOfType(node, PsiMethod.class); | ||
|
|
||
| if (parentMethod != null && FINALIZE_METHOD_NAME.equals(parentMethod.getName())) { | ||
| ChangeCorrectionProposal proposal = new RemoveMethodProposal(NAME, | ||
| context.getSource().getCompilationUnit(), | ||
| context.getASTRoot(), | ||
| parentType, | ||
| 0, | ||
| Collections.singletonList(parentMethod)); | ||
|
|
||
| ExceptionUtil.executeWithWorkspaceEditHandling(context, proposal, toResolve, LOGGER, | ||
| "Unable to create workspace edit for removing finalize() method"); | ||
| } | ||
| return toResolve; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
171 changes: 171 additions & 0 deletions
171
src/test/java/io/openliberty/tools/intellij/lsp4jakarta/it/ejb/SessionBeanFinalizeTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,171 @@ | ||
| /******************************************************************************* | ||
| * Copyright (c) 2026 IBM Corporation and others. | ||
| * | ||
| * This program and the accompanying materials are made available under the | ||
| * terms of the Eclipse Public License v. 2.0 which is available at | ||
| * http://www.eclipse.org/legal/epl-2.0. | ||
| * | ||
| * SPDX-License-Identifier: EPL-2.0 | ||
| * | ||
| * Contributors: | ||
| * IBM Corporation - initial API and implementation | ||
| *******************************************************************************/ | ||
|
|
||
| package io.openliberty.tools.intellij.lsp4jakarta.it.ejb; | ||
|
|
||
| import com.intellij.openapi.module.Module; | ||
| import com.intellij.openapi.module.ModuleUtilCore; | ||
| import com.intellij.openapi.vfs.LocalFileSystem; | ||
| import com.intellij.openapi.vfs.VfsUtilCore; | ||
| import com.intellij.openapi.vfs.VirtualFile; | ||
| import io.openliberty.tools.intellij.lsp4jakarta.it.core.BaseJakartaTest; | ||
| import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.utils.IPsiUtils; | ||
| import io.openliberty.tools.intellij.lsp4mp4ij.psi.internal.core.ls.PsiUtilsLSImpl; | ||
| import org.eclipse.lsp4j.CodeAction; | ||
| import org.eclipse.lsp4j.Diagnostic; | ||
| import org.eclipse.lsp4j.DiagnosticSeverity; | ||
| import org.eclipse.lsp4j.TextEdit; | ||
| import org.eclipse.lsp4jakarta.commons.JakartaJavaCodeActionParams; | ||
| import org.eclipse.lsp4jakarta.commons.JakartaJavaDiagnosticsParams; | ||
| import org.junit.Test; | ||
| import org.junit.runner.RunWith; | ||
| import org.junit.runners.JUnit4; | ||
|
|
||
| import java.io.File; | ||
| import java.util.Arrays; | ||
|
|
||
| import static io.openliberty.tools.intellij.lsp4jakarta.it.core.JakartaForJavaAssert.*; | ||
|
|
||
| @RunWith(JUnit4.class) | ||
| public class SessionBeanFinalizeTest extends BaseJakartaTest { | ||
|
|
||
| @Test | ||
| public void testInvalidStatelessBeanFinalize() throws Exception { | ||
| Module module = createMavenModule(new File("src/test/resources/projects/maven/jakarta-sample")); | ||
| IPsiUtils utils = PsiUtilsLSImpl.getInstance(getProject()); | ||
|
|
||
| VirtualFile javaFile = LocalFileSystem.getInstance().refreshAndFindFileByPath(ModuleUtilCore.getModuleDirPath(module) | ||
| + "/src/main/java/io/openliberty/sample/jakarta/ejb/InvalidStatelessBeanFinalize.java"); | ||
| String uri = VfsUtilCore.virtualToIoFile(javaFile).toURI().toString(); | ||
|
|
||
| JakartaJavaDiagnosticsParams diagnosticsParams = new JakartaJavaDiagnosticsParams(); | ||
| diagnosticsParams.setUris(Arrays.asList(uri)); | ||
|
|
||
| // Test expected diagnostic | ||
| Diagnostic expectedDiagnostic = d(18, 19, 27, | ||
| "Session bean classes must not override or define the finalize() method.", | ||
| DiagnosticSeverity.Error, "jakarta-ejb", "SessionBeanFinalizeMethod"); | ||
|
|
||
| assertJavaDiagnostics(diagnosticsParams, utils, expectedDiagnostic); | ||
|
|
||
| // Test expected quick-fix | ||
| JakartaJavaCodeActionParams codeActionParams = createCodeActionParams(uri, expectedDiagnostic); | ||
| String newText = "package io.openliberty.sample.jakarta.ejb;" + | ||
| "\n\nimport jakarta.ejb.Stateless;" + | ||
| "\n\n@Stateless" + | ||
| "\npublic class InvalidStatelessBeanFinalize {" + | ||
| "\n private String data;" + | ||
| "\n\n public String getData() {" + | ||
| "\n return data;" + | ||
| "\n }" + | ||
| "\n\n public void setData(String data) {" + | ||
| "\n this.data = data;" + | ||
| "\n }" + | ||
| "\n\n}"; | ||
| TextEdit expectedTextEdit = te(0, 0, 22, 1, newText); | ||
| CodeAction expectedCodeAction = ca(uri, "Remove the finalize() method", expectedDiagnostic, expectedTextEdit); | ||
| assertJavaCodeAction(codeActionParams, utils, expectedCodeAction); | ||
| } | ||
|
|
||
| @Test | ||
| public void testInvalidStatefulBeanFinalize() throws Exception { | ||
| Module module = createMavenModule(new File("src/test/resources/projects/maven/jakarta-sample")); | ||
| IPsiUtils utils = PsiUtilsLSImpl.getInstance(getProject()); | ||
|
|
||
| VirtualFile javaFile = LocalFileSystem.getInstance().refreshAndFindFileByPath(ModuleUtilCore.getModuleDirPath(module) | ||
| + "/src/main/java/io/openliberty/sample/jakarta/ejb/InvalidStatefulBeanFinalize.java"); | ||
| String uri = VfsUtilCore.virtualToIoFile(javaFile).toURI().toString(); | ||
|
|
||
| JakartaJavaDiagnosticsParams diagnosticsParams = new JakartaJavaDiagnosticsParams(); | ||
| diagnosticsParams.setUris(Arrays.asList(uri)); | ||
|
|
||
| // Test expected diagnostic | ||
| Diagnostic expectedDiagnostic = d(18, 19, 27, | ||
| "Session bean classes must not override or define the finalize() method.", | ||
| DiagnosticSeverity.Error, "jakarta-ejb", "SessionBeanFinalizeMethod"); | ||
|
|
||
| assertJavaDiagnostics(diagnosticsParams, utils, expectedDiagnostic); | ||
|
|
||
| // Test expected quick-fix | ||
| JakartaJavaCodeActionParams codeActionParams = createCodeActionParams(uri, expectedDiagnostic); | ||
| String newText = "package io.openliberty.sample.jakarta.ejb;" + | ||
| "\n\nimport jakarta.ejb.Stateful;" + | ||
| "\n\n@Stateful" + | ||
| "\npublic class InvalidStatefulBeanFinalize {" + | ||
| "\n private int count;" + | ||
| "\n\n public int getCount() {" + | ||
| "\n return count;" + | ||
| "\n }" + | ||
| "\n\n public void setCount(int count) {" + | ||
| "\n this.count = count;" + | ||
| "\n }" + | ||
| "\n\n}"; | ||
| TextEdit expectedTextEdit = te(0, 0, 22, 1, newText); | ||
| CodeAction expectedCodeAction = ca(uri, "Remove the finalize() method", expectedDiagnostic, expectedTextEdit); | ||
| assertJavaCodeAction(codeActionParams, utils, expectedCodeAction); | ||
| } | ||
|
|
||
| @Test | ||
| public void testInvalidSingletonBeanFinalize() throws Exception { | ||
| Module module = createMavenModule(new File("src/test/resources/projects/maven/jakarta-sample")); | ||
| IPsiUtils utils = PsiUtilsLSImpl.getInstance(getProject()); | ||
|
|
||
| VirtualFile javaFile = LocalFileSystem.getInstance().refreshAndFindFileByPath(ModuleUtilCore.getModuleDirPath(module) | ||
| + "/src/main/java/io/openliberty/sample/jakarta/ejb/InvalidSingletonBeanFinalize.java"); | ||
| String uri = VfsUtilCore.virtualToIoFile(javaFile).toURI().toString(); | ||
|
|
||
| JakartaJavaDiagnosticsParams diagnosticsParams = new JakartaJavaDiagnosticsParams(); | ||
| diagnosticsParams.setUris(Arrays.asList(uri)); | ||
|
|
||
| // Test expected diagnostic | ||
| Diagnostic expectedDiagnostic = d(18, 19, 27, | ||
| "Session bean classes must not override or define the finalize() method.", | ||
| DiagnosticSeverity.Error, "jakarta-ejb", "SessionBeanFinalizeMethod"); | ||
|
|
||
| assertJavaDiagnostics(diagnosticsParams, utils, expectedDiagnostic); | ||
|
|
||
| // Test expected quick-fix | ||
| JakartaJavaCodeActionParams codeActionParams = createCodeActionParams(uri, expectedDiagnostic); | ||
| String newText = "package io.openliberty.sample.jakarta.ejb;" + | ||
| "\n\nimport jakarta.ejb.Singleton;" + | ||
| "\n\n@Singleton" + | ||
| "\npublic class InvalidSingletonBeanFinalize {" + | ||
| "\n private String config;" + | ||
| "\n\n public String getConfig() {" + | ||
| "\n return config;" + | ||
| "\n }" + | ||
| "\n\n public void setConfig(String config) {" + | ||
| "\n this.config = config;" + | ||
| "\n }" + | ||
| "\n\n}"; | ||
| TextEdit expectedTextEdit = te(0, 0, 22, 1, newText); | ||
| CodeAction expectedCodeAction = ca(uri, "Remove the finalize() method", expectedDiagnostic, expectedTextEdit); | ||
| assertJavaCodeAction(codeActionParams, utils, expectedCodeAction); | ||
| } | ||
|
|
||
| @Test | ||
| public void testValidStatelessBeanNoFinalize() throws Exception { | ||
| Module module = createMavenModule(new File("src/test/resources/projects/maven/jakarta-sample")); | ||
| IPsiUtils utils = PsiUtilsLSImpl.getInstance(getProject()); | ||
|
|
||
| VirtualFile javaFile = LocalFileSystem.getInstance().refreshAndFindFileByPath(ModuleUtilCore.getModuleDirPath(module) | ||
| + "/src/main/java/io/openliberty/sample/jakarta/ejb/ValidStatelessBeanNoFinalize.java"); | ||
| String uri = VfsUtilCore.virtualToIoFile(javaFile).toURI().toString(); | ||
|
|
||
| JakartaJavaDiagnosticsParams diagnosticsParams = new JakartaJavaDiagnosticsParams(); | ||
| diagnosticsParams.setUris(Arrays.asList(uri)); | ||
|
|
||
| // Should not report any diagnostics for valid bean without finalize() | ||
| assertJavaDiagnostics(diagnosticsParams, utils); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.