From bc487b38cce07b705f6adf990cb16e84e93896fb Mon Sep 17 00:00:00 2001 From: anusreelakshmi934 Date: Wed, 4 Oct 2023 16:44:06 +0530 Subject: [PATCH 1/8] 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 +++++++++++-------- src/main/resources/META-INF/plugin.xml | 8 ++ .../it/cdi/ManagedBeanConstructorTest.java | 24 +++-- .../persistence/JakartaPersistenceTest.java | 13 ++- 5 files changed, 131 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/main/resources/META-INF/plugin.xml b/src/main/resources/META-INF/plugin.xml index 46307861d..a08a9ce4e 100644 --- a/src/main/resources/META-INF/plugin.xml +++ b/src/main/resources/META-INF/plugin.xml @@ -166,6 +166,14 @@ group="jakarta" targetDiagnostic="jakarta-jax_rs#NonPublicResourceMethod" implementationClass="io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.jax_rs.NonPublicResourceMethodQuickFix"/> + + 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 6ed4af2b9e29b17d2e6a584ad2c64de774ea20e4 Mon Sep 17 00:00:00 2001 From: anusreelakshmi934 Date: Tue, 10 Oct 2023 10:24:44 +0530 Subject: [PATCH 2/8] 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 77cba5a92a1f6607041859578181ba87d6aa7198 Mon Sep 17 00:00:00 2001 From: anusreelakshmi934 Date: Tue, 10 Oct 2023 10:51:30 +0530 Subject: [PATCH 3/8] 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 04fcfb34f0be89322466c8e79926e01fab799795 Mon Sep 17 00:00:00 2001 From: anusreelakshmi934 Date: Wed, 18 Oct 2023 12:57:40 +0530 Subject: [PATCH 4/8] 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 31ac241d8e8faa87de78764b7f5f49942ba72946 Mon Sep 17 00:00:00 2001 From: anusreelakshmi934 Date: Wed, 18 Oct 2023 21:26:33 +0530 Subject: [PATCH 5/8] resolved conflicts --- src/main/resources/META-INF/plugin.xml | 27 ++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/main/resources/META-INF/plugin.xml b/src/main/resources/META-INF/plugin.xml index a08a9ce4e..3ccc3e5d1 100644 --- a/src/main/resources/META-INF/plugin.xml +++ b/src/main/resources/META-INF/plugin.xml @@ -166,6 +166,33 @@ group="jakarta" targetDiagnostic="jakarta-jax_rs#NonPublicResourceMethod" implementationClass="io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.jax_rs.NonPublicResourceMethodQuickFix"/> + + + + + + + + + Date: Thu, 19 Oct 2023 10:42:56 +0530 Subject: [PATCH 6/8] 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 2e3e4d2fa..c8d1474fa 100644 --- a/src/main/resources/META-INF/plugin.xml +++ b/src/main/resources/META-INF/plugin.xml @@ -198,11 +198,17 @@ group="jakarta" targetDiagnostic="jakarta-persistence#MissingEmptyConstructor" implementationClass="io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.persistence.PersistenceEntityQuickFix"/> + + + From aa3401e012302b31492543cc143a1f3596a21964 Mon Sep 17 00:00:00 2001 From: anusreelakshmi934 Date: Thu, 19 Oct 2023 10:44:30 +0530 Subject: [PATCH 7/8] 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 c8d1474fa..2e3e4d2fa 100644 --- a/src/main/resources/META-INF/plugin.xml +++ b/src/main/resources/META-INF/plugin.xml @@ -198,17 +198,11 @@ group="jakarta" targetDiagnostic="jakarta-persistence#MissingEmptyConstructor" implementationClass="io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.persistence.PersistenceEntityQuickFix"/> - - - From dae6cd5fe3f8c6fa529eb2757fa19217fe1794ee Mon Sep 17 00:00:00 2001 From: anusreelakshmi934 Date: Thu, 19 Oct 2023 10:46:14 +0530 Subject: [PATCH 8/8] 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 2e3e4d2fa..e48ec794d 100644 --- a/src/main/resources/META-INF/plugin.xml +++ b/src/main/resources/META-INF/plugin.xml @@ -194,10 +194,16 @@ targetDiagnostic="jakarta-annotations#MissingResourceNameAttribute" implementationClass="io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.codeAction.annotations.AddResourceMissingNameQuickFix"/> + + +