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

LSP4Jakarta: Reduce code duplication. Use getSimpleName() utility method for computing short names. #1141

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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,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
Expand Down Expand Up @@ -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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -82,7 +83,7 @@ public CodeAction resolveCodeAction(JavaCodeActionResolveContext context) {
private void removeConstraintAnnotationsCodeActions(Diagnostic diagnostic, JavaCodeActionContext context, List<CodeAction> 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<String, Object> extendedData = new HashMap<>();
extendedData.put(ANNOTATION_NAME, annotationName);
codeActions.add(JDTUtils.createCodeAction(context, diagnostic, name, getParticipantId(), extendedData));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -80,9 +82,9 @@ public List<? extends CodeAction> getCodeActions(JavaCodeActionContext context,
private String getLabel(PsiParameter parameter,List<String> 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());
}
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}