From f5bc75b5e54ef7d660a74d4f143582d13a65d38a Mon Sep 17 00:00:00 2001 From: dessina-devasia <143582034+dessina-devasia@users.noreply.github.com> Date: Mon, 11 Nov 2024 19:27:38 +0530 Subject: [PATCH 1/2] [#1025] Resolved inconsistent behaviour of @Dependency quickfix --- .../intellij/lsp4jakarta/lsp4ij/cdi/ManagedBeanQuickFix.java | 4 +++- .../tools/intellij/lsp4jakarta/it/cdi/ManagedBeanTest.java | 4 ++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/cdi/ManagedBeanQuickFix.java b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/cdi/ManagedBeanQuickFix.java index 2707f34a5..2241ad4b0 100644 --- a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/cdi/ManagedBeanQuickFix.java +++ b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/cdi/ManagedBeanQuickFix.java @@ -17,8 +17,10 @@ import com.intellij.openapi.progress.ProcessCanceledException; import com.intellij.openapi.project.IndexNotReadyException; +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.JDTUtils; import io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.Messages; import io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.codeAction.proposal.quickfix.InsertAnnotationMissingQuickFix; @@ -69,7 +71,7 @@ public CodeAction resolveCodeAction(JavaCodeActionResolveContext context) { final CodeAction toResolve = context.getUnresolved(); String name = toResolve.getTitle(); PsiElement node = context.getCoveringNode(); - PsiModifierListOwner parentType = getBinding(node); + PsiModifierListOwner parentType = PsiTreeUtil.getParentOfType(node, PsiClass.class); ChangeCorrectionProposal proposal = new ReplaceAnnotationProposal(name, context.getCompilationUnit(), context.getASTRoot(), parentType, 0, ADD_ANNOTATION, context.getSource().getCompilationUnit(), REMOVE_ANNOTATION_NAMES); diff --git a/src/test/java/io/openliberty/tools/intellij/lsp4jakarta/it/cdi/ManagedBeanTest.java b/src/test/java/io/openliberty/tools/intellij/lsp4jakarta/it/cdi/ManagedBeanTest.java index 7b4fea4a5..4ba618e4b 100644 --- a/src/test/java/io/openliberty/tools/intellij/lsp4jakarta/it/cdi/ManagedBeanTest.java +++ b/src/test/java/io/openliberty/tools/intellij/lsp4jakarta/it/cdi/ManagedBeanTest.java @@ -65,8 +65,8 @@ public void managedBeanAnnotations() throws Exception { assertJavaDiagnostics(diagnosticsParams, utils, d1, d2); String newText1 = "package io.openliberty.sample.jakarta.cdi;\n\n" + "import jakarta.enterprise.context.*;\n\n" + - "@RequestScoped\npublic class ManagedBean {\n " + - "@Dependent\n public int a;\n\n\n " + + "@Dependent\npublic class ManagedBean {\n " + + "public int a;\n\n\n " + "public ManagedBean() {\n this.a = 10;\n }\n}\n"; String newText2 = "package io.openliberty.sample.jakarta.cdi;\n\n" + "import jakarta.enterprise.context.*;\n\n" + From 768307d8db1c09f5f4bf15c0051855828300e4e5 Mon Sep 17 00:00:00 2001 From: dessina-devasia <143582034+dessina-devasia@users.noreply.github.com> Date: Tue, 12 Nov 2024 12:49:15 +0530 Subject: [PATCH 2/2] [#1025] Changed field level dependency to class level --- .../lsp4jakarta/lsp4ij/cdi/ManagedBeanDiagnosticsCollector.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/cdi/ManagedBeanDiagnosticsCollector.java b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/cdi/ManagedBeanDiagnosticsCollector.java index 2391688e7..42eac428a 100644 --- a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/cdi/ManagedBeanDiagnosticsCollector.java +++ b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/cdi/ManagedBeanDiagnosticsCollector.java @@ -80,7 +80,7 @@ public void collectDiagnostics(PsiJavaFile unit, List diagnostics) { if (isManagedBean && field.hasModifierProperty(PsiModifier.PUBLIC) && !field.hasModifierProperty(PsiModifier.STATIC) - && (fieldScopes.size() != 1 || !fieldScopes.get(0).equals(DEPENDENT_FQ_NAME))) { + && !managedBeanAnnotations.contains(DEPENDENT_FQ_NAME)) { diagnostics.add(createDiagnostic(field, unit, Messages.getMessage("ManagedBeanWithNonStaticPublicField"), DIAGNOSTIC_CODE, null,