diff --git a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/AbstractDiagnosticsCollector.java b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/AbstractDiagnosticsCollector.java index 1573bbe45..5c1b2375b 100644 --- a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/AbstractDiagnosticsCollector.java +++ b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/AbstractDiagnosticsCollector.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2022, 2023 IBM Corporation and others. + * Copyright (c) 2022, 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 @@ -231,20 +231,6 @@ protected static boolean nameEndsWith(String fqName, String name) { return fqName.equals(name) || fqName.endsWith("." + name); } - /** - * Returns simple name for the given fully qualified name. - * - * @param fqName a fully qualified name or simple name - * @return simple name for given fully qualified name - */ - public static String getSimpleName(String fqName) { - int idx = fqName.lastIndexOf('.'); - if (idx != -1 && idx != fqName.length() - 1) { - return fqName.substring(idx + 1); - } - return fqName; - } - /** * Returns true if the given method is a constructor and false otherwise. * diff --git a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/JDTUtils.java b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/JDTUtils.java index 2492e7221..b92eece93 100644 --- a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/JDTUtils.java +++ b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/JDTUtils.java @@ -109,4 +109,18 @@ public static CodeAction createCodeAction(JavaCodeActionContext context, Diagnos String quickFixMessage, String participantId) { return createCodeAction(context, diagnostic, quickFixMessage, participantId, null); } + + /** + * Returns simple name for the given fully qualified name. + * + * @param fqName a fully qualified name or simple name + * @return simple name for given fully qualified name + */ + public static String getSimpleName(String fqName) { + final int idx = fqName.lastIndexOf('.'); + if (idx != -1 && idx != fqName.length() - 1) { + return fqName.substring(idx + 1); + } + return fqName; + } } diff --git a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/beanvalidation/BeanValidationDiagnosticsCollector.java b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/beanvalidation/BeanValidationDiagnosticsCollector.java index 5eb3f6d8f..b92de2bb0 100644 --- a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/beanvalidation/BeanValidationDiagnosticsCollector.java +++ b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/beanvalidation/BeanValidationDiagnosticsCollector.java @@ -19,6 +19,7 @@ import org.eclipse.lsp4j.Diagnostic; import org.eclipse.lsp4j.DiagnosticSeverity; +import static io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.JDTUtils.getSimpleName; import static io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.beanvalidation.BeanValidationConstants.*; import java.util.List; 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 6cbd49f74..cd835a8d5 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 @@ -16,7 +16,6 @@ import com.intellij.openapi.project.IndexNotReadyException; import com.intellij.psi.*; import com.intellij.psi.util.PsiTreeUtil; -import io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.AbstractDiagnosticsCollector; 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; @@ -34,6 +33,8 @@ import java.util.logging.Level; import java.util.logging.Logger; +import static io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.JDTUtils.getSimpleName; + /** * Quickfix for fixing {@link BeanValidationConstants#DIAGNOSTIC_CODE_STATIC} error by either action * 1. Removing constraint annotation on static field or method @@ -82,7 +83,7 @@ public CodeAction resolveCodeAction(JavaCodeActionResolveContext context) { private void removeConstraintAnnotationsCodeActions(Diagnostic diagnostic, JavaCodeActionContext context, List codeActions) { final String annotationName = diagnostic.getData().toString().replace("\"", ""); - final String name = Messages.getMessage("RemoveConstraintAnnotation", AbstractDiagnosticsCollector.getSimpleName(annotationName)); + final String name = Messages.getMessage("RemoveConstraintAnnotation", getSimpleName(annotationName)); Map extendedData = new HashMap<>(); extendedData.put(ANNOTATION_NAME, annotationName); codeActions.add(JDTUtils.createCodeAction(context, diagnostic, name, getParticipantId(), extendedData)); diff --git a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/cdi/ManagedBeanDiagnosticsCollector.java b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/cdi/ManagedBeanDiagnosticsCollector.java index 42eac428a..cb92c793c 100644 --- a/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/cdi/ManagedBeanDiagnosticsCollector.java +++ b/src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/cdi/ManagedBeanDiagnosticsCollector.java @@ -17,7 +17,6 @@ import java.util.List; import java.util.Set; import java.util.TreeSet; -import java.util.stream.Collectors; import java.util.stream.Stream; import com.intellij.psi.*; @@ -29,6 +28,7 @@ import com.google.gson.Gson; import com.google.gson.JsonArray; +import static io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.JDTUtils.getSimpleName; import static io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.cdi.ManagedBeanConstants.*; public class ManagedBeanDiagnosticsCollector extends AbstractDiagnosticsCollector { 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 2241ad4b0..0083102d5 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 @@ -38,6 +38,7 @@ import java.util.logging.Level; import java.util.logging.Logger; +import static io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.JDTUtils.getSimpleName; import static io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.cdi.ManagedBeanConstants.SCOPE_FQ_NAMES; public class ManagedBeanQuickFix extends InsertAnnotationMissingQuickFix { @@ -88,7 +89,7 @@ public CodeAction resolveCodeAction(JavaCodeActionResolveContext context) { } private static String getLabel(String annotation) { - String annotationName = annotation.substring(annotation.lastIndexOf('.') + 1, annotation.length()); + String annotationName = getSimpleName(annotation); return Messages.getMessage("ReplaceCurrentScope", "@" + annotationName); } 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..fd776c8a6 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 @@ -34,6 +34,8 @@ import java.util.logging.Level; import java.util.logging.Logger; +import static io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.JDTUtils.getSimpleName; + /** * QuickFix for inserting annotations. * Reused from https://github.com/eclipse/lsp4mp/blob/6f2d700a88a3262e39cc2ba04beedb429e162246/microprofile.jdt/org.eclipse.lsp4mp.jdt.core/src/main/java/org/eclipse/lsp4mp/jdt/core/java/codeaction/InsertAnnotationMissingQuickFix.java @@ -139,7 +141,7 @@ private static String getLabel(String[] annotations) { StringBuilder list = new StringBuilder(); for (int i = 0; i < annotations.length; i++) { String annotation = annotations[i]; - String annotationName = annotation.substring(annotation.lastIndexOf('.') + 1, annotation.length()); + String annotationName = getSimpleName(annotation); if (i > 0) { list.append(", "); // assume comma list is ok: @A, @B, @C } 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..e23616923 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 @@ -46,6 +46,8 @@ import java.util.logging.Logger; import java.util.stream.Collectors; +import static io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.JDTUtils.getSimpleName; + /** * QuickFix for removing annotations. Modified from * https://github.com/eclipse/lsp4mp/blob/6f2d700a88a3262e39cc2ba04beedb429e162246/microprofile.jdt/org.eclipse.lsp4mp.jdt.core/src/main/java/org/eclipse/lsp4mp/jdt/core/java/codeaction/InsertAnnotationMissingQuickFix.java @@ -172,7 +174,7 @@ private static String getLabel(String[] annotations) { StringBuilder list = new StringBuilder(); for (int i = 0; i < annotations.length; i++) { String annotation = annotations[i]; - String annotationName = annotation.substring(annotation.lastIndexOf('.') + 1, annotation.length()); + String annotationName = getSimpleName(annotation); if (i > 0) { list.append(", "); // assume comma list is ok: @A, @B, @C } 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 8b8dabea5..4483401e3 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 @@ -32,6 +32,8 @@ import java.util.logging.Logger; import java.util.logging.Level; + import static io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.JDTUtils.getSimpleName; + /** * QuickFix for removing parameter annotations */ @@ -80,9 +82,9 @@ public List getCodeActions(JavaCodeActionContext context, private String getLabel(PsiParameter parameter,List annotationsToRemove) { final StringBuilder sb = new StringBuilder(); // Java annotations in comma delimited list, assume that is ok. - sb.append("@").append(getShortName(annotationsToRemove.get(0))); + sb.append("@").append(getSimpleName(annotationsToRemove.get(0))); for (int j = 1; j < annotationsToRemove.size(); ++j) { - sb.append(", @").append(getShortName(annotationsToRemove.get(j))); + sb.append(", @").append(getSimpleName(annotationsToRemove.get(j))); } return Messages.getMessage("RemoveTheModifierFromParameter", sb.toString(), parameter.getName().toString()); } @@ -135,12 +137,4 @@ public CodeAction resolveCodeAction(JavaCodeActionResolveContext context) { protected PsiClass getBinding(PsiElement node) { return PsiTreeUtil.getParentOfType(node, PsiClass.class); } - - protected static String getShortName(String qualifiedName) { - final int i = qualifiedName.lastIndexOf('.'); - if (i != -1) { - return qualifiedName.substring(i+1); - } - return qualifiedName; - } } 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..aaf3cd773 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 @@ -40,6 +40,8 @@ import java.util.logging.Level; import java.util.logging.Logger; +import static io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.JDTUtils.getSimpleName; + /** * QuickFix for fixing {@link ServletConstants#DIAGNOSTIC_CODE_FILTER_MISSING_ATTRIBUTE} error * and {@link ServletConstants#DIAGNOSTIC_CODE_FILTER_MISSING_ATTRIBUTE} error @@ -168,7 +170,7 @@ private void insertAndReplaceAnnotation(Diagnostic diagnostic, JavaCodeActionCon } private static String getLabel(String annotation, String attribute, String labelType) { - String annotationName = annotation.substring(annotation.lastIndexOf('.') + 1, annotation.length()); + String annotationName = getSimpleName(annotation); annotationName = "@" + annotationName; if (labelType.equals("Remove")) { return Messages.getMessage("RemoveTheAttributeFrom", attribute, annotationName); 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..0cc1a6252 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 @@ -40,6 +40,8 @@ import java.util.logging.Level; import java.util.logging.Logger; +import static io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.JDTUtils.getSimpleName; + /** * QuickFix for fixing * {@link ServletConstants#DIAGNOSTIC_CODE_MISSING_ATTRIBUTE} error and @@ -163,7 +165,7 @@ private void insertAndReplaceAnnotation(Diagnostic diagnostic, JavaCodeActionCon } private static String getLabel(String annotation, String attribute, String labelType) { - String annotationName = annotation.substring(annotation.lastIndexOf('.') + 1, annotation.length()); + String annotationName = getSimpleName(annotation); annotationName = "@" + annotationName; if (labelType.equals("Remove")) { return Messages.getMessage("RemoveTheAttributeFrom", attribute, annotationName); 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..72dd155ca 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 @@ -40,6 +40,8 @@ import java.util.logging.Level; import java.util.logging.Logger; +import static io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.JDTUtils.getSimpleName; + /** * QuickFix for fixing HttpServlet extension error by providing the code actions * which implements IJavaCodeActionParticipant @@ -115,7 +117,7 @@ private PsiClass getBinding(PsiElement node) { } private static String getLabel(String fqAnnotation, PsiClass parentType) { - String annotationName = fqAnnotation.substring(fqAnnotation.lastIndexOf('.') + 1, fqAnnotation.length()); + String annotationName = getSimpleName(fqAnnotation); return Messages.getMessage("LetClassImplement", parentType.getName(), annotationName); } }