forked from OpenLiberty/liberty-tools-intellij
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revert "Revert "Merge branch 'OpenLiberty#593-Enable-RemoveAnnotation…
…ConflictQuickFix' of https://github.com/vaisakhkannan/liberty-tools-intellij into OpenLiberty#593-Enable-RemoveAnnotationConflictQuickFix"" This reverts commit 5f46c5d.
- Loading branch information
1 parent
5f46c5d
commit 403c013
Showing
44 changed files
with
2,744 additions
and
845 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
.../tools/intellij/lsp4jakarta/lsp4ij/annotations/RemovePostConstructAnnotationQuickFix.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...rty/tools/intellij/lsp4jakarta/lsp4ij/annotations/RemovePreDestroyAnnotationQuickFix.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
128 changes: 97 additions & 31 deletions
128
.../openliberty/tools/intellij/lsp4jakarta/lsp4ij/beanvalidation/BeanValidationQuickFix.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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())); | ||
} | ||
} | ||
|
||
|
2 changes: 1 addition & 1 deletion
2
.../io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/cdi/ConflictProducesInjectQuickFix.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/cdi/RemoveProduceAnnotationQuickFix.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.