Skip to content

Commit

Permalink
Revert "Revert "Merge branch 'OpenLiberty#593-Enable-RemoveAnnotation…
Browse files Browse the repository at this point in the history
…ConflictQuickFix' of https://github.com/vaisakhkannan/liberty-tools-intellij into OpenLiberty#593-Enable-RemoveAnnotationConflictQuickFix""

This reverts commit 5f46c5d.
  • Loading branch information
aparnamichael committed Mar 6, 2024
1 parent 5f46c5d commit 403c013
Show file tree
Hide file tree
Showing 44 changed files with 2,744 additions and 845 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ jobs:
with:
repository: MicroShed/lsp4ij
path: lsp4ij
ref: refs/tags/0.0.1
- name: 'Checkout liberty-tools-intellij'
uses: actions/checkout@v3
with:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2021, 2023 IBM Corporation and others.
* Copyright (c) 2021, 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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2021, 2023 IBM Corporation and others.
* Copyright (c) 2021, 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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,82 +1,148 @@
/*******************************************************************************
* Copyright (c) 2021, 2023 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
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
* Copyright (c) 2021, 2023 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
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.beanvalidation;

import com.intellij.psi.*;
import com.intellij.psi.util.PsiTreeUtil;
import io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.JDTUtils;
import io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.Messages;
import io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.codeAction.proposal.ModifyModifiersProposal;
import io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.codeAction.proposal.RemoveAnnotationsProposal;
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 org.eclipse.lsp4j.CodeAction;
import org.eclipse.lsp4j.Diagnostic;
import org.eclipse.lsp4j.WorkspaceEdit;
import org.eclipse.lsp4mp.commons.CodeActionResolveData;

import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
* Quickfix for fixing {@link BeanValidationConstants#DIAGNOSTIC_CODE_STATIC} error by either action
* 1. Removing constraint annotation on static field or method
* 2. Removing static modifier from field or method
*
* @author Leslie Dawson (lamminade)
*
* @author Leslie Dawson (lamminade)
*/
public class BeanValidationQuickFix {
public class BeanValidationQuickFix implements IJavaCodeActionParticipant {

private static final Logger LOGGER = Logger.getLogger(BeanValidationQuickFix.class.getName());

private static final String ANNOTATION_NAME = "annotationName";

@Override
public String getParticipantId() {
return BeanValidationQuickFix.class.getName();
}

public List<? extends CodeAction> getCodeActions(JavaCodeActionContext context, Diagnostic diagnostic) {
List<CodeAction> codeActions = new ArrayList<>();
removeConstraintAnnotations(diagnostic, context.copy(), codeActions);
removeConstraintAnnotationsCodeActions(diagnostic, context, codeActions);

if (diagnostic.getCode().getLeft().equals(BeanValidationConstants.DIAGNOSTIC_CODE_STATIC)) {
removeStaticModifier(diagnostic, context.copy(), codeActions);
removeStaticModifierCodeActions(diagnostic, context, codeActions);
}
return codeActions;
}

private void removeConstraintAnnotations(Diagnostic diagnostic, JavaCodeActionContext context, List<CodeAction> codeActions) {
@Override
public CodeAction resolveCodeAction(JavaCodeActionResolveContext context) {

final CodeAction toResolve = context.getUnresolved();

String message = toResolve.getTitle();

if (message.equals(Messages.getMessage("RemoveStaticModifier"))) {
resolveStaticModifierCodeAction(context);
return toResolve;
}

resolveRemoveConstraintAnnotationsCodeAction(context);

return toResolve;
}

private void removeConstraintAnnotationsCodeActions(Diagnostic diagnostic, JavaCodeActionContext context, List<CodeAction> codeActions) {

final String annotationName = diagnostic.getData().toString().replace("\"", "");
final String name = Messages.getMessage("RemoveConstraintAnnotation", annotationName);
Map<String, Object> extendedData = new HashMap<>();
extendedData.put(ANNOTATION_NAME, annotationName);
codeActions.add(JDTUtils.createCodeAction(context, diagnostic, name, getParticipantId(), extendedData));
}

private void resolveRemoveConstraintAnnotationsCodeAction(JavaCodeActionResolveContext context) {

final CodeAction toResolve = context.getUnresolved();
final PsiElement node = context.getCoveredNode();
final PsiClass parentType = PsiTreeUtil.getParentOfType(node, PsiClass.class);
CodeActionResolveData data = (CodeActionResolveData) toResolve.getData();
String annotationName;
if (data.getExtendedDataEntry(ANNOTATION_NAME) instanceof String) {
annotationName = (String) data.getExtendedDataEntry(ANNOTATION_NAME);
} else {
annotationName = "";
}
final String name = toResolve.getTitle();
final PsiModifierListOwner modifierListOwner = PsiTreeUtil.getParentOfType(node, PsiModifierListOwner.class);

final String annotationName = diagnostic.getData().toString().replace("\"", "");
final PsiAnnotation[] annotations = modifierListOwner.getAnnotations();

if (annotations != null && annotations.length > 0) {
final Optional<PsiAnnotation> annotationToRemove =
Arrays.stream(annotations).filter(a -> annotationName.equals(a.getQualifiedName())).findFirst();
if (annotationToRemove.isPresent()) {
final String name = Messages.getMessage("RemoveConstraintAnnotation", annotationName);
boolean isFormatRequired = false;
final RemoveAnnotationsProposal proposal = new RemoveAnnotationsProposal(name, context.getSource().getCompilationUnit(),
context.getASTRoot(), parentType, 0, Collections.singletonList(annotationToRemove.get()));
final CodeAction codeAction = context.convertToCodeAction(proposal, diagnostic);
if (codeAction != null) {
codeActions.add(codeAction);
context.getASTRoot(), parentType, 0, Collections.singletonList(annotationToRemove.get()), isFormatRequired);

try {
WorkspaceEdit we = context.convertToWorkspaceEdit(proposal);
toResolve.setEdit(we);
} catch (Exception e) {
LOGGER.log(Level.WARNING, "Unable to create workspace edit for code action to remove constraint annotation", e);
}
}
}
}

private void removeStaticModifier(Diagnostic diagnostic, JavaCodeActionContext context, List<CodeAction> codeActions) {
private void resolveStaticModifierCodeAction(JavaCodeActionResolveContext context) {
final CodeAction toResolve = context.getUnresolved();
final PsiElement node = context.getCoveredNode();
final PsiClass parentType = PsiTreeUtil.getParentOfType(node, PsiClass.class);
final PsiModifierListOwner modifierListOwner = PsiTreeUtil.getParentOfType(node, PsiModifierListOwner.class);

final String name = Messages.getMessage("RemoveStaticModifier");
final ModifyModifiersProposal proposal = new ModifyModifiersProposal(name, context.getSource().getCompilationUnit(),
final ModifyModifiersProposal proposal = new ModifyModifiersProposal(toResolve.getTitle()
, context.getSource().getCompilationUnit(),
context.getASTRoot(), parentType, 0, modifierListOwner.getModifierList(), Collections.emptyList(),
Collections.singletonList("static"));
final CodeAction codeAction = context.convertToCodeAction(proposal, diagnostic);
if (codeAction != null) {
codeActions.add(codeAction);

try {
WorkspaceEdit we = context.convertToWorkspaceEdit(proposal);
toResolve.setEdit(we);
} catch (Exception e) {
LOGGER.log(Level.WARNING, "Unable to create workspace edit for code action to remove static modifier", e);
}
}
}

private void removeStaticModifierCodeActions(Diagnostic diagnostic, JavaCodeActionContext context,
List<CodeAction> codeActions) {

final String name = Messages.getMessage("RemoveStaticModifier");
codeActions.add(JDTUtils.createCodeAction(context, diagnostic, name, getParticipantId()));
}
}


Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2021, 2023 IBM Corporation and others.
* Copyright (c) 2021, 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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright (c) 2021 IBM Corporation.
/* Copyright (c) 2021, 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
Expand All @@ -17,4 +17,9 @@ public class ManagedBeanConstructorQuickFix extends InsertAnnotationMissingQuick
public ManagedBeanConstructorQuickFix() {
super("jakarta.inject.Inject");
}

@Override
public String getParticipantId() {
return ManagedBeanConstructorQuickFix.class.getName();
}
}
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -15,25 +15,32 @@

package io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.cdi;

import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiElement;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.PsiModifierListOwner;
import io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.JDTUtils;
import io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.Messages;
import io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.codeAction.proposal.quickfix.InsertAnnotationMissingQuickFix;
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.lsp4mp4ij.psi.core.java.corrections.proposal.ReplaceAnnotationProposal;
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.logging.Level;
import java.util.logging.Logger;

import static io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.cdi.ManagedBeanConstants.SCOPE_FQ_NAMES;

public class ManagedBeanQuickFix extends InsertAnnotationMissingQuickFix {
private static final Logger LOGGER = Logger.getLogger(ManagedBeanQuickFix.class.getName());
private static final String ADD_ANNOTATION = "jakarta.enterprise.context.Dependent";

public ManagedBeanQuickFix() {
super("jakarta.enterprise.context.Dependent");
super(ADD_ANNOTATION);
}

private static final String[] REMOVE_ANNOTATION_NAMES = new ArrayList<>(SCOPE_FQ_NAMES).toArray(new String[SCOPE_FQ_NAMES.size()]);
Expand All @@ -48,29 +55,38 @@ protected void insertAnnotations(Diagnostic diagnostic, JavaCodeActionContext co
}
}

private static void insertAndReplaceAnnotation(Diagnostic diagnostic, JavaCodeActionContext context,
List<CodeAction> codeActions, String annotation) {
// Diagnostic is reported on the variable declaration, however the
// annotations that need to be replaced are on the type declaration (class
// definition) containing the variable declaration. We retrieve the type
// declaration container here.
PsiElement parentNode = context.getCoveredNode();
PsiClass classBinding = PsiTreeUtil.getParentOfType(parentNode, PsiClass.class);

// Insert the annotation and the proper import by using JDT Core Manipulation
// API
private void insertAndReplaceAnnotation(Diagnostic diagnostic, JavaCodeActionContext context,
List<CodeAction> codeActions, String annotation) {
String name = getLabel(annotation);
codeActions.add(JDTUtils.createCodeAction(context, diagnostic, name, getParticipantId()));
}

@Override
public CodeAction resolveCodeAction(JavaCodeActionResolveContext context) {
final CodeAction toResolve = context.getUnresolved();
String name = toResolve.getTitle();
PsiElement node = context.getCoveringNode();
PsiModifierListOwner parentType = getBinding(node);
ChangeCorrectionProposal proposal = new ReplaceAnnotationProposal(name, context.getCompilationUnit(),
context.getASTRoot(), classBinding, 0, annotation, context.getCompilationUnit(), REMOVE_ANNOTATION_NAMES);
// Convert the proposal to LSP4J CodeAction
CodeAction codeAction = context.convertToCodeAction(proposal, diagnostic);
if (codeAction != null) {
codeActions.add(codeAction);
context.getASTRoot(), parentType, 0, ADD_ANNOTATION, context.getSource().getCompilationUnit(),
REMOVE_ANNOTATION_NAMES);

try {
WorkspaceEdit we = context.convertToWorkspaceEdit(proposal);
toResolve.setEdit(we);
} catch (Exception e) {
LOGGER.log(Level.WARNING, "Unable to create workspace edit for code action.", e);
}
return toResolve;
}

private static String getLabel(String annotation) {
String annotationName = annotation.substring(annotation.lastIndexOf('.') + 1, annotation.length());
return Messages.getMessage("ReplaceCurrentScope", "@" + annotationName);
}

@Override
public String getParticipantId() {
return ManagedBeanQuickFix.class.getName();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,8 @@ public RemoveInvalidInjectParamAnnotationQuickFix() {
super(Arrays.copyOf(ManagedBeanConstants.INVALID_INJECT_PARAMS_FQ,
ManagedBeanConstants.INVALID_INJECT_PARAMS_FQ.length));
}
public String getParticipantId() {
return RemoveInvalidInjectParamAnnotationQuickFix.class.getName();
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2021, 2023 IBM Corporation and others.
* Copyright (c) 2021, 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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2021, 2023 IBM Corporation and others.
* Copyright (c) 2021, 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
Expand Down Expand Up @@ -64,11 +64,7 @@ public List<? extends CodeAction> getCodeActions(JavaCodeActionContext context,
for (String annotation : fqAnnotations) {
List<String> resultingAnnotations = new ArrayList<>(fqAnnotations);
resultingAnnotations.remove(annotation);
// For each list we will create one code action in its own context
JavaCodeActionContext newContext = context.copy();
PsiElement owningNode = getBinding(newContext.getCoveredNode());

removeAnnotation(diagnostic, newContext, owningNode, codeActions,
removeAnnotation(diagnostic, context, codeActions,
resultingAnnotations.toArray(new String[] {}));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,10 @@ public RemoveAnnotationsProposal(String label, PsiFile sourceCU, PsiFile invocat
List<PsiAnnotation> annotationsToRemove) {
super(label, sourceCU, invocationNode, binding, relevance, annotationsToRemove);
}

public RemoveAnnotationsProposal(String label, PsiFile sourceCU, PsiFile invocationNode,
PsiModifierListOwner binding, int relevance,
List<PsiAnnotation> annotationsToRemove, boolean isFormatRequired) {
super(label, sourceCU, invocationNode, binding, relevance, annotationsToRemove, isFormatRequired);
}
}
Loading

0 comments on commit 403c013

Please sign in to comment.