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

Resolved issues with matching qualified names in AbstractDiagnosticsCollector #1111

Merged
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
Expand Up @@ -126,24 +126,9 @@ public void collectDiagnostics(PsiJavaFile unit, List<Diagnostic> diagnostics) {
* @return true if the given annotation matches the given annotation name and
* false otherwise.
*/
protected static boolean isMatchedAnnotation(PsiClass unit, PsiAnnotation annotation, String annotationFQName) {
protected static boolean isMatchedAnnotation(PsiAnnotation annotation, String annotationFQName) {
String elementName = annotation.getQualifiedName();
if (nameEndsWith(annotationFQName, elementName) && unit != null) {
// For performance reason, we check if the import of annotation name is
// declared
if (isImportedJavaElement(unit, annotationFQName))
dessina-devasia marked this conversation as resolved.
Show resolved Hide resolved
return true;
// only check fully qualified annotations
if (annotationFQName.equals(elementName)) {
PsiReference ref = annotation.getReference();
PsiElement def = ref.resolve();
if (def instanceof PsiAnnotation) {
String fqName = ((PsiAnnotation)def).getQualifiedName();
return fqName.equals(annotationFQName);
}
}
}
return false;
return annotationFQName.equals(elementName);
}

/**
Expand All @@ -157,65 +142,10 @@ 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);
}
}
return false;
}

/**
* Returns true if the given Java class imports the given Java element and false
* otherwise.
* Must handle "import pkg.MyClass" and "import pkg.*"
*
* @param unit Java class.
* @param javaElementFQName given Java element fully qualified name.
* @return true if the Java class imports the given Java element and false
* otherwise.
*/
protected static boolean isImportedJavaElement(PsiClass unit, String javaElementFQName) {
return isImportedJavaElement(unit, new String[] { javaElementFQName });
}

protected static boolean isImportedJavaElement(PsiClass unit, String[] javaElementFQNames) {
PsiFile file = unit.getContainingFile();
if (file instanceof PsiJavaFile) {
PsiJavaFile jFile = (PsiJavaFile) file;
PsiClass[] importClasses = jFile.getSingleClassImports(true);
for (PsiClass c : importClasses) {
for (String name : javaElementFQNames) {
if (name.equals(c.getQualifiedName())) {
return true;
}
}
}
PsiElement[] importOnDemand = jFile.getOnDemandImports(false, true);
for (PsiElement e : importOnDemand) {
// should be class or package
if (e instanceof PsiClass) {
for (String name : javaElementFQNames) {
if (name.equals(((PsiClass) e).getQualifiedName())) {
return true;
}
}
}
if (e instanceof PsiPackage) {
for (String name : javaElementFQNames) {
if (name.startsWith(((PsiPackage) e).getQualifiedName())) {
return true;
}
}
}
}
if (javaElementFQName.equals(javaElementName)) {
JavaPsiFacade facade = JavaPsiFacade.getInstance(type.getProject());
Object o = facade.findClass(javaElementFQName, GlobalSearchScope.allScope(type.getProject()));
return (o != null);
}
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,8 @@ public void collectDiagnostics(PsiJavaFile unit, List<Diagnostic> diagnostics) {
for (Tuple.Two<PsiAnnotation, PsiElement> annotatable : annotatables) {
PsiAnnotation annotation = annotatable.getFirst();
PsiElement element = annotatable.getSecond();
PsiClass topLevel = PsiUtil.getTopLevelClass(element);

if (isMatchedAnnotation(topLevel, annotation, AnnotationConstants.GENERATED_FQ_NAME)) {
if (isMatchedAnnotation(annotation, AnnotationConstants.GENERATED_FQ_NAME)) {
for (PsiNameValuePair pair : annotation.getParameterList().getAttributes()) {
// If date element exists and is non-empty, it must follow ISO 8601 format.
if (pair.getAttributeName().equals("date")) {
Expand All @@ -115,7 +114,7 @@ public void collectDiagnostics(PsiJavaFile unit, List<Diagnostic> diagnostics) {
}
}
}
} else if (isMatchedAnnotation(topLevel, annotation, AnnotationConstants.RESOURCE_FQ_NAME)) {
} else if (isMatchedAnnotation(annotation, AnnotationConstants.RESOURCE_FQ_NAME)) {
if (element instanceof PsiClass) {
PsiClass type = (PsiClass) element;
Boolean nameEmpty = true;
Expand Down Expand Up @@ -146,7 +145,7 @@ public void collectDiagnostics(PsiJavaFile unit, List<Diagnostic> diagnostics) {
}
}
}
if (isMatchedAnnotation(topLevel, annotation, AnnotationConstants.POST_CONSTRUCT_FQ_NAME)) {
if (isMatchedAnnotation(annotation, AnnotationConstants.POST_CONSTRUCT_FQ_NAME)) {
if (element instanceof PsiMethod) {
PsiMethod method = (PsiMethod) element;
if (method.getParameters().length != 0) {
Expand All @@ -173,7 +172,7 @@ public void collectDiagnostics(PsiJavaFile unit, List<Diagnostic> diagnostics) {
DiagnosticSeverity.Warning));
}
}
} else if (isMatchedAnnotation(topLevel, annotation, AnnotationConstants.PRE_DESTROY_FQ_NAME)) {
} else if (isMatchedAnnotation(annotation, AnnotationConstants.PRE_DESTROY_FQ_NAME)) {
if (element instanceof PsiMethod) {
PsiMethod method = (PsiMethod) element;
if (method.getParameters().length != 0) {
Expand Down
Loading