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

Refactor methods in AbstractDiagnosticsCollector #1064

Closed
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 @@ -127,14 +127,19 @@ public void collectDiagnostics(PsiJavaFile unit, List<Diagnostic> 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) {
Expand All @@ -143,7 +148,7 @@ protected static boolean isMatchedAnnotation(PsiClass unit, PsiAnnotation annota
}
}
}
return false;
return false; // Return false if no match is found
}

/**
Expand All @@ -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;
}

Expand Down
Loading