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..b6febf4cd 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 @@ -127,14 +127,19 @@ public void collectDiagnostics(PsiJavaFile unit, List diagnostics) { * false otherwise. */ protected static boolean isMatchedAnnotation(PsiClass unit, PsiAnnotation annotation, String annotationFQName) { + // Get the qualified name of the annotation element String elementName = annotation.getQualifiedName(); + + // Preliminary check to ensure elementName ends with the expected suffix if (nameEndsWith(annotationFQName, elementName) && unit != null) { - // For performance reason, we check if the import of annotation name is - // declared + + // Check if the annotation is directly imported in the file for performance if (isImportedJavaElement(unit, annotationFQName)) return true; - // only check fully qualified annotations + + // Check if the fully qualified names match when import is not available if (annotationFQName.equals(elementName)) { + // Resolve the annotation reference to check its fully qualified name PsiReference ref = annotation.getReference(); PsiElement def = ref.resolve(); if (def instanceof PsiAnnotation) { @@ -143,7 +148,7 @@ protected static boolean isMatchedAnnotation(PsiClass unit, PsiAnnotation annota } } } - return false; + return false; // Return false if no match is found } /** @@ -157,18 +162,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; }