From 8d9fff9becf94f65e20ed0ac9e9b8f3d85c112cf Mon Sep 17 00:00:00 2001 From: dessina-devasia <143582034+dessina-devasia@users.noreply.github.com> Date: Wed, 30 Oct 2024 12:23:26 +0530 Subject: [PATCH] [#1025]: Refactored the isMatchedJavaElement function in AbstractDiagnosticsCollector for performance optimisation --- .../lsp4ij/AbstractDiagnosticsCollector.java | 28 +++++++++++-------- 1 file changed, 17 insertions(+), 11 deletions(-) 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 638278b24..5d2a7fc7c 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 @@ -157,18 +157,24 @@ protected static boolean isMatchedAnnotation(PsiClass unit, PsiAnnotation annota * element name and false otherwise. */ protected static boolean isMatchedJavaElement(PsiClass type, String javaElementName, String javaElementFQName) { - if (nameEndsWith(javaElementFQName, javaElementName)) { - // For performance reason, we check if the import of annotation name is - // declared - if (isImportedJavaElement(type, javaElementFQName)) - return true; - // only check fully qualified java element - if (javaElementFQName.equals(javaElementName)) { - JavaPsiFacade facade = JavaPsiFacade.getInstance(type.getProject()); - Object o = facade.findClass(javaElementFQName, GlobalSearchScope.allScope(type.getProject())); - return (o != null); - } + // Quick check if the fully qualified name ends with the element's simple name + if (!nameEndsWith(javaElementFQName, javaElementName)) { + return false; // If not, return early for efficiency } + + // Check if the element is directly imported for performance + if (isImportedJavaElement(type, javaElementFQName)) { + return true; // Early return if the import is present + } + + // Confirm the element name matches the fully qualified name + if (javaElementFQName.equals(javaElementName)) { + // Use JavaPsiFacade to locate the class by its fully qualified name + JavaPsiFacade facade = JavaPsiFacade.getInstance(type.getProject()); + return facade.findClass(javaElementFQName, GlobalSearchScope.allScope(type.getProject())) != null; + } + + // Return false if no match is found return false; }