From 8da79c1b6470764839f5c540614c9c0442ac6f04 Mon Sep 17 00:00:00 2001 From: VAISAKH T Date: Wed, 4 Oct 2023 11:48:54 +0530 Subject: [PATCH 1/3] [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 46307861d..ffa56492c 100644 --- a/src/main/resources/META-INF/plugin.xml +++ b/src/main/resources/META-INF/plugin.xml @@ -167,6 +167,11 @@ 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/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 0d5047a3c21beb9e28dc29d75858bf5e2a853394 Mon Sep 17 00:00:00 2001 From: VAISAKH T Date: Thu, 5 Oct 2023 12:59:41 +0530 Subject: [PATCH 2/3] [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 d2f708bc296790399052d17b804d298e7b4fdcb3 Mon Sep 17 00:00:00 2001 From: VAISAKH T Date: Fri, 13 Oct 2023 10:33:57 +0530 Subject: [PATCH 3/3] 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);