From 147e3cdf9986309b22820a8e4ca8655ce253b1a4 Mon Sep 17 00:00:00 2001 From: Michael Glavassevich Date: Thu, 27 Jul 2023 14:54:27 -0400 Subject: [PATCH 01/34] Proof-of-concept for unifying the design for MicroProfile and Jakarta EE code actions. Signed-off-by: Michael Glavassevich --- .../lsp4ij/PropertiesManagerForJakarta.java | 27 +++++++-- .../NonPublicResourceMethodQuickFix.java | 60 +++++++++++++++---- .../psi/core/PropertiesManagerForJava.java | 2 +- .../java/codeaction/CodeActionHandler.java | 8 +++ .../codeaction/JavaCodeActionDefinition.java | 15 ++++- src/main/resources/META-INF/plugin.xml | 15 +++++ 6 files changed, 110 insertions(+), 17 deletions(-) diff --git a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/PropertiesManagerForJakarta.java b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/PropertiesManagerForJakarta.java index 8e5887edf..76f8e8791 100644 --- a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/PropertiesManagerForJakarta.java +++ b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/PropertiesManagerForJakarta.java @@ -22,14 +22,15 @@ import com.intellij.psi.*; import com.intellij.psi.search.GlobalSearchScope; import com.intellij.psi.util.PsiTreeUtil; -import io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.codeAction.JakartaCodeActionHandler; import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.java.diagnostics.DiagnosticsHandler; import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.utils.IPsiUtils; +import io.openliberty.tools.intellij.lsp4mp4ij.psi.internal.core.java.codeaction.CodeActionHandler; import io.openliberty.tools.intellij.lsp4mp4ij.psi.internal.core.ls.PsiUtilsLSImpl; import org.eclipse.lsp4j.CodeAction; import org.eclipse.lsp4j.Position; import org.eclipse.lsp4j.PublishDiagnosticsParams; import org.eclipse.lsp4jakarta.commons.*; +import org.eclipse.lsp4mp.commons.MicroProfileJavaCodeActionParams; import org.eclipse.lsp4mp.commons.MicroProfileJavaDiagnosticsParams; import org.eclipse.lsp4mp.commons.MicroProfileJavaDiagnosticsSettings; import org.slf4j.Logger; @@ -51,12 +52,13 @@ public static PropertiesManagerForJakarta getInstance() { return INSTANCE; } - private JakartaCodeActionHandler codeActionHandler = new JakartaCodeActionHandler(); + private List diagnosticsCollectors = new ArrayList<>(); + private final CodeActionHandler codeActionHandler; private final DiagnosticsHandler diagnosticsHandler; private PropertiesManagerForJakarta() { - codeActionHandler = new JakartaCodeActionHandler(); + codeActionHandler = new CodeActionHandler("jakarta"); diagnosticsHandler = new DiagnosticsHandler("jakarta"); } @@ -346,7 +348,17 @@ private static PsiFile resolveTypeRoot(String uri, Project project) { public List getCodeAction(JakartaJavaCodeActionParams params, IPsiUtils utils) { return ApplicationManager.getApplication().runReadAction((Computable>) () -> { - return codeActionHandler.codeAction(params, utils); + final List unresolvedCodeActions = codeActionHandler.codeAction(adapt(params), utils); + if (unresolvedCodeActions != null) { + final List resolvedCodeActions = new ArrayList<>(unresolvedCodeActions.size()); + unresolvedCodeActions.forEach(unresolvedCodeAction -> { + if (unresolvedCodeAction != null) { + resolvedCodeActions.add(codeActionHandler.resolveCodeAction(unresolvedCodeAction, utils)); + } + }); + return resolvedCodeActions; + } + return null; }); } @@ -356,4 +368,11 @@ private MicroProfileJavaDiagnosticsParams adapt(JakartaDiagnosticsParams params) mpParams.setDocumentFormat(params.getDocumentFormat()); return mpParams; } + + private MicroProfileJavaCodeActionParams adapt(JakartaJavaCodeActionParams params) { + MicroProfileJavaCodeActionParams mpParams = new MicroProfileJavaCodeActionParams(params.getTextDocument(), params.getRange(), params.getContext()); + mpParams.setResourceOperationSupported(params.isResourceOperationSupported()); + mpParams.setResolveSupported(true); + return mpParams; + } } diff --git a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/jax_rs/NonPublicResourceMethodQuickFix.java b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/jax_rs/NonPublicResourceMethodQuickFix.java index c5919256e..3dc541b38 100644 --- a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/jax_rs/NonPublicResourceMethodQuickFix.java +++ b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/jax_rs/NonPublicResourceMethodQuickFix.java @@ -19,13 +19,20 @@ import com.intellij.psi.util.PsiTreeUtil; import io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.Messages; import io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.codeAction.proposal.ModifyModifiersProposal; +import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.java.codeaction.ExtendedCodeAction; +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 org.eclipse.lsp4j.CodeAction; +import org.eclipse.lsp4j.CodeActionKind; import org.eclipse.lsp4j.Diagnostic; +import org.eclipse.lsp4j.WorkspaceEdit; +import org.eclipse.lsp4mp.commons.CodeActionResolveData; -import java.util.Collections; -import java.util.List; +import java.util.*; +import java.util.logging.Level; +import java.util.logging.Logger; /** * Quick fix for ResourceMethodDiagnosticsCollector that uses @@ -34,26 +41,57 @@ * @author Matthew Shocrylas * */ -public class NonPublicResourceMethodQuickFix { +public class NonPublicResourceMethodQuickFix implements IJavaCodeActionParticipant { + + private static final Logger LOGGER = Logger.getLogger(NonPublicResourceMethodQuickFix.class.getName()); private final static String TITLE_MESSAGE = Messages.getMessage("MakeMethodPublic"); + @Override + public String getParticipantId() { + return NonPublicResourceMethodQuickFix.class.getName(); + } + public List getCodeActions(JavaCodeActionContext context, Diagnostic diagnostic) { final PsiElement node = context.getCoveredNode(); - final PsiClass parentType = PsiTreeUtil.getParentOfType(node, PsiClass.class); final PsiMethod parentMethod = PsiTreeUtil.getParentOfType(node, PsiMethod.class); if (parentMethod != null) { - ChangeCorrectionProposal proposal = new ModifyModifiersProposal(TITLE_MESSAGE, context.getSource().getCompilationUnit(), - context.getASTRoot(), parentType, 0, parentMethod.getModifierList(), Collections.singletonList("public")); - - // Convert the proposal to LSP4J CodeAction - CodeAction codeAction = context.convertToCodeAction(proposal, diagnostic); - codeAction.setTitle(TITLE_MESSAGE); - return Collections.singletonList(codeAction); + return Collections.singletonList(createCodeAction(context, diagnostic)); } return Collections.emptyList(); } + @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); + + assert parentMethod != null; + ChangeCorrectionProposal proposal = new ModifyModifiersProposal(TITLE_MESSAGE, context.getSource().getCompilationUnit(), + context.getASTRoot(), parentType, 0, parentMethod.getModifierList(), Collections.singletonList("public")); + try { + WorkspaceEdit we = context.convertToWorkspaceEdit(proposal); + toResolve.setEdit(we); + } catch (Exception e) { + LOGGER.log(Level.WARNING, "Unable to create workspace edit for code action to make method public", e); + } + return toResolve; + } + + private CodeAction createCodeAction(JavaCodeActionContext context, Diagnostic diagnostic) { + ExtendedCodeAction codeAction = new ExtendedCodeAction(TITLE_MESSAGE); + codeAction.setRelevance(0); + codeAction.setDiagnostics(Collections.singletonList(diagnostic)); + codeAction.setKind(CodeActionKind.QuickFix); + codeAction.setData(new CodeActionResolveData(context.getUri(), getParticipantId(), + context.getParams().getRange(), Collections.emptyMap(), + context.getParams().isResourceOperationSupported(), + context.getParams().isCommandConfigurationUpdateSupported())); + return codeAction; + } } diff --git a/src/main/java/io/openliberty/tools/intellij/lsp4mp4ij/psi/core/PropertiesManagerForJava.java b/src/main/java/io/openliberty/tools/intellij/lsp4mp4ij/psi/core/PropertiesManagerForJava.java index 18f3f5e0d..c7cb709e8 100644 --- a/src/main/java/io/openliberty/tools/intellij/lsp4mp4ij/psi/core/PropertiesManagerForJava.java +++ b/src/main/java/io/openliberty/tools/intellij/lsp4mp4ij/psi/core/PropertiesManagerForJava.java @@ -83,7 +83,7 @@ public static PropertiesManagerForJava getInstance() { private final DiagnosticsHandler diagnosticsHandler; private PropertiesManagerForJava() { - this.codeActionHandler = new CodeActionHandler(); + this.codeActionHandler = new CodeActionHandler("mp"); this.diagnosticsHandler = new DiagnosticsHandler("mp"); } diff --git a/src/main/java/io/openliberty/tools/intellij/lsp4mp4ij/psi/internal/core/java/codeaction/CodeActionHandler.java b/src/main/java/io/openliberty/tools/intellij/lsp4mp4ij/psi/internal/core/java/codeaction/CodeActionHandler.java index 713f36344..a6bf1347e 100644 --- a/src/main/java/io/openliberty/tools/intellij/lsp4mp4ij/psi/internal/core/java/codeaction/CodeActionHandler.java +++ b/src/main/java/io/openliberty/tools/intellij/lsp4mp4ij/psi/internal/core/java/codeaction/CodeActionHandler.java @@ -47,6 +47,12 @@ */ public class CodeActionHandler { + private final String group; + + public CodeActionHandler(String group) { + this.group = group; + } + /** * Returns all the code actions applicable for the context given by the * parameters. @@ -98,6 +104,7 @@ public List codeAction(MicroProfileJavaCodeActionParams pa // Get list of code action definition for the given kind List codeActionDefinitions = io.openliberty.tools.intellij.lsp4mp4ij.psi.internal.core.java.codeaction.JavaCodeActionDefinition.EP.extensions() .filter(definition -> definition.isAdaptedForCodeAction(context)) + .filter(definition -> group.equals(definition.getGroup())) .filter(definition -> codeActionKind.equals(definition.getKind())) .collect(Collectors.toList()); if (codeActionDefinitions != null) { @@ -201,6 +208,7 @@ public CodeAction resolveCodeAction(CodeAction unresolved, IPsiUtils utils) { IJavaCodeActionParticipant participant = JavaCodeActionDefinition.EP.extensions() .filter(definition -> unresolved.getKind().startsWith(definition.getKind())) + .filter(definition -> group.equals(definition.getGroup())) .filter(definition -> participantId.equals(definition.getParticipantId())) .findFirst().orElse(null); return participant.resolveCodeAction(context.copy()); diff --git a/src/main/java/io/openliberty/tools/intellij/lsp4mp4ij/psi/internal/core/java/codeaction/JavaCodeActionDefinition.java b/src/main/java/io/openliberty/tools/intellij/lsp4mp4ij/psi/internal/core/java/codeaction/JavaCodeActionDefinition.java index 6d48ae62f..e452bf9f2 100644 --- a/src/main/java/io/openliberty/tools/intellij/lsp4mp4ij/psi/internal/core/java/codeaction/JavaCodeActionDefinition.java +++ b/src/main/java/io/openliberty/tools/intellij/lsp4mp4ij/psi/internal/core/java/codeaction/JavaCodeActionDefinition.java @@ -41,11 +41,15 @@ public class JavaCodeActionDefinition extends BaseKeyedLazyInstance + + From ae06e56a7e2fb272d88ecfe74447243f25611bc9 Mon Sep 17 00:00:00 2001 From: Rahul Biju Date: Wed, 4 Oct 2023 17:13:02 +0530 Subject: [PATCH 02/34] [issue24_refactor_httpservlet]: Refactored HTTPServletQuickFix --- .../lsp4ij/servlet/HttpServletQuickFix.java | 73 +++++++++++++++---- src/main/resources/META-INF/plugin.xml | 4 + 2 files changed, 62 insertions(+), 15 deletions(-) diff --git a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/servlet/HttpServletQuickFix.java b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/servlet/HttpServletQuickFix.java index 338f6a91d..21641d9fb 100644 --- a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/servlet/HttpServletQuickFix.java +++ b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/servlet/HttpServletQuickFix.java @@ -20,13 +20,22 @@ import com.intellij.psi.util.PsiTreeUtil; import io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.Messages; import io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.codeAction.proposal.ExtendClassProposal; +import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.java.codeaction.ExtendedCodeAction; +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 org.eclipse.lsp4j.CodeAction; +import org.eclipse.lsp4j.CodeActionKind; import org.eclipse.lsp4j.Diagnostic; +import org.eclipse.lsp4j.WorkspaceEdit; +import org.eclipse.lsp4mp.commons.CodeActionResolveData; import java.util.ArrayList; +import java.util.Collections; import java.util.List; +import java.util.logging.Level; +import java.util.logging.Logger; /** * QuickFix for fixing HttpServlet extension error by providing the code actions @@ -38,28 +47,62 @@ * @author Credit to Angelo ZERR * */ -public class HttpServletQuickFix { +public class HttpServletQuickFix implements IJavaCodeActionParticipant { + + private static final Logger LOGGER = Logger.getLogger(HttpServletQuickFix.class.getName()); + + @Override + public String getParticipantId() { + return HttpServletQuickFix.class.getName(); + } + public List getCodeActions(JavaCodeActionContext context, Diagnostic diagnostic) { List codeActions = new ArrayList<>(); PsiElement node = context.getCoveredNode(); PsiClass parentType = getBinding(node); - if (parentType != null) { - // Create code action - // interface - String title = Messages.getMessage("LetClassExtend", - parentType.getName(), - ServletConstants.HTTP_SERVLET); - ChangeCorrectionProposal proposal = new ExtendClassProposal(title, - context.getSource().getCompilationUnit(), parentType, context.getASTRoot(), - "jakarta.servlet.http.HttpServlet", 0); - CodeAction codeAction = context.convertToCodeAction(proposal, diagnostic); - if (codeAction != null) { - codeActions.add(codeAction); - } - } + + String title = Messages.getMessage("LetClassExtend", + parentType.getName(), + ServletConstants.HTTP_SERVLET); + codeActions.add(createCodeAction(context, diagnostic, title)); return codeActions; } + @Override + public CodeAction resolveCodeAction(JavaCodeActionResolveContext context) { + final CodeAction toResolve = context.getUnresolved(); + final PsiElement node = context.getCoveredNode(); + final PsiClass parentType = getBinding(node); + + assert parentType != null; + String title = Messages.getMessage("LetClassExtend", + parentType.getName(), + ServletConstants.HTTP_SERVLET); + ChangeCorrectionProposal proposal = new ExtendClassProposal(title, + context.getSource().getCompilationUnit(), parentType, context.getASTRoot(), + "jakarta.servlet.http.HttpServlet", 0); + + try { + WorkspaceEdit we = context.convertToWorkspaceEdit(proposal); + toResolve.setEdit(we); + } catch (Exception e) { + LOGGER.log(Level.WARNING, "Unable to create workspace edit for code action to extend the HttpServlet class.", e); + } + return toResolve; + } + + private CodeAction createCodeAction(JavaCodeActionContext context, Diagnostic diagnostic, String title) { + ExtendedCodeAction codeAction = new ExtendedCodeAction(title); + codeAction.setRelevance(0); + codeAction.setDiagnostics(Collections.singletonList(diagnostic)); + codeAction.setKind(CodeActionKind.QuickFix); + codeAction.setData(new CodeActionResolveData(context.getUri(), getParticipantId(), + context.getParams().getRange(), Collections.emptyMap(), + context.getParams().isResourceOperationSupported(), + context.getParams().isCommandConfigurationUpdateSupported())); + return codeAction; + } + private PsiClass getBinding(PsiElement node) { return PsiTreeUtil.getParentOfType(node, PsiClass.class); } diff --git a/src/main/resources/META-INF/plugin.xml b/src/main/resources/META-INF/plugin.xml index 440a0ba87..08aec2134 100644 --- a/src/main/resources/META-INF/plugin.xml +++ b/src/main/resources/META-INF/plugin.xml @@ -215,6 +215,10 @@ group="jakarta" targetDiagnostic="jakarta-jax_rs#NonPublicResourceMethod" implementationClass="io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.jax_rs.NonPublicResourceMethodQuickFix"/> + From 20187672f034022a761f18f84209424d10f006b8 Mon Sep 17 00:00:00 2001 From: Trevor Crawford Date: Thu, 5 Oct 2023 16:38:43 -0400 Subject: [PATCH 03/34] Enable builds for code-action-unification feature branch --- .github/workflows/build.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index 191505dba..af72c2212 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -4,7 +4,7 @@ on: push: branches: '**' pull_request: - branches: [ main ] + branches: [ main, code-action-unification ] jobs: build: @@ -62,4 +62,4 @@ jobs: ./**/*liberty-tools-intellij*.zip ./**/libs/*liberty-tools-intellij*.jar if-no-files-found: warn - retention-days: 7 \ No newline at end of file + retention-days: 7 From 8132c8eeb77ad501d7dc384fa6a417a88870b497 Mon Sep 17 00:00:00 2001 From: Michael Glavassevich Date: Wed, 11 Oct 2023 13:28:12 -0400 Subject: [PATCH 04/34] Resolve functional issues with ExtendClassProposal. This fixes an NPE and corrects a problem with the code change computed by the quick fix. Signed-off-by: Michael Glavassevich --- .../proposal/ExtendClassProposal.java | 33 +++++++++++++++---- .../lsp4ij/servlet/HttpServletQuickFix.java | 22 +++++++++---- 2 files changed, 43 insertions(+), 12 deletions(-) diff --git a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/codeAction/proposal/ExtendClassProposal.java b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/codeAction/proposal/ExtendClassProposal.java index 43d8cba1c..854b08427 100644 --- a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/codeAction/proposal/ExtendClassProposal.java +++ b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/codeAction/proposal/ExtendClassProposal.java @@ -14,15 +14,36 @@ *******************************************************************************/ package io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.codeAction.proposal; -import com.intellij.psi.PsiClass; -import com.intellij.psi.PsiFile; +import com.intellij.openapi.project.Project; +import com.intellij.psi.*; +import com.intellij.psi.search.GlobalSearchScope; +import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.java.corrections.proposal.ASTRewriteCorrectionProposal; import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.java.corrections.proposal.ImplementInterfaceProposal; +import org.eclipse.lsp4j.CodeActionKind; -public class ExtendClassProposal extends ImplementInterfaceProposal { +public class ExtendClassProposal extends ASTRewriteCorrectionProposal { - public ExtendClassProposal(String name, PsiFile sourceCU, PsiClass binding, PsiFile astRoot, - String interfaceType, int relevance) { - super(null, binding, astRoot, interfaceType, relevance, sourceCU); + private final PsiClass fBinding; + private final String parentClassType; + + public ExtendClassProposal(String name, PsiFile targetCU, PsiFile sourceCU, + PsiClass binding, String parentClassType, int relevance) { + super(name, CodeActionKind.QuickFix, targetCU, relevance, sourceCU); + fBinding = binding; + this.parentClassType = parentClassType; } + @Override + public void performUpdate() { + final Project project = fBinding.getProject(); + final PsiClass parentClass = JavaPsiFacade.getInstance(project). + findClass(parentClassType, GlobalSearchScope.allScope(project)); + if (parentClass != null) { + final PsiReferenceList extendsList = fBinding.getExtendsList(); + if (extendsList != null) { + extendsList.add(PsiElementFactory.getInstance(project). + createClassReferenceElement(parentClass)); + } + } + } } diff --git a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/servlet/HttpServletQuickFix.java b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/servlet/HttpServletQuickFix.java index 21641d9fb..bf622aca7 100644 --- a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/servlet/HttpServletQuickFix.java +++ b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/servlet/HttpServletQuickFix.java @@ -61,10 +61,20 @@ public List getCodeActions(JavaCodeActionContext context, PsiElement node = context.getCoveredNode(); PsiClass parentType = getBinding(node); - String title = Messages.getMessage("LetClassExtend", - parentType.getName(), - ServletConstants.HTTP_SERVLET); - codeActions.add(createCodeAction(context, diagnostic, title)); + if (parentType != null) { + // Create code action + // interface + String title = Messages.getMessage("LetClassExtend", + parentType.getName(), + ServletConstants.HTTP_SERVLET); + ChangeCorrectionProposal proposal = new ExtendClassProposal(title, context.getCompilationUnit(), + context.getSource().getCompilationUnit(), parentType, + "jakarta.servlet.http.HttpServlet", 0); + CodeAction codeAction = context.convertToCodeAction(proposal, diagnostic); + if (codeAction != null) { + codeActions.add(codeAction); + } + } return codeActions; } @@ -78,8 +88,8 @@ public CodeAction resolveCodeAction(JavaCodeActionResolveContext context) { String title = Messages.getMessage("LetClassExtend", parentType.getName(), ServletConstants.HTTP_SERVLET); - ChangeCorrectionProposal proposal = new ExtendClassProposal(title, - context.getSource().getCompilationUnit(), parentType, context.getASTRoot(), + ChangeCorrectionProposal proposal = new ExtendClassProposal(title, context.getCompilationUnit(), + context.getSource().getCompilationUnit(), parentType, "jakarta.servlet.http.HttpServlet", 0); try { From b6c6802c2fbb099c7be3ca8efcd76f6d8ec54a03 Mon Sep 17 00:00:00 2001 From: Rahul Biju Date: Mon, 16 Oct 2023 17:38:53 +0530 Subject: [PATCH 05/34] [#24] Implement Code Action Participant in HttpServletQuickFix --- .../lsp4ij/servlet/HttpServletQuickFix.java | 44 ++++++++++++------- .../it/servlet/JakartaServletTest.java | 29 +++++++----- 2 files changed, 44 insertions(+), 29 deletions(-) diff --git a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/servlet/HttpServletQuickFix.java b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/servlet/HttpServletQuickFix.java index bf622aca7..d4670d78d 100644 --- a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/servlet/HttpServletQuickFix.java +++ b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/servlet/HttpServletQuickFix.java @@ -40,12 +40,11 @@ /** * QuickFix for fixing HttpServlet extension error by providing the code actions * which implements IJavaCodeActionParticipant - * + *

* Adapted from * https://github.com/eclipse/lsp4mp/blob/master/microprofile.jdt/org.eclipse.lsp4mp.jdt.core/src/main/java/org/eclipse/lsp4mp/jdt/internal/health/java/ImplementHealthCheckQuickFix.java * * @author Credit to Angelo ZERR - * */ public class HttpServletQuickFix implements IJavaCodeActionParticipant { @@ -56,28 +55,32 @@ public String getParticipantId() { return HttpServletQuickFix.class.getName(); } + /** + * Generates a list of code actions based on the provided JavaCodeActionContext and Diagnostic. + * + * @param context the java code action context. + * @param diagnostic the diagnostic which must be fixed and null otherwise. + * @return + */ public List getCodeActions(JavaCodeActionContext context, Diagnostic diagnostic) { List codeActions = new ArrayList<>(); PsiElement node = context.getCoveredNode(); PsiClass parentType = getBinding(node); - if (parentType != null) { - // Create code action - // interface - String title = Messages.getMessage("LetClassExtend", - parentType.getName(), - ServletConstants.HTTP_SERVLET); - ChangeCorrectionProposal proposal = new ExtendClassProposal(title, context.getCompilationUnit(), - context.getSource().getCompilationUnit(), parentType, - "jakarta.servlet.http.HttpServlet", 0); - CodeAction codeAction = context.convertToCodeAction(proposal, diagnostic); - if (codeAction != null) { - codeActions.add(codeAction); - } - } + String title = Messages.getMessage("LetClassExtend", + parentType.getName(), + ServletConstants.HTTP_SERVLET); + codeActions.add(createCodeAction(context, diagnostic, title)); + return codeActions; } + /** + * this method will resolve the code for the quick actions + * + * @param context the code action context to resolve + * @return + */ @Override public CodeAction resolveCodeAction(JavaCodeActionResolveContext context) { final CodeAction toResolve = context.getUnresolved(); @@ -91,7 +94,6 @@ public CodeAction resolveCodeAction(JavaCodeActionResolveContext context) { ChangeCorrectionProposal proposal = new ExtendClassProposal(title, context.getCompilationUnit(), context.getSource().getCompilationUnit(), parentType, "jakarta.servlet.http.HttpServlet", 0); - try { WorkspaceEdit we = context.convertToWorkspaceEdit(proposal); toResolve.setEdit(we); @@ -101,6 +103,14 @@ public CodeAction resolveCodeAction(JavaCodeActionResolveContext context) { return toResolve; } + /** + * Creates a CodeAction with the specified title, relevance, diagnostics, kind, and additional data. + * + * @param context + * @param diagnostic + * @param title + * @return + */ private CodeAction createCodeAction(JavaCodeActionContext context, Diagnostic diagnostic, String title) { ExtendedCodeAction codeAction = new ExtendedCodeAction(title); codeAction.setRelevance(0); diff --git a/src/test/java/io/openliberty/tools/intellij/lsp4jakarta/it/servlet/JakartaServletTest.java b/src/test/java/io/openliberty/tools/intellij/lsp4jakarta/it/servlet/JakartaServletTest.java index 688fa6b62..aea66cb9b 100644 --- a/src/test/java/io/openliberty/tools/intellij/lsp4jakarta/it/servlet/JakartaServletTest.java +++ b/src/test/java/io/openliberty/tools/intellij/lsp4jakarta/it/servlet/JakartaServletTest.java @@ -1,15 +1,15 @@ /******************************************************************************* -* Copyright (c) 2021, 2023 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 -*******************************************************************************/ + * Copyright (c) 2021, 2023 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.servlet; @@ -60,7 +60,12 @@ public void ExtendWebServlet() throws Exception { if (CHECK_CODE_ACTIONS) { // test associated quick-fix code action JakartaJavaCodeActionParams codeActionParams = JakartaForJavaAssert.createCodeActionParams(uri, d); - TextEdit te = JakartaForJavaAssert.te(5, 34, 5, 34, " extends HttpServlet"); + String newText = "package io.openliberty.sample.jakarta.servlet;\n\n" + + "import jakarta.servlet.annotation.WebServlet;\nimport jakarta.servlet.http.HttpServlet;\n\n" + + "@WebServlet(name = \"demoServlet\", urlPatterns = {\"/demo\"})\n" + + "public class DontExtendHttpServlet extends HttpServlet {\n\n}"; + + TextEdit te = JakartaForJavaAssert.te(0, 0, 7, 1, newText); CodeAction ca = JakartaForJavaAssert.ca(uri, "Let 'DontExtendHttpServlet' extend 'HttpServlet'", d, te); JakartaForJavaAssert.assertJavaCodeAction(codeActionParams, utils, ca); } From 636e2fc80cd0882a9a52ebca895e48db7f70e003 Mon Sep 17 00:00:00 2001 From: Rahul Biju Date: Tue, 17 Oct 2023 11:54:51 +0530 Subject: [PATCH 06/34] [#517] Implement Code Action Participant in HttpServletQuickFix --- .../lsp4ij/servlet/HttpServletQuickFix.java | 12 ++++----- .../it/servlet/JakartaServletTest.java | 25 ++++++++++--------- 2 files changed, 19 insertions(+), 18 deletions(-) diff --git a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/servlet/HttpServletQuickFix.java b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/servlet/HttpServletQuickFix.java index d4670d78d..03efb2790 100644 --- a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/servlet/HttpServletQuickFix.java +++ b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/servlet/HttpServletQuickFix.java @@ -66,12 +66,12 @@ public List getCodeActions(JavaCodeActionContext context, List codeActions = new ArrayList<>(); PsiElement node = context.getCoveredNode(); PsiClass parentType = getBinding(node); - - String title = Messages.getMessage("LetClassExtend", - parentType.getName(), - ServletConstants.HTTP_SERVLET); - codeActions.add(createCodeAction(context, diagnostic, title)); - + if (parentType != null) { + String title = Messages.getMessage("LetClassExtend", + parentType.getName(), + ServletConstants.HTTP_SERVLET); + codeActions.add(createCodeAction(context, diagnostic, title)); + } return codeActions; } diff --git a/src/test/java/io/openliberty/tools/intellij/lsp4jakarta/it/servlet/JakartaServletTest.java b/src/test/java/io/openliberty/tools/intellij/lsp4jakarta/it/servlet/JakartaServletTest.java index aea66cb9b..9a2e1989a 100644 --- a/src/test/java/io/openliberty/tools/intellij/lsp4jakarta/it/servlet/JakartaServletTest.java +++ b/src/test/java/io/openliberty/tools/intellij/lsp4jakarta/it/servlet/JakartaServletTest.java @@ -57,18 +57,19 @@ public void ExtendWebServlet() throws Exception { JakartaForJavaAssert.assertJavaDiagnostics(diagnosticsParams, utils, d); - if (CHECK_CODE_ACTIONS) { - // test associated quick-fix code action - JakartaJavaCodeActionParams codeActionParams = JakartaForJavaAssert.createCodeActionParams(uri, d); - String newText = "package io.openliberty.sample.jakarta.servlet;\n\n" + - "import jakarta.servlet.annotation.WebServlet;\nimport jakarta.servlet.http.HttpServlet;\n\n" + - "@WebServlet(name = \"demoServlet\", urlPatterns = {\"/demo\"})\n" + - "public class DontExtendHttpServlet extends HttpServlet {\n\n}"; - - TextEdit te = JakartaForJavaAssert.te(0, 0, 7, 1, newText); - CodeAction ca = JakartaForJavaAssert.ca(uri, "Let 'DontExtendHttpServlet' extend 'HttpServlet'", d, te); - JakartaForJavaAssert.assertJavaCodeAction(codeActionParams, utils, ca); - } + //TODO: this condition will be enabled when all quickfixes are refactored. + // if (CHECK_CODE_ACTIONS) { + // test associated quick-fix code action + JakartaJavaCodeActionParams codeActionParams = JakartaForJavaAssert.createCodeActionParams(uri, d); + String newText = "package io.openliberty.sample.jakarta.servlet;\n\n" + + "import jakarta.servlet.annotation.WebServlet;\nimport jakarta.servlet.http.HttpServlet;\n\n" + + "@WebServlet(name = \"demoServlet\", urlPatterns = {\"/demo\"})\n" + + "public class DontExtendHttpServlet extends HttpServlet {\n\n}"; + + TextEdit te = JakartaForJavaAssert.te(0, 0, 7, 1, newText); + CodeAction ca = JakartaForJavaAssert.ca(uri, "Let 'DontExtendHttpServlet' extend 'HttpServlet'", d, te); + JakartaForJavaAssert.assertJavaCodeAction(codeActionParams, utils, ca); + // } } @Test From e3c33e79e25bb5baf7b78e2c83d54a8a346f35f7 Mon Sep 17 00:00:00 2001 From: Rahul Biju Date: Tue, 17 Oct 2023 11:56:58 +0530 Subject: [PATCH 07/34] added spaceing. --- .../intellij/lsp4jakarta/lsp4ij/servlet/HttpServletQuickFix.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/servlet/HttpServletQuickFix.java b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/servlet/HttpServletQuickFix.java index 03efb2790..4b680590c 100644 --- a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/servlet/HttpServletQuickFix.java +++ b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/servlet/HttpServletQuickFix.java @@ -66,6 +66,7 @@ public List getCodeActions(JavaCodeActionContext context, List codeActions = new ArrayList<>(); PsiElement node = context.getCoveredNode(); PsiClass parentType = getBinding(node); + if (parentType != null) { String title = Messages.getMessage("LetClassExtend", parentType.getName(), From 4ede49639b495a1da5450f84b01c8097a8d9216a Mon Sep 17 00:00:00 2001 From: Aparna Michael Date: Fri, 29 Sep 2023 20:06:03 +0530 Subject: [PATCH 08/34] Refactored the InsertAnnotationQuickfix class 1. Implemented IJavaCodeActionParticipant 2. Made the class abstract --- .../InsertAnnotationAttributesQuickFix.java | 5 ++ .../quickfix/InsertAnnotationQuickFix.java | 72 ++++++++++++++----- .../websocket/AddPathParamQuickFix.java | 5 ++ 3 files changed, 64 insertions(+), 18 deletions(-) diff --git a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/codeAction/proposal/quickfix/InsertAnnotationAttributesQuickFix.java b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/codeAction/proposal/quickfix/InsertAnnotationAttributesQuickFix.java index 95aebd14d..f02628348 100644 --- a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/codeAction/proposal/quickfix/InsertAnnotationAttributesQuickFix.java +++ b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/codeAction/proposal/quickfix/InsertAnnotationAttributesQuickFix.java @@ -32,4 +32,9 @@ public InsertAnnotationAttributesQuickFix(String annotation, boolean generateOnl //@Override protected String getLabel(String annotation, String... attributes) { return Messages.getMessage("AddAtoB", attributes[0], annotation); } + + @Override + public String getParticipantId() { + return InsertAnnotationAttributesQuickFix.class.getName(); + } } diff --git a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/codeAction/proposal/quickfix/InsertAnnotationQuickFix.java b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/codeAction/proposal/quickfix/InsertAnnotationQuickFix.java index e2383fe94..d1a950922 100644 --- a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/codeAction/proposal/quickfix/InsertAnnotationQuickFix.java +++ b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/codeAction/proposal/quickfix/InsertAnnotationQuickFix.java @@ -17,14 +17,23 @@ import com.intellij.psi.util.PsiTreeUtil; import io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.Messages; import io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.codeAction.proposal.ModifyAnnotationProposal; +import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.java.codeaction.ExtendedCodeAction; +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 org.eclipse.lsp4j.CodeAction; +import org.eclipse.lsp4j.CodeActionKind; import org.eclipse.lsp4j.Diagnostic; +import org.eclipse.lsp4j.WorkspaceEdit; +import org.eclipse.lsp4mp.commons.CodeActionResolveData; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; import java.util.List; +import java.util.logging.Level; +import java.util.logging.Logger; /** * Quickfix for adding new annotations with or without attributes @@ -33,7 +42,7 @@ * @author Lidia Ataupillco Ramos * */ -public class InsertAnnotationQuickFix { +public abstract class InsertAnnotationQuickFix implements IJavaCodeActionParticipant { private final String[] attributes; @@ -41,6 +50,8 @@ public class InsertAnnotationQuickFix { protected final boolean generateOnlyOneCodeAction; + private static final Logger LOGGER = Logger.getLogger(InsertAnnotationQuickFix.class.getName()); + public InsertAnnotationQuickFix(String annotation, String... attributes) { this(annotation, false, attributes); } @@ -62,23 +73,41 @@ public InsertAnnotationQuickFix(String annotation, boolean generateOnlyOneCodeAc public List getCodeActions(JavaCodeActionContext context, Diagnostic diagnostic) { PsiElement node = context.getCoveredNode(); + List codeActions = new ArrayList<>(); + addAttributes(diagnostic, context, codeActions, this.annotation); + + return codeActions; + } + + @Override + public CodeAction resolveCodeAction(JavaCodeActionResolveContext context) { + + final CodeAction toResolve = context.getUnresolved(); + final PsiElement node = context.getCoveredNode(); + final PsiMethod parentMethod = PsiTreeUtil.getParentOfType(node, PsiMethod.class); PsiModifierListOwner binding = getBinding(node); - // annotationNode is null when adding an annotation and non-null when adding attributes. PsiAnnotation annotationNode = PsiTreeUtil.getParentOfType(node, PsiAnnotation.class); - List codeActions = new ArrayList<>(); - addAttributes(diagnostic, context, binding, annotationNode, codeActions, this.annotation); + assert parentMethod != null; + String label = getLabel(this.annotation, attributes); - return codeActions; + ChangeCorrectionProposal proposal = new ModifyAnnotationProposal(label, context.getSource().getCompilationUnit(), + context.getASTRoot(), binding, annotationNode, 0, this.annotation, Arrays.asList(attributes)); + try { + WorkspaceEdit we = context.convertToWorkspaceEdit(proposal); + toResolve.setEdit(we); + } catch (Exception e) { + LOGGER.log(Level.WARNING, "Unable to create workspace edit for code action " + label, e); + } + return toResolve; } - protected void addAttributes(Diagnostic diagnostic, JavaCodeActionContext context, PsiModifierListOwner binding, - PsiAnnotation annotation, List codeActions, String name) { + protected void addAttributes(Diagnostic diagnostic, JavaCodeActionContext context, List codeActions, String name) { if (generateOnlyOneCodeAction) { - addAttribute(diagnostic, context, binding, annotation, codeActions, name, attributes); + addAttribute(diagnostic, context, codeActions, name, attributes); } else { for (String attribute : attributes) { - addAttribute(diagnostic, context, binding, annotation, codeActions, name, attribute); + addAttribute(diagnostic, context, codeActions, name, attribute); } } } @@ -88,16 +117,9 @@ protected void addAttributes(Diagnostic diagnostic, JavaCodeActionContext contex * collector class. * */ - private void addAttribute(Diagnostic diagnostic, JavaCodeActionContext context, PsiModifierListOwner binding, - PsiAnnotation annotation, List codeActions, String name, String... attributes) { + private void addAttribute(Diagnostic diagnostic, JavaCodeActionContext context, List codeActions, String name, String... attributes) { String label = getLabel(name, attributes); - ChangeCorrectionProposal proposal = new ModifyAnnotationProposal(label, context.getSource().getCompilationUnit(), - context.getASTRoot(), binding, annotation, 0, name, Arrays.asList(attributes)); - CodeAction codeAction = context.convertToCodeAction(proposal, diagnostic); - - if (codeAction != null) { - codeActions.add(codeAction); - } + codeActions.add(createCodeAction(context, diagnostic, label)); } protected PsiModifierListOwner getBinding(PsiElement node) { @@ -112,4 +134,18 @@ protected PsiModifierListOwner getBinding(PsiElement node) { protected String getLabel(String annotationName, String... attributes) { return Messages.getMessage("InsertItem", "@" + annotation); // uses Java syntax } + + private CodeAction createCodeAction(JavaCodeActionContext context, Diagnostic diagnostic, String label) { + LOGGER.log(Level.WARNING, "#####&&&&&&&&& print label --" + label + "####", label); + + ExtendedCodeAction codeAction = new ExtendedCodeAction(label); + codeAction.setRelevance(0); + codeAction.setDiagnostics(Collections.singletonList(diagnostic)); + codeAction.setKind(CodeActionKind.QuickFix); + codeAction.setData(new CodeActionResolveData(context.getUri(), getParticipantId(), + context.getParams().getRange(), Collections.emptyMap(), + context.getParams().isResourceOperationSupported(), + context.getParams().isCommandConfigurationUpdateSupported())); + return codeAction; + } } diff --git a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/websocket/AddPathParamQuickFix.java b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/websocket/AddPathParamQuickFix.java index 34b24295f..bb5be1464 100644 --- a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/websocket/AddPathParamQuickFix.java +++ b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/websocket/AddPathParamQuickFix.java @@ -25,4 +25,9 @@ public class AddPathParamQuickFix extends InsertAnnotationQuickFix { public AddPathParamQuickFix() { super("jakarta.websocket.server.PathParam", false, "value"); } + + @Override + public String getParticipantId() { + return AddPathParamQuickFix.class.getName(); + } } From 8d54ef97f4736432cc7ce6fd3a60e81b2925639b Mon Sep 17 00:00:00 2001 From: Aparna Michael Date: Fri, 29 Sep 2023 20:12:29 +0530 Subject: [PATCH 09/34] Added extension point in plugin.xml for AddPathParamsAnnotation --- src/main/resources/META-INF/plugin.xml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/main/resources/META-INF/plugin.xml b/src/main/resources/META-INF/plugin.xml index 08aec2134..dc1eead35 100644 --- a/src/main/resources/META-INF/plugin.xml +++ b/src/main/resources/META-INF/plugin.xml @@ -220,6 +220,11 @@ targetDiagnostic="jakarta-servlet#ExtendHttpServlet" implementationClass="io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.servlet.HttpServletQuickFix"/> + + From 6d50b78388289b69e11e19526eaa68422baea5c6 Mon Sep 17 00:00:00 2001 From: Aparna Michael Date: Fri, 29 Sep 2023 21:05:00 +0530 Subject: [PATCH 10/34] enabled the code action test case and updated test case --- .../quickfix/InsertAnnotationQuickFix.java | 2 -- .../it/websocket/JakartaWebSocketTest.java | 16 ++++++---------- 2 files changed, 6 insertions(+), 12 deletions(-) diff --git a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/codeAction/proposal/quickfix/InsertAnnotationQuickFix.java b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/codeAction/proposal/quickfix/InsertAnnotationQuickFix.java index d1a950922..e78810636 100644 --- a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/codeAction/proposal/quickfix/InsertAnnotationQuickFix.java +++ b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/codeAction/proposal/quickfix/InsertAnnotationQuickFix.java @@ -136,8 +136,6 @@ protected String getLabel(String annotationName, String... attributes) { } private CodeAction createCodeAction(JavaCodeActionContext context, Diagnostic diagnostic, String label) { - LOGGER.log(Level.WARNING, "#####&&&&&&&&& print label --" + label + "####", label); - ExtendedCodeAction codeAction = new ExtendedCodeAction(label); codeAction.setRelevance(0); codeAction.setDiagnostics(Collections.singletonList(diagnostic)); diff --git a/src/test/java/io/openliberty/tools/intellij/lsp4jakarta/it/websocket/JakartaWebSocketTest.java b/src/test/java/io/openliberty/tools/intellij/lsp4jakarta/it/websocket/JakartaWebSocketTest.java index 56b410a0e..99bbe7d82 100644 --- a/src/test/java/io/openliberty/tools/intellij/lsp4jakarta/it/websocket/JakartaWebSocketTest.java +++ b/src/test/java/io/openliberty/tools/intellij/lsp4jakarta/it/websocket/JakartaWebSocketTest.java @@ -70,16 +70,12 @@ public void addPathParamsAnnotation() throws Exception { JakartaForJavaAssert.assertJavaDiagnostics(diagnosticsParams, utils, d1, d2, d3); - if (CHECK_CODE_ACTIONS) { - // Expected code actions - JakartaJavaCodeActionParams codeActionsParams = JakartaForJavaAssert.createCodeActionParams(uri, d1); - String newText = "\nimport jakarta.websocket.server.PathParam;\nimport jakarta.websocket.server.ServerEndpoint;\nimport jakarta.websocket.Session;\n\n" - + "/**\n * Expected Diagnostics are related to validating that the parameters have the \n * valid annotation @PathParam (code: AddPathParamsAnnotation)\n * See issue #247 (onOpen) and #248 (onClose)\n */\n" - + "@ServerEndpoint(value = \"/infos\")\npublic class AnnotationTest {\n // @PathParam missing annotation for \"String missingAnnotation\"\n @OnOpen\n public void OnOpen(Session session, @PathParam(value = \"\") "; - TextEdit te = JakartaForJavaAssert.te(5, 32, 18, 40, newText); - CodeAction ca = JakartaForJavaAssert.ca(uri, "Insert @jakarta.websocket.server.PathParam", d1, te); - JakartaForJavaAssert.assertJavaCodeAction(codeActionsParams, utils, ca); - } + // Expected code actions + JakartaJavaCodeActionParams codeActionsParams = JakartaForJavaAssert.createCodeActionParams(uri, d1); + String newText = "package io.openliberty.sample.jakarta.websocket;\\n\\nimport java.io.IOException;\\n\\nimport jakarta.websocket.OnClose;\\nimport jakarta.websocket.OnOpen;\\nimport jakarta.websocket.server.PathParam;import jakarta.websocket.server.ServerEndpoint;\\nimport jakarta.websocket.Session;\\n\\n/**\\n * Expected Diagnostics are related to validating that the parameters have the \\n * valid annotation @PathParam (code: AddPathParamsAnnotation)\\n * See issue #247 (onOpen) and #248 (onClose)\\n */\\n@ServerEndpoint(value = \"/infos\")\\npublic class AnnotationTest {\\n // @PathParam missing annotation for \"String missingAnnotation\"\\n @OnOpen\\n public void OnOpen(Session session, @PathParam(\"\") String missingAnnotation) throws IOException {\\n System.out.println(\"Websocket opened: \" + session.getId().toString());\\n }\\n \\n // Used to check that the expected diagnostic handle more than one case\\n @OnClose\\n public void OnClose(Session session, Integer missingAnnotation1, String missingAnnotation2) {\\n System.out.println(\"Websocket opened: \" + session.getId().toString());\\n }\\n}\\n"; + TextEdit te = JakartaForJavaAssert.te(0, 0, 28, 0, newText); + CodeAction ca = JakartaForJavaAssert.ca(uri, "Insert @jakarta.websocket.server.PathParam", d1, te); + JakartaForJavaAssert.assertJavaCodeAction(codeActionsParams, utils, ca); } @Test From 2827d7761200f90cac77e98f6f726a09731d297d Mon Sep 17 00:00:00 2001 From: Aparna Michael Date: Wed, 4 Oct 2023 20:16:34 +0530 Subject: [PATCH 11/34] Enabled quick fix testcase and updated the expected value --- .../it/websocket/JakartaWebSocketTest.java | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/test/java/io/openliberty/tools/intellij/lsp4jakarta/it/websocket/JakartaWebSocketTest.java b/src/test/java/io/openliberty/tools/intellij/lsp4jakarta/it/websocket/JakartaWebSocketTest.java index 99bbe7d82..813605436 100644 --- a/src/test/java/io/openliberty/tools/intellij/lsp4jakarta/it/websocket/JakartaWebSocketTest.java +++ b/src/test/java/io/openliberty/tools/intellij/lsp4jakarta/it/websocket/JakartaWebSocketTest.java @@ -36,6 +36,8 @@ import java.io.File; import java.util.Arrays; +import static io.openliberty.tools.intellij.lsp4jakarta.it.core.JakartaForJavaAssert.*; + @RunWith(JUnit4.class) public class JakartaWebSocketTest extends BaseJakartaTest { @@ -56,26 +58,29 @@ public void addPathParamsAnnotation() throws Exception { "Parameters of type String, any Java primitive type, or boxed version thereof must be annotated with @PathParams.", DiagnosticSeverity.Error, "jakarta-websocket", "AddPathParamsAnnotation" ); - + // OnClose PathParams Annotation check Diagnostic d2 = JakartaForJavaAssert.d(24, 49, 67, "Parameters of type String, any Java primitive type, or boxed version thereof must be annotated with @PathParams.", DiagnosticSeverity.Error, "jakarta-websocket", "AddPathParamsAnnotation" ); - + Diagnostic d3 = JakartaForJavaAssert.d(24, 76, 94, "Parameters of type String, any Java primitive type, or boxed version thereof must be annotated with @PathParams.", DiagnosticSeverity.Error, "jakarta-websocket", "AddPathParamsAnnotation" ); - JakartaForJavaAssert.assertJavaDiagnostics(diagnosticsParams, utils, d1, d2, d3); + //TODO: uncomment this if condition, once refactoring is done for all the quick fixes. +// if (CHECK_CODE_ACTIONS) { // Expected code actions - JakartaJavaCodeActionParams codeActionsParams = JakartaForJavaAssert.createCodeActionParams(uri, d1); - String newText = "package io.openliberty.sample.jakarta.websocket;\\n\\nimport java.io.IOException;\\n\\nimport jakarta.websocket.OnClose;\\nimport jakarta.websocket.OnOpen;\\nimport jakarta.websocket.server.PathParam;import jakarta.websocket.server.ServerEndpoint;\\nimport jakarta.websocket.Session;\\n\\n/**\\n * Expected Diagnostics are related to validating that the parameters have the \\n * valid annotation @PathParam (code: AddPathParamsAnnotation)\\n * See issue #247 (onOpen) and #248 (onClose)\\n */\\n@ServerEndpoint(value = \"/infos\")\\npublic class AnnotationTest {\\n // @PathParam missing annotation for \"String missingAnnotation\"\\n @OnOpen\\n public void OnOpen(Session session, @PathParam(\"\") String missingAnnotation) throws IOException {\\n System.out.println(\"Websocket opened: \" + session.getId().toString());\\n }\\n \\n // Used to check that the expected diagnostic handle more than one case\\n @OnClose\\n public void OnClose(Session session, Integer missingAnnotation1, String missingAnnotation2) {\\n System.out.println(\"Websocket opened: \" + session.getId().toString());\\n }\\n}\\n"; - TextEdit te = JakartaForJavaAssert.te(0, 0, 28, 0, newText); - CodeAction ca = JakartaForJavaAssert.ca(uri, "Insert @jakarta.websocket.server.PathParam", d1, te); + JakartaJavaCodeActionParams codeActionsParams = createCodeActionParams(uri, d1); + String newText = "package io.openliberty.sample.jakarta.websocket;\n\nimport java.io.IOException;\n\nimport jakarta.websocket.OnClose;\nimport jakarta.websocket.OnOpen;\nimport jakarta.websocket.server.PathParam;import jakarta.websocket.server.ServerEndpoint;\nimport jakarta.websocket.Session;\n\n/**\n * Expected Diagnostics are related to validating that the parameters have the \n * valid annotation @PathParam (code: AddPathParamsAnnotation)\n * See issue #247 (onOpen) and #248 (onClose)\n */\n@ServerEndpoint(value = \"/infos\")\npublic class AnnotationTest {\n // @PathParam missing annotation for \"String missingAnnotation\"\n @OnOpen\n public void OnOpen(Session session, @PathParam(\"\") String missingAnnotation) throws IOException {\n System.out.println(\"Websocket opened: \" + session.getId().toString());\n }\n \n // Used to check that the expected diagnostic handle more than one case\n @OnClose\n public void OnClose(Session session, Integer missingAnnotation1, String missingAnnotation2) {\n System.out.println(\"Websocket opened: \" + session.getId().toString());\n }\n}\n"; + TextEdit te = te(0, 0, 28, 0, newText); + CodeAction ca = ca(uri, "Insert @jakarta.websocket.server.PathParam", d1, te); + JakartaForJavaAssert.assertJavaCodeAction(codeActionsParams, utils, ca); +// } } @Test From c597525d5378ac1fdb6d2e8762b2d047531e5c10 Mon Sep 17 00:00:00 2001 From: Aparna Michael Date: Mon, 9 Oct 2023 16:17:25 +0530 Subject: [PATCH 12/34] Added extension point for AddResourceMissingName and AddResourceMissingType quick fix Added extension point and enabled the tests --- .../AddResourceMissingNameQuickFix.java | 5 +++ .../AddResourceMissingTypeQuickFix.java | 5 +++ .../InsertAnnotationAttributesQuickFix.java | 8 +--- .../quickfix/InsertAnnotationQuickFix.java | 5 +-- src/main/resources/META-INF/plugin.xml | 10 +++++ .../annotations/ResourceAnnotationTest.java | 44 ++++++++++++++----- .../it/websocket/JakartaWebSocketTest.java | 15 ++++++- 7 files changed, 68 insertions(+), 24 deletions(-) diff --git a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/codeAction/annotations/AddResourceMissingNameQuickFix.java b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/codeAction/annotations/AddResourceMissingNameQuickFix.java index 82480dc58..59ef91d0d 100644 --- a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/codeAction/annotations/AddResourceMissingNameQuickFix.java +++ b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/codeAction/annotations/AddResourceMissingNameQuickFix.java @@ -25,4 +25,9 @@ public class AddResourceMissingNameQuickFix extends InsertAnnotationAttributesQu public AddResourceMissingNameQuickFix() { super("jakarta.annotation.Resource", false, "name"); } + + @Override + public String getParticipantId() { + return AddResourceMissingNameQuickFix.class.getName(); + } } diff --git a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/codeAction/annotations/AddResourceMissingTypeQuickFix.java b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/codeAction/annotations/AddResourceMissingTypeQuickFix.java index 3a5beb446..a439ffa7d 100644 --- a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/codeAction/annotations/AddResourceMissingTypeQuickFix.java +++ b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/codeAction/annotations/AddResourceMissingTypeQuickFix.java @@ -26,4 +26,9 @@ public class AddResourceMissingTypeQuickFix extends InsertAnnotationAttributesQu public AddResourceMissingTypeQuickFix() { super("jakarta.annotation.Resource", false, "type"); } + + @Override + public String getParticipantId() { + return AddResourceMissingTypeQuickFix.class.getName(); + } } diff --git a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/codeAction/proposal/quickfix/InsertAnnotationAttributesQuickFix.java b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/codeAction/proposal/quickfix/InsertAnnotationAttributesQuickFix.java index f02628348..d65a08d91 100644 --- a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/codeAction/proposal/quickfix/InsertAnnotationAttributesQuickFix.java +++ b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/codeAction/proposal/quickfix/InsertAnnotationAttributesQuickFix.java @@ -20,7 +20,7 @@ * @author Lidia Ataupillco Ramos * */ -public class InsertAnnotationAttributesQuickFix extends InsertAnnotationQuickFix { +public abstract class InsertAnnotationAttributesQuickFix extends InsertAnnotationQuickFix { public InsertAnnotationAttributesQuickFix(String annotation, String... attributes) { this(annotation, false, attributes); } @@ -29,12 +29,8 @@ public InsertAnnotationAttributesQuickFix(String annotation, boolean generateOnl super(annotation, generateOnlyOneCodeAction, attributes); } - //@Override + @Override protected String getLabel(String annotation, String... attributes) { return Messages.getMessage("AddAtoB", attributes[0], annotation); } - @Override - public String getParticipantId() { - return InsertAnnotationAttributesQuickFix.class.getName(); - } } diff --git a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/codeAction/proposal/quickfix/InsertAnnotationQuickFix.java b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/codeAction/proposal/quickfix/InsertAnnotationQuickFix.java index e78810636..686e494fa 100644 --- a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/codeAction/proposal/quickfix/InsertAnnotationQuickFix.java +++ b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/codeAction/proposal/quickfix/InsertAnnotationQuickFix.java @@ -75,7 +75,6 @@ public List getCodeActions(JavaCodeActionContext context, PsiElement node = context.getCoveredNode(); List codeActions = new ArrayList<>(); addAttributes(diagnostic, context, codeActions, this.annotation); - return codeActions; } @@ -84,13 +83,11 @@ public CodeAction resolveCodeAction(JavaCodeActionResolveContext context) { final CodeAction toResolve = context.getUnresolved(); final PsiElement node = context.getCoveredNode(); - final PsiMethod parentMethod = PsiTreeUtil.getParentOfType(node, PsiMethod.class); PsiModifierListOwner binding = getBinding(node); PsiAnnotation annotationNode = PsiTreeUtil.getParentOfType(node, PsiAnnotation.class); - assert parentMethod != null; + assert binding != null; String label = getLabel(this.annotation, attributes); - ChangeCorrectionProposal proposal = new ModifyAnnotationProposal(label, context.getSource().getCompilationUnit(), context.getASTRoot(), binding, annotationNode, 0, this.annotation, Arrays.asList(attributes)); try { diff --git a/src/main/resources/META-INF/plugin.xml b/src/main/resources/META-INF/plugin.xml index dc1eead35..3b1045a68 100644 --- a/src/main/resources/META-INF/plugin.xml +++ b/src/main/resources/META-INF/plugin.xml @@ -225,6 +225,16 @@ targetDiagnostic="jakarta-websocket#AddPathParamsAnnotation" implementationClass="io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.websocket.AddPathParamQuickFix"/> + + + + diff --git a/src/test/java/io/openliberty/tools/intellij/lsp4jakarta/it/annotations/ResourceAnnotationTest.java b/src/test/java/io/openliberty/tools/intellij/lsp4jakarta/it/annotations/ResourceAnnotationTest.java index 118318a9b..eb079dd54 100644 --- a/src/test/java/io/openliberty/tools/intellij/lsp4jakarta/it/annotations/ResourceAnnotationTest.java +++ b/src/test/java/io/openliberty/tools/intellij/lsp4jakarta/it/annotations/ResourceAnnotationTest.java @@ -52,24 +52,44 @@ public void ResourceAnnotation() throws Exception { diagnosticsParams.setUris(Arrays.asList(uri)); // expected annotations - Diagnostic d1 = d(22, 0, 22, "The @Resource annotation must define the attribute 'type'.", + Diagnostic d1 = d(24, 0, 22, "The @Resource annotation must define the attribute 'type'.", DiagnosticSeverity.Error, "jakarta-annotations", "MissingResourceTypeAttribute"); - Diagnostic d2 = d(39, 0, 30, "The @Resource annotation must define the attribute 'name'.", + Diagnostic d2 = d(41, 0, 30, "The @Resource annotation must define the attribute 'name'.", DiagnosticSeverity.Error, "jakarta-annotations", "MissingResourceNameAttribute"); assertJavaDiagnostics(diagnosticsParams, utils, d1, d2); - if (CHECK_CODE_ACTIONS) { - JakartaJavaCodeActionParams codeActionParams = createCodeActionParams(uri, d1); - TextEdit te = te(22, 0, 22, 22, "@Resource(name = \"aa\", type = \"\")"); - CodeAction ca = ca(uri, "Add type to jakarta.annotation.Resource", d1, te); - assertJavaCodeAction(codeActionParams, utils, ca); + //TODO: uncomment this if condition, once refactoring is done for all the quick fixes. +// if (CHECK_CODE_ACTIONS) { + JakartaJavaCodeActionParams codeActionParams = createCodeActionParams(uri, d1); + String newText = "package io.openliberty.sample.jakarta.annotations;\n\nimport jakarta.annotation.Resource;" + + "\n\n@Resource(type = Object.class, name = \"aa\")\npublic class ResourceAnnotation {\n\n " + + "private Integer studentId;\n\n\n @Resource(shareable = true)\n " + + "private boolean isHappy;\n\n @Resource(name = \"test\")\n private boolean isSad;\n\n\n " + + "private String emailAddress;\n\n\n public ResourceAnnotation() {\n " + + "}\n}\n\n@Resource(name = \"aa\",type=\"\")\nclass PostDoctoralStudent {\n\n " + + "private Integer studentId;\n\n\n @Resource(shareable = true)\n private boolean isHappy;\n\n " + + "@Resource\n private boolean isSad;\n\n\n " + + "private String emailAddress;\n\n}\n\n@Resource(type = Object.class)\nclass MasterStudent {\n\n " + + "private Integer studentId;\n\n} \n"; + TextEdit te = te(0, 0, 47, 0, newText); + CodeAction ca = ca(uri, "Add type to jakarta.annotation.Resource", d1, te); + assertJavaCodeAction(codeActionParams, utils, ca); - JakartaJavaCodeActionParams codeActionParams1 = createCodeActionParams(uri, d2); - TextEdit te1 = te(39, 0, 39, 30, "@Resource(type = \"\", name = \"\")"); - CodeAction ca1 = ca(uri, "Add name to jakarta.annotation.Resource", d2, te1); - assertJavaCodeAction(codeActionParams1, utils, ca1); - } + JakartaJavaCodeActionParams codeActionParams1 = createCodeActionParams(uri, d2); + newText = "package io.openliberty.sample.jakarta.annotations;\n\nimport jakarta.annotation.Resource;" + + "\n\n@Resource(type = Object.class, name = \"aa\")\npublic class ResourceAnnotation {\n\n " + + "private Integer studentId;\n\n\n @Resource(shareable = true)\n private boolean isHappy;\n\n " + + "@Resource(name = \"test\")\n private boolean isSad;\n\n\n private String emailAddress;\n\n\n " + + "public ResourceAnnotation() {\n }\n}\n\n@Resource(name = \"aa\")\nclass PostDoctoralStudent {\n\n " + + "private Integer studentId;\n\n\n @Resource(shareable = true)\n private boolean isHappy;\n\n " + + "@Resource\n private boolean isSad;\n\n\n private String emailAddress;\n\n}\n\n" + + "@Resource(type = Object.class, name=\"\")\nclass MasterStudent {\n\n " + + "private Integer studentId;\n\n} \n"; + TextEdit te1 = te(0, 0, 47, 0, newText); + CodeAction ca1 = ca(uri, "Add name to jakarta.annotation.Resource", d2, te1); + assertJavaCodeAction(codeActionParams1, utils, ca1); +// } } } diff --git a/src/test/java/io/openliberty/tools/intellij/lsp4jakarta/it/websocket/JakartaWebSocketTest.java b/src/test/java/io/openliberty/tools/intellij/lsp4jakarta/it/websocket/JakartaWebSocketTest.java index 813605436..c82baf304 100644 --- a/src/test/java/io/openliberty/tools/intellij/lsp4jakarta/it/websocket/JakartaWebSocketTest.java +++ b/src/test/java/io/openliberty/tools/intellij/lsp4jakarta/it/websocket/JakartaWebSocketTest.java @@ -75,10 +75,21 @@ public void addPathParamsAnnotation() throws Exception { // if (CHECK_CODE_ACTIONS) { // Expected code actions JakartaJavaCodeActionParams codeActionsParams = createCodeActionParams(uri, d1); - String newText = "package io.openliberty.sample.jakarta.websocket;\n\nimport java.io.IOException;\n\nimport jakarta.websocket.OnClose;\nimport jakarta.websocket.OnOpen;\nimport jakarta.websocket.server.PathParam;import jakarta.websocket.server.ServerEndpoint;\nimport jakarta.websocket.Session;\n\n/**\n * Expected Diagnostics are related to validating that the parameters have the \n * valid annotation @PathParam (code: AddPathParamsAnnotation)\n * See issue #247 (onOpen) and #248 (onClose)\n */\n@ServerEndpoint(value = \"/infos\")\npublic class AnnotationTest {\n // @PathParam missing annotation for \"String missingAnnotation\"\n @OnOpen\n public void OnOpen(Session session, @PathParam(\"\") String missingAnnotation) throws IOException {\n System.out.println(\"Websocket opened: \" + session.getId().toString());\n }\n \n // Used to check that the expected diagnostic handle more than one case\n @OnClose\n public void OnClose(Session session, Integer missingAnnotation1, String missingAnnotation2) {\n System.out.println(\"Websocket opened: \" + session.getId().toString());\n }\n}\n"; + String newText = "package io.openliberty.sample.jakarta.websocket;\n\nimport java.io.IOException;" + + "\n\nimport jakarta.websocket.OnClose;\nimport jakarta.websocket.OnOpen;\n" + + "import jakarta.websocket.server.PathParam;import jakarta.websocket.server.ServerEndpoint;\n" + + "import jakarta.websocket.Session;\n\n/**\n * Expected Diagnostics are related to validating that the " + + "parameters have the \n * valid annotation @PathParam (code: AddPathParamsAnnotation)\n * " + + "See issue #247 (onOpen) and #248 (onClose)\n */\n@ServerEndpoint(value = \"/infos\")\n" + + "public class AnnotationTest {\n // @PathParam missing annotation for \"String missingAnnotation\"\n" + + " @OnOpen\n public void OnOpen(Session session, @PathParam(\"\") String missingAnnotation) " + + "throws IOException {\n System.out.println(\"Websocket opened: \" + session.getId().toString());" + + "\n }\n \n // Used to check that the expected diagnostic handle more than one case\n " + + "@OnClose\n public void OnClose(Session session, Integer missingAnnotation1, " + + "String missingAnnotation2) {\n System.out.println(\"Websocket opened: \" + " + + "session.getId().toString());\n }\n}\n"; TextEdit te = te(0, 0, 28, 0, newText); CodeAction ca = ca(uri, "Insert @jakarta.websocket.server.PathParam", d1, te); - JakartaForJavaAssert.assertJavaCodeAction(codeActionsParams, utils, ca); // } } From a7c9fd0a19e0d23a8996a55d455129f8d539dcda Mon Sep 17 00:00:00 2001 From: Aparna Michael Date: Mon, 9 Oct 2023 16:23:07 +0530 Subject: [PATCH 13/34] Formatted the newly added code and added back the removed comment --- .../codeAction/proposal/quickfix/InsertAnnotationQuickFix.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/codeAction/proposal/quickfix/InsertAnnotationQuickFix.java b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/codeAction/proposal/quickfix/InsertAnnotationQuickFix.java index 686e494fa..5646d3fa5 100644 --- a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/codeAction/proposal/quickfix/InsertAnnotationQuickFix.java +++ b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/codeAction/proposal/quickfix/InsertAnnotationQuickFix.java @@ -80,10 +80,10 @@ public List getCodeActions(JavaCodeActionContext context, @Override public CodeAction resolveCodeAction(JavaCodeActionResolveContext context) { - final CodeAction toResolve = context.getUnresolved(); final PsiElement node = context.getCoveredNode(); PsiModifierListOwner binding = getBinding(node); + // annotationNode is null when adding an annotation and non-null when adding attributes. PsiAnnotation annotationNode = PsiTreeUtil.getParentOfType(node, PsiAnnotation.class); assert binding != null; From 41bac1eb59581cab733cb5a27f8308617434dad7 Mon Sep 17 00:00:00 2001 From: Aparna Michael Date: Tue, 17 Oct 2023 12:29:01 +0530 Subject: [PATCH 14/34] updated the resource annotation test --- .../annotations/ResourceAnnotationTest.java | 40 ++++++++++--------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/src/test/java/io/openliberty/tools/intellij/lsp4jakarta/it/annotations/ResourceAnnotationTest.java b/src/test/java/io/openliberty/tools/intellij/lsp4jakarta/it/annotations/ResourceAnnotationTest.java index eb079dd54..de5e5fd9f 100644 --- a/src/test/java/io/openliberty/tools/intellij/lsp4jakarta/it/annotations/ResourceAnnotationTest.java +++ b/src/test/java/io/openliberty/tools/intellij/lsp4jakarta/it/annotations/ResourceAnnotationTest.java @@ -52,10 +52,10 @@ public void ResourceAnnotation() throws Exception { diagnosticsParams.setUris(Arrays.asList(uri)); // expected annotations - Diagnostic d1 = d(24, 0, 22, "The @Resource annotation must define the attribute 'type'.", + Diagnostic d1 = d(22, 0, 22, "The @Resource annotation must define the attribute 'type'.", DiagnosticSeverity.Error, "jakarta-annotations", "MissingResourceTypeAttribute"); - Diagnostic d2 = d(41, 0, 30, "The @Resource annotation must define the attribute 'name'.", + Diagnostic d2 = d(39, 0, 30, "The @Resource annotation must define the attribute 'name'.", DiagnosticSeverity.Error, "jakarta-annotations", "MissingResourceNameAttribute"); assertJavaDiagnostics(diagnosticsParams, utils, d1, d2); @@ -64,30 +64,32 @@ public void ResourceAnnotation() throws Exception { // if (CHECK_CODE_ACTIONS) { JakartaJavaCodeActionParams codeActionParams = createCodeActionParams(uri, d1); String newText = "package io.openliberty.sample.jakarta.annotations;\n\nimport jakarta.annotation.Resource;" + - "\n\n@Resource(type = Object.class, name = \"aa\")\npublic class ResourceAnnotation {\n\n " + + "\n\n@Resource(type = Object.class, name = \"aa\")\npublic class ResourceAnnotation " + + "{\n\n private Integer studentId;\n\n\n @Resource(shareable = true)\n " + + "private boolean isHappy;\n\n @Resource(name = \"test\")\n " + + " private boolean isSad;\n\n\n private String emailAddress;\n\n\n}\n\n" + + "@Resource(name = \"aa\",type=\"\")\nclass PostDoctoralStudent {\n\n " + "private Integer studentId;\n\n\n @Resource(shareable = true)\n " + - "private boolean isHappy;\n\n @Resource(name = \"test\")\n private boolean isSad;\n\n\n " + - "private String emailAddress;\n\n\n public ResourceAnnotation() {\n " + - "}\n}\n\n@Resource(name = \"aa\",type=\"\")\nclass PostDoctoralStudent {\n\n " + - "private Integer studentId;\n\n\n @Resource(shareable = true)\n private boolean isHappy;\n\n " + - "@Resource\n private boolean isSad;\n\n\n " + - "private String emailAddress;\n\n}\n\n@Resource(type = Object.class)\nclass MasterStudent {\n\n " + - "private Integer studentId;\n\n} \n"; - TextEdit te = te(0, 0, 47, 0, newText); + "private boolean isHappy;\n\n @Resource\n private boolean isSad;\n\n\n " + + "private String emailAddress;\n\n}\n\n@Resource(type = Object.class)\nclass " + + "MasterStudent {\n\n private Integer studentId;\n\n} \n"; + TextEdit te = te(0, 0, 45, 0, newText); CodeAction ca = ca(uri, "Add type to jakarta.annotation.Resource", d1, te); assertJavaCodeAction(codeActionParams, utils, ca); JakartaJavaCodeActionParams codeActionParams1 = createCodeActionParams(uri, d2); - newText = "package io.openliberty.sample.jakarta.annotations;\n\nimport jakarta.annotation.Resource;" + - "\n\n@Resource(type = Object.class, name = \"aa\")\npublic class ResourceAnnotation {\n\n " + - "private Integer studentId;\n\n\n @Resource(shareable = true)\n private boolean isHappy;\n\n " + - "@Resource(name = \"test\")\n private boolean isSad;\n\n\n private String emailAddress;\n\n\n " + - "public ResourceAnnotation() {\n }\n}\n\n@Resource(name = \"aa\")\nclass PostDoctoralStudent {\n\n " + - "private Integer studentId;\n\n\n @Resource(shareable = true)\n private boolean isHappy;\n\n " + - "@Resource\n private boolean isSad;\n\n\n private String emailAddress;\n\n}\n\n" + + newText = "package io.openliberty.sample.jakarta.annotations;\n\n" + + "import jakarta.annotation.Resource;\n\n@Resource(type = Object.class, " + + "name = \"aa\")\npublic class ResourceAnnotation {\n\n private Integer studentId;" + + "\n\n\n @Resource(shareable = true)\n private boolean isHappy;\n\n " + + "@Resource(name = \"test\")\n private boolean isSad;\n\n\n " + + "private String emailAddress;\n\n\n}\n\n@Resource(name = \"aa\")\n" + + "class PostDoctoralStudent {\n\n private Integer studentId;\n\n\n " + + "@Resource(shareable = true)\n private boolean isHappy;\n\n @Resource\n " + + "private boolean isSad;\n\n\n private String emailAddress;\n\n}\n\n" + "@Resource(type = Object.class, name=\"\")\nclass MasterStudent {\n\n " + "private Integer studentId;\n\n} \n"; - TextEdit te1 = te(0, 0, 47, 0, newText); + TextEdit te1 = te(0, 0, 45, 0, newText); CodeAction ca1 = ca(uri, "Add name to jakarta.annotation.Resource", d2, te1); assertJavaCodeAction(codeActionParams1, utils, ca1); // } From 42519fb0d98ae92c33d0632e89b15fd6cfa10d1c Mon Sep 17 00:00:00 2001 From: dessina-devasia <143582034+dessina-devasia@users.noreply.github.com> Date: Wed, 11 Oct 2023 10:44:35 +0530 Subject: [PATCH 15/34] [#522]: Refactoring and enabling tests on RemoveModifierConflictQuickFix --- .../codeAction/JakartaCodeActionHandler.java | 31 ------- .../RemoveModifierConflictQuickFix.java | 81 ++++++++++++++----- src/main/resources/META-INF/plugin.xml | 31 +++++-- 3 files changed, 88 insertions(+), 55 deletions(-) diff --git a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/codeAction/JakartaCodeActionHandler.java b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/codeAction/JakartaCodeActionHandler.java index ebf372a8a..dbffeb3bc 100644 --- a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/codeAction/JakartaCodeActionHandler.java +++ b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/codeAction/JakartaCodeActionHandler.java @@ -36,9 +36,6 @@ import io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.servlet.*; import io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.websocket.AddPathParamQuickFix; import io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.websocket.WebSocketConstants; -import io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.codeAction.proposal.quickfix.RemoveAbstractModifierQuickFix; -import io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.codeAction.proposal.quickfix.RemoveFinalModifierQuickFix; -import io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.codeAction.proposal.quickfix.RemoveStaticModifierQuickFix; import io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.di.DependencyInjectionConstants; import io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.persistence.PersistenceConstants; import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.java.codeaction.JavaCodeActionContext; @@ -112,12 +109,9 @@ public List codeAction(JakartaJavaCodeActionParams params, IPsiUtils RemovePreDestroyAnnotationQuickFix RemovePreDestroyAnnotationQuickFix = new RemovePreDestroyAnnotationQuickFix(); RemovePostConstructAnnotationQuickFix RemovePostConstructAnnotationQuickFix = new RemovePostConstructAnnotationQuickFix(); PostConstructReturnTypeQuickFix PostConstructReturnTypeQuickFix = new PostConstructReturnTypeQuickFix(); - RemoveFinalModifierQuickFix RemoveFinalModifierQuickFix = new RemoveFinalModifierQuickFix(); - RemoveStaticModifierQuickFix RemoveStaticModifierQuickFix = new RemoveStaticModifierQuickFix(); RemoveMethodParametersQuickFix RemoveMethodParametersQuickFix = new RemoveMethodParametersQuickFix(); AddResourceMissingNameQuickFix AddResourceMissingNameQuickFix = new AddResourceMissingNameQuickFix(); AddResourceMissingTypeQuickFix AddResourceMissingTypeQuickFix = new AddResourceMissingTypeQuickFix(); - RemoveAbstractModifierQuickFix RemoveAbstractModifierQuickFix = new RemoveAbstractModifierQuickFix(); RemoveInjectAnnotationQuickFix RemoveInjectAnnotationQuickFix = new RemoveInjectAnnotationQuickFix(); RemoveProduceAnnotationQuickFix RemoveProduceAnnotationQuickFix = new RemoveProduceAnnotationQuickFix(); RemoveInvalidInjectParamAnnotationQuickFix RemoveInvalidInjectParamAnnotationQuickFix = new RemoveInvalidInjectParamAnnotationQuickFix(); @@ -175,11 +169,6 @@ public List codeAction(JakartaJavaCodeActionParams params, IPsiUtils if (diagnostic.getCode().getLeft().equals(PersistenceConstants.DIAGNOSTIC_CODE_MISSING_EMPTY_CONSTRUCTOR)) { codeActions.addAll(PersistenceEntityQuickFix.getCodeActions(context, diagnostic)); } - if (diagnostic.getCode().getLeft().equals(PersistenceConstants.DIAGNOSTIC_CODE_FINAL_METHODS) - || diagnostic.getCode().getLeft().equals(PersistenceConstants.DIAGNOSTIC_CODE_FINAL_VARIABLES) - || diagnostic.getCode().getLeft().equals(PersistenceConstants.DIAGNOSTIC_CODE_FINAL_CLASS)) { - codeActions.addAll(RemoveFinalModifierQuickFix.getCodeActions(context, diagnostic)); - } if (diagnostic.getCode().getLeft().equals(ManagedBeanConstants.DIAGNOSTIC_CODE)) { codeActions.addAll(ManagedBeanQuickFix.getCodeActions(context, diagnostic)); } @@ -214,36 +203,16 @@ public List codeAction(JakartaJavaCodeActionParams params, IPsiUtils if (diagnostic.getCode().getLeft().equals(ManagedBeanConstants.DIAGNOSTIC_CODE_SCOPEDECL)) { codeActions.addAll(ScopeDeclarationQuickFix.getCodeActions(context, diagnostic)); } - if (diagnostic.getCode().getLeft().equals(DependencyInjectionConstants.DIAGNOSTIC_CODE_INJECT_FINAL)) { - JavaCodeActionContext contextCopy = context.copy(); // each code action needs its own context. - codeActions.addAll(RemoveInjectAnnotationQuickFix.getCodeActions(context, diagnostic)); - codeActions.addAll(RemoveFinalModifierQuickFix.getCodeActions(contextCopy, diagnostic)); - } if (diagnostic.getCode().getLeft().equals(DependencyInjectionConstants.DIAGNOSTIC_CODE_INJECT_CONSTRUCTOR) || diagnostic.getCode().getLeft().equals(DependencyInjectionConstants.DIAGNOSTIC_CODE_INJECT_GENERIC)) { codeActions.addAll(RemoveInjectAnnotationQuickFix.getCodeActions(context, diagnostic)); } - if (diagnostic.getCode().getLeft().equals(DependencyInjectionConstants.DIAGNOSTIC_CODE_INJECT_ABSTRACT)) { - JavaCodeActionContext contextCopy = context.copy(); // each code action needs its own context. - codeActions.addAll(RemoveInjectAnnotationQuickFix.getCodeActions(context, diagnostic)); - codeActions.addAll(RemoveAbstractModifierQuickFix.getCodeActions(contextCopy, diagnostic)); - } - if (diagnostic.getCode().getLeft().equals(DependencyInjectionConstants.DIAGNOSTIC_CODE_INJECT_STATIC)) { - JavaCodeActionContext contextCopy = context.copy(); // each code action needs its own context. - codeActions.addAll(RemoveInjectAnnotationQuickFix.getCodeActions(context, diagnostic)); - codeActions.addAll(RemoveStaticModifierQuickFix.getCodeActions(contextCopy, diagnostic)); - } if (diagnostic.getCode().getLeft().equals(AnnotationConstants.DIAGNOSTIC_CODE_POSTCONSTRUCT_PARAMS)) { JavaCodeActionContext contextCopy = context.copy(); // each code action needs its own context. codeActions.addAll(RemovePostConstructAnnotationQuickFix.getCodeActions(context, diagnostic)); codeActions.addAll(RemoveMethodParametersQuickFix.getCodeActions(contextCopy, diagnostic)); } - if (diagnostic.getCode().getLeft().equals(AnnotationConstants.DIAGNOSTIC_CODE_PREDESTROY_STATIC)) { - JavaCodeActionContext contextCopy = context.copy(); // each code action needs its own context. - codeActions.addAll(RemovePreDestroyAnnotationQuickFix.getCodeActions(context, diagnostic)); - codeActions.addAll(RemoveStaticModifierQuickFix.getCodeActions(contextCopy, diagnostic)); - } if (diagnostic.getCode().getLeft().equals(AnnotationConstants.DIAGNOSTIC_CODE_PREDESTROY_PARAMS)) { JavaCodeActionContext contextCopy = context.copy(); // each code action needs its own context. codeActions.addAll(RemovePreDestroyAnnotationQuickFix.getCodeActions(context, diagnostic)); diff --git a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/codeAction/proposal/quickfix/RemoveModifierConflictQuickFix.java b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/codeAction/proposal/quickfix/RemoveModifierConflictQuickFix.java index 43b671504..40f87bc8c 100644 --- a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/codeAction/proposal/quickfix/RemoveModifierConflictQuickFix.java +++ b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/codeAction/proposal/quickfix/RemoveModifierConflictQuickFix.java @@ -17,9 +17,19 @@ import com.intellij.psi.util.PsiTreeUtil; import io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.Messages; import io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.codeAction.proposal.ModifyModifiersProposal; +import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.java.codeaction.ExtendedCodeAction; +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 org.eclipse.lsp4j.CodeAction; +import org.eclipse.lsp4j.CodeActionKind; import org.eclipse.lsp4j.Diagnostic; +import org.eclipse.lsp4j.WorkspaceEdit; +import org.eclipse.lsp4mp.commons.CodeActionResolveData; + + +import java.util.logging.Level; +import java.util.logging.Logger; import java.util.ArrayList; import java.util.Arrays; @@ -32,11 +42,13 @@ * @author Himanshu Chotwani * */ -public class RemoveModifierConflictQuickFix { +public class RemoveModifierConflictQuickFix implements IJavaCodeActionParticipant { private final String[] modifiers; protected final boolean generateOnlyOneCodeAction; + + private static final Logger LOGGER = Logger.getLogger(RemoveModifierConflictQuickFix.class.getName()); /** @@ -64,16 +76,27 @@ public RemoveModifierConflictQuickFix(boolean generateOnlyOneCodeAction, String. this.generateOnlyOneCodeAction = generateOnlyOneCodeAction; this.modifiers = modifiers; } - + + @Override + public String getParticipantId() { + return RemoveModifierConflictQuickFix.class.getName(); + } public List getCodeActions(JavaCodeActionContext context, Diagnostic diagnostic) { List codeActions = new ArrayList<>(); removeModifiers(diagnostic, context, codeActions); +// PsiElement node = context.getCoveredNode(); +// PsiModifierListOwner modifierListOwner = PsiTreeUtil.getParentOfType(node, PsiModifierListOwner.class); +// for (String modifier : modifiers) { +// String label = getLabel(modifierListOwner, modifier); +// codeActions.add(createCodeAction(context, diagnostic, label)); +// } return codeActions; } - + + protected void removeModifiers(Diagnostic diagnostic, JavaCodeActionContext context, - List codeActions) { + List codeActions) { if (generateOnlyOneCodeAction || modifiers.length == 1) { removeModifier(diagnostic, context, codeActions, modifiers); } else { @@ -83,26 +106,34 @@ protected void removeModifiers(Diagnostic diagnostic, JavaCodeActionContext cont } } } - - /** - * use setData() API with diagnostic to pass in ElementType in diagnostic collector class. - * - */ - private void removeModifier(Diagnostic diagnostic, JavaCodeActionContext context, - List codeActions, String... modifier) { - PsiElement node = context.getCoveredNode(); - PsiClass parentType = getBinding(node); - PsiModifierListOwner modifierListOwner = PsiTreeUtil.getParentOfType(node, PsiModifierListOwner.class); - String label = getLabel(modifierListOwner, modifier); + @Override + public CodeAction resolveCodeAction(JavaCodeActionResolveContext context) { + final CodeAction toResolve = context.getUnresolved(); + final PsiElement node = context.getCoveredNode(); + final PsiClass parentType = getBinding(node); + final PsiModifierListOwner modifierListOwner = PsiTreeUtil.getParentOfType(node, PsiModifierListOwner.class); + + String label = getLabel(modifierListOwner, modifiers); ModifyModifiersProposal proposal = new ModifyModifiersProposal(label, context.getSource().getCompilationUnit(), - context.getASTRoot(), parentType, 0, modifierListOwner.getModifierList(), Collections.emptyList(), Arrays.asList(modifier)); - CodeAction codeAction = context.convertToCodeAction(proposal, diagnostic); + context.getASTRoot(), parentType, 0, modifierListOwner.getModifierList(), Collections.emptyList(), Arrays.asList(modifiers)); - if (codeAction != null) { - codeActions.add(codeAction); + try { + WorkspaceEdit we = context.convertToWorkspaceEdit(proposal); + toResolve.setEdit(we); + } catch (Exception e) { + LOGGER.log(Level.WARNING, "Unable to create workspace edit for code action " + label, e); } + return toResolve; + } + + private void removeModifier(Diagnostic diagnostic, JavaCodeActionContext context, + List codeActions, String... modifier) { + PsiElement node = context.getCoveredNode(); + PsiModifierListOwner modifierListOwner = PsiTreeUtil.getParentOfType(node, PsiModifierListOwner.class); + String label = getLabel(modifierListOwner, modifier); + codeActions.add(createCodeAction(context, diagnostic, label)); } private String getLabel(PsiModifierListOwner modifierListOwner, String... modifier) { @@ -125,4 +156,16 @@ protected PsiClass getBinding(PsiElement node) { return PsiTreeUtil.getParentOfType(node, PsiClass.class); } + private CodeAction createCodeAction(JavaCodeActionContext context, Diagnostic diagnostic, String title) { + ExtendedCodeAction codeAction = new ExtendedCodeAction(title); + codeAction.setRelevance(0); + codeAction.setDiagnostics(Collections.singletonList(diagnostic)); + codeAction.setKind(CodeActionKind.QuickFix); + codeAction.setData(new CodeActionResolveData(context.getUri(), getParticipantId(), + context.getParams().getRange(), Collections.emptyMap(), + context.getParams().isResourceOperationSupported(), + context.getParams().isCommandConfigurationUpdateSupported())); + return codeAction; + } + } diff --git a/src/main/resources/META-INF/plugin.xml b/src/main/resources/META-INF/plugin.xml index 3b1045a68..97eb3c874 100644 --- a/src/main/resources/META-INF/plugin.xml +++ b/src/main/resources/META-INF/plugin.xml @@ -211,29 +211,50 @@ group="mp" implementationClass="io.openliberty.tools.intellij.lsp4mp4ij.psi.internal.openapi.java.MicroProfileGenerateOpenAPIOperation"/> + - + targetDiagnostic="jakarta-di#RemoveInjectOrFinal" + implementationClass="io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.codeAction.proposal.quickfix.RemoveFinalModifierQuickFix"/> + + - - + + + From 886862442a2538f997690b286d6e6fb947076c87 Mon Sep 17 00:00:00 2001 From: dessina-devasia <143582034+dessina-devasia@users.noreply.github.com> Date: Tue, 17 Oct 2023 16:17:19 +0530 Subject: [PATCH 16/34] [Fixes #522]: Resolved the issues in RemoveModifierConflictQuickFix and bring the test ready --- .../proposal/ModifyModifiersProposal.java | 25 ++- .../RemoveAbstractModifierQuickFix.java | 5 + .../quickfix/RemoveFinalModifierQuickFix.java | 5 + .../RemoveModifierConflictQuickFix.java | 16 +- .../RemoveStaticModifierQuickFix.java | 5 + .../it/di/DependencyInjectionTest.java | 149 ++++++++++++++++-- 6 files changed, 175 insertions(+), 30 deletions(-) diff --git a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/codeAction/proposal/ModifyModifiersProposal.java b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/codeAction/proposal/ModifyModifiersProposal.java index 2576c9bd3..3a9b9e115 100644 --- a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/codeAction/proposal/ModifyModifiersProposal.java +++ b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/codeAction/proposal/ModifyModifiersProposal.java @@ -46,6 +46,8 @@ public class ModifyModifiersProposal extends ChangeCorrectionProposal { // list of modifiers (if they exist) to remove private final List modifiersToRemove; + private boolean isFormatRequired = true; + /** * Constructor for ModifyModifiersProposal that accepts both a list of modifiers to remove as well as to add * @@ -62,6 +64,25 @@ public ModifyModifiersProposal(String label, PsiFile sourceCU, PsiFile invocatio this.modifiersToAdd = modifiersToAdd; this.modifiersToRemove = modifiersToRemove; } + + /** + * Constructor for ModifyModifiersProposal that accepts both a list of modifiers to remove as well as to add + * this constructor having an optional parameter isFormatRequired + * + * @param modifiersToAdd list of valid modifiers as strings to be added + * @param modifiersToRemove list of modifiers as strings to be removed + */ + public ModifyModifiersProposal(String label, PsiFile sourceCU, PsiFile invocationNode, + PsiModifierListOwner binding, int relevance, PsiModifierList modifiers, List modifiersToAdd, List modifiersToRemove, boolean isFormatRequired) { + super(label, CodeActionKind.QuickFix, relevance); + this.sourceCU = sourceCU; + this.invocationNode = invocationNode; + this.binding = binding; + this.modifiers = modifiers; + this.modifiersToAdd = modifiersToAdd; + this.modifiersToRemove = modifiersToRemove; + this.isFormatRequired = isFormatRequired; + } /** * Constructor for ModifyModifiersProposal that accepts only a list of modifiers to add @@ -100,7 +121,9 @@ public Change getChange() { modifiers.setModifierProperty(modifier, true); } }); - PositionUtils.formatDocument(binding); // add the necessary new lines, must use 'binding,' it's already in the document + if (isFormatRequired) { + PositionUtils.formatDocument(binding); // add the necessary new lines, must use 'binding,' it's already in the document + } final Document document = invocationNode.getViewProvider().getDocument(); return new Change(sourceCU.getViewProvider().getDocument(), document); } diff --git a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/codeAction/proposal/quickfix/RemoveAbstractModifierQuickFix.java b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/codeAction/proposal/quickfix/RemoveAbstractModifierQuickFix.java index 65a34ac76..db8ae2b97 100644 --- a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/codeAction/proposal/quickfix/RemoveAbstractModifierQuickFix.java +++ b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/codeAction/proposal/quickfix/RemoveAbstractModifierQuickFix.java @@ -23,4 +23,9 @@ public class RemoveAbstractModifierQuickFix extends RemoveModifierConflictQuickF public RemoveAbstractModifierQuickFix() { super (false, "abstract"); } + + @Override + public String getParticipantId() { + return RemoveAbstractModifierQuickFix.class.getName(); + } } diff --git a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/codeAction/proposal/quickfix/RemoveFinalModifierQuickFix.java b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/codeAction/proposal/quickfix/RemoveFinalModifierQuickFix.java index 5e084daac..2b5b3710d 100644 --- a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/codeAction/proposal/quickfix/RemoveFinalModifierQuickFix.java +++ b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/codeAction/proposal/quickfix/RemoveFinalModifierQuickFix.java @@ -25,4 +25,9 @@ public class RemoveFinalModifierQuickFix extends RemoveModifierConflictQuickFix public RemoveFinalModifierQuickFix() { super(false, "final"); } + + @Override + public String getParticipantId() { + return RemoveFinalModifierQuickFix.class.getName(); + } } diff --git a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/codeAction/proposal/quickfix/RemoveModifierConflictQuickFix.java b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/codeAction/proposal/quickfix/RemoveModifierConflictQuickFix.java index 40f87bc8c..dc4f6624b 100644 --- a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/codeAction/proposal/quickfix/RemoveModifierConflictQuickFix.java +++ b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/codeAction/proposal/quickfix/RemoveModifierConflictQuickFix.java @@ -42,7 +42,7 @@ * @author Himanshu Chotwani * */ -public class RemoveModifierConflictQuickFix implements IJavaCodeActionParticipant { +public abstract class RemoveModifierConflictQuickFix implements IJavaCodeActionParticipant { private final String[] modifiers; @@ -77,20 +77,9 @@ public RemoveModifierConflictQuickFix(boolean generateOnlyOneCodeAction, String. this.modifiers = modifiers; } - @Override - public String getParticipantId() { - return RemoveModifierConflictQuickFix.class.getName(); - } - public List getCodeActions(JavaCodeActionContext context, Diagnostic diagnostic) { List codeActions = new ArrayList<>(); removeModifiers(diagnostic, context, codeActions); -// PsiElement node = context.getCoveredNode(); -// PsiModifierListOwner modifierListOwner = PsiTreeUtil.getParentOfType(node, PsiModifierListOwner.class); -// for (String modifier : modifiers) { -// String label = getLabel(modifierListOwner, modifier); -// codeActions.add(createCodeAction(context, diagnostic, label)); -// } return codeActions; } @@ -116,8 +105,9 @@ public CodeAction resolveCodeAction(JavaCodeActionResolveContext context) { String label = getLabel(modifierListOwner, modifiers); + assert parentType != null; ModifyModifiersProposal proposal = new ModifyModifiersProposal(label, context.getSource().getCompilationUnit(), - context.getASTRoot(), parentType, 0, modifierListOwner.getModifierList(), Collections.emptyList(), Arrays.asList(modifiers)); + context.getASTRoot(), parentType, 0, modifierListOwner.getModifierList(), Collections.emptyList(), Arrays.asList(modifiers), false); try { WorkspaceEdit we = context.convertToWorkspaceEdit(proposal); diff --git a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/codeAction/proposal/quickfix/RemoveStaticModifierQuickFix.java b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/codeAction/proposal/quickfix/RemoveStaticModifierQuickFix.java index d46449d2c..6bc8acf5e 100644 --- a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/codeAction/proposal/quickfix/RemoveStaticModifierQuickFix.java +++ b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/codeAction/proposal/quickfix/RemoveStaticModifierQuickFix.java @@ -26,4 +26,9 @@ public class RemoveStaticModifierQuickFix extends RemoveModifierConflictQuickFix public RemoveStaticModifierQuickFix() { super(false, "static"); } + + @Override + public String getParticipantId() { + return RemoveStaticModifierQuickFix.class.getName(); + } } diff --git a/src/test/java/io/openliberty/tools/intellij/lsp4jakarta/it/di/DependencyInjectionTest.java b/src/test/java/io/openliberty/tools/intellij/lsp4jakarta/it/di/DependencyInjectionTest.java index 924d70718..6ddd72b8a 100644 --- a/src/test/java/io/openliberty/tools/intellij/lsp4jakarta/it/di/DependencyInjectionTest.java +++ b/src/test/java/io/openliberty/tools/intellij/lsp4jakarta/it/di/DependencyInjectionTest.java @@ -87,30 +87,21 @@ public void DependencyInjectionDiagnostics() throws Exception { TextEdit te = JakartaForJavaAssert.te(16, 4, 17, 4, ""); CodeAction ca = JakartaForJavaAssert.ca(uri, "Remove @Inject", d1, te); - TextEdit te1 = JakartaForJavaAssert.te(17, 11, 17, 17, - ""); - CodeAction ca1 = JakartaForJavaAssert.ca(uri, "Remove the 'final' modifier from this field", d1, te1); - JakartaForJavaAssert.assertJavaCodeAction(codeActionParams, utils, ca, ca1); + JakartaForJavaAssert.assertJavaCodeAction(codeActionParams, utils, ca); // for d2 codeActionParams = JakartaForJavaAssert.createCodeActionParams(uri, d2); te = JakartaForJavaAssert.te(32, 4, 33, 4, ""); ca = JakartaForJavaAssert.ca(uri, "Remove @Inject", d2, te); - te1 = JakartaForJavaAssert.te(33, 10, 33, 19, - ""); - ca1 = JakartaForJavaAssert.ca(uri, "Remove the 'abstract' modifier from this method", d2, te1); - JakartaForJavaAssert.assertJavaCodeAction(codeActionParams, utils, ca, ca1); + JakartaForJavaAssert.assertJavaCodeAction(codeActionParams, utils, ca); // for d3 codeActionParams = JakartaForJavaAssert.createCodeActionParams(uri, d3); te = JakartaForJavaAssert.te(25, 4, 26, 4, ""); ca = JakartaForJavaAssert.ca(uri, "Remove @Inject", d3, te); - te1 = JakartaForJavaAssert.te(26, 10, 26, 16, - ""); - ca1 = JakartaForJavaAssert.ca(uri, "Remove the 'final' modifier from this method", d3, te1); - JakartaForJavaAssert.assertJavaCodeAction(codeActionParams, utils, ca, ca1); + JakartaForJavaAssert.assertJavaCodeAction(codeActionParams, utils, ca); // for d4 codeActionParams = JakartaForJavaAssert.createCodeActionParams(uri, d4); @@ -124,10 +115,136 @@ public void DependencyInjectionDiagnostics() throws Exception { te = JakartaForJavaAssert.te(36, 4, 37, 4, ""); ca = JakartaForJavaAssert.ca(uri, "Remove @Inject", d5, te); - te1 = JakartaForJavaAssert.te(37, 10, 37, 17, - ""); - ca1 = JakartaForJavaAssert.ca(uri, "Remove the 'static' modifier from this method", d5, te1); - JakartaForJavaAssert.assertJavaCodeAction(codeActionParams, utils, ca, ca1); + JakartaForJavaAssert.assertJavaCodeAction(codeActionParams, utils, ca); } + + JakartaJavaCodeActionParams codeActionParams = JakartaForJavaAssert.createCodeActionParams(uri, d1); + String textD1 = "package io.openliberty.sample.jakarta.di;\n\n" + + "import jakarta.inject.Inject;\n" + + "import jakarta.enterprise.inject.Produces;\n\n" + + "import java.util.ArrayList;\n" + + "import java.util.List;\n\n" + + "public abstract class GreetingServlet {\n\n " + + "/**\n *\n */\n " + + "private static final long serialVersionUID = 1L;\n\n " + + "// d1: test code for @Inject fields cannot be final\n " + + "@Inject\n private Greeting greeting = new Greeting();\n\n " + + "@Produces\n " + + "public GreetingNoDefaultConstructor getInstance() {\n " + + "return new GreetingNoDefaultConstructor(\"Howdy\");\n }\n\n " + + "// d2\n " + + "@Inject\n public final void injectFinal() {\n " + + "// test code for @Inject methods cannot be final\n " + + "return;\n }\n\n " + + "// d3: test code for @Inject methods cannot be abstract\n " + + "@Inject\n public abstract void injectAbstract();\n\n " + + "// d4: test code for @Inject methods cannot be static\n " + + "@Inject\n " + + "public static void injectStatic() {\n " + + "return;\n }\n\n " + + "// d5: test code for @Inject methods cannot be generic\n " + + "@Inject\n " + + "public List injectGeneric(T arg) {\n " + + "// do nothing\n " + + "return new ArrayList();\n " + + "};\n\n}\n"; + + TextEdit te1 = JakartaForJavaAssert.te(0, 0, 49, 0, textD1); + CodeAction ca1 = JakartaForJavaAssert.ca(uri, "Remove the 'final' modifier from this field", d1, te1); + JakartaForJavaAssert.assertJavaCodeAction(codeActionParams, utils, ca1); + + String textD2 = "package io.openliberty.sample.jakarta.di;\n\n" + + "import jakarta.inject.Inject;\n" + + "import jakarta.enterprise.inject.Produces;\n\n" + + "import java.util.ArrayList;\n" + + "import java.util.List;\n\n" + + "public abstract class GreetingServlet {\n\n " + + "/**\n *\n */\n " + + "private static final long serialVersionUID = 1L;\n\n " + + "// d1: test code for @Inject fields cannot be final\n " + + "@Inject\n private final Greeting greeting = new Greeting();\n\n " + + "@Produces\n public GreetingNoDefaultConstructor getInstance() {\n " + + "return new GreetingNoDefaultConstructor(\"Howdy\");\n }\n\n " + + "// d2\n @Inject\n public final void injectFinal() {\n " + + "// test code for @Inject methods cannot be final\n " + + "return;\n }\n\n " + + "// d3: test code for @Inject methods cannot be abstract\n " + + "@Inject\n public void injectAbstract();\n\n " + + "// d4: test code for @Inject methods cannot be static\n " + + "@Inject\n public static void injectStatic() {\n " + + "return;\n }\n\n " + + "// d5: test code for @Inject methods cannot be generic\n " + + "@Inject\n " + + "public List injectGeneric(T arg) {\n " + + "// do nothing\n " + + "return new ArrayList();\n " + + "};\n\n}\n"; + codeActionParams = JakartaForJavaAssert.createCodeActionParams(uri, d2); + te1 = JakartaForJavaAssert.te(0, 0, 49, 0, textD2); + ca1 = JakartaForJavaAssert.ca(uri, "Remove the 'abstract' modifier from this method", d2, te1); + JakartaForJavaAssert.assertJavaCodeAction(codeActionParams, utils, ca1); + + String textD3 = "package io.openliberty.sample.jakarta.di;\n\n" + + "import jakarta.inject.Inject;\n" + + "import jakarta.enterprise.inject.Produces;\n\n" + + "import java.util.ArrayList;\n" + + "import java.util.List;\n\n" + + "public abstract class GreetingServlet {\n\n " + + "/**\n *\n */\n " + + "private static final long serialVersionUID = 1L;\n\n // d1: test code for @Inject fields cannot be final\n " + + "@Inject\n private final Greeting greeting = new Greeting();\n\n " + + "@Produces\n " + + "public GreetingNoDefaultConstructor getInstance() {\n " + + "return new GreetingNoDefaultConstructor(\"Howdy\");\n " + + "}\n\n " + + "// d2\n @Inject\n public void injectFinal() {\n " + + "// test code for @Inject methods cannot be final\n " + + "return;\n }\n\n " + + "// d3: test code for @Inject methods cannot be abstract\n " + + "@Inject\n public abstract void injectAbstract();\n\n " + + "// d4: test code for @Inject methods cannot be static\n " + + "@Inject\n public static void injectStatic() {\n " + + "return;\n }\n\n " + + "// d5: test code for @Inject methods cannot be generic\n " + + "@Inject\n " + + "public List injectGeneric(T arg) {\n " + + "// do nothing\n " + + "return new ArrayList();\n };\n\n}\n"; + codeActionParams = JakartaForJavaAssert.createCodeActionParams(uri, d3); + te1 = JakartaForJavaAssert.te(0, 0, 49, 0, textD3); + ca1 = JakartaForJavaAssert.ca(uri, "Remove the 'final' modifier from this method", d3, te1); + JakartaForJavaAssert.assertJavaCodeAction(codeActionParams, utils, ca1); + + String textD4 = "package io.openliberty.sample.jakarta.di;\n\n" + + "import jakarta.inject.Inject;\n" + + "import jakarta.enterprise.inject.Produces;\n\n" + + "import java.util.ArrayList;\nimport java.util.List;\n\n" + + "public abstract class GreetingServlet " + + "{\n\n /**\n *\n */\n " + + "private static final long serialVersionUID = 1L;\n\n " + + "// d1: test code for @Inject fields cannot be final\n " + + "@Inject\n " + + "private final Greeting greeting = new Greeting();\n\n " + + "@Produces\n " + + "public GreetingNoDefaultConstructor getInstance() {\n " + + "return new GreetingNoDefaultConstructor(\"Howdy\");\n }\n\n " + + "// d2\n @Inject\n " + + "public final void injectFinal() {\n " + + "// test code for @Inject methods cannot be final\n " + + "return;\n }\n\n " + + "// d3: test code for @Inject methods cannot be abstract\n " + + "@Inject\n " + + "public abstract void injectAbstract();\n\n " + + "// d4: test code for @Inject methods cannot be static\n " + + "@Inject\n public void injectStatic() {\n " + + "return;\n }\n\n " + + "// d5: test code for @Inject methods cannot be generic\n " + + "@Inject\n " + + "public List injectGeneric(T arg) {\n " + + "// do nothing\n return new ArrayList();\n };\n\n}\n"; + codeActionParams = JakartaForJavaAssert.createCodeActionParams(uri, d5); + te1 = JakartaForJavaAssert.te(0, 0, 49, 0, textD4); + ca1 = JakartaForJavaAssert.ca(uri, "Remove the 'static' modifier from this method", d5, te1); + JakartaForJavaAssert.assertJavaCodeAction(codeActionParams, utils, ca1); } } From 6f0d509a2d27c091cd0d2abd3d6466552abc9cc5 Mon Sep 17 00:00:00 2001 From: dessina-devasia <143582034+dessina-devasia@users.noreply.github.com> Date: Wed, 18 Oct 2023 12:54:05 +0530 Subject: [PATCH 17/34] Reverted changes in JakartaCodeActionHandler.java --- .../codeAction/JakartaCodeActionHandler.java | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/codeAction/JakartaCodeActionHandler.java b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/codeAction/JakartaCodeActionHandler.java index dbffeb3bc..ebf372a8a 100644 --- a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/codeAction/JakartaCodeActionHandler.java +++ b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/codeAction/JakartaCodeActionHandler.java @@ -36,6 +36,9 @@ import io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.servlet.*; import io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.websocket.AddPathParamQuickFix; import io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.websocket.WebSocketConstants; +import io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.codeAction.proposal.quickfix.RemoveAbstractModifierQuickFix; +import io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.codeAction.proposal.quickfix.RemoveFinalModifierQuickFix; +import io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.codeAction.proposal.quickfix.RemoveStaticModifierQuickFix; import io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.di.DependencyInjectionConstants; import io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.persistence.PersistenceConstants; import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.java.codeaction.JavaCodeActionContext; @@ -109,9 +112,12 @@ public List codeAction(JakartaJavaCodeActionParams params, IPsiUtils RemovePreDestroyAnnotationQuickFix RemovePreDestroyAnnotationQuickFix = new RemovePreDestroyAnnotationQuickFix(); RemovePostConstructAnnotationQuickFix RemovePostConstructAnnotationQuickFix = new RemovePostConstructAnnotationQuickFix(); PostConstructReturnTypeQuickFix PostConstructReturnTypeQuickFix = new PostConstructReturnTypeQuickFix(); + RemoveFinalModifierQuickFix RemoveFinalModifierQuickFix = new RemoveFinalModifierQuickFix(); + RemoveStaticModifierQuickFix RemoveStaticModifierQuickFix = new RemoveStaticModifierQuickFix(); RemoveMethodParametersQuickFix RemoveMethodParametersQuickFix = new RemoveMethodParametersQuickFix(); AddResourceMissingNameQuickFix AddResourceMissingNameQuickFix = new AddResourceMissingNameQuickFix(); AddResourceMissingTypeQuickFix AddResourceMissingTypeQuickFix = new AddResourceMissingTypeQuickFix(); + RemoveAbstractModifierQuickFix RemoveAbstractModifierQuickFix = new RemoveAbstractModifierQuickFix(); RemoveInjectAnnotationQuickFix RemoveInjectAnnotationQuickFix = new RemoveInjectAnnotationQuickFix(); RemoveProduceAnnotationQuickFix RemoveProduceAnnotationQuickFix = new RemoveProduceAnnotationQuickFix(); RemoveInvalidInjectParamAnnotationQuickFix RemoveInvalidInjectParamAnnotationQuickFix = new RemoveInvalidInjectParamAnnotationQuickFix(); @@ -169,6 +175,11 @@ public List codeAction(JakartaJavaCodeActionParams params, IPsiUtils if (diagnostic.getCode().getLeft().equals(PersistenceConstants.DIAGNOSTIC_CODE_MISSING_EMPTY_CONSTRUCTOR)) { codeActions.addAll(PersistenceEntityQuickFix.getCodeActions(context, diagnostic)); } + if (diagnostic.getCode().getLeft().equals(PersistenceConstants.DIAGNOSTIC_CODE_FINAL_METHODS) + || diagnostic.getCode().getLeft().equals(PersistenceConstants.DIAGNOSTIC_CODE_FINAL_VARIABLES) + || diagnostic.getCode().getLeft().equals(PersistenceConstants.DIAGNOSTIC_CODE_FINAL_CLASS)) { + codeActions.addAll(RemoveFinalModifierQuickFix.getCodeActions(context, diagnostic)); + } if (diagnostic.getCode().getLeft().equals(ManagedBeanConstants.DIAGNOSTIC_CODE)) { codeActions.addAll(ManagedBeanQuickFix.getCodeActions(context, diagnostic)); } @@ -203,16 +214,36 @@ public List codeAction(JakartaJavaCodeActionParams params, IPsiUtils if (diagnostic.getCode().getLeft().equals(ManagedBeanConstants.DIAGNOSTIC_CODE_SCOPEDECL)) { codeActions.addAll(ScopeDeclarationQuickFix.getCodeActions(context, diagnostic)); } + if (diagnostic.getCode().getLeft().equals(DependencyInjectionConstants.DIAGNOSTIC_CODE_INJECT_FINAL)) { + JavaCodeActionContext contextCopy = context.copy(); // each code action needs its own context. + codeActions.addAll(RemoveInjectAnnotationQuickFix.getCodeActions(context, diagnostic)); + codeActions.addAll(RemoveFinalModifierQuickFix.getCodeActions(contextCopy, diagnostic)); + } if (diagnostic.getCode().getLeft().equals(DependencyInjectionConstants.DIAGNOSTIC_CODE_INJECT_CONSTRUCTOR) || diagnostic.getCode().getLeft().equals(DependencyInjectionConstants.DIAGNOSTIC_CODE_INJECT_GENERIC)) { codeActions.addAll(RemoveInjectAnnotationQuickFix.getCodeActions(context, diagnostic)); } + if (diagnostic.getCode().getLeft().equals(DependencyInjectionConstants.DIAGNOSTIC_CODE_INJECT_ABSTRACT)) { + JavaCodeActionContext contextCopy = context.copy(); // each code action needs its own context. + codeActions.addAll(RemoveInjectAnnotationQuickFix.getCodeActions(context, diagnostic)); + codeActions.addAll(RemoveAbstractModifierQuickFix.getCodeActions(contextCopy, diagnostic)); + } + if (diagnostic.getCode().getLeft().equals(DependencyInjectionConstants.DIAGNOSTIC_CODE_INJECT_STATIC)) { + JavaCodeActionContext contextCopy = context.copy(); // each code action needs its own context. + codeActions.addAll(RemoveInjectAnnotationQuickFix.getCodeActions(context, diagnostic)); + codeActions.addAll(RemoveStaticModifierQuickFix.getCodeActions(contextCopy, diagnostic)); + } if (diagnostic.getCode().getLeft().equals(AnnotationConstants.DIAGNOSTIC_CODE_POSTCONSTRUCT_PARAMS)) { JavaCodeActionContext contextCopy = context.copy(); // each code action needs its own context. codeActions.addAll(RemovePostConstructAnnotationQuickFix.getCodeActions(context, diagnostic)); codeActions.addAll(RemoveMethodParametersQuickFix.getCodeActions(contextCopy, diagnostic)); } + if (diagnostic.getCode().getLeft().equals(AnnotationConstants.DIAGNOSTIC_CODE_PREDESTROY_STATIC)) { + JavaCodeActionContext contextCopy = context.copy(); // each code action needs its own context. + codeActions.addAll(RemovePreDestroyAnnotationQuickFix.getCodeActions(context, diagnostic)); + codeActions.addAll(RemoveStaticModifierQuickFix.getCodeActions(contextCopy, diagnostic)); + } if (diagnostic.getCode().getLeft().equals(AnnotationConstants.DIAGNOSTIC_CODE_PREDESTROY_PARAMS)) { JavaCodeActionContext contextCopy = context.copy(); // each code action needs its own context. codeActions.addAll(RemovePreDestroyAnnotationQuickFix.getCodeActions(context, diagnostic)); From 3326cd8babc294f4b9c404883590168a0b75aafb Mon Sep 17 00:00:00 2001 From: VAISAKH T Date: Wed, 4 Oct 2023 11:48:54 +0530 Subject: [PATCH 18/34] [OpenLiberty#513] - Implemented code action for the QuickFix class PostConstructReturnTypeQuickFix. --- .../PostConstructReturnTypeQuickFix.java | 61 ++++++++++++++++--- src/main/resources/META-INF/plugin.xml | 5 ++ .../PostConstructAnnotationTest.java | 27 ++++---- .../annotations/PostConstructAnnotation.java | 16 ----- 4 files changed, 74 insertions(+), 35 deletions(-) diff --git a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/annotations/PostConstructReturnTypeQuickFix.java b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/annotations/PostConstructReturnTypeQuickFix.java index 913645bab..fff4b57cd 100644 --- a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/annotations/PostConstructReturnTypeQuickFix.java +++ b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/annotations/PostConstructReturnTypeQuickFix.java @@ -18,13 +18,22 @@ import com.intellij.psi.util.PsiTreeUtil; import io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.Messages; import io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.codeAction.proposal.ModifyReturnTypeProposal; +import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.java.codeaction.ExtendedCodeAction; +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 org.eclipse.lsp4j.CodeAction; +import org.eclipse.lsp4j.CodeActionKind; import org.eclipse.lsp4j.Diagnostic; +import org.eclipse.lsp4j.WorkspaceEdit; +import org.eclipse.lsp4mp.commons.CodeActionResolveData; import java.util.ArrayList; +import java.util.Collections; import java.util.List; +import java.util.logging.Level; +import java.util.logging.Logger; /** * Quick fix for AnnotationDiagnosticsCollector that changes the return type of a method to void. @@ -33,19 +42,55 @@ * @author Yijia Jing * */ -public class PostConstructReturnTypeQuickFix { +public class PostConstructReturnTypeQuickFix implements IJavaCodeActionParticipant { + + private static final Logger LOGGER = Logger.getLogger(PostConstructReturnTypeQuickFix.class.getName()); + + private final static String TITLE_MESSAGE = Messages.getMessage("ChangeReturnTypeToVoid"); + @Override + public String getParticipantId() { + return PostConstructReturnTypeQuickFix.class.getName(); + } + public List getCodeActions(JavaCodeActionContext context, Diagnostic diagnostic) { List codeActions = new ArrayList<>(); - PsiElement node = context.getCoveredNode(); - PsiMethod parentType = getBinding(node); - String name = Messages.getMessage("ChangeReturnTypeToVoid"); - ChangeCorrectionProposal proposal = new ModifyReturnTypeProposal(name, context.getSource().getCompilationUnit(), - context.getASTRoot(), parentType, 0, PsiPrimitiveType.VOID); - CodeAction codeAction = context.convertToCodeAction(proposal, diagnostic); - codeActions.add(codeAction); + final PsiElement node = context.getCoveredNode(); + final PsiMethod parentType = getBinding(node); + if (parentType != null) { + codeActions.add(createCodeAction(context, diagnostic)); + } return codeActions; } + @Override + public CodeAction resolveCodeAction(JavaCodeActionResolveContext context) { + final CodeAction toResolve = context.getUnresolved(); + final PsiElement node = context.getCoveredNode(); + final PsiMethod parentType = getBinding(node); + assert parentType != null; + ChangeCorrectionProposal proposal = new ModifyReturnTypeProposal(TITLE_MESSAGE, context.getCompilationUnit(), + context.getASTRoot(), parentType, 0, PsiPrimitiveType.VOID); + try { + WorkspaceEdit we = context.convertToWorkspaceEdit(proposal); + toResolve.setEdit(we); + } catch (Exception e) { + LOGGER.log(Level.WARNING, "Unable to create workspace edit for code action to change return type to void", e); + } + return toResolve; + } + + private CodeAction createCodeAction(JavaCodeActionContext context, Diagnostic diagnostic) { + ExtendedCodeAction codeAction = new ExtendedCodeAction(TITLE_MESSAGE); + codeAction.setRelevance(0); + codeAction.setDiagnostics(Collections.singletonList(diagnostic)); + codeAction.setKind(CodeActionKind.QuickFix); + codeAction.setData(new CodeActionResolveData(context.getUri(), getParticipantId(), + context.getParams().getRange(), Collections.emptyMap(), + context.getParams().isResourceOperationSupported(), + context.getParams().isCommandConfigurationUpdateSupported())); + return codeAction; + } + protected PsiMethod getBinding(PsiElement node) { if (node instanceof PsiMethod) { return (PsiMethod) node; diff --git a/src/main/resources/META-INF/plugin.xml b/src/main/resources/META-INF/plugin.xml index 97eb3c874..8af0adaaa 100644 --- a/src/main/resources/META-INF/plugin.xml +++ b/src/main/resources/META-INF/plugin.xml @@ -256,6 +256,11 @@ targetDiagnostic="jakarta-cdi#InvalidManagedBeanConstructor" implementationClass="io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.cdi.ManagedBeanNoArgConstructorQuickFix"/> + + diff --git a/src/test/java/io/openliberty/tools/intellij/lsp4jakarta/it/annotations/PostConstructAnnotationTest.java b/src/test/java/io/openliberty/tools/intellij/lsp4jakarta/it/annotations/PostConstructAnnotationTest.java index 025a3e7f6..cb28690cc 100644 --- a/src/test/java/io/openliberty/tools/intellij/lsp4jakarta/it/annotations/PostConstructAnnotationTest.java +++ b/src/test/java/io/openliberty/tools/intellij/lsp4jakarta/it/annotations/PostConstructAnnotationTest.java @@ -26,7 +26,6 @@ import org.eclipse.lsp4j.TextEdit; import org.eclipse.lsp4jakarta.commons.JakartaDiagnosticsParams; import org.eclipse.lsp4jakarta.commons.JakartaJavaCodeActionParams; -import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @@ -53,18 +52,28 @@ public void GeneratedAnnotation() throws Exception { // expected Diagnostics - Diagnostic d1 = d(15, 19, 31, "A method with the @PostConstruct annotation must be void.", + Diagnostic d1 = d(11, 19, 31, "A method with the @PostConstruct annotation must be void.", DiagnosticSeverity.Error, "jakarta-annotations", "PostConstructReturnType"); - Diagnostic d2 = d(20, 16, 28, "A method with the @PostConstruct annotation must not have any parameters.", - DiagnosticSeverity.Error, "jakarta-annotations", "PostConstructParams"); + assertJavaDiagnostics(diagnosticsParams, utils, d1); - Diagnostic d3 = d(25, 16, 28, "A method with the @PostConstruct annotation must not throw checked exceptions.", - DiagnosticSeverity.Warning, "jakarta-annotations", "PostConstructException"); + JakartaJavaCodeActionParams codeActionParams2 = createCodeActionParams(uri, d1); + TextEdit te3 = te(0, 0, 16, 0, "package io.openliberty.sample.jakarta.annotations;\n\nimport jakarta.annotation.PostConstruct;\nimport jakarta.annotation.Resource;\n\n@Resource(type = Object.class, name = \"aa\")\npublic class PostConstructAnnotation {\n\n private Integer studentId;\n\n @PostConstruct()\n public void getStudentId() {\n return this.studentId;\n }\n\n}\n"); + CodeAction ca3 = ca(uri, "Change return type to void", d1, te3); + assertJavaCodeAction(codeActionParams2, utils, ca3); - assertJavaDiagnostics(diagnosticsParams, utils, d1, d2, d3); + // TODO : Enable the remaining test cases once the refactoring is completed. if (CHECK_CODE_ACTIONS) { + + Diagnostic d2 = d(20, 16, 28, "A method with the @PostConstruct annotation must not have any parameters.", + DiagnosticSeverity.Error, "jakarta-annotations", "PostConstructParams"); + + Diagnostic d3 = d(25, 16, 28, "A method with the @PostConstruct annotation must not throw checked exceptions.", + DiagnosticSeverity.Warning, "jakarta-annotations", "PostConstructException"); + + assertJavaDiagnostics(diagnosticsParams, utils, d1, d2, d3); + JakartaJavaCodeActionParams codeActionParams1 = createCodeActionParams(uri, d2); TextEdit te1 = te(19, 4, 20, 4, ""); TextEdit te2 = te(20, 29, 20, 40, ""); @@ -72,10 +81,6 @@ public void GeneratedAnnotation() throws Exception { CodeAction ca2 = ca(uri, "Remove all parameters", d2, te2); assertJavaCodeAction(codeActionParams1, utils, ca1, ca2); - JakartaJavaCodeActionParams codeActionParams2 = createCodeActionParams(uri, d1); - TextEdit te3 = te(15, 11, 15, 18, "void"); - CodeAction ca3 = ca(uri, "Change return type to void", d1, te3); - assertJavaCodeAction(codeActionParams2, utils, ca3); } } } diff --git a/src/test/resources/projects/maven/jakarta-sample/src/main/java/io/openliberty/sample/jakarta/annotations/PostConstructAnnotation.java b/src/test/resources/projects/maven/jakarta-sample/src/main/java/io/openliberty/sample/jakarta/annotations/PostConstructAnnotation.java index 52b1166c8..ef8708c77 100644 --- a/src/test/resources/projects/maven/jakarta-sample/src/main/java/io/openliberty/sample/jakarta/annotations/PostConstructAnnotation.java +++ b/src/test/resources/projects/maven/jakarta-sample/src/main/java/io/openliberty/sample/jakarta/annotations/PostConstructAnnotation.java @@ -8,25 +8,9 @@ public class PostConstructAnnotation { private Integer studentId; - private boolean isHappy; - - private boolean isSad; - @PostConstruct() public Integer getStudentId() { return this.studentId; } - @PostConstruct - public void getHappiness(String type) { - - } - - @PostConstruct - public void throwTantrum() throws Exception { - System.out.println("I'm sad"); - } - - private String emailAddress; - } From b568431fa790a561f6e7112f8c9b2e7a9c854518 Mon Sep 17 00:00:00 2001 From: VAISAKH T Date: Thu, 5 Oct 2023 12:59:41 +0530 Subject: [PATCH 19/34] [OpenLiberty#513] - Reverting the changes done for GeneratedAnnotation method in PostConstructAnnotationTest --- .../PostConstructReturnTypeQuickFix.java | 4 +- .../PostConstructAnnotationTest.java | 51 +++++++++++++++---- .../annotations/PostConstructAnnotation.java | 18 ++++++- 3 files changed, 60 insertions(+), 13 deletions(-) diff --git a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/annotations/PostConstructReturnTypeQuickFix.java b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/annotations/PostConstructReturnTypeQuickFix.java index fff4b57cd..dcb10ecb7 100644 --- a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/annotations/PostConstructReturnTypeQuickFix.java +++ b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/annotations/PostConstructReturnTypeQuickFix.java @@ -45,8 +45,8 @@ public class PostConstructReturnTypeQuickFix implements IJavaCodeActionParticipant { private static final Logger LOGGER = Logger.getLogger(PostConstructReturnTypeQuickFix.class.getName()); - private final static String TITLE_MESSAGE = Messages.getMessage("ChangeReturnTypeToVoid"); + @Override public String getParticipantId() { return PostConstructReturnTypeQuickFix.class.getName(); @@ -56,6 +56,7 @@ public List getCodeActions(JavaCodeActionContext context, List codeActions = new ArrayList<>(); final PsiElement node = context.getCoveredNode(); final PsiMethod parentType = getBinding(node); + if (parentType != null) { codeActions.add(createCodeAction(context, diagnostic)); } @@ -67,6 +68,7 @@ public CodeAction resolveCodeAction(JavaCodeActionResolveContext context) { final CodeAction toResolve = context.getUnresolved(); final PsiElement node = context.getCoveredNode(); final PsiMethod parentType = getBinding(node); + assert parentType != null; ChangeCorrectionProposal proposal = new ModifyReturnTypeProposal(TITLE_MESSAGE, context.getCompilationUnit(), context.getASTRoot(), parentType, 0, PsiPrimitiveType.VOID); diff --git a/src/test/java/io/openliberty/tools/intellij/lsp4jakarta/it/annotations/PostConstructAnnotationTest.java b/src/test/java/io/openliberty/tools/intellij/lsp4jakarta/it/annotations/PostConstructAnnotationTest.java index cb28690cc..62b9efa6d 100644 --- a/src/test/java/io/openliberty/tools/intellij/lsp4jakarta/it/annotations/PostConstructAnnotationTest.java +++ b/src/test/java/io/openliberty/tools/intellij/lsp4jakarta/it/annotations/PostConstructAnnotationTest.java @@ -52,13 +52,50 @@ public void GeneratedAnnotation() throws Exception { // expected Diagnostics - Diagnostic d1 = d(11, 19, 31, "A method with the @PostConstruct annotation must be void.", + Diagnostic d1 = d(15, 19, 31, "A method with the @PostConstruct annotation must be void.", DiagnosticSeverity.Error, "jakarta-annotations", "PostConstructReturnType"); - assertJavaDiagnostics(diagnosticsParams, utils, d1); + Diagnostic d2 = d(20, 16, 28, "A method with the @PostConstruct annotation must not have any parameters.", + DiagnosticSeverity.Error, "jakarta-annotations", "PostConstructParams"); + + Diagnostic d3 = d(25, 16, 28, "A method with the @PostConstruct annotation must not throw checked exceptions.", + DiagnosticSeverity.Warning, "jakarta-annotations", "PostConstructException"); + + assertJavaDiagnostics(diagnosticsParams, utils, d1, d2, d3); JakartaJavaCodeActionParams codeActionParams2 = createCodeActionParams(uri, d1); - TextEdit te3 = te(0, 0, 16, 0, "package io.openliberty.sample.jakarta.annotations;\n\nimport jakarta.annotation.PostConstruct;\nimport jakarta.annotation.Resource;\n\n@Resource(type = Object.class, name = \"aa\")\npublic class PostConstructAnnotation {\n\n private Integer studentId;\n\n @PostConstruct()\n public void getStudentId() {\n return this.studentId;\n }\n\n}\n"); + TextEdit te3 = te(0, 0, 31, 1, "package io.openliberty.sample.jakarta.annotations;\n" + + "\n" + + "import jakarta.annotation.PostConstruct;\n" + + "import jakarta.annotation.Resource;\n" + + "\n" + + "@Resource(type = Object.class, name = \"aa\")\n" + + "public class PostConstructAnnotation {\n" + + "\n" + + " private Integer studentId;\n" + + "\n" + + " private boolean isHappy;\n" + + "\n" + + " private boolean isSad;\n" + + "\n" + + " @PostConstruct()\n" + + " public void getStudentId() {\n" + + " return this.studentId;\n" + + " }\n" + + "\n" + + " @PostConstruct\n" + + " public void getHappiness(String type) {\n" + + "\n" + + " }\n" + + "\n" + + " @PostConstruct\n" + + " public void throwTantrum() throws Exception {\n" + + " System.out.println(\"I'm sad\");\n" + + " }\n" + + "\n" + + " private String emailAddress;\n" + + "\n" + + "}"); CodeAction ca3 = ca(uri, "Change return type to void", d1, te3); assertJavaCodeAction(codeActionParams2, utils, ca3); @@ -66,14 +103,6 @@ public void GeneratedAnnotation() throws Exception { if (CHECK_CODE_ACTIONS) { - Diagnostic d2 = d(20, 16, 28, "A method with the @PostConstruct annotation must not have any parameters.", - DiagnosticSeverity.Error, "jakarta-annotations", "PostConstructParams"); - - Diagnostic d3 = d(25, 16, 28, "A method with the @PostConstruct annotation must not throw checked exceptions.", - DiagnosticSeverity.Warning, "jakarta-annotations", "PostConstructException"); - - assertJavaDiagnostics(diagnosticsParams, utils, d1, d2, d3); - JakartaJavaCodeActionParams codeActionParams1 = createCodeActionParams(uri, d2); TextEdit te1 = te(19, 4, 20, 4, ""); TextEdit te2 = te(20, 29, 20, 40, ""); diff --git a/src/test/resources/projects/maven/jakarta-sample/src/main/java/io/openliberty/sample/jakarta/annotations/PostConstructAnnotation.java b/src/test/resources/projects/maven/jakarta-sample/src/main/java/io/openliberty/sample/jakarta/annotations/PostConstructAnnotation.java index ef8708c77..64c7b3ba2 100644 --- a/src/test/resources/projects/maven/jakarta-sample/src/main/java/io/openliberty/sample/jakarta/annotations/PostConstructAnnotation.java +++ b/src/test/resources/projects/maven/jakarta-sample/src/main/java/io/openliberty/sample/jakarta/annotations/PostConstructAnnotation.java @@ -8,9 +8,25 @@ public class PostConstructAnnotation { private Integer studentId; + private boolean isHappy; + + private boolean isSad; + @PostConstruct() public Integer getStudentId() { return this.studentId; } -} + @PostConstruct + public void getHappiness(String type) { + + } + + @PostConstruct + public void throwTantrum() throws Exception { + System.out.println("I'm sad"); + } + + private String emailAddress; + +} \ No newline at end of file From b0235a31b77f277800fefa6c1372f9b5e3750fcb Mon Sep 17 00:00:00 2001 From: VAISAKH T Date: Fri, 13 Oct 2023 10:33:57 +0530 Subject: [PATCH 20/34] Updated source PsiFile parameter --- .../lsp4ij/annotations/PostConstructReturnTypeQuickFix.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/annotations/PostConstructReturnTypeQuickFix.java b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/annotations/PostConstructReturnTypeQuickFix.java index dcb10ecb7..e57af2971 100644 --- a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/annotations/PostConstructReturnTypeQuickFix.java +++ b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/annotations/PostConstructReturnTypeQuickFix.java @@ -70,7 +70,7 @@ public CodeAction resolveCodeAction(JavaCodeActionResolveContext context) { final PsiMethod parentType = getBinding(node); assert parentType != null; - ChangeCorrectionProposal proposal = new ModifyReturnTypeProposal(TITLE_MESSAGE, context.getCompilationUnit(), + ChangeCorrectionProposal proposal = new ModifyReturnTypeProposal(TITLE_MESSAGE, context.getSource().getCompilationUnit(), context.getASTRoot(), parentType, 0, PsiPrimitiveType.VOID); try { WorkspaceEdit we = context.convertToWorkspaceEdit(proposal); From 18b34b3845a8f4a10102af13e3c731e00d9ea40b Mon Sep 17 00:00:00 2001 From: Rahul Biju Date: Fri, 20 Oct 2023 06:26:05 +0530 Subject: [PATCH 21/34] [#517] fixes the plugin in HttpServletQuickFix. --- src/main/resources/META-INF/plugin.xml | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/main/resources/META-INF/plugin.xml b/src/main/resources/META-INF/plugin.xml index 8af0adaaa..97eb3c874 100644 --- a/src/main/resources/META-INF/plugin.xml +++ b/src/main/resources/META-INF/plugin.xml @@ -256,11 +256,6 @@ targetDiagnostic="jakarta-cdi#InvalidManagedBeanConstructor" implementationClass="io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.cdi.ManagedBeanNoArgConstructorQuickFix"/> - - From 29bec0d784d1b4f5875d01f04620d6d6c6ada631 Mon Sep 17 00:00:00 2001 From: anusreelakshmi934 Date: Wed, 4 Oct 2023 16:44:06 +0530 Subject: [PATCH 22/34] Transforming Quickfixes related to AddConstructorProposal #514 Refactored the code to implement the code action participant extension point. Jakarta EE code action tests enabled. --- .../ManagedBeanNoArgConstructorQuickFix.java | 90 ++++++++++-------- .../PersistenceEntityQuickFix.java | 92 +++++++++++-------- .../it/cdi/ManagedBeanConstructorTest.java | 24 +++-- .../persistence/JakartaPersistenceTest.java | 13 ++- 4 files changed, 123 insertions(+), 96 deletions(-) diff --git a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/cdi/ManagedBeanNoArgConstructorQuickFix.java b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/cdi/ManagedBeanNoArgConstructorQuickFix.java index 388779d68..5f2d777a8 100644 --- a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/cdi/ManagedBeanNoArgConstructorQuickFix.java +++ b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/cdi/ManagedBeanNoArgConstructorQuickFix.java @@ -15,18 +15,24 @@ import com.intellij.psi.PsiClass; import com.intellij.psi.PsiElement; -import com.intellij.psi.PsiModifierListOwner; import com.intellij.psi.util.PsiTreeUtil; -import io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.Messages; import io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.codeAction.proposal.AddConstructorProposal; +import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.java.codeaction.ExtendedCodeAction; +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 org.eclipse.lsp4j.CodeAction; +import org.eclipse.lsp4j.CodeActionKind; import org.eclipse.lsp4j.Diagnostic; +import org.eclipse.lsp4j.WorkspaceEdit; +import org.eclipse.lsp4mp.commons.CodeActionResolveData; import java.util.ArrayList; import java.util.Collections; import java.util.List; +import java.util.logging.Level; +import java.util.logging.Logger; /** * @@ -37,57 +43,65 @@ * */ -public class ManagedBeanNoArgConstructorQuickFix { +public class ManagedBeanNoArgConstructorQuickFix implements IJavaCodeActionParticipant { + private static final Logger LOGGER = Logger.getLogger(ManagedBeanNoArgConstructorQuickFix.class.getName()); + @Override + public String getParticipantId() { + return ManagedBeanNoArgConstructorQuickFix.class.getName(); + } public List getCodeActions(JavaCodeActionContext context, Diagnostic diagnostic) { - PsiElement node = context.getCoveredNode(); - PsiClass parentType = getBinding(node); + return addConstructor(diagnostic,context); + } + + @Override + public CodeAction resolveCodeAction(JavaCodeActionResolveContext context) { + final CodeAction toResolve = context.getUnresolved(); + final PsiElement node = context.getCoveredNode(); + final PsiClass parentType = PsiTreeUtil.getParentOfType(node, PsiClass.class); + if (parentType != null) { - List codeActions = new ArrayList<>(); + String constructorName = toResolve.getTitle(); + ChangeCorrectionProposal proposal = new AddConstructorProposal(constructorName, + context.getSource().getCompilationUnit(), context.getASTRoot(), parentType, 0, + constructorName.equals("AddProtectedConstructor") ? "protected" : "public"); - codeActions.addAll(addConstructor(diagnostic, context, parentType)); + try { + WorkspaceEdit we = context.convertToWorkspaceEdit(proposal); + toResolve.setEdit(we); + } catch (Exception e) { + LOGGER.log(Level.WARNING, "Unable to create workspace edit for code actions to add constructors", e); + } - return codeActions; } - return Collections.emptyList(); + return toResolve; } protected PsiClass getBinding(PsiElement node) { return PsiTreeUtil.getParentOfType(node, PsiClass.class); } - private List addConstructor(Diagnostic diagnostic, JavaCodeActionContext context, PsiClass notUsed) { + private List addConstructor(Diagnostic diagnostic, JavaCodeActionContext context) { List codeActions = new ArrayList<>(); - JavaCodeActionContext targetContext = null; - PsiElement node = null; - PsiClass parentType = null; - - // option for protected constructor - targetContext = context.copy(); - node = targetContext.getCoveredNode(); - parentType = getBinding(node); - String name = Messages.getMessage("AddProtectedConstructor"); - ChangeCorrectionProposal proposal = new AddConstructorProposal(name, - targetContext.getSource().getCompilationUnit(), targetContext.getASTRoot(), parentType, 0); - CodeAction codeAction = targetContext.convertToCodeAction(proposal, diagnostic); - - if (codeAction != null) { - codeActions.add(codeAction); - } - - // option for public constructor - targetContext = context.copy(); - node = targetContext.getCoveredNode(); - parentType = getBinding(node); - name = Messages.getMessage("AddPublicConstructor"); - proposal = new AddConstructorProposal(name, - targetContext.getSource().getCompilationUnit(), targetContext.getASTRoot(), parentType, 0, "public"); - codeAction = targetContext.convertToCodeAction(proposal, diagnostic); + String[] constructorNames = {"AddProtectedConstructor", "AddPublicConstructor"}; - if (codeAction != null) { + for (String name : constructorNames) { + CodeAction codeAction = createCodeAction(context, diagnostic, name); codeActions.add(codeAction); } - return codeActions; } -} + private CodeAction createCodeAction(JavaCodeActionContext context, Diagnostic diagnostic, String label) { + ExtendedCodeAction codeAction = new ExtendedCodeAction(label); + codeAction.setRelevance(0); + codeAction.setDiagnostics(Collections.singletonList(diagnostic)); + codeAction.setKind(CodeActionKind.QuickFix); + codeAction.setTitle(label); + codeAction.setData(new CodeActionResolveData(context.getUri(), getParticipantId(), + context.getParams().getRange(), Collections.emptyMap(), + context.getParams().isResourceOperationSupported(), + context.getParams().isCommandConfigurationUpdateSupported())); + return codeAction; + } + +} \ No newline at end of file diff --git a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/persistence/PersistenceEntityQuickFix.java b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/persistence/PersistenceEntityQuickFix.java index 3f3635ab7..78d9824b3 100644 --- a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/persistence/PersistenceEntityQuickFix.java +++ b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/persistence/PersistenceEntityQuickFix.java @@ -16,15 +16,23 @@ import com.intellij.psi.PsiClass; import com.intellij.psi.PsiElement; import com.intellij.psi.util.PsiTreeUtil; -import io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.Messages; import io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.codeAction.proposal.AddConstructorProposal; +import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.java.codeaction.ExtendedCodeAction; +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 org.eclipse.lsp4j.CodeAction; +import org.eclipse.lsp4j.CodeActionKind; import org.eclipse.lsp4j.Diagnostic; +import org.eclipse.lsp4j.WorkspaceEdit; +import org.eclipse.lsp4mp.commons.CodeActionResolveData; import java.util.ArrayList; +import java.util.Collections; import java.util.List; +import java.util.logging.Level; +import java.util.logging.Logger; /** * QuickFix for fixing {@link PersistenceConstants#DIAGNOSTIC_CODE_MISSING_ATTRIBUTES} error @@ -54,59 +62,67 @@ * @author Leslie Dawson (lamminade) * */ -public class PersistenceEntityQuickFix { +public class PersistenceEntityQuickFix implements IJavaCodeActionParticipant { + private static final Logger LOGGER = Logger.getLogger(PersistenceEntityQuickFix.class.getName()); + @Override + public String getParticipantId() { + return PersistenceEntityQuickFix.class.getName(); + } + public List getCodeActions(JavaCodeActionContext context, Diagnostic diagnostic) { - List codeActions = new ArrayList<>(); - PsiElement node = context.getCoveredNode(); - PsiClass parentType = getBinding(node); + return addConstructor(diagnostic,context); + } + + @Override + public CodeAction resolveCodeAction(JavaCodeActionResolveContext context) { + final CodeAction toResolve = context.getUnresolved(); + final PsiElement node = context.getCoveredNode(); + final PsiClass parentType = PsiTreeUtil.getParentOfType(node, PsiClass.class); + if (parentType != null) { + String constructorName = toResolve.getTitle(); + ChangeCorrectionProposal proposal = new AddConstructorProposal(constructorName, + context.getSource().getCompilationUnit(), context.getASTRoot(), parentType, 0, + constructorName.equals("AddNoArgProtectedConstructor") ? "protected" : "public"); - // add constructor - if (diagnostic.getCode().getLeft().equals(PersistenceConstants.DIAGNOSTIC_CODE_MISSING_EMPTY_CONSTRUCTOR)) { - codeActions.addAll(addConstructor(diagnostic, context, parentType)); + try { + WorkspaceEdit we = context.convertToWorkspaceEdit(proposal); + toResolve.setEdit(we); + } catch (Exception e) { + LOGGER.log(Level.WARNING, "Unable to create workspace edit for code actions to add constructors", e); } } - return codeActions; + return toResolve; } + + protected PsiClass getBinding(PsiElement node) { return PsiTreeUtil.getParentOfType(node, PsiClass.class); } - private List addConstructor(Diagnostic diagnostic, JavaCodeActionContext context, PsiClass notUsed) { + private List addConstructor(Diagnostic diagnostic, JavaCodeActionContext context) { List codeActions = new ArrayList<>(); - JavaCodeActionContext targetContext = null; - PsiElement node = null; - PsiClass parentType = null; - - // option for protected constructor - targetContext = context.copy(); - node = targetContext.getCoveredNode(); - parentType = getBinding(node); - String name = Messages.getMessage("AddNoArgProtectedConstructor"); - ChangeCorrectionProposal proposal = new AddConstructorProposal(name, - targetContext.getSource().getCompilationUnit(), targetContext.getASTRoot(), parentType, 0); - CodeAction codeAction = targetContext.convertToCodeAction(proposal, diagnostic); - - if (codeAction != null) { - codeActions.add(codeAction); - } - - // option for public constructor - targetContext = context.copy(); - node = targetContext.getCoveredNode(); - parentType = getBinding(node); - name = Messages.getMessage("AddNoArgPublicConstructor"); - proposal = new AddConstructorProposal(name, - targetContext.getSource().getCompilationUnit(), targetContext.getASTRoot(), parentType, 0, "public"); - codeAction = targetContext.convertToCodeAction(proposal, diagnostic); + String[] constructorNames = {"AddNoArgProtectedConstructor", "AddNoArgPublicConstructor"}; - if (codeAction != null) { + for (String name : constructorNames) { + CodeAction codeAction = createCodeAction(context, diagnostic, name); codeActions.add(codeAction); } - return codeActions; } + private CodeAction createCodeAction(JavaCodeActionContext context, Diagnostic diagnostic, String label) { + ExtendedCodeAction codeAction = new ExtendedCodeAction(label); + codeAction.setRelevance(0); + codeAction.setDiagnostics(Collections.singletonList(diagnostic)); + codeAction.setKind(CodeActionKind.QuickFix); + codeAction.setTitle(label); + codeAction.setData(new CodeActionResolveData(context.getUri(), getParticipantId(), + context.getParams().getRange(), Collections.emptyMap(), + context.getParams().isResourceOperationSupported(), + context.getParams().isCommandConfigurationUpdateSupported())); + return codeAction; + } -} +} \ No newline at end of file diff --git a/src/test/java/io/openliberty/tools/intellij/lsp4jakarta/it/cdi/ManagedBeanConstructorTest.java b/src/test/java/io/openliberty/tools/intellij/lsp4jakarta/it/cdi/ManagedBeanConstructorTest.java index 9c68b711e..13ae3d2f2 100644 --- a/src/test/java/io/openliberty/tools/intellij/lsp4jakarta/it/cdi/ManagedBeanConstructorTest.java +++ b/src/test/java/io/openliberty/tools/intellij/lsp4jakarta/it/cdi/ManagedBeanConstructorTest.java @@ -27,7 +27,6 @@ import org.eclipse.lsp4j.TextEdit; import org.eclipse.lsp4jakarta.commons.JakartaDiagnosticsParams; import org.eclipse.lsp4jakarta.commons.JakartaJavaCodeActionParams; -import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @@ -59,20 +58,19 @@ public void managedBeanAnnotations() throws Exception { assertJavaDiagnostics(diagnosticsParams, utils, d); - if (CHECK_CODE_ACTIONS) { +// if (CHECK_CODE_ACTIONS) { +// TextEdit te1 = te(15, 44, 21, 1, +// "\nimport jakarta.inject.Inject;\n\n@Dependent\npublic class ManagedBeanConstructor {\n private int a;\n \n @Inject\n "); +// CodeAction ca1 = ca(uri, "Insert @Inject", d, te1); + // test expected quick-fix JakartaJavaCodeActionParams codeActionParams1 = createCodeActionParams(uri, d); - TextEdit te1 = te(15, 44, 21, 1, - "\nimport jakarta.inject.Inject;\n\n@Dependent\npublic class ManagedBeanConstructor {\n private int a;\n \n @Inject\n "); - TextEdit te2 = te(19, 1, 19, 1, - "protected ManagedBeanConstructor() {\n\t}\n\n\t"); - TextEdit te3 = te(19, 1, 19, 1, - "public ManagedBeanConstructor() {\n\t}\n\n\t"); - CodeAction ca1 = ca(uri, "Insert @Inject", d, te1); - CodeAction ca2 = ca(uri, "Add a no-arg protected constructor to this class", d, te2); - CodeAction ca3 = ca(uri, "Add a no-arg public constructor to this class", d, te3); - assertJavaCodeAction(codeActionParams1, utils, ca1, ca2, ca3); - } + TextEdit te2 = te(0, 0, 25, 0, "/*******************************************************************************\n * Copyright (c) 2021 IBM Corporation.\n *\n * This program and the accompanying materials are made available under the\n * terms of the Eclipse Public License v. 2.0 which is available at\n * http://www.eclipse.org/legal/epl-2.0.\n *\n * SPDX-License-Identifier: EPL-2.0\n *\n * Contributors:\n * Hani Damlaj\n *******************************************************************************/\n\npackage io.openliberty.sample.jakarta.cdi;\n\nimport jakarta.enterprise.context.Dependent;\n\n@Dependent\npublic class ManagedBeanConstructor {\n private int a;\n\n protected ManagedBeanConstructor() {\n }\n\n public ManagedBeanConstructor(int a) {\n this.a = a;\n }\n}\n"); + TextEdit te3 = te(0, 0, 25, 0, "/*******************************************************************************\n * Copyright (c) 2021 IBM Corporation.\n *\n * This program and the accompanying materials are made available under the\n * terms of the Eclipse Public License v. 2.0 which is available at\n * http://www.eclipse.org/legal/epl-2.0.\n *\n * SPDX-License-Identifier: EPL-2.0\n *\n * Contributors:\n * Hani Damlaj\n *******************************************************************************/\n\npackage io.openliberty.sample.jakarta.cdi;\n\nimport jakarta.enterprise.context.Dependent;\n\n@Dependent\npublic class ManagedBeanConstructor {\n private int a;\n\n public ManagedBeanConstructor() {\n }\n\n public ManagedBeanConstructor(int a) {\n this.a = a;\n }\n}\n"); + CodeAction ca2 = ca(uri, "AddProtectedConstructor", d, te2); + CodeAction ca3 = ca(uri, "AddPublicConstructor", d, te3); + assertJavaCodeAction(codeActionParams1, utils, ca2, ca3); +// } } } diff --git a/src/test/java/io/openliberty/tools/intellij/lsp4jakarta/it/persistence/JakartaPersistenceTest.java b/src/test/java/io/openliberty/tools/intellij/lsp4jakarta/it/persistence/JakartaPersistenceTest.java index f2fc112c3..3b139b53c 100644 --- a/src/test/java/io/openliberty/tools/intellij/lsp4jakarta/it/persistence/JakartaPersistenceTest.java +++ b/src/test/java/io/openliberty/tools/intellij/lsp4jakarta/it/persistence/JakartaPersistenceTest.java @@ -26,7 +26,6 @@ import org.eclipse.lsp4j.TextEdit; import org.eclipse.lsp4jakarta.commons.JakartaDiagnosticsParams; import org.eclipse.lsp4jakarta.commons.JakartaJavaCodeActionParams; -import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @@ -156,16 +155,16 @@ public void addEmptyConstructor() throws Exception { assertJavaDiagnostics(diagnosticsParams, utils, d); - if (CHECK_CODE_ACTIONS) { +// if (CHECK_CODE_ACTIONS) { // test quick fixes JakartaJavaCodeActionParams codeActionParams1 = createCodeActionParams(uri, d); - TextEdit te1 = te(7, 4, 7, 4, "protected EntityMissingConstructor() {\n\t}\n\n\t"); - CodeAction ca1 = ca(uri, "Add a no-arg protected constructor to this class", d, te1); - TextEdit te2 = te(7, 4, 7, 4, "public EntityMissingConstructor() {\n\t}\n\n\t"); - CodeAction ca2 = ca(uri, "Add a no-arg public constructor to this class", d, te2); + TextEdit te1 = te(0, 0, 9, 1, "package io.openliberty.sample.jakarta.persistence;\n\nimport jakarta.persistence.Entity;\n\n@Entity\npublic class EntityMissingConstructor {\n\n protected EntityMissingConstructor() {\n }\n\n private EntityMissingConstructor(int x) {\n }\n\n}"); + CodeAction ca1 = ca(uri, "AddNoArgProtectedConstructor", d, te1); + TextEdit te2 = te(0, 0, 9, 1, "package io.openliberty.sample.jakarta.persistence;\n\nimport jakarta.persistence.Entity;\n\n@Entity\npublic class EntityMissingConstructor {\n\n public EntityMissingConstructor() {\n }\n\n private EntityMissingConstructor(int x) {\n }\n\n}"); + CodeAction ca2 = ca(uri, "AddNoArgPublicConstructor", d, te2); assertJavaCodeAction(codeActionParams1, utils, ca1, ca2); - } +// } } @Test From 5abdd080badae736075e094b0137114152be455a Mon Sep 17 00:00:00 2001 From: anusreelakshmi934 Date: Tue, 10 Oct 2023 10:24:44 +0530 Subject: [PATCH 23/34] Changes completed #514 --- .../ManagedBeanNoArgConstructorQuickFix.java | 7 +++++-- .../persistence/PersistenceEntityQuickFix.java | 9 +++++---- .../it/cdi/ManagedBeanConstructorTest.java | 17 ++++++++++------- .../it/persistence/JakartaPersistenceTest.java | 5 +++-- 4 files changed, 23 insertions(+), 15 deletions(-) diff --git a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/cdi/ManagedBeanNoArgConstructorQuickFix.java b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/cdi/ManagedBeanNoArgConstructorQuickFix.java index 5f2d777a8..f1ed5b71c 100644 --- a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/cdi/ManagedBeanNoArgConstructorQuickFix.java +++ b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/cdi/ManagedBeanNoArgConstructorQuickFix.java @@ -16,6 +16,7 @@ import com.intellij.psi.PsiClass; import com.intellij.psi.PsiElement; import com.intellij.psi.util.PsiTreeUtil; +import io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.Messages; import io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.codeAction.proposal.AddConstructorProposal; import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.java.codeaction.ExtendedCodeAction; import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.java.codeaction.IJavaCodeActionParticipant; @@ -45,6 +46,7 @@ public class ManagedBeanNoArgConstructorQuickFix implements IJavaCodeActionParticipant { private static final Logger LOGGER = Logger.getLogger(ManagedBeanNoArgConstructorQuickFix.class.getName()); + @Override public String getParticipantId() { return ManagedBeanNoArgConstructorQuickFix.class.getName(); @@ -64,7 +66,7 @@ public CodeAction resolveCodeAction(JavaCodeActionResolveContext context) { String constructorName = toResolve.getTitle(); ChangeCorrectionProposal proposal = new AddConstructorProposal(constructorName, context.getSource().getCompilationUnit(), context.getASTRoot(), parentType, 0, - constructorName.equals("AddProtectedConstructor") ? "protected" : "public"); + constructorName.equals(Messages.getMessage("AddProtectedConstructor")) ? "protected" : "public"); try { WorkspaceEdit we = context.convertToWorkspaceEdit(proposal); @@ -83,7 +85,7 @@ protected PsiClass getBinding(PsiElement node) { private List addConstructor(Diagnostic diagnostic, JavaCodeActionContext context) { List codeActions = new ArrayList<>(); - String[] constructorNames = {"AddProtectedConstructor", "AddPublicConstructor"}; + String[] constructorNames = {Messages.getMessage("AddProtectedConstructor"), Messages.getMessage("AddPublicConstructor")}; for (String name : constructorNames) { CodeAction codeAction = createCodeAction(context, diagnostic, name); @@ -91,6 +93,7 @@ private List addConstructor(Diagnostic diagnostic, JavaCodeActionCon } return codeActions; } + private CodeAction createCodeAction(JavaCodeActionContext context, Diagnostic diagnostic, String label) { ExtendedCodeAction codeAction = new ExtendedCodeAction(label); codeAction.setRelevance(0); diff --git a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/persistence/PersistenceEntityQuickFix.java b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/persistence/PersistenceEntityQuickFix.java index 78d9824b3..ba85a629c 100644 --- a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/persistence/PersistenceEntityQuickFix.java +++ b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/persistence/PersistenceEntityQuickFix.java @@ -16,6 +16,7 @@ import com.intellij.psi.PsiClass; import com.intellij.psi.PsiElement; import com.intellij.psi.util.PsiTreeUtil; +import io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.Messages; import io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.codeAction.proposal.AddConstructorProposal; import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.java.codeaction.ExtendedCodeAction; import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.java.codeaction.IJavaCodeActionParticipant; @@ -64,6 +65,7 @@ */ public class PersistenceEntityQuickFix implements IJavaCodeActionParticipant { private static final Logger LOGGER = Logger.getLogger(PersistenceEntityQuickFix.class.getName()); + @Override public String getParticipantId() { return PersistenceEntityQuickFix.class.getName(); @@ -83,7 +85,7 @@ public CodeAction resolveCodeAction(JavaCodeActionResolveContext context) { String constructorName = toResolve.getTitle(); ChangeCorrectionProposal proposal = new AddConstructorProposal(constructorName, context.getSource().getCompilationUnit(), context.getASTRoot(), parentType, 0, - constructorName.equals("AddNoArgProtectedConstructor") ? "protected" : "public"); + constructorName.equals(Messages.getMessage("AddNoArgProtectedConstructor")) ? "protected" : "public"); try { WorkspaceEdit we = context.convertToWorkspaceEdit(proposal); @@ -96,15 +98,13 @@ public CodeAction resolveCodeAction(JavaCodeActionResolveContext context) { return toResolve; } - - protected PsiClass getBinding(PsiElement node) { return PsiTreeUtil.getParentOfType(node, PsiClass.class); } private List addConstructor(Diagnostic diagnostic, JavaCodeActionContext context) { List codeActions = new ArrayList<>(); - String[] constructorNames = {"AddNoArgProtectedConstructor", "AddNoArgPublicConstructor"}; + String[] constructorNames = {Messages.getMessage("AddNoArgProtectedConstructor"), Messages.getMessage("AddNoArgPublicConstructor")}; for (String name : constructorNames) { CodeAction codeAction = createCodeAction(context, diagnostic, name); @@ -112,6 +112,7 @@ private List addConstructor(Diagnostic diagnostic, JavaCodeActionCon } return codeActions; } + private CodeAction createCodeAction(JavaCodeActionContext context, Diagnostic diagnostic, String label) { ExtendedCodeAction codeAction = new ExtendedCodeAction(label); codeAction.setRelevance(0); diff --git a/src/test/java/io/openliberty/tools/intellij/lsp4jakarta/it/cdi/ManagedBeanConstructorTest.java b/src/test/java/io/openliberty/tools/intellij/lsp4jakarta/it/cdi/ManagedBeanConstructorTest.java index 13ae3d2f2..0467ed0c3 100644 --- a/src/test/java/io/openliberty/tools/intellij/lsp4jakarta/it/cdi/ManagedBeanConstructorTest.java +++ b/src/test/java/io/openliberty/tools/intellij/lsp4jakarta/it/cdi/ManagedBeanConstructorTest.java @@ -19,6 +19,7 @@ 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.lsp4jakarta.lsp4ij.Messages; 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; @@ -58,19 +59,21 @@ public void managedBeanAnnotations() throws Exception { assertJavaDiagnostics(diagnosticsParams, utils, d); -// if (CHECK_CODE_ACTIONS) { -// TextEdit te1 = te(15, 44, 21, 1, -// "\nimport jakarta.inject.Inject;\n\n@Dependent\npublic class ManagedBeanConstructor {\n private int a;\n \n @Inject\n "); -// CodeAction ca1 = ca(uri, "Insert @Inject", d, te1); + //TODO Enable the remaining test cases once the refactoring is completed + if (CHECK_CODE_ACTIONS) { + TextEdit te1 = te(15, 44, 21, 1, + "\nimport jakarta.inject.Inject;\n\n@Dependent\npublic class ManagedBeanConstructor {\n private int a;\n \n @Inject\n "); + CodeAction ca1 = ca(uri, "Insert @Inject", d, te1); + } // test expected quick-fix JakartaJavaCodeActionParams codeActionParams1 = createCodeActionParams(uri, d); TextEdit te2 = te(0, 0, 25, 0, "/*******************************************************************************\n * Copyright (c) 2021 IBM Corporation.\n *\n * This program and the accompanying materials are made available under the\n * terms of the Eclipse Public License v. 2.0 which is available at\n * http://www.eclipse.org/legal/epl-2.0.\n *\n * SPDX-License-Identifier: EPL-2.0\n *\n * Contributors:\n * Hani Damlaj\n *******************************************************************************/\n\npackage io.openliberty.sample.jakarta.cdi;\n\nimport jakarta.enterprise.context.Dependent;\n\n@Dependent\npublic class ManagedBeanConstructor {\n private int a;\n\n protected ManagedBeanConstructor() {\n }\n\n public ManagedBeanConstructor(int a) {\n this.a = a;\n }\n}\n"); TextEdit te3 = te(0, 0, 25, 0, "/*******************************************************************************\n * Copyright (c) 2021 IBM Corporation.\n *\n * This program and the accompanying materials are made available under the\n * terms of the Eclipse Public License v. 2.0 which is available at\n * http://www.eclipse.org/legal/epl-2.0.\n *\n * SPDX-License-Identifier: EPL-2.0\n *\n * Contributors:\n * Hani Damlaj\n *******************************************************************************/\n\npackage io.openliberty.sample.jakarta.cdi;\n\nimport jakarta.enterprise.context.Dependent;\n\n@Dependent\npublic class ManagedBeanConstructor {\n private int a;\n\n public ManagedBeanConstructor() {\n }\n\n public ManagedBeanConstructor(int a) {\n this.a = a;\n }\n}\n"); - CodeAction ca2 = ca(uri, "AddProtectedConstructor", d, te2); - CodeAction ca3 = ca(uri, "AddPublicConstructor", d, te3); + CodeAction ca2 = ca(uri, Messages.getMessage("AddProtectedConstructor"), d, te2); + CodeAction ca3 = ca(uri, Messages.getMessage("AddPublicConstructor"), d, te3); assertJavaCodeAction(codeActionParams1, utils, ca2, ca3); -// } + } } diff --git a/src/test/java/io/openliberty/tools/intellij/lsp4jakarta/it/persistence/JakartaPersistenceTest.java b/src/test/java/io/openliberty/tools/intellij/lsp4jakarta/it/persistence/JakartaPersistenceTest.java index 3b139b53c..6f5319556 100644 --- a/src/test/java/io/openliberty/tools/intellij/lsp4jakarta/it/persistence/JakartaPersistenceTest.java +++ b/src/test/java/io/openliberty/tools/intellij/lsp4jakarta/it/persistence/JakartaPersistenceTest.java @@ -18,6 +18,7 @@ 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.lsp4jakarta.lsp4ij.Messages; 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; @@ -159,9 +160,9 @@ public void addEmptyConstructor() throws Exception { // test quick fixes JakartaJavaCodeActionParams codeActionParams1 = createCodeActionParams(uri, d); TextEdit te1 = te(0, 0, 9, 1, "package io.openliberty.sample.jakarta.persistence;\n\nimport jakarta.persistence.Entity;\n\n@Entity\npublic class EntityMissingConstructor {\n\n protected EntityMissingConstructor() {\n }\n\n private EntityMissingConstructor(int x) {\n }\n\n}"); - CodeAction ca1 = ca(uri, "AddNoArgProtectedConstructor", d, te1); + CodeAction ca1 = ca(uri, Messages.getMessage("AddNoArgProtectedConstructor"), d, te1); TextEdit te2 = te(0, 0, 9, 1, "package io.openliberty.sample.jakarta.persistence;\n\nimport jakarta.persistence.Entity;\n\n@Entity\npublic class EntityMissingConstructor {\n\n public EntityMissingConstructor() {\n }\n\n private EntityMissingConstructor(int x) {\n }\n\n}"); - CodeAction ca2 = ca(uri, "AddNoArgPublicConstructor", d, te2); + CodeAction ca2 = ca(uri, Messages.getMessage("AddNoArgPublicConstructor"), d, te2); assertJavaCodeAction(codeActionParams1, utils, ca1, ca2); // } From d4be8d643954af507a43a99cde3f7a95b1eed6bf Mon Sep 17 00:00:00 2001 From: anusreelakshmi934 Date: Tue, 10 Oct 2023 10:51:30 +0530 Subject: [PATCH 24/34] Formatted the code and addressed review comments #514 --- .../ManagedBeanNoArgConstructorQuickFix.java | 3 +-- .../PersistenceEntityQuickFix.java | 4 +-- .../it/cdi/ManagedBeanConstructorTest.java | 25 +++++++++---------- .../persistence/JakartaPersistenceTest.java | 16 ++++++------ 4 files changed, 22 insertions(+), 26 deletions(-) diff --git a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/cdi/ManagedBeanNoArgConstructorQuickFix.java b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/cdi/ManagedBeanNoArgConstructorQuickFix.java index f1ed5b71c..161cdf510 100644 --- a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/cdi/ManagedBeanNoArgConstructorQuickFix.java +++ b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/cdi/ManagedBeanNoArgConstructorQuickFix.java @@ -53,7 +53,7 @@ public String getParticipantId() { } public List getCodeActions(JavaCodeActionContext context, Diagnostic diagnostic) { - return addConstructor(diagnostic,context); + return addConstructor(diagnostic, context); } @Override @@ -106,5 +106,4 @@ private CodeAction createCodeAction(JavaCodeActionContext context, Diagnostic di context.getParams().isCommandConfigurationUpdateSupported())); return codeAction; } - } \ No newline at end of file diff --git a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/persistence/PersistenceEntityQuickFix.java b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/persistence/PersistenceEntityQuickFix.java index ba85a629c..65c2f63d4 100644 --- a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/persistence/PersistenceEntityQuickFix.java +++ b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/persistence/PersistenceEntityQuickFix.java @@ -72,7 +72,7 @@ public String getParticipantId() { } public List getCodeActions(JavaCodeActionContext context, Diagnostic diagnostic) { - return addConstructor(diagnostic,context); + return addConstructor(diagnostic, context); } @Override @@ -93,7 +93,6 @@ public CodeAction resolveCodeAction(JavaCodeActionResolveContext context) { } catch (Exception e) { LOGGER.log(Level.WARNING, "Unable to create workspace edit for code actions to add constructors", e); } - } return toResolve; } @@ -125,5 +124,4 @@ private CodeAction createCodeAction(JavaCodeActionContext context, Diagnostic di context.getParams().isCommandConfigurationUpdateSupported())); return codeAction; } - } \ No newline at end of file diff --git a/src/test/java/io/openliberty/tools/intellij/lsp4jakarta/it/cdi/ManagedBeanConstructorTest.java b/src/test/java/io/openliberty/tools/intellij/lsp4jakarta/it/cdi/ManagedBeanConstructorTest.java index 0467ed0c3..f8a407c09 100644 --- a/src/test/java/io/openliberty/tools/intellij/lsp4jakarta/it/cdi/ManagedBeanConstructorTest.java +++ b/src/test/java/io/openliberty/tools/intellij/lsp4jakarta/it/cdi/ManagedBeanConstructorTest.java @@ -59,21 +59,20 @@ public void managedBeanAnnotations() throws Exception { assertJavaDiagnostics(diagnosticsParams, utils, d); - //TODO Enable the remaining test cases once the refactoring is completed + //TODO Enable the remaining test cases once the refactoring is completed if (CHECK_CODE_ACTIONS) { - TextEdit te1 = te(15, 44, 21, 1, - "\nimport jakarta.inject.Inject;\n\n@Dependent\npublic class ManagedBeanConstructor {\n private int a;\n \n @Inject\n "); - CodeAction ca1 = ca(uri, "Insert @Inject", d, te1); - } + TextEdit te1 = te(15, 44, 21, 1, + "\nimport jakarta.inject.Inject;\n\n@Dependent\npublic class ManagedBeanConstructor {\n private int a;\n \n @Inject\n "); + CodeAction ca1 = ca(uri, "Insert @Inject", d, te1); + } - // test expected quick-fix - JakartaJavaCodeActionParams codeActionParams1 = createCodeActionParams(uri, d); - TextEdit te2 = te(0, 0, 25, 0, "/*******************************************************************************\n * Copyright (c) 2021 IBM Corporation.\n *\n * This program and the accompanying materials are made available under the\n * terms of the Eclipse Public License v. 2.0 which is available at\n * http://www.eclipse.org/legal/epl-2.0.\n *\n * SPDX-License-Identifier: EPL-2.0\n *\n * Contributors:\n * Hani Damlaj\n *******************************************************************************/\n\npackage io.openliberty.sample.jakarta.cdi;\n\nimport jakarta.enterprise.context.Dependent;\n\n@Dependent\npublic class ManagedBeanConstructor {\n private int a;\n\n protected ManagedBeanConstructor() {\n }\n\n public ManagedBeanConstructor(int a) {\n this.a = a;\n }\n}\n"); - TextEdit te3 = te(0, 0, 25, 0, "/*******************************************************************************\n * Copyright (c) 2021 IBM Corporation.\n *\n * This program and the accompanying materials are made available under the\n * terms of the Eclipse Public License v. 2.0 which is available at\n * http://www.eclipse.org/legal/epl-2.0.\n *\n * SPDX-License-Identifier: EPL-2.0\n *\n * Contributors:\n * Hani Damlaj\n *******************************************************************************/\n\npackage io.openliberty.sample.jakarta.cdi;\n\nimport jakarta.enterprise.context.Dependent;\n\n@Dependent\npublic class ManagedBeanConstructor {\n private int a;\n\n public ManagedBeanConstructor() {\n }\n\n public ManagedBeanConstructor(int a) {\n this.a = a;\n }\n}\n"); - CodeAction ca2 = ca(uri, Messages.getMessage("AddProtectedConstructor"), d, te2); - CodeAction ca3 = ca(uri, Messages.getMessage("AddPublicConstructor"), d, te3); - assertJavaCodeAction(codeActionParams1, utils, ca2, ca3); + // test expected quick-fix + JakartaJavaCodeActionParams codeActionParams1 = createCodeActionParams(uri, d); + TextEdit te2 = te(0, 0, 25, 0, "/*******************************************************************************\n * Copyright (c) 2021 IBM Corporation.\n *\n * This program and the accompanying materials are made available under the\n * terms of the Eclipse Public License v. 2.0 which is available at\n * http://www.eclipse.org/legal/epl-2.0.\n *\n * SPDX-License-Identifier: EPL-2.0\n *\n * Contributors:\n * Hani Damlaj\n *******************************************************************************/\n\npackage io.openliberty.sample.jakarta.cdi;\n\nimport jakarta.enterprise.context.Dependent;\n\n@Dependent\npublic class ManagedBeanConstructor {\n private int a;\n\n protected ManagedBeanConstructor() {\n }\n\n public ManagedBeanConstructor(int a) {\n this.a = a;\n }\n}\n"); + TextEdit te3 = te(0, 0, 25, 0, "/*******************************************************************************\n * Copyright (c) 2021 IBM Corporation.\n *\n * This program and the accompanying materials are made available under the\n * terms of the Eclipse Public License v. 2.0 which is available at\n * http://www.eclipse.org/legal/epl-2.0.\n *\n * SPDX-License-Identifier: EPL-2.0\n *\n * Contributors:\n * Hani Damlaj\n *******************************************************************************/\n\npackage io.openliberty.sample.jakarta.cdi;\n\nimport jakarta.enterprise.context.Dependent;\n\n@Dependent\npublic class ManagedBeanConstructor {\n private int a;\n\n public ManagedBeanConstructor() {\n }\n\n public ManagedBeanConstructor(int a) {\n this.a = a;\n }\n}\n"); + CodeAction ca2 = ca(uri, Messages.getMessage("AddProtectedConstructor"), d, te2); + CodeAction ca3 = ca(uri, Messages.getMessage("AddPublicConstructor"), d, te3); + assertJavaCodeAction(codeActionParams1, utils, ca2, ca3); } - } diff --git a/src/test/java/io/openliberty/tools/intellij/lsp4jakarta/it/persistence/JakartaPersistenceTest.java b/src/test/java/io/openliberty/tools/intellij/lsp4jakarta/it/persistence/JakartaPersistenceTest.java index 6f5319556..42e0300fb 100644 --- a/src/test/java/io/openliberty/tools/intellij/lsp4jakarta/it/persistence/JakartaPersistenceTest.java +++ b/src/test/java/io/openliberty/tools/intellij/lsp4jakarta/it/persistence/JakartaPersistenceTest.java @@ -157,14 +157,14 @@ public void addEmptyConstructor() throws Exception { assertJavaDiagnostics(diagnosticsParams, utils, d); // if (CHECK_CODE_ACTIONS) { - // test quick fixes - JakartaJavaCodeActionParams codeActionParams1 = createCodeActionParams(uri, d); - TextEdit te1 = te(0, 0, 9, 1, "package io.openliberty.sample.jakarta.persistence;\n\nimport jakarta.persistence.Entity;\n\n@Entity\npublic class EntityMissingConstructor {\n\n protected EntityMissingConstructor() {\n }\n\n private EntityMissingConstructor(int x) {\n }\n\n}"); - CodeAction ca1 = ca(uri, Messages.getMessage("AddNoArgProtectedConstructor"), d, te1); - TextEdit te2 = te(0, 0, 9, 1, "package io.openliberty.sample.jakarta.persistence;\n\nimport jakarta.persistence.Entity;\n\n@Entity\npublic class EntityMissingConstructor {\n\n public EntityMissingConstructor() {\n }\n\n private EntityMissingConstructor(int x) {\n }\n\n}"); - CodeAction ca2 = ca(uri, Messages.getMessage("AddNoArgPublicConstructor"), d, te2); - - assertJavaCodeAction(codeActionParams1, utils, ca1, ca2); + // test quick fixes + JakartaJavaCodeActionParams codeActionParams1 = createCodeActionParams(uri, d); + TextEdit te1 = te(0, 0, 9, 1, "package io.openliberty.sample.jakarta.persistence;\n\nimport jakarta.persistence.Entity;\n\n@Entity\npublic class EntityMissingConstructor {\n\n protected EntityMissingConstructor() {\n }\n\n private EntityMissingConstructor(int x) {\n }\n\n}"); + CodeAction ca1 = ca(uri, Messages.getMessage("AddNoArgProtectedConstructor"), d, te1); + TextEdit te2 = te(0, 0, 9, 1, "package io.openliberty.sample.jakarta.persistence;\n\nimport jakarta.persistence.Entity;\n\n@Entity\npublic class EntityMissingConstructor {\n\n public EntityMissingConstructor() {\n }\n\n private EntityMissingConstructor(int x) {\n }\n\n}"); + CodeAction ca2 = ca(uri, Messages.getMessage("AddNoArgPublicConstructor"), d, te2); + + assertJavaCodeAction(codeActionParams1, utils, ca1, ca2); // } } From cf234233c7f399cab492564cff5ea68ad6107267 Mon Sep 17 00:00:00 2001 From: anusreelakshmi934 Date: Wed, 18 Oct 2023 12:57:40 +0530 Subject: [PATCH 25/34] Moved the checks into getCodeActions() #514 --- .../ManagedBeanNoArgConstructorQuickFix.java | 34 +++++++++++-------- .../PersistenceEntityQuickFix.java | 33 ++++++++++-------- 2 files changed, 38 insertions(+), 29 deletions(-) diff --git a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/cdi/ManagedBeanNoArgConstructorQuickFix.java b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/cdi/ManagedBeanNoArgConstructorQuickFix.java index 161cdf510..0a05b56db 100644 --- a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/cdi/ManagedBeanNoArgConstructorQuickFix.java +++ b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/cdi/ManagedBeanNoArgConstructorQuickFix.java @@ -53,29 +53,33 @@ public String getParticipantId() { } public List getCodeActions(JavaCodeActionContext context, Diagnostic diagnostic) { - return addConstructor(diagnostic, context); + PsiElement node = context.getCoveredNode(); + PsiClass parentType = getBinding(node); + + if (parentType != null) { + return addConstructor(diagnostic, context); + } + return Collections.emptyList(); } @Override public CodeAction resolveCodeAction(JavaCodeActionResolveContext context) { final CodeAction toResolve = context.getUnresolved(); - final PsiElement node = context.getCoveredNode(); - final PsiClass parentType = PsiTreeUtil.getParentOfType(node, PsiClass.class); + PsiElement node = context.getCoveredNode(); + PsiClass parentType = getBinding(node); - if (parentType != null) { - String constructorName = toResolve.getTitle(); - ChangeCorrectionProposal proposal = new AddConstructorProposal(constructorName, - context.getSource().getCompilationUnit(), context.getASTRoot(), parentType, 0, - constructorName.equals(Messages.getMessage("AddProtectedConstructor")) ? "protected" : "public"); - - try { - WorkspaceEdit we = context.convertToWorkspaceEdit(proposal); - toResolve.setEdit(we); - } catch (Exception e) { - LOGGER.log(Level.WARNING, "Unable to create workspace edit for code actions to add constructors", e); - } + String constructorName = toResolve.getTitle(); + ChangeCorrectionProposal proposal = new AddConstructorProposal(constructorName, + context.getSource().getCompilationUnit(), context.getASTRoot(), parentType, 0, + constructorName.equals(Messages.getMessage("AddProtectedConstructor")) ? "protected" : "public"); + try { + WorkspaceEdit we = context.convertToWorkspaceEdit(proposal); + toResolve.setEdit(we); + } catch (Exception e) { + LOGGER.log(Level.WARNING, "Unable to create workspace edit for code actions to add constructors", e); } + return toResolve; } diff --git a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/persistence/PersistenceEntityQuickFix.java b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/persistence/PersistenceEntityQuickFix.java index 65c2f63d4..5aee7f1fb 100644 --- a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/persistence/PersistenceEntityQuickFix.java +++ b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/persistence/PersistenceEntityQuickFix.java @@ -72,27 +72,32 @@ public String getParticipantId() { } public List getCodeActions(JavaCodeActionContext context, Diagnostic diagnostic) { - return addConstructor(diagnostic, context); + List codeActions = new ArrayList<>(); + PsiElement node = context.getCoveredNode(); + PsiClass parentType = getBinding(node); + + if (parentType != null) { + codeActions.addAll(addConstructor(diagnostic, context)); + } + 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); + PsiElement node = context.getCoveredNode(); + PsiClass parentType = getBinding(node); - if (parentType != null) { - String constructorName = toResolve.getTitle(); - ChangeCorrectionProposal proposal = new AddConstructorProposal(constructorName, - context.getSource().getCompilationUnit(), context.getASTRoot(), parentType, 0, - constructorName.equals(Messages.getMessage("AddNoArgProtectedConstructor")) ? "protected" : "public"); + String constructorName = toResolve.getTitle(); + ChangeCorrectionProposal proposal = new AddConstructorProposal(constructorName, + context.getSource().getCompilationUnit(), context.getASTRoot(), parentType, 0, + constructorName.equals(Messages.getMessage("AddNoArgProtectedConstructor")) ? "protected" : "public"); - try { - WorkspaceEdit we = context.convertToWorkspaceEdit(proposal); - toResolve.setEdit(we); - } catch (Exception e) { - LOGGER.log(Level.WARNING, "Unable to create workspace edit for code actions to add constructors", e); - } + try { + WorkspaceEdit we = context.convertToWorkspaceEdit(proposal); + toResolve.setEdit(we); + } catch (Exception e) { + LOGGER.log(Level.WARNING, "Unable to create workspace edit for code actions to add constructors", e); } return toResolve; } From dbed82349b68bc5cb73950bb6c5f3d8835bf759e Mon Sep 17 00:00:00 2001 From: anusreelakshmi934 Date: Thu, 19 Oct 2023 10:42:56 +0530 Subject: [PATCH 26/34] conflicts --- src/main/resources/META-INF/plugin.xml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/main/resources/META-INF/plugin.xml b/src/main/resources/META-INF/plugin.xml index 97eb3c874..a02f9d9ea 100644 --- a/src/main/resources/META-INF/plugin.xml +++ b/src/main/resources/META-INF/plugin.xml @@ -251,11 +251,17 @@ group="jakarta" targetDiagnostic="jakarta-persistence#MissingEmptyConstructor" implementationClass="io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.persistence.PersistenceEntityQuickFix"/> + + + From fd638df1a919d19cedf42a962d0efd8d1ffd16a5 Mon Sep 17 00:00:00 2001 From: anusreelakshmi934 Date: Thu, 19 Oct 2023 10:44:30 +0530 Subject: [PATCH 27/34] Revert "conflicts" This reverts commit 9f6efa4db8f83e78e3c23cb0b21e691b8d64c144. --- src/main/resources/META-INF/plugin.xml | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/main/resources/META-INF/plugin.xml b/src/main/resources/META-INF/plugin.xml index a02f9d9ea..97eb3c874 100644 --- a/src/main/resources/META-INF/plugin.xml +++ b/src/main/resources/META-INF/plugin.xml @@ -251,17 +251,11 @@ group="jakarta" targetDiagnostic="jakarta-persistence#MissingEmptyConstructor" implementationClass="io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.persistence.PersistenceEntityQuickFix"/> - - - From 7b2033a70e06c97aa36931be48067b4aa2d472c9 Mon Sep 17 00:00:00 2001 From: anusreelakshmi934 Date: Thu, 19 Oct 2023 10:46:14 +0530 Subject: [PATCH 28/34] conflicts --- src/main/resources/META-INF/plugin.xml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/main/resources/META-INF/plugin.xml b/src/main/resources/META-INF/plugin.xml index 97eb3c874..6f892c102 100644 --- a/src/main/resources/META-INF/plugin.xml +++ b/src/main/resources/META-INF/plugin.xml @@ -247,10 +247,16 @@ group="jakarta" targetDiagnostic="jakarta-annotations#PostConstructReturnType" implementationClass="io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.annotations.PostConstructReturnTypeQuickFix"/> + + + Date: Mon, 16 Oct 2023 15:43:43 +0530 Subject: [PATCH 29/34] Moved the createCodeAction method to JDTUtils --- .../intellij/lsp4jakarta/lsp4ij/JDTUtils.java | 29 +++++++++++++++++++ .../NonPublicResourceMethodQuickFix.java | 15 ++-------- 2 files changed, 31 insertions(+), 13 deletions(-) diff --git a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/JDTUtils.java b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/JDTUtils.java index 7d250f4ef..d80e49e88 100644 --- a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/JDTUtils.java +++ b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/JDTUtils.java @@ -17,8 +17,15 @@ import com.intellij.psi.PsiField; import com.intellij.psi.PsiJavaFile; import com.intellij.psi.PsiMethod; +import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.java.codeaction.ExtendedCodeAction; +import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.java.codeaction.JavaCodeActionContext; +import org.eclipse.lsp4j.CodeAction; +import org.eclipse.lsp4j.CodeActionKind; +import org.eclipse.lsp4j.Diagnostic; +import org.eclipse.lsp4mp.commons.CodeActionResolveData; import java.util.ArrayList; +import java.util.Collections; import java.util.List; public class JDTUtils { @@ -73,4 +80,26 @@ public static List getFieldAccessors(PsiJavaFile unit, PsiField field } return accessors; } + + /** + * Returns CodeAction object, which contains details of quickfix. + * + * @param context JavaCodeActionContext + * @param diagnostic diagnostic message + * @param quickFixMessage quickfix message + * @param participantId participant id + * @return CodeAction + */ + public static CodeAction createCodeAction(JavaCodeActionContext context, Diagnostic diagnostic, + String quickFixMessage, String participantId) { + ExtendedCodeAction codeAction = new ExtendedCodeAction(quickFixMessage); + codeAction.setRelevance(0); + codeAction.setDiagnostics(Collections.singletonList(diagnostic)); + codeAction.setKind(CodeActionKind.QuickFix); + codeAction.setData(new CodeActionResolveData(context.getUri(), participantId, + context.getParams().getRange(), Collections.emptyMap(), + context.getParams().isResourceOperationSupported(), + context.getParams().isCommandConfigurationUpdateSupported())); + return codeAction; + } } diff --git a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/jax_rs/NonPublicResourceMethodQuickFix.java b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/jax_rs/NonPublicResourceMethodQuickFix.java index 3dc541b38..effd6643c 100644 --- a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/jax_rs/NonPublicResourceMethodQuickFix.java +++ b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/jax_rs/NonPublicResourceMethodQuickFix.java @@ -17,6 +17,7 @@ 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.ModifyModifiersProposal; import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.java.codeaction.ExtendedCodeAction; @@ -58,7 +59,7 @@ public List getCodeActions(JavaCodeActionContext context, final PsiMethod parentMethod = PsiTreeUtil.getParentOfType(node, PsiMethod.class); if (parentMethod != null) { - return Collections.singletonList(createCodeAction(context, diagnostic)); + return Collections.singletonList(JDTUtils.createCodeAction(context, diagnostic, TITLE_MESSAGE, getParticipantId())); } return Collections.emptyList(); } @@ -82,16 +83,4 @@ public CodeAction resolveCodeAction(JavaCodeActionResolveContext context) { } return toResolve; } - - private CodeAction createCodeAction(JavaCodeActionContext context, Diagnostic diagnostic) { - ExtendedCodeAction codeAction = new ExtendedCodeAction(TITLE_MESSAGE); - codeAction.setRelevance(0); - codeAction.setDiagnostics(Collections.singletonList(diagnostic)); - codeAction.setKind(CodeActionKind.QuickFix); - codeAction.setData(new CodeActionResolveData(context.getUri(), getParticipantId(), - context.getParams().getRange(), Collections.emptyMap(), - context.getParams().isResourceOperationSupported(), - context.getParams().isCommandConfigurationUpdateSupported())); - return codeAction; - } } From 31911e7a0c1f403128f5ea63bb5c303a7e406f5b Mon Sep 17 00:00:00 2001 From: Aparna Michael Date: Wed, 18 Oct 2023 21:26:06 +0530 Subject: [PATCH 30/34] Removed duplicate implementation of createCodeAction --- .../quickfix/InsertAnnotationQuickFix.java | 15 ++------------- .../quickfix/RemoveModifierConflictQuickFix.java | 16 ++-------------- 2 files changed, 4 insertions(+), 27 deletions(-) diff --git a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/codeAction/proposal/quickfix/InsertAnnotationQuickFix.java b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/codeAction/proposal/quickfix/InsertAnnotationQuickFix.java index 5646d3fa5..963c4ed7e 100644 --- a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/codeAction/proposal/quickfix/InsertAnnotationQuickFix.java +++ b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/codeAction/proposal/quickfix/InsertAnnotationQuickFix.java @@ -15,6 +15,7 @@ import com.intellij.psi.*; 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.ModifyAnnotationProposal; import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.java.codeaction.ExtendedCodeAction; @@ -116,7 +117,7 @@ protected void addAttributes(Diagnostic diagnostic, JavaCodeActionContext contex */ private void addAttribute(Diagnostic diagnostic, JavaCodeActionContext context, List codeActions, String name, String... attributes) { String label = getLabel(name, attributes); - codeActions.add(createCodeAction(context, diagnostic, label)); + codeActions.add(JDTUtils.createCodeAction(context, diagnostic, label, getParticipantId())); } protected PsiModifierListOwner getBinding(PsiElement node) { @@ -131,16 +132,4 @@ protected PsiModifierListOwner getBinding(PsiElement node) { protected String getLabel(String annotationName, String... attributes) { return Messages.getMessage("InsertItem", "@" + annotation); // uses Java syntax } - - private CodeAction createCodeAction(JavaCodeActionContext context, Diagnostic diagnostic, String label) { - ExtendedCodeAction codeAction = new ExtendedCodeAction(label); - codeAction.setRelevance(0); - codeAction.setDiagnostics(Collections.singletonList(diagnostic)); - codeAction.setKind(CodeActionKind.QuickFix); - codeAction.setData(new CodeActionResolveData(context.getUri(), getParticipantId(), - context.getParams().getRange(), Collections.emptyMap(), - context.getParams().isResourceOperationSupported(), - context.getParams().isCommandConfigurationUpdateSupported())); - return codeAction; - } } diff --git a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/codeAction/proposal/quickfix/RemoveModifierConflictQuickFix.java b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/codeAction/proposal/quickfix/RemoveModifierConflictQuickFix.java index dc4f6624b..f0735152a 100644 --- a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/codeAction/proposal/quickfix/RemoveModifierConflictQuickFix.java +++ b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/codeAction/proposal/quickfix/RemoveModifierConflictQuickFix.java @@ -15,6 +15,7 @@ import com.intellij.psi.*; 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.ModifyModifiersProposal; import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.java.codeaction.ExtendedCodeAction; @@ -123,7 +124,7 @@ private void removeModifier(Diagnostic diagnostic, JavaCodeActionContext context PsiElement node = context.getCoveredNode(); PsiModifierListOwner modifierListOwner = PsiTreeUtil.getParentOfType(node, PsiModifierListOwner.class); String label = getLabel(modifierListOwner, modifier); - codeActions.add(createCodeAction(context, diagnostic, label)); + codeActions.add(JDTUtils.createCodeAction(context, diagnostic, label, getParticipantId())); } private String getLabel(PsiModifierListOwner modifierListOwner, String... modifier) { @@ -145,17 +146,4 @@ private String getLabel(PsiModifierListOwner modifierListOwner, String... modifi protected PsiClass getBinding(PsiElement node) { return PsiTreeUtil.getParentOfType(node, PsiClass.class); } - - private CodeAction createCodeAction(JavaCodeActionContext context, Diagnostic diagnostic, String title) { - ExtendedCodeAction codeAction = new ExtendedCodeAction(title); - codeAction.setRelevance(0); - codeAction.setDiagnostics(Collections.singletonList(diagnostic)); - codeAction.setKind(CodeActionKind.QuickFix); - codeAction.setData(new CodeActionResolveData(context.getUri(), getParticipantId(), - context.getParams().getRange(), Collections.emptyMap(), - context.getParams().isResourceOperationSupported(), - context.getParams().isCommandConfigurationUpdateSupported())); - return codeAction; - } - } From 2ec5274033c06bc216f35cc35e829e5cb933aeec Mon Sep 17 00:00:00 2001 From: Aparna Michael Date: Thu, 19 Oct 2023 11:54:26 +0530 Subject: [PATCH 31/34] Removed duplicate createCodeAction method --- .../PostConstructReturnTypeQuickFix.java | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/annotations/PostConstructReturnTypeQuickFix.java b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/annotations/PostConstructReturnTypeQuickFix.java index e57af2971..bb17f646b 100644 --- a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/annotations/PostConstructReturnTypeQuickFix.java +++ b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/annotations/PostConstructReturnTypeQuickFix.java @@ -16,6 +16,7 @@ import com.intellij.psi.PsiMethod; import com.intellij.psi.PsiPrimitiveType; 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.ModifyReturnTypeProposal; import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.java.codeaction.ExtendedCodeAction; @@ -58,7 +59,7 @@ public List getCodeActions(JavaCodeActionContext context, final PsiMethod parentType = getBinding(node); if (parentType != null) { - codeActions.add(createCodeAction(context, diagnostic)); + codeActions.add(JDTUtils.createCodeAction(context, diagnostic, TITLE_MESSAGE, getParticipantId())); } return codeActions; } @@ -81,18 +82,6 @@ public CodeAction resolveCodeAction(JavaCodeActionResolveContext context) { return toResolve; } - private CodeAction createCodeAction(JavaCodeActionContext context, Diagnostic diagnostic) { - ExtendedCodeAction codeAction = new ExtendedCodeAction(TITLE_MESSAGE); - codeAction.setRelevance(0); - codeAction.setDiagnostics(Collections.singletonList(diagnostic)); - codeAction.setKind(CodeActionKind.QuickFix); - codeAction.setData(new CodeActionResolveData(context.getUri(), getParticipantId(), - context.getParams().getRange(), Collections.emptyMap(), - context.getParams().isResourceOperationSupported(), - context.getParams().isCommandConfigurationUpdateSupported())); - return codeAction; - } - protected PsiMethod getBinding(PsiElement node) { if (node instanceof PsiMethod) { return (PsiMethod) node; From 51200aec6ecb55bd2c40cf7bfca7ce1578ef49c1 Mon Sep 17 00:00:00 2001 From: Rahul Biju Date: Fri, 20 Oct 2023 11:01:25 +0530 Subject: [PATCH 32/34] removed create code action method from quick fix class --- .../lsp4ij/servlet/HttpServletQuickFix.java | 23 ++----------------- 1 file changed, 2 insertions(+), 21 deletions(-) diff --git a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/servlet/HttpServletQuickFix.java b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/servlet/HttpServletQuickFix.java index 4b680590c..a945e3ace 100644 --- a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/servlet/HttpServletQuickFix.java +++ b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/servlet/HttpServletQuickFix.java @@ -18,6 +18,7 @@ import com.intellij.psi.PsiClass; import com.intellij.psi.PsiElement; 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.ExtendClassProposal; import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.java.codeaction.ExtendedCodeAction; @@ -71,7 +72,7 @@ public List getCodeActions(JavaCodeActionContext context, String title = Messages.getMessage("LetClassExtend", parentType.getName(), ServletConstants.HTTP_SERVLET); - codeActions.add(createCodeAction(context, diagnostic, title)); + codeActions.add(JDTUtils.createCodeAction(context, diagnostic, title, getParticipantId())); } return codeActions; } @@ -104,26 +105,6 @@ public CodeAction resolveCodeAction(JavaCodeActionResolveContext context) { return toResolve; } - /** - * Creates a CodeAction with the specified title, relevance, diagnostics, kind, and additional data. - * - * @param context - * @param diagnostic - * @param title - * @return - */ - private CodeAction createCodeAction(JavaCodeActionContext context, Diagnostic diagnostic, String title) { - ExtendedCodeAction codeAction = new ExtendedCodeAction(title); - codeAction.setRelevance(0); - codeAction.setDiagnostics(Collections.singletonList(diagnostic)); - codeAction.setKind(CodeActionKind.QuickFix); - codeAction.setData(new CodeActionResolveData(context.getUri(), getParticipantId(), - context.getParams().getRange(), Collections.emptyMap(), - context.getParams().isResourceOperationSupported(), - context.getParams().isCommandConfigurationUpdateSupported())); - return codeAction; - } - private PsiClass getBinding(PsiElement node) { return PsiTreeUtil.getParentOfType(node, PsiClass.class); } From 30c21a50fe48bec04d7c169f647cbf10feb0a751 Mon Sep 17 00:00:00 2001 From: Rahul Biju Date: Fri, 20 Oct 2023 12:33:46 +0530 Subject: [PATCH 33/34] removed the extension points --- src/main/resources/META-INF/plugin.xml | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/main/resources/META-INF/plugin.xml b/src/main/resources/META-INF/plugin.xml index 6f892c102..97eb3c874 100644 --- a/src/main/resources/META-INF/plugin.xml +++ b/src/main/resources/META-INF/plugin.xml @@ -247,16 +247,10 @@ group="jakarta" targetDiagnostic="jakarta-annotations#PostConstructReturnType" implementationClass="io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.annotations.PostConstructReturnTypeQuickFix"/> - - - Date: Fri, 20 Oct 2023 12:41:06 +0530 Subject: [PATCH 34/34] added spaces in between extension point --- src/main/resources/META-INF/plugin.xml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/main/resources/META-INF/plugin.xml b/src/main/resources/META-INF/plugin.xml index 97eb3c874..0286fa28e 100644 --- a/src/main/resources/META-INF/plugin.xml +++ b/src/main/resources/META-INF/plugin.xml @@ -231,22 +231,27 @@ group="jakarta" targetDiagnostic="jakarta-di#RemoveInjectOrAbstract" implementationClass="io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.codeAction.proposal.quickfix.RemoveAbstractModifierQuickFix"/> + + + + +