diff --git a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/AnnotationUtil.java b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/AnnotationUtil.java index 8bdcc526c..5f5fc1e6e 100644 --- a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/AnnotationUtil.java +++ b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/AnnotationUtil.java @@ -1,4 +1,4 @@ -/* Copyright (c) 2022 IBM Corporation, Lidia Ataupillco Ramos and others. +/* Copyright (c) 2022, 2024 IBM Corporation, Lidia Ataupillco Ramos 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 @@ -12,14 +12,14 @@ package io.openliberty.tools.intellij.lsp4jakarta.lsp4ij; -import com.intellij.openapi.progress.ProcessCanceledException; -import com.intellij.openapi.project.IndexNotReadyException; import com.intellij.psi.PsiClass; +import io.openliberty.tools.intellij.util.ExceptionUtil; import java.util.Arrays; import java.util.List; import java.util.Set; -import java.util.concurrent.CancellationException; +import java.util.logging.Level; +import java.util.logging.Logger; import java.util.stream.Collectors; import java.util.Collections; @@ -32,16 +32,20 @@ * @return list of recognised defining annotations applied to a class */ public class AnnotationUtil { + private static final Logger LOGGER = Logger.getLogger(AnnotationUtil.class.getName()); public static List getScopeAnnotations(PsiClass type, Set scopes) { - try { - // Construct a stream of only the annotations applied to the type that are also - // recognised annotations found in scopes. - return Arrays.stream(type.getAnnotations()).map(annotation -> annotation.getNameReferenceElement().getQualifiedName()) - .filter(scopes::contains).distinct().collect(Collectors.toList()); - } catch (IndexNotReadyException | ProcessCanceledException | CancellationException e) { - throw e; - } catch (Exception e) { - return Collections.emptyList(); - } + return ExceptionUtil.executeWithExceptionHandling( + // Construct a stream of only the annotations applied to the type that are also + // recognised annotations found in scopes. + () -> Arrays.stream(type.getAnnotations()) + .map(annotation -> annotation.getNameReferenceElement().getQualifiedName()) + .filter(scopes::contains) + .distinct() + .collect(Collectors.toList()), + e -> { + LOGGER.log(Level.WARNING, "Error while calling getScopeAnnotations", e); + return Collections.emptyList(); + } + ); } } 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 eaca420e5..b00f72dde 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 @@ -12,11 +12,8 @@ *******************************************************************************/ package io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.annotations; -import com.intellij.openapi.progress.ProcessCanceledException; -import com.intellij.openapi.project.IndexNotReadyException; import com.intellij.psi.PsiElement; import com.intellij.psi.PsiMethod; -import com.intellij.psi.PsiPrimitiveType; import com.intellij.psi.PsiTypes; import com.intellij.psi.util.PsiTreeUtil; import io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.JDTUtils; @@ -26,14 +23,12 @@ import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.java.codeaction.JavaCodeActionContext; import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.java.codeaction.JavaCodeActionResolveContext; import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.java.corrections.proposal.ChangeCorrectionProposal; +import io.openliberty.tools.intellij.util.ExceptionUtil; import org.eclipse.lsp4j.CodeAction; import org.eclipse.lsp4j.Diagnostic; -import org.eclipse.lsp4j.WorkspaceEdit; import java.util.ArrayList; import java.util.List; -import java.util.concurrent.CancellationException; -import java.util.logging.Level; import java.util.logging.Logger; /** @@ -74,14 +69,8 @@ public CodeAction resolveCodeAction(JavaCodeActionResolveContext context) { assert parentType != null; ChangeCorrectionProposal proposal = new ModifyReturnTypeProposal(TITLE_MESSAGE, context.getSource().getCompilationUnit(), context.getASTRoot(), parentType, 0, PsiTypes.voidType()); - try { - WorkspaceEdit we = context.convertToWorkspaceEdit(proposal); - toResolve.setEdit(we); - } catch (IndexNotReadyException | ProcessCanceledException | CancellationException e) { - throw e; - } catch (Exception e) { - LOGGER.log(Level.WARNING, "Unable to create workspace edit for code action to change return type to void", e); - } + + ExceptionUtil.executeWithWorkspaceEditHandling(context, proposal, toResolve, LOGGER, "Unable to create workspace edit for code action to change return type to void"); return toResolve; } diff --git a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/beanvalidation/BeanValidationQuickFix.java b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/beanvalidation/BeanValidationQuickFix.java index 7429e4d04..89a3635c6 100644 --- a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/beanvalidation/BeanValidationQuickFix.java +++ b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/beanvalidation/BeanValidationQuickFix.java @@ -12,8 +12,6 @@ *******************************************************************************/ package io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.beanvalidation; -import com.intellij.openapi.progress.ProcessCanceledException; -import com.intellij.openapi.project.IndexNotReadyException; import com.intellij.psi.*; import com.intellij.psi.util.PsiTreeUtil; import io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.JDTUtils; @@ -23,14 +21,12 @@ 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.util.ExceptionUtil; import org.eclipse.lsp4j.CodeAction; import org.eclipse.lsp4j.Diagnostic; -import org.eclipse.lsp4j.WorkspaceEdit; import org.eclipse.lsp4mp.commons.codeaction.CodeActionResolveData; import java.util.*; -import java.util.concurrent.CancellationException; -import java.util.logging.Level; import java.util.logging.Logger; /** @@ -111,14 +107,7 @@ private void resolveRemoveConstraintAnnotationsCodeAction(JavaCodeActionResolveC final RemoveAnnotationsProposal proposal = new RemoveAnnotationsProposal(name, context.getSource().getCompilationUnit(), context.getASTRoot(), parentType, 0, Collections.singletonList(annotationToRemove.get()), isFormatRequired); - try { - WorkspaceEdit we = context.convertToWorkspaceEdit(proposal); - toResolve.setEdit(we); - } catch (IndexNotReadyException | ProcessCanceledException | CancellationException e) { - throw e; - } catch (Exception e) { - LOGGER.log(Level.WARNING, "Unable to create workspace edit for code action to remove constraint annotation", e); - } + ExceptionUtil.executeWithWorkspaceEditHandling(context, proposal, toResolve, LOGGER, "Unable to create workspace edit for code action to remove constraint annotation"); } } } @@ -134,14 +123,7 @@ private void resolveStaticModifierCodeAction(JavaCodeActionResolveContext contex context.getASTRoot(), parentType, 0, modifierListOwner.getModifierList(), Collections.emptyList(), Collections.singletonList("static")); - try { - WorkspaceEdit we = context.convertToWorkspaceEdit(proposal); - toResolve.setEdit(we); - } catch (IndexNotReadyException | ProcessCanceledException | CancellationException e) { - throw e; - } catch (Exception e) { - LOGGER.log(Level.WARNING, "Unable to create workspace edit for code action to remove static modifier", e); - } + ExceptionUtil.executeWithWorkspaceEditHandling(context, proposal, toResolve, LOGGER, "Unable to create workspace edit for code action to remove static modifier"); } private void removeStaticModifierCodeActions(Diagnostic diagnostic, JavaCodeActionContext context, 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 c131cc87f..d62c3ce56 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 @@ -13,8 +13,6 @@ package io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.cdi; -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.util.PsiTreeUtil; @@ -25,15 +23,13 @@ import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.java.codeaction.JavaCodeActionContext; import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.java.codeaction.JavaCodeActionResolveContext; import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.java.corrections.proposal.ChangeCorrectionProposal; +import io.openliberty.tools.intellij.util.ExceptionUtil; import org.eclipse.lsp4j.CodeAction; import org.eclipse.lsp4j.Diagnostic; -import org.eclipse.lsp4j.WorkspaceEdit; import java.util.ArrayList; import java.util.Collections; import java.util.List; -import java.util.concurrent.CancellationException; -import java.util.logging.Level; import java.util.logging.Logger; /** @@ -74,14 +70,7 @@ public CodeAction resolveCodeAction(JavaCodeActionResolveContext context) { context.getSource().getCompilationUnit(), context.getASTRoot(), parentType, 0, constructorName.equals(Messages.getMessage("AddProtectedConstructor")) ? "protected" : "public"); - try { - WorkspaceEdit we = context.convertToWorkspaceEdit(proposal); - toResolve.setEdit(we); - } catch (IndexNotReadyException | ProcessCanceledException | CancellationException e) { - throw e; - } catch (Exception e) { - LOGGER.log(Level.WARNING, "Unable to create workspace edit for code actions to add constructors", e); - } + ExceptionUtil.executeWithWorkspaceEditHandling(context, proposal, toResolve, LOGGER, "Unable to create workspace edit for code actions to add constructors"); return toResolve; } 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..239303fd9 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 @@ -15,8 +15,6 @@ package io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.cdi; -import com.intellij.openapi.progress.ProcessCanceledException; -import com.intellij.openapi.project.IndexNotReadyException; import com.intellij.psi.PsiElement; import com.intellij.psi.PsiModifierListOwner; import io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.JDTUtils; @@ -26,14 +24,12 @@ import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.java.codeaction.JavaCodeActionResolveContext; import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.java.corrections.proposal.ChangeCorrectionProposal; import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.java.corrections.proposal.ReplaceAnnotationProposal; +import io.openliberty.tools.intellij.util.ExceptionUtil; import org.eclipse.lsp4j.CodeAction; import org.eclipse.lsp4j.Diagnostic; -import org.eclipse.lsp4j.WorkspaceEdit; import java.util.ArrayList; import java.util.List; -import java.util.concurrent.CancellationException; -import java.util.logging.Level; import java.util.logging.Logger; import static io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.cdi.ManagedBeanConstants.SCOPE_FQ_NAMES; @@ -74,14 +70,7 @@ public CodeAction resolveCodeAction(JavaCodeActionResolveContext context) { context.getASTRoot(), parentType, 0, ADD_ANNOTATION, context.getSource().getCompilationUnit(), REMOVE_ANNOTATION_NAMES); - try { - WorkspaceEdit we = context.convertToWorkspaceEdit(proposal); - toResolve.setEdit(we); - } catch (IndexNotReadyException | ProcessCanceledException | CancellationException e) { - throw e; - } catch (Exception e) { - LOGGER.log(Level.WARNING, "Unable to create workspace edit for code action.", e); - } + ExceptionUtil.executeWithWorkspaceEditHandling(context, proposal, toResolve, LOGGER, "Unable to create workspace edit for code action"); return toResolve; } diff --git a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/codeAction/proposal/quickfix/InsertAnnotationMissingQuickFix.java b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/codeAction/proposal/quickfix/InsertAnnotationMissingQuickFix.java index 5254ef960..c2683e005 100644 --- a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/codeAction/proposal/quickfix/InsertAnnotationMissingQuickFix.java +++ b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/codeAction/proposal/quickfix/InsertAnnotationMissingQuickFix.java @@ -14,8 +14,6 @@ *******************************************************************************/ package io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.codeAction.proposal.quickfix; -import com.intellij.openapi.progress.ProcessCanceledException; -import com.intellij.openapi.project.IndexNotReadyException; import com.intellij.psi.*; import com.intellij.psi.util.PsiTreeUtil; import io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.JDTUtils; @@ -25,13 +23,11 @@ import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.java.codeaction.JavaCodeActionResolveContext; import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.java.corrections.proposal.ChangeCorrectionProposal; import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.java.corrections.proposal.InsertAnnotationProposal; +import io.openliberty.tools.intellij.util.ExceptionUtil; import org.eclipse.lsp4j.CodeAction; import org.eclipse.lsp4j.Diagnostic; -import org.eclipse.lsp4j.WorkspaceEdit; import java.util.*; -import java.util.concurrent.CancellationException; -import java.util.logging.Level; import java.util.logging.Logger; /** @@ -90,14 +86,8 @@ public CodeAction resolveCodeAction(JavaCodeActionResolveContext context) { ChangeCorrectionProposal proposal = new InsertAnnotationProposal(name, context.getCompilationUnit(), context.getASTRoot(), parentType, 0, context.getSource().getCompilationUnit(), annotations); - try { - WorkspaceEdit we = context.convertToWorkspaceEdit(proposal); - toResolve.setEdit(we); - } catch (IndexNotReadyException | ProcessCanceledException | CancellationException e) { - throw e; - } catch (Exception e) { - LOGGER.log(Level.WARNING, "Unable to create workspace edit for code action.", e); - } + + ExceptionUtil.executeWithWorkspaceEditHandling(context, proposal, toResolve, LOGGER, "Unable to create workspace edit for code action."); return toResolve; } 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 5dbc73014..dc19ca9e7 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 @@ -13,8 +13,6 @@ *******************************************************************************/ package io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.codeAction.proposal.quickfix; -import com.intellij.openapi.progress.ProcessCanceledException; -import com.intellij.openapi.project.IndexNotReadyException; import com.intellij.psi.PsiAnnotation; import com.intellij.psi.PsiClass; import com.intellij.psi.PsiElement; @@ -27,15 +25,13 @@ import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.java.codeaction.JavaCodeActionContext; import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.java.codeaction.JavaCodeActionResolveContext; import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.java.corrections.proposal.ChangeCorrectionProposal; +import io.openliberty.tools.intellij.util.ExceptionUtil; import org.eclipse.lsp4j.CodeAction; import org.eclipse.lsp4j.Diagnostic; -import org.eclipse.lsp4j.WorkspaceEdit; import java.util.ArrayList; import java.util.Arrays; import java.util.List; -import java.util.concurrent.CancellationException; -import java.util.logging.Level; import java.util.logging.Logger; /** @@ -93,14 +89,8 @@ public CodeAction resolveCodeAction(JavaCodeActionResolveContext context) { 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 { - WorkspaceEdit we = context.convertToWorkspaceEdit(proposal); - toResolve.setEdit(we); - } catch (IndexNotReadyException | ProcessCanceledException | CancellationException e) { - throw e; - } catch (Exception e) { - LOGGER.log(Level.WARNING, "Unable to create workspace edit for code action " + label, e); - } + + ExceptionUtil.executeWithWorkspaceEditHandling(context, proposal, toResolve, LOGGER, "Unable to create workspace edit for code action " + label); return toResolve; } diff --git a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/codeAction/proposal/quickfix/RemoveAnnotationConflictQuickFix.java b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/codeAction/proposal/quickfix/RemoveAnnotationConflictQuickFix.java index 0fd04e40d..5121ac425 100644 --- a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/codeAction/proposal/quickfix/RemoveAnnotationConflictQuickFix.java +++ b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/codeAction/proposal/quickfix/RemoveAnnotationConflictQuickFix.java @@ -14,8 +14,6 @@ *******************************************************************************/ package io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.codeAction.proposal.quickfix; -import com.intellij.openapi.progress.ProcessCanceledException; -import com.intellij.openapi.project.IndexNotReadyException; import com.intellij.openapi.project.Project; import com.intellij.psi.PsiClass; import com.intellij.psi.PsiElement; @@ -30,9 +28,9 @@ import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.java.codeaction.JavaCodeActionContext; import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.java.codeaction.JavaCodeActionResolveContext; import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.java.corrections.proposal.ChangeCorrectionProposal; +import io.openliberty.tools.intellij.util.ExceptionUtil; import org.eclipse.lsp4j.CodeAction; import org.eclipse.lsp4j.Diagnostic; -import org.eclipse.lsp4j.WorkspaceEdit; import org.eclipse.lsp4mp.commons.codeaction.CodeActionResolveData; import java.util.HashMap; @@ -41,8 +39,6 @@ import java.util.Arrays; import java.util.Collections; import java.util.List; -import java.util.concurrent.CancellationException; -import java.util.logging.Level; import java.util.logging.Logger; import java.util.stream.Collectors; @@ -117,14 +113,7 @@ public CodeAction resolveCodeAction(JavaCodeActionResolveContext context) { ChangeCorrectionProposal proposal = new DeleteAnnotationProposal(name, context.getSource().getCompilationUnit(), context.getASTRoot(), parentType, 0, declaringNode, resolveAnnotationsArray); - try { - WorkspaceEdit we = context.convertToWorkspaceEdit(proposal); - toResolve.setEdit(we); - } catch (IndexNotReadyException | ProcessCanceledException | CancellationException e) { - throw e; - } catch (Exception e) { - LOGGER.log(Level.WARNING, "Unable to create workspace edit for code action to remove annotation", e); - } + ExceptionUtil.executeWithWorkspaceEditHandling(context, proposal, toResolve, LOGGER, "Unable to create workspace edit for code action to remove annotation"); return toResolve; } diff --git a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/codeAction/proposal/quickfix/RemoveMethodParametersQuickFix.java b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/codeAction/proposal/quickfix/RemoveMethodParametersQuickFix.java index 66c47ded8..91f0b4296 100644 --- a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/codeAction/proposal/quickfix/RemoveMethodParametersQuickFix.java +++ b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/codeAction/proposal/quickfix/RemoveMethodParametersQuickFix.java @@ -13,8 +13,6 @@ package io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.codeAction.proposal.quickfix; -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.PsiMethod; @@ -27,15 +25,13 @@ import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.java.codeaction.JavaCodeActionContext; import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.java.codeaction.JavaCodeActionResolveContext; import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.java.corrections.proposal.ChangeCorrectionProposal; +import io.openliberty.tools.intellij.util.ExceptionUtil; import org.eclipse.lsp4j.CodeAction; import org.eclipse.lsp4j.Diagnostic; -import org.eclipse.lsp4j.WorkspaceEdit; import java.util.ArrayList; import java.util.Arrays; import java.util.List; -import java.util.concurrent.CancellationException; -import java.util.logging.Level; import java.util.logging.Logger; /** @@ -75,14 +71,8 @@ public CodeAction resolveCodeAction(JavaCodeActionResolveContext context) { final PsiParameterList parameterList = parentMethod.getParameterList(); ChangeCorrectionProposal proposal = new RemoveParamsProposal(NAME, context.getSource().getCompilationUnit(), context.getASTRoot(), parentType, 0, Arrays.asList(parameterList.getParameters()), false); - try { - WorkspaceEdit we = context.convertToWorkspaceEdit(proposal); - toResolve.setEdit(we); - } catch (IndexNotReadyException | ProcessCanceledException | CancellationException e) { - throw e; - } catch (Exception e) { - LOGGER.log(Level.WARNING, "Unable to create workspace edit for code action", e); - } + + ExceptionUtil.executeWithWorkspaceEditHandling(context, proposal, toResolve, LOGGER, "Unable to create workspace edit for code action"); return toResolve; } } 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 36d1963ef..01ec25ddb 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 @@ -13,8 +13,6 @@ package io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.codeAction.proposal.quickfix; -import com.intellij.openapi.progress.ProcessCanceledException; -import com.intellij.openapi.project.IndexNotReadyException; import com.intellij.psi.*; import com.intellij.psi.util.PsiTreeUtil; import io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.JDTUtils; @@ -23,16 +21,14 @@ 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.util.ExceptionUtil; import org.eclipse.lsp4j.CodeAction; import org.eclipse.lsp4j.Diagnostic; -import org.eclipse.lsp4j.WorkspaceEdit; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; -import java.util.concurrent.CancellationException; -import java.util.logging.Level; import java.util.logging.Logger; /** @@ -108,14 +104,7 @@ public CodeAction resolveCodeAction(JavaCodeActionResolveContext context) { ModifyModifiersProposal proposal = new ModifyModifiersProposal(label, context.getSource().getCompilationUnit(), context.getASTRoot(), parentType, 0, modifierListOwner.getModifierList(), Collections.emptyList(), Arrays.asList(modifiers), false); - try { - WorkspaceEdit we = context.convertToWorkspaceEdit(proposal); - toResolve.setEdit(we); - } catch (IndexNotReadyException | ProcessCanceledException | CancellationException e) { - throw e; - } catch (Exception e) { - LOGGER.log(Level.WARNING, "Unable to create workspace edit for code action " + label, e); - } + ExceptionUtil.executeWithWorkspaceEditHandling(context, proposal, toResolve, LOGGER, "Unable to create workspace edit for code action " + label); return toResolve; } diff --git a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/codeAction/proposal/quickfix/RemoveParamAnnotationQuickFix.java b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/codeAction/proposal/quickfix/RemoveParamAnnotationQuickFix.java index 45afcc066..1c9c606ed 100644 --- a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/codeAction/proposal/quickfix/RemoveParamAnnotationQuickFix.java +++ b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/codeAction/proposal/quickfix/RemoveParamAnnotationQuickFix.java @@ -12,8 +12,6 @@ *******************************************************************************/ package io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.codeAction.proposal.quickfix; - import com.intellij.openapi.progress.ProcessCanceledException; - import com.intellij.openapi.project.IndexNotReadyException; import com.intellij.psi.*; import com.intellij.psi.util.PsiTreeUtil; import io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.JDTUtils; @@ -22,13 +20,12 @@ 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.util.ExceptionUtil; import org.eclipse.lsp4j.CodeAction; import org.eclipse.lsp4j.Diagnostic; - import org.eclipse.lsp4j.WorkspaceEdit; import org.eclipse.lsp4mp.commons.codeaction.CodeActionResolveData; import java.util.*; - import java.util.concurrent.CancellationException; import java.util.logging.Logger; import java.util.logging.Level; @@ -121,14 +118,8 @@ public CodeAction resolveCodeAction(JavaCodeActionResolveContext context) { String label = getLabel(parameter, annotationsToRemove); RemoveAnnotationsProposal proposal = new RemoveAnnotationsProposal(label, context.getSource().getCompilationUnit(), context.getASTRoot(), parentType, 0, psiAnnotationsToRemove); - try { - WorkspaceEdit we = context.convertToWorkspaceEdit(proposal); - toResolve.setEdit(we); - } catch (IndexNotReadyException | ProcessCanceledException | CancellationException e) { - throw e; - } catch (Exception e) { - LOGGER.log(Level.WARNING, "Unable to create workspace edit for code action to extend the HttpServlet class.", e); - } + + ExceptionUtil.executeWithWorkspaceEditHandling(context, proposal, toResolve, LOGGER, "Unable to create workspace edit for code action to extend the HttpServlet class"); return toResolve; } diff --git a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/jax_rs/NoResourcePublicConstructorQuickFix.java b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/jax_rs/NoResourcePublicConstructorQuickFix.java index 807d6d36b..cdff757be 100644 --- a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/jax_rs/NoResourcePublicConstructorQuickFix.java +++ b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/jax_rs/NoResourcePublicConstructorQuickFix.java @@ -13,8 +13,6 @@ package io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.jax_rs; -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.PsiMethod; @@ -27,15 +25,13 @@ import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.java.codeaction.JavaCodeActionContext; import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.java.codeaction.JavaCodeActionResolveContext; import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.java.corrections.proposal.ChangeCorrectionProposal; +import io.openliberty.tools.intellij.util.ExceptionUtil; import org.eclipse.lsp4j.CodeAction; import org.eclipse.lsp4j.Diagnostic; -import org.eclipse.lsp4j.WorkspaceEdit; import java.util.ArrayList; import java.util.Collections; import java.util.List; -import java.util.concurrent.CancellationException; -import java.util.logging.Level; import java.util.logging.Logger; /** @@ -103,13 +99,7 @@ public CodeAction resolveCodeAction(JavaCodeActionResolveContext context) { } public void convertWorkspaceEdit(ChangeCorrectionProposal proposal, String warningMessage, JavaCodeActionResolveContext context) { - try { - WorkspaceEdit we = context.convertToWorkspaceEdit(proposal); - context.getUnresolved().setEdit(we); - } catch (IndexNotReadyException | ProcessCanceledException | CancellationException e) { - throw e; - } catch (Exception e) { - LOGGER.log(Level.WARNING, warningMessage, e); - } + final CodeAction toResolve = context.getUnresolved(); + ExceptionUtil.executeWithWorkspaceEditHandling(context, proposal, toResolve, LOGGER, warningMessage); } } 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 27b4edea0..815776b9b 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 @@ -13,8 +13,6 @@ package io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.jax_rs; -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.PsiMethod; @@ -26,14 +24,12 @@ import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.java.codeaction.JavaCodeActionContext; import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.java.codeaction.JavaCodeActionResolveContext; import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.java.corrections.proposal.ChangeCorrectionProposal; +import io.openliberty.tools.intellij.util.ExceptionUtil; import org.eclipse.lsp4j.CodeAction; import org.eclipse.lsp4j.Diagnostic; -import org.eclipse.lsp4j.WorkspaceEdit; import java.util.Collections; import java.util.List; -import java.util.concurrent.CancellationException; -import java.util.logging.Level; import java.util.logging.Logger; /** @@ -77,14 +73,8 @@ public CodeAction resolveCodeAction(JavaCodeActionResolveContext context) { 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 (IndexNotReadyException | ProcessCanceledException | CancellationException e) { - throw e; - } catch (Exception e) { - LOGGER.log(Level.WARNING, "Unable to create workspace edit for code action to make method public", e); - } + + ExceptionUtil.executeWithWorkspaceEditHandling(context, proposal, toResolve, LOGGER, "Unable to create workspace edit for code action to make method public"); return toResolve; } } diff --git a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/jax_rs/ResourceMethodMultipleEntityParamsQuickFix.java b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/jax_rs/ResourceMethodMultipleEntityParamsQuickFix.java index d27c0740e..57b6d5162 100644 --- a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/jax_rs/ResourceMethodMultipleEntityParamsQuickFix.java +++ b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/jax_rs/ResourceMethodMultipleEntityParamsQuickFix.java @@ -13,8 +13,6 @@ package io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.jax_rs; -import com.intellij.openapi.progress.ProcessCanceledException; -import com.intellij.openapi.project.IndexNotReadyException; import com.intellij.psi.*; import com.intellij.psi.util.PsiTreeUtil; import io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.JDTUtils; @@ -24,14 +22,12 @@ import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.java.codeaction.JavaCodeActionContext; import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.java.codeaction.JavaCodeActionResolveContext; import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.java.corrections.proposal.ChangeCorrectionProposal; +import io.openliberty.tools.intellij.util.ExceptionUtil; import org.eclipse.lsp4j.CodeAction; import org.eclipse.lsp4j.Diagnostic; -import org.eclipse.lsp4j.WorkspaceEdit; import org.eclipse.lsp4mp.commons.codeaction.CodeActionResolveData; import java.util.*; -import java.util.concurrent.CancellationException; -import java.util.logging.Level; import java.util.logging.Logger; import java.util.stream.Collectors; @@ -129,14 +125,7 @@ public CodeAction resolveCodeAction(JavaCodeActionResolveContext context) { ChangeCorrectionProposal proposal = new RemoveParamsProposal(title, context.getSource().getCompilationUnit(), context.getASTRoot(), parentType, 0, entityParams, false); - try { - WorkspaceEdit we = context.convertToWorkspaceEdit(proposal); - toResolve.setEdit(we); - } catch (IndexNotReadyException | ProcessCanceledException | CancellationException e) { - throw e; - } catch (Exception e) { - LOGGER.log(Level.WARNING, "Unable to create workspace edit for code action", e); - } + ExceptionUtil.executeWithWorkspaceEditHandling(context, proposal, toResolve, LOGGER, "Unable to create workspace edit for code action"); return toResolve; } diff --git a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/persistence/PersistenceAnnotationQuickFix.java b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/persistence/PersistenceAnnotationQuickFix.java index fa891bf63..5d02f8170 100644 --- a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/persistence/PersistenceAnnotationQuickFix.java +++ b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/persistence/PersistenceAnnotationQuickFix.java @@ -12,9 +12,6 @@ *******************************************************************************/ package io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.persistence; - -import com.intellij.openapi.progress.ProcessCanceledException; -import com.intellij.openapi.project.IndexNotReadyException; import com.intellij.psi.*; import io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.JDTUtils; import io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.Messages; @@ -23,15 +20,13 @@ import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.java.codeaction.JavaCodeActionContext; import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.java.codeaction.JavaCodeActionResolveContext; import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.java.corrections.proposal.ChangeCorrectionProposal; +import io.openliberty.tools.intellij.util.ExceptionUtil; import org.eclipse.lsp4j.CodeAction; import org.eclipse.lsp4j.Diagnostic; -import org.eclipse.lsp4j.WorkspaceEdit; import java.util.ArrayList; import java.util.Arrays; import java.util.List; -import java.util.concurrent.CancellationException; -import java.util.logging.Level; import java.util.logging.Logger; import java.util.stream.Collectors; @@ -75,14 +70,8 @@ public CodeAction resolveCodeAction(JavaCodeActionResolveContext context) { for (PsiAnnotation annotationNode : annotationNodes) { ChangeCorrectionProposal proposal = new ModifyAnnotationProposal(name, context.getSource().getCompilationUnit(), context.getASTRoot(), binding, annotationNode, 0, attributes, this.getAnnotations()); - try { - WorkspaceEdit we = context.convertToWorkspaceEdit(proposal); - toResolve.setEdit(we); - } catch (IndexNotReadyException | ProcessCanceledException | CancellationException e) { - throw e; - } catch (Exception e) { - LOGGER.log(Level.WARNING, "Unable to create workspace edit for code action.", e); - } + + ExceptionUtil.executeWithWorkspaceEditHandling(context, proposal, toResolve, LOGGER, "Unable to create workspace edit for code action."); } 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 7bab08d4e..d8c324e93 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 @@ -12,9 +12,6 @@ *******************************************************************************/ package io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.persistence; - -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.util.PsiTreeUtil; @@ -25,14 +22,12 @@ import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.java.codeaction.JavaCodeActionContext; import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.java.codeaction.JavaCodeActionResolveContext; import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.java.corrections.proposal.ChangeCorrectionProposal; +import io.openliberty.tools.intellij.util.ExceptionUtil; import org.eclipse.lsp4j.CodeAction; import org.eclipse.lsp4j.Diagnostic; -import org.eclipse.lsp4j.WorkspaceEdit; import java.util.ArrayList; import java.util.List; -import java.util.concurrent.CancellationException; -import java.util.logging.Level; import java.util.logging.Logger; /** @@ -93,14 +88,7 @@ public CodeAction resolveCodeAction(JavaCodeActionResolveContext context) { context.getSource().getCompilationUnit(), context.getASTRoot(), parentType, 0, constructorName.equals(Messages.getMessage("AddNoArgProtectedConstructor")) ? "protected" : "public"); - try { - WorkspaceEdit we = context.convertToWorkspaceEdit(proposal); - toResolve.setEdit(we); - } catch (IndexNotReadyException | ProcessCanceledException | CancellationException e) { - throw e; - } catch (Exception e) { - LOGGER.log(Level.WARNING, "Unable to create workspace edit for code actions to add constructors", e); - } + ExceptionUtil.executeWithWorkspaceEditHandling(context, proposal, toResolve, LOGGER, "Unable to create workspace edit for code actions to add constructors"); return toResolve; } diff --git a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/servlet/CompleteFilterAnnotationQuickFix.java b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/servlet/CompleteFilterAnnotationQuickFix.java index 2de17b474..48d88491e 100644 --- a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/servlet/CompleteFilterAnnotationQuickFix.java +++ b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/servlet/CompleteFilterAnnotationQuickFix.java @@ -14,8 +14,6 @@ package io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.servlet; -import com.intellij.openapi.progress.ProcessCanceledException; -import com.intellij.openapi.project.IndexNotReadyException; import com.intellij.psi.PsiAnnotation; import com.intellij.psi.PsiElement; import com.intellij.psi.PsiModifierListOwner; @@ -27,17 +25,15 @@ import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.java.codeaction.JavaCodeActionContext; import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.java.codeaction.JavaCodeActionResolveContext; import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.java.corrections.proposal.ChangeCorrectionProposal; +import io.openliberty.tools.intellij.util.ExceptionUtil; import org.eclipse.lsp4j.CodeAction; import org.eclipse.lsp4j.Diagnostic; -import org.eclipse.lsp4j.WorkspaceEdit; import org.eclipse.lsp4mp.commons.codeaction.CodeActionResolveData; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.concurrent.CancellationException; -import java.util.logging.Level; import java.util.logging.Logger; /** @@ -102,14 +98,8 @@ public CodeAction resolveCodeAction(JavaCodeActionResolveContext context) { String name = toResolve.getTitle(); ChangeCorrectionProposal proposal = new ModifyAnnotationProposal(name, context.getSource().getCompilationUnit(), context.getASTRoot(), parentType, annotationNode, 0, annotation, attributesToAdd); - try { - WorkspaceEdit we = context.convertToWorkspaceEdit(proposal); - toResolve.setEdit(we); - } catch (IndexNotReadyException | ProcessCanceledException | CancellationException e) { - throw e; - } catch (Exception e) { - LOGGER.log(Level.WARNING, "Unable to create workspace edit for code action.", e); - } + + ExceptionUtil.executeWithWorkspaceEditHandling(context, proposal, toResolve, LOGGER, "Unable to create workspace edit for code action."); } if (diagnosticCode.equals(ServletConstants.DIAGNOSTIC_CODE_FILTER_DUPLICATE_ATTRIBUTES)) { @@ -122,14 +112,8 @@ public CodeAction resolveCodeAction(JavaCodeActionResolveContext context) { String name = toResolve.getTitle(); ChangeCorrectionProposal proposal = new ModifyAnnotationProposal(name, context.getSource().getCompilationUnit(), context.getASTRoot(), parentType, annotationNode, 0, annotation, new ArrayList(), attributesToRemove); - try { - WorkspaceEdit we = context.convertToWorkspaceEdit(proposal); - toResolve.setEdit(we); - } catch (IndexNotReadyException | ProcessCanceledException | CancellationException e) { - throw e; - } catch (Exception e) { - LOGGER.log(Level.WARNING, "Unable to create workspace edit for code action.", e); - } + + ExceptionUtil.executeWithWorkspaceEditHandling(context, proposal, toResolve, LOGGER, "Unable to create workspace edit for code action."); } return toResolve; } diff --git a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/servlet/CompleteServletAnnotationQuickFix.java b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/servlet/CompleteServletAnnotationQuickFix.java index 0c8323a71..d50a53469 100644 --- a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/servlet/CompleteServletAnnotationQuickFix.java +++ b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/servlet/CompleteServletAnnotationQuickFix.java @@ -14,8 +14,6 @@ package io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.servlet; -import com.intellij.openapi.progress.ProcessCanceledException; -import com.intellij.openapi.project.IndexNotReadyException; import com.intellij.psi.PsiAnnotation; import com.intellij.psi.PsiElement; import com.intellij.psi.PsiModifierListOwner; @@ -27,17 +25,15 @@ import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.java.codeaction.JavaCodeActionContext; import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.java.codeaction.JavaCodeActionResolveContext; import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.java.corrections.proposal.ChangeCorrectionProposal; +import io.openliberty.tools.intellij.util.ExceptionUtil; import org.eclipse.lsp4j.CodeAction; import org.eclipse.lsp4j.Diagnostic; -import org.eclipse.lsp4j.WorkspaceEdit; import org.eclipse.lsp4mp.commons.codeaction.CodeActionResolveData; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.concurrent.CancellationException; -import java.util.logging.Level; import java.util.logging.Logger; /** @@ -101,14 +97,8 @@ public CodeAction resolveCodeAction(JavaCodeActionResolveContext context) { String name = toResolve.getTitle(); ChangeCorrectionProposal proposal = new ModifyAnnotationProposal(name, context.getSource().getCompilationUnit(), context.getASTRoot(), parentType, annotationNode, 0, annotation, attributesToAdd); - try { - WorkspaceEdit we = context.convertToWorkspaceEdit(proposal); - toResolve.setEdit(we); - } catch (IndexNotReadyException | ProcessCanceledException | CancellationException e) { - throw e; - } catch (Exception e) { - LOGGER.log(Level.WARNING, "Unable to create workspace edit for code action.", e); - } + + ExceptionUtil.executeWithWorkspaceEditHandling(context, proposal, toResolve, LOGGER, "Unable to create workspace edit for code action."); } if (diagnosticCode.equals(ServletConstants.DIAGNOSTIC_CODE_DUPLICATE_ATTRIBUTES)) { node = context.getCoveringNode(); @@ -120,14 +110,8 @@ public CodeAction resolveCodeAction(JavaCodeActionResolveContext context) { String name = toResolve.getTitle(); ChangeCorrectionProposal proposal = new ModifyAnnotationProposal(name, context.getSource().getCompilationUnit(), context.getASTRoot(), parentType, annotationNode, 0, annotation, new ArrayList(), attributesToRemove); - try { - WorkspaceEdit we = context.convertToWorkspaceEdit(proposal); - toResolve.setEdit(we); - } catch (IndexNotReadyException | ProcessCanceledException | CancellationException e) { - throw e; - } catch (Exception e) { - LOGGER.log(Level.WARNING, "Unable to create workspace edit for code action.", e); - } + + ExceptionUtil.executeWithWorkspaceEditHandling(context, proposal, toResolve, LOGGER, "Unable to create workspace edit for code action."); } return toResolve; diff --git a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/servlet/FilterImplementationQuickFix.java b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/servlet/FilterImplementationQuickFix.java index dcb777194..ea727581d 100644 --- a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/servlet/FilterImplementationQuickFix.java +++ b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/servlet/FilterImplementationQuickFix.java @@ -14,8 +14,6 @@ *******************************************************************************/ package io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.servlet; -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.util.PsiTreeUtil; @@ -26,14 +24,12 @@ import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.java.codeaction.JavaCodeActionResolveContext; import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.java.corrections.proposal.ChangeCorrectionProposal; import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.java.corrections.proposal.ImplementInterfaceProposal; +import io.openliberty.tools.intellij.util.ExceptionUtil; import org.eclipse.lsp4j.CodeAction; import org.eclipse.lsp4j.Diagnostic; -import org.eclipse.lsp4j.WorkspaceEdit; import java.util.ArrayList; import java.util.List; -import java.util.concurrent.CancellationException; -import java.util.logging.Level; import java.util.logging.Logger; /** @@ -78,14 +74,8 @@ public CodeAction resolveCodeAction(JavaCodeActionResolveContext context) { ChangeCorrectionProposal proposal = new ImplementInterfaceProposal( context.getCompilationUnit(), parentType, context.getASTRoot(), "jakarta.servlet.Filter", 0, context.getSource().getCompilationUnit()); - try { - WorkspaceEdit we = context.convertToWorkspaceEdit(proposal); - toResolve.setEdit(we); - } catch (IndexNotReadyException | ProcessCanceledException | CancellationException e) { - throw e; - } catch (Exception e) { - LOGGER.log(Level.WARNING, "Unable to create workspace edit for code action to filter implementation", e); - } + + ExceptionUtil.executeWithWorkspaceEditHandling(context, proposal, toResolve, LOGGER, "Unable to create workspace edit for code action to filter implementation"); return toResolve; } 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 5411064d1..eb1b3ae56 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 @@ -15,8 +15,6 @@ package io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.servlet; -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.util.PsiTreeUtil; @@ -27,14 +25,12 @@ import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.java.codeaction.JavaCodeActionContext; import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.java.codeaction.JavaCodeActionResolveContext; import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.java.corrections.proposal.ChangeCorrectionProposal; +import io.openliberty.tools.intellij.util.ExceptionUtil; import org.eclipse.lsp4j.CodeAction; import org.eclipse.lsp4j.Diagnostic; -import org.eclipse.lsp4j.WorkspaceEdit; import java.util.ArrayList; import java.util.List; -import java.util.concurrent.CancellationException; -import java.util.logging.Level; import java.util.logging.Logger; /** @@ -95,14 +91,8 @@ 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); - } catch (IndexNotReadyException | ProcessCanceledException | CancellationException e) { - throw e; - } catch (Exception e) { - LOGGER.log(Level.WARNING, "Unable to create workspace edit for code action to extend the HttpServlet class.", e); - } + + ExceptionUtil.executeWithWorkspaceEditHandling(context, proposal, toResolve, LOGGER, "Unable to create workspace edit for code action to extend the HttpServlet class."); return toResolve; } diff --git a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/servlet/ListenerImplementationQuickFix.java b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/servlet/ListenerImplementationQuickFix.java index 7801b91f4..e8db20899 100644 --- a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/servlet/ListenerImplementationQuickFix.java +++ b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/servlet/ListenerImplementationQuickFix.java @@ -15,8 +15,6 @@ package io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.servlet; -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.util.PsiTreeUtil; @@ -27,17 +25,15 @@ import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.java.codeaction.JavaCodeActionResolveContext; import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.java.corrections.proposal.ChangeCorrectionProposal; import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.java.corrections.proposal.ImplementInterfaceProposal; +import io.openliberty.tools.intellij.util.ExceptionUtil; import org.eclipse.lsp4j.CodeAction; import org.eclipse.lsp4j.Diagnostic; -import org.eclipse.lsp4j.WorkspaceEdit; import org.eclipse.lsp4mp.commons.codeaction.CodeActionResolveData; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.concurrent.CancellationException; -import java.util.logging.Level; import java.util.logging.Logger; /** @@ -99,14 +95,8 @@ public CodeAction resolveCodeAction(JavaCodeActionResolveContext context) { ChangeCorrectionProposal proposal = new ImplementInterfaceProposal( context.getCompilationUnit(), parentType, context.getASTRoot(), interfaceType, 0, context.getSource().getCompilationUnit()); - try { - WorkspaceEdit we = context.convertToWorkspaceEdit(proposal); - toResolve.setEdit(we); - } catch (IndexNotReadyException | ProcessCanceledException | CancellationException e) { - throw e; - } catch (Exception e) { - LOGGER.log(Level.WARNING, "Unable to create workspace edit for code action to listener implementation", e); - } + + ExceptionUtil.executeWithWorkspaceEditHandling(context, proposal, toResolve, LOGGER, "Unable to create workspace edit for code action to listener implementation"); return toResolve; } diff --git a/src/main/java/io/openliberty/tools/intellij/lsp4mp4ij/psi/core/java/codeaction/InsertAnnotationAttributeQuickFix.java b/src/main/java/io/openliberty/tools/intellij/lsp4mp4ij/psi/core/java/codeaction/InsertAnnotationAttributeQuickFix.java index 27ceabb17..8c0109dab 100644 --- a/src/main/java/io/openliberty/tools/intellij/lsp4mp4ij/psi/core/java/codeaction/InsertAnnotationAttributeQuickFix.java +++ b/src/main/java/io/openliberty/tools/intellij/lsp4mp4ij/psi/core/java/codeaction/InsertAnnotationAttributeQuickFix.java @@ -1,5 +1,5 @@ /******************************************************************************* -* Copyright (c) 2021 Red Hat Inc. and others. +* Copyright (c) 2021, 2024 Red Hat Inc. 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 @@ -13,13 +13,13 @@ *******************************************************************************/ package io.openliberty.tools.intellij.lsp4mp4ij.psi.core.java.codeaction; -import com.intellij.openapi.progress.ProcessCanceledException; -import com.intellij.openapi.project.IndexNotReadyException; + import com.intellij.psi.PsiAnnotation; import com.intellij.psi.PsiElement; import com.intellij.psi.util.PsiTreeUtil; import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.java.corrections.proposal.ChangeCorrectionProposal; import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.java.corrections.proposal.InsertAnnotationAttributeProposal; +import io.openliberty.tools.intellij.util.ExceptionUtil; import org.eclipse.lsp4j.CodeAction; import org.eclipse.lsp4j.CodeActionKind; import org.eclipse.lsp4j.Diagnostic; @@ -27,12 +27,10 @@ import org.eclipse.lsp4mp.commons.codeaction.MicroProfileCodeActionId; import java.text.MessageFormat; -import java.util.ArrayList; + import java.util.Arrays; import java.util.Collections; import java.util.List; -import java.util.concurrent.CancellationException; -import java.util.logging.Level; import java.util.logging.Logger; /** @@ -81,13 +79,8 @@ public CodeAction resolveCodeAction(JavaCodeActionResolveContext context) { String name = getLabel(attributeName); ChangeCorrectionProposal proposal = new InsertAnnotationAttributeProposal(name, context.getCompilationUnit(), annotation, 0, context.getSource().getCompilationUnit(), attributeName); - try { - toResolve.setEdit(context.convertToWorkspaceEdit(proposal)); - } catch (IndexNotReadyException | ProcessCanceledException | CancellationException e) { - throw e; - } catch (Exception e) { - LOGGER.log(Level.WARNING, "Unable to resolve code action edit for inserting an attribute value", e); - } + + ExceptionUtil.executeWithWorkspaceEditHandling(context, proposal, toResolve, LOGGER, "Unable to resolve code action edit for inserting an attribute value"); return toResolve; } diff --git a/src/main/java/io/openliberty/tools/intellij/lsp4mp4ij/psi/core/java/codeaction/InsertAnnotationMissingQuickFix.java b/src/main/java/io/openliberty/tools/intellij/lsp4mp4ij/psi/core/java/codeaction/InsertAnnotationMissingQuickFix.java index f9f6608ed..daa81cd75 100644 --- a/src/main/java/io/openliberty/tools/intellij/lsp4mp4ij/psi/core/java/codeaction/InsertAnnotationMissingQuickFix.java +++ b/src/main/java/io/openliberty/tools/intellij/lsp4mp4ij/psi/core/java/codeaction/InsertAnnotationMissingQuickFix.java @@ -1,5 +1,5 @@ /******************************************************************************* -* Copyright (c) 2020 Red Hat Inc. and others. +* Copyright (c) 2020, 2024 Red Hat Inc. 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 @@ -19,12 +19,8 @@ import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.concurrent.CancellationException; -import java.util.logging.Level; import java.util.logging.Logger; -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; @@ -32,10 +28,10 @@ import com.intellij.psi.util.PsiTreeUtil; import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.java.corrections.proposal.ChangeCorrectionProposal; import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.java.corrections.proposal.InsertAnnotationProposal; +import io.openliberty.tools.intellij.util.ExceptionUtil; 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.codeaction.CodeActionResolveData; import org.eclipse.lsp4mp.commons.codeaction.ICodeActionId; @@ -102,14 +98,8 @@ public CodeAction resolveCodeAction(JavaCodeActionResolveContext context) { ChangeCorrectionProposal proposal = new InsertAnnotationProposal(name, context.getCompilationUnit(), context.getASTRoot(), parentType, 0, context.getSource().getCompilationUnit(), resolveAnnotationsArray); - try { - WorkspaceEdit we = context.convertToWorkspaceEdit(proposal); - toResolve.setEdit(we); - } catch (IndexNotReadyException | ProcessCanceledException | CancellationException e) { - throw e; - } catch (Exception e) { - LOGGER.log(Level.WARNING, "Unable to create workspace edit for code action to insert missing annotation", e); - } + + ExceptionUtil.executeWithWorkspaceEditHandling(context, proposal, toResolve, LOGGER, "Unable to create workspace edit for code action to insert missing annotation"); return toResolve; } diff --git a/src/main/java/io/openliberty/tools/intellij/lsp4mp4ij/psi/core/java/validators/annotations/AnnotationValidator.java b/src/main/java/io/openliberty/tools/intellij/lsp4mp4ij/psi/core/java/validators/annotations/AnnotationValidator.java index d39d1d5e2..d58d8219f 100644 --- a/src/main/java/io/openliberty/tools/intellij/lsp4mp4ij/psi/core/java/validators/annotations/AnnotationValidator.java +++ b/src/main/java/io/openliberty/tools/intellij/lsp4mp4ij/psi/core/java/validators/annotations/AnnotationValidator.java @@ -1,5 +1,5 @@ /******************************************************************************* -* Copyright (c) 2021 Red Hat Inc. and others. +* Copyright (c) 2021, 2024 Red Hat Inc. 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 @@ -16,6 +16,7 @@ import com.intellij.openapi.extensions.ExtensionPointName; import com.intellij.openapi.progress.ProcessCanceledException; import com.intellij.openapi.project.IndexNotReadyException; +import io.openliberty.tools.intellij.util.ExceptionUtil; import java.util.Collection; import java.util.HashMap; @@ -88,17 +89,18 @@ public void unregisterRule(AnnotationRule rule) { * @return the error message of the validation result of the attribute value and null otherwise. */ public String validate(String value, AnnotationAttributeRule rule) { - try { - if (rule == null) { + return ExceptionUtil.executeWithExceptionHandling( + () -> { + if (rule == null) { + return null; + } + return rule.validate(value); + }, + e -> { + LOGGER.log(Level.WARNING, e.getLocalizedMessage(), e); return null; } - return rule.validate(value); - } catch (IndexNotReadyException | ProcessCanceledException | CancellationException e) { - throw e; - } catch (Exception e) { - LOGGER.log(Level.WARNING, e.getLocalizedMessage(), e); - return null; - } + ); } /** diff --git a/src/main/java/io/openliberty/tools/intellij/lsp4mp4ij/psi/internal/core/java/completion/JavaCompletionDefinition.java b/src/main/java/io/openliberty/tools/intellij/lsp4mp4ij/psi/internal/core/java/completion/JavaCompletionDefinition.java index ab969ccf7..b1f39de33 100644 --- a/src/main/java/io/openliberty/tools/intellij/lsp4mp4ij/psi/internal/core/java/completion/JavaCompletionDefinition.java +++ b/src/main/java/io/openliberty/tools/intellij/lsp4mp4ij/psi/internal/core/java/completion/JavaCompletionDefinition.java @@ -15,18 +15,16 @@ package io.openliberty.tools.intellij.lsp4mp4ij.psi.internal.core.java.completion; import com.intellij.openapi.extensions.ExtensionPointName; -import com.intellij.openapi.progress.ProcessCanceledException; -import com.intellij.openapi.project.IndexNotReadyException; import com.intellij.serviceContainer.BaseKeyedLazyInstance; import com.intellij.util.xmlb.annotations.Attribute; import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.java.completion.IJavaCompletionParticipant; import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.java.completion.JavaCompletionContext; +import io.openliberty.tools.intellij.util.ExceptionUtil; import org.eclipse.lsp4j.CompletionItem; import org.jetbrains.annotations.Nullable; import java.util.Collections; import java.util.List; -import java.util.concurrent.CancellationException; import java.util.logging.Level; import java.util.logging.Logger; @@ -50,30 +48,24 @@ public final class JavaCompletionDefinition extends BaseKeyedLazyInstance getInstance().isAdaptedForCompletion(context), + e -> { + LOGGER.log(Level.WARNING, "Error while calling isAdaptedForCompletion", e); + return false; + } + ); } @Override public List collectCompletionItems(JavaCompletionContext context) { - try { - return getInstance().collectCompletionItems(context); - } - catch (IndexNotReadyException | ProcessCanceledException | CancellationException e) { - throw e; - } - catch (Exception e) { - LOGGER.log(Level.WARNING, "Error while calling collectCompletionItems", e); - return Collections.emptyList(); - } + return ExceptionUtil.executeWithExceptionHandling( + () -> getInstance().collectCompletionItems(context), + e -> { + LOGGER.log(Level.WARNING, "Error while calling collectCompletionItems", e); + return Collections.emptyList(); + } + ); } public @Nullable String getGroup() { diff --git a/src/main/java/io/openliberty/tools/intellij/lsp4mp4ij/psi/internal/core/java/corrections/JavaDiagnosticsDefinition.java b/src/main/java/io/openliberty/tools/intellij/lsp4mp4ij/psi/internal/core/java/corrections/JavaDiagnosticsDefinition.java index 1a32519cd..ef344f4e4 100644 --- a/src/main/java/io/openliberty/tools/intellij/lsp4mp4ij/psi/internal/core/java/corrections/JavaDiagnosticsDefinition.java +++ b/src/main/java/io/openliberty/tools/intellij/lsp4mp4ij/psi/internal/core/java/corrections/JavaDiagnosticsDefinition.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2020, 2023 Red Hat Inc. and others. + * Copyright (c) 2020, 2024 Red Hat Inc. 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 @@ -21,6 +21,7 @@ import com.intellij.util.xmlb.annotations.Attribute; import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.java.diagnostics.IJavaDiagnosticsParticipant; import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.java.diagnostics.JavaDiagnosticsContext; +import io.openliberty.tools.intellij.util.ExceptionUtil; import org.eclipse.lsp4j.Diagnostic; import org.jetbrains.annotations.Nullable; @@ -50,49 +51,56 @@ public final class JavaDiagnosticsDefinition extends BaseKeyedLazyInstance getInstance().isAdaptedForDiagnostics(context), + e -> { + LOGGER.log(Level.WARNING, "Error while calling isAdaptedForDiagnostics", e); + return false; + } + ); } @Override public void beginDiagnostics(JavaDiagnosticsContext context) { - try { - getInstance().beginDiagnostics(context); - } catch (IndexNotReadyException | ProcessCanceledException | CancellationException e) { - throw e; - } catch (Exception e) { - LOGGER.log(Level.WARNING, "Error while calling beginDiagnostics", e); - } + ExceptionUtil.executeWithExceptionHandling( + () -> { + getInstance().beginDiagnostics(context); + return true; + }, + e -> { + LOGGER.log(Level.WARNING, "Error while calling beginDiagnostics", e); + return false; + } + ); } @Override public List collectDiagnostics(JavaDiagnosticsContext context) { - try { - List diagnostics = getInstance().collectDiagnostics(context); - return diagnostics != null ? diagnostics : Collections.emptyList(); - } catch (IndexNotReadyException | ProcessCanceledException | CancellationException e) { - throw e; - } catch (Exception e) { - LOGGER.log(Level.WARNING, "Error while calling collectDiagnostics", e); - return Collections.emptyList(); - } + return ExceptionUtil.executeWithExceptionHandling( + () -> { + List diagnostics = getInstance().collectDiagnostics(context); + return diagnostics != null ? diagnostics : Collections.emptyList(); + }, + e -> { + LOGGER.log(Level.WARNING, "Error while calling collectDiagnostics", e); + return Collections.emptyList(); + } + ); } + @Override public void endDiagnostics(JavaDiagnosticsContext context) { - try { - getInstance().endDiagnostics(context); - } catch (IndexNotReadyException | ProcessCanceledException | CancellationException e) { - throw e; - } catch (Exception e) { - LOGGER.log(Level.WARNING, "Error while calling endDiagnostics", e); - } + ExceptionUtil.executeWithExceptionHandling( + () -> { + getInstance().endDiagnostics(context); + return true; + }, + e -> { + LOGGER.log(Level.WARNING, "Error while calling endDiagnostics", e); + return false; + } + ); } public @Nullable String getGroup() { diff --git a/src/main/java/io/openliberty/tools/intellij/lsp4mp4ij/psi/internal/faulttolerance/java/MicroProfileFaultToleranceASTValidator.java b/src/main/java/io/openliberty/tools/intellij/lsp4mp4ij/psi/internal/faulttolerance/java/MicroProfileFaultToleranceASTValidator.java index ce3de6aa2..0a7d23823 100644 --- a/src/main/java/io/openliberty/tools/intellij/lsp4mp4ij/psi/internal/faulttolerance/java/MicroProfileFaultToleranceASTValidator.java +++ b/src/main/java/io/openliberty/tools/intellij/lsp4mp4ij/psi/internal/faulttolerance/java/MicroProfileFaultToleranceASTValidator.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2021 Red Hat Inc. and others. + * Copyright (c) 2021, 2024 Red Hat Inc. 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 @@ -14,13 +14,12 @@ package io.openliberty.tools.intellij.lsp4mp4ij.psi.internal.faulttolerance.java; import com.intellij.openapi.module.Module; -import com.intellij.openapi.progress.ProcessCanceledException; -import com.intellij.openapi.project.IndexNotReadyException; import com.intellij.psi.*; import com.intellij.psi.util.PsiTreeUtil; import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.utils.PsiTypeUtils; import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.java.diagnostics.JavaDiagnosticsContext; import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.java.validators.JavaASTValidator; +import io.openliberty.tools.intellij.util.ExceptionUtil; import org.eclipse.lsp4j.DiagnosticSeverity; import java.text.MessageFormat; @@ -31,7 +30,7 @@ import java.util.HashSet; import java.util.Map; import java.util.Set; -import java.util.concurrent.CancellationException; +import java.util.logging.Level; import java.util.logging.Logger; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -152,15 +151,18 @@ private void validateFallbackAnnotation(PsiMethod node, PsiAnnotation annotation * @param annotation The @Asynchronous annotation */ private void validateAsynchronousAnnotation(PsiMethod node, PsiAnnotation annotation) { - PsiType methodReturnType = node.getReturnType(); - String methodReturnTypeString; - try { - methodReturnTypeString = methodReturnType.getCanonicalText(); - } catch (IndexNotReadyException | ProcessCanceledException | CancellationException e) { - throw e; - } catch (Exception e) { - throw e; - } + // Use lambda expression to avoid code duplication + String methodReturnTypeString = ExceptionUtil.executeWithExceptionHandling( + () -> { + PsiType methodReturnType = node.getReturnType(); + return methodReturnType.getCanonicalText(); + }, + e -> { + LOGGER.log(Level.WARNING, "An error occurred", e); + throw e; + } + ); + if ((!isAllowedReturnTypeForAsynchronousAnnotation(methodReturnTypeString))) { String allowedTypes = allowedReturnTypesForAsynchronousAnnotation.stream() .collect(Collectors.joining("', '", "'", "'")); diff --git a/src/main/java/io/openliberty/tools/intellij/lsp4mp4ij/psi/internal/health/java/ImplementHealthCheckQuickFix.java b/src/main/java/io/openliberty/tools/intellij/lsp4mp4ij/psi/internal/health/java/ImplementHealthCheckQuickFix.java index 2769fc303..9c0375a34 100644 --- a/src/main/java/io/openliberty/tools/intellij/lsp4mp4ij/psi/internal/health/java/ImplementHealthCheckQuickFix.java +++ b/src/main/java/io/openliberty/tools/intellij/lsp4mp4ij/psi/internal/health/java/ImplementHealthCheckQuickFix.java @@ -1,5 +1,5 @@ /******************************************************************************* -* Copyright (c) 2020 Red Hat Inc. and others. +* Copyright (c) 2020, 2024 Red Hat Inc. 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 @@ -13,11 +13,8 @@ *******************************************************************************/ package io.openliberty.tools.intellij.lsp4mp4ij.psi.internal.health.java; -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.PsiNamedElement; import com.intellij.psi.PsiVariable; import com.intellij.psi.util.PsiTreeUtil; @@ -28,6 +25,7 @@ import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.java.corrections.proposal.ChangeCorrectionProposal; import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.java.corrections.proposal.ImplementInterfaceProposal; import io.openliberty.tools.intellij.lsp4mp4ij.psi.internal.health.MicroProfileHealthConstants; +import io.openliberty.tools.intellij.util.ExceptionUtil; import org.eclipse.lsp4j.CodeAction; import org.eclipse.lsp4j.CodeActionKind; import org.eclipse.lsp4j.Diagnostic; @@ -36,8 +34,6 @@ import java.util.Collections; import java.util.List; -import java.util.concurrent.CancellationException; -import java.util.logging.Level; import java.util.logging.Logger; /** @@ -85,13 +81,8 @@ public CodeAction resolveCodeAction(JavaCodeActionResolveContext context) { ChangeCorrectionProposal proposal = new ImplementInterfaceProposal(context.getCompilationUnit(), parentType, context.getASTRoot(), MicroProfileHealthConstants.HEALTH_CHECK_INTERFACE, 0, context.getSource().getCompilationUnit()); - try { - toResolve.setEdit(context.convertToWorkspaceEdit(proposal)); - } catch (IndexNotReadyException | ProcessCanceledException | CancellationException e) { - throw e; - } catch (Exception e) { - LOGGER.log(Level.WARNING, "Unable to create workspace edit to make the class implement @HealthCheck", e); - } + + ExceptionUtil.executeWithWorkspaceEditHandling(context, proposal, toResolve, LOGGER, "Unable to create workspace edit to make the class implement @HealthCheck"); } return context.getUnresolved(); } diff --git a/src/main/java/io/openliberty/tools/intellij/lsp4mp4ij/psi/internal/metrics/java/ApplicationScopedAnnotationMissingQuickFix.java b/src/main/java/io/openliberty/tools/intellij/lsp4mp4ij/psi/internal/metrics/java/ApplicationScopedAnnotationMissingQuickFix.java index 003de920a..c41547b2f 100644 --- a/src/main/java/io/openliberty/tools/intellij/lsp4mp4ij/psi/internal/metrics/java/ApplicationScopedAnnotationMissingQuickFix.java +++ b/src/main/java/io/openliberty/tools/intellij/lsp4mp4ij/psi/internal/metrics/java/ApplicationScopedAnnotationMissingQuickFix.java @@ -1,5 +1,5 @@ /******************************************************************************* -* Copyright (c) 2020 IBM Corporation and others. +* Copyright (c) 2020, 2024 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 @@ -13,8 +13,6 @@ *******************************************************************************/ package io.openliberty.tools.intellij.lsp4mp4ij.psi.internal.metrics.java; -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; @@ -28,6 +26,7 @@ import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.java.corrections.proposal.ReplaceAnnotationProposal; import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.utils.PsiTypeUtils; import io.openliberty.tools.intellij.lsp4mp4ij.psi.internal.metrics.MicroProfileMetricsConstants; +import io.openliberty.tools.intellij.util.ExceptionUtil; import org.eclipse.lsp4j.CodeAction; import org.eclipse.lsp4j.CodeActionKind; import org.eclipse.lsp4j.Diagnostic; @@ -36,8 +35,6 @@ import java.util.Collections; import java.util.List; -import java.util.concurrent.CancellationException; -import java.util.logging.Level; import java.util.logging.Logger; /** @@ -101,13 +98,8 @@ public CodeAction resolveCodeAction(JavaCodeActionResolveContext context) { ChangeCorrectionProposal proposal = new ReplaceAnnotationProposal(name, context.getCompilationUnit(), context.getASTRoot(), parentType, 0, addAnnotation, context.getSource().getCompilationUnit(), REMOVE_ANNOTATION_NAMES); - try { - toResolve.setEdit(context.convertToWorkspaceEdit(proposal)); - } catch (IndexNotReadyException | ProcessCanceledException | CancellationException e) { - throw e; - } catch (Exception e) { - LOGGER.log(Level.WARNING, "Failed to create workspace edit to replace bean scope annotation", e); - } + + ExceptionUtil.executeWithWorkspaceEditHandling(context, proposal, toResolve, LOGGER, "Failed to create workspace edit to replace bean scope annotation"); return toResolve; } diff --git a/src/main/java/io/openliberty/tools/intellij/lsp4mp4ij/psi/internal/openapi/java/MicroProfileGenerateOpenAPIOperation.java b/src/main/java/io/openliberty/tools/intellij/lsp4mp4ij/psi/internal/openapi/java/MicroProfileGenerateOpenAPIOperation.java index c2df79979..250582021 100644 --- a/src/main/java/io/openliberty/tools/intellij/lsp4mp4ij/psi/internal/openapi/java/MicroProfileGenerateOpenAPIOperation.java +++ b/src/main/java/io/openliberty/tools/intellij/lsp4mp4ij/psi/internal/openapi/java/MicroProfileGenerateOpenAPIOperation.java @@ -1,5 +1,5 @@ /******************************************************************************* -* Copyright (c) 2020 Red Hat Inc. and others. +* Copyright (c) 2020, 2024 Red Hat Inc. 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 @@ -14,8 +14,6 @@ package io.openliberty.tools.intellij.lsp4mp4ij.psi.internal.openapi.java; import com.intellij.openapi.module.Module; -import com.intellij.openapi.progress.ProcessCanceledException; -import com.intellij.openapi.project.IndexNotReadyException; import com.intellij.psi.PsiClass; import com.intellij.psi.PsiFile; import com.intellij.psi.util.PsiTreeUtil; @@ -26,6 +24,7 @@ import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.java.corrections.proposal.ChangeCorrectionProposal; import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.utils.PsiTypeUtils; import io.openliberty.tools.intellij.lsp4mp4ij.psi.internal.openapi.MicroProfileOpenAPIConstants; +import io.openliberty.tools.intellij.util.ExceptionUtil; import org.apache.commons.lang3.StringUtils; import org.eclipse.lsp4j.CodeAction; import org.eclipse.lsp4j.CodeActionKind; @@ -35,7 +34,7 @@ import java.text.MessageFormat; import java.util.*; -import java.util.concurrent.CancellationException; +import java.util.logging.Logger; /** * Generate OpenAPI annotations by the "Source" kind code action. @@ -47,6 +46,8 @@ public class MicroProfileGenerateOpenAPIOperation implements IJavaCodeActionPart private final static String MESSAGE = "Generate OpenAPI Annotations for ''{0}''"; + private static final Logger LOGGER = Logger.getLogger(MicroProfileGenerateOpenAPIOperation.class.getName()); + private final static String TYPE_NAME_KEY = "type"; @Override @@ -123,13 +124,7 @@ public CodeAction resolveCodeAction(JavaCodeActionResolveContext context) { typeDeclaration, MicroProfileOpenAPIConstants.OPERATION_ANNOTATION, 0, context.getSource().getCompilationUnit()); - try { - toResolve.setEdit(context.convertToWorkspaceEdit(proposal)); - } catch (IndexNotReadyException | ProcessCanceledException | CancellationException e) { - throw e; - } catch (Exception e) { - } - + ExceptionUtil.executeWithWorkspaceEditHandling(context, proposal, toResolve, LOGGER, "Unable to create workspace edit for code action"); return toResolve; } diff --git a/src/main/java/io/openliberty/tools/intellij/util/ExceptionUtil.java b/src/main/java/io/openliberty/tools/intellij/util/ExceptionUtil.java new file mode 100644 index 000000000..1cc860363 --- /dev/null +++ b/src/main/java/io/openliberty/tools/intellij/util/ExceptionUtil.java @@ -0,0 +1,76 @@ +/******************************************************************************* + * Copyright (c) 2024 IBM Corporation. + * + * 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 + ******************************************************************************/ +package io.openliberty.tools.intellij.util; + +import com.intellij.openapi.progress.ProcessCanceledException; +import com.intellij.openapi.project.IndexNotReadyException; + +import java.util.concurrent.CancellationException; +import java.util.function.Function; +import java.util.function.Supplier; +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.WorkspaceEdit; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * This class is intended for cases where exception handling is repetitive + */ +public class ExceptionUtil { + + /** + * Executes a supplied action with structured exception handling, allowing + * the caller to specify a fallback function in case of an exception. + * + * @param action the main operation to execute, represented by a Supplier + * @param fallback a function to handle exceptions and provide a safe return value + * @param the type of result expected from the action + * @return the result of the action or the fallback value in case of an exception + */ + public static T executeWithExceptionHandling(Supplier action, Function fallback) { + try { + return action.get(); + } catch (ProcessCanceledException e) { + //Since 2024.2 ProcessCanceledException extends CancellationException so we can't use multi-catch to keep backward compatibility + //TODO delete block when minimum required version is 2024.2 + throw e; + } catch (IndexNotReadyException | CancellationException e) { + throw e; + } catch (RuntimeException e) { + // Invoke the fallback function with the exception to generate a safe return value. + return fallback.apply(e); + } + } + + /** + * Executes a workspace edit operation, handling any errors by logging them. + * + * @param context the context required to convert a correction proposal to a workspace edit + * @param proposal the proposed correction to be applied + * @param toResolve the code action to be resolved with the edit + * @param logger a logger instance for logging exceptions if they occur + * @param logMessage a message to log if an exception is encountered + */ + public static void executeWithWorkspaceEditHandling(JavaCodeActionResolveContext context, ChangeCorrectionProposal proposal, CodeAction toResolve, Logger logger, String logMessage) { + executeWithExceptionHandling( + () -> { + WorkspaceEdit we = context.convertToWorkspaceEdit(proposal); + toResolve.setEdit(we); + return true; + }, + e -> { + logger.log(Level.WARNING, logMessage, e); + return false; + } + ); + } +} \ No newline at end of file