Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix usage of ProcessCanceledException to support 2024.2 #1047

Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -22,6 +22,7 @@
import java.util.concurrent.CancellationException;
import java.util.stream.Collectors;
import java.util.Collections;
import java.util.function.Supplier;

/**
* Returns the list of recognised defining annotations applied to a
Expand All @@ -33,15 +34,28 @@
*/
public class AnnotationUtil {
public static List<String> getScopeAnnotations(PsiClass type, Set<String> scopes) {
return 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()),
Collections::emptyList
);
}

private static <T> T executeWithExceptionHandling(Supplier<T> action, Supplier<T> fallback) {
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) {
return action.get();
} catch (ProcessCanceledException e) {
//Since 2024.2 ProcessCanceledException extends CancellationException so we can't use multi-catch to keep backward compatibility
throw e;
} catch (IndexNotReadyException | CancellationException e) {
throw e;
} catch (Exception e) {
return Collections.<String>emptyList();
return fallback.get(); // Return fallback value
}
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see this method is private and only called by getScopeAnnotations(). Is there some way to combine this with the more widely used utility method you introduced in ExceptionUtil? Otherwise this has just introduced more complexity without reducing code duplication in this spot.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes @mrglavas , I made the changes , please take a look , Thanks

Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -26,13 +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.List;
import java.util.concurrent.CancellationException;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand Down Expand Up @@ -74,13 +71,17 @@ 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);

Boolean success = ExceptionUtil.executeWithExceptionHandling(
() -> {
WorkspaceEdit we = context.convertToWorkspaceEdit(proposal);
toResolve.setEdit(we);
return true;
},
e -> LOGGER.log(Level.WARNING, "Unable to create workspace edit for code action to change return type to void", e)
);
if (success == null || !success) {
System.out.println("An error occurred during the code action resolution.");
}
return toResolve;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -23,13 +21,13 @@
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;

Expand Down Expand Up @@ -111,13 +109,16 @@ 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);
Boolean success = ExceptionUtil.executeWithExceptionHandling(
() -> {
WorkspaceEdit we = context.convertToWorkspaceEdit(proposal);
toResolve.setEdit(we);
return true;
},
e -> LOGGER.log(Level.WARNING, "Unable to create workspace edit for code action to remove constraint annotation", e)
);
if (success == null || !success) {
System.out.println("An error occurred during the code action resolution.");
}
}
}
Expand All @@ -134,13 +135,16 @@ 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);
Boolean success = ExceptionUtil.executeWithExceptionHandling(
() -> {
WorkspaceEdit we = context.convertToWorkspaceEdit(proposal);
toResolve.setEdit(we);
return true;
},
e -> LOGGER.log(Level.WARNING, "Unable to create workspace edit for code action to remove static modifier", e)
);
if (success == null || !success) {
System.out.println("An error occurred during the code action resolution.");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -25,14 +23,14 @@
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;

Expand Down Expand Up @@ -74,13 +72,16 @@ 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);
Boolean success = ExceptionUtil.executeWithExceptionHandling(
() -> {
WorkspaceEdit we = context.convertToWorkspaceEdit(proposal);
toResolve.setEdit(we);
return true;
},
e -> LOGGER.log(Level.WARNING, "Unable to create workspace edit for code actions to add constructors", e)
);
if (success == null || !success) {
System.out.println("An error occurred during the code action resolution.");
}

return toResolve;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -26,13 +24,13 @@
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;

Expand Down Expand Up @@ -74,13 +72,16 @@ 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);
Boolean success = ExceptionUtil.executeWithExceptionHandling(
() -> {
WorkspaceEdit we = context.convertToWorkspaceEdit(proposal);
toResolve.setEdit(we);
return true;
},
e -> LOGGER.log(Level.WARNING, "Unable to create workspace edit for code action", e)
);
if (success == null || !success) {
System.out.println("An error occurred during the code action resolution.");
}
return toResolve;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -25,12 +23,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.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;

Expand Down Expand Up @@ -90,13 +88,17 @@ 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);

Boolean success = ExceptionUtil.executeWithExceptionHandling(
() -> {
WorkspaceEdit we = context.convertToWorkspaceEdit(proposal);
toResolve.setEdit(we);
return true;
},
e -> LOGGER.log(Level.WARNING, "Unable to create workspace edit for code action.", e)
);
if (success == null || !success) {
System.out.println("An error occurred during the code action resolution.");
}
return toResolve;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -27,14 +25,14 @@
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;

Expand Down Expand Up @@ -93,13 +91,17 @@ 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);

Boolean success = ExceptionUtil.executeWithExceptionHandling(
() -> {
WorkspaceEdit we = context.convertToWorkspaceEdit(proposal);
toResolve.setEdit(we);
return true;
},
e -> LOGGER.log(Level.WARNING, "Unable to create workspace edit for code action " + label, e)
);
if (success == null || !success) {
System.out.println("An error occurred during the code action resolution.");
}
return toResolve;
}
Expand Down
Loading
Loading