From 359c525798e93750407e97880a12ea2757d3bff8 Mon Sep 17 00:00:00 2001 From: anna Date: Mon, 7 Dec 2009 12:18:55 +0300 Subject: [PATCH] check for annotations in hierarchy (IDEADEV-41871) --- .../com/intellij/codeInsight/AnnotationUtil.java | 71 +++++++++++++++------- 1 file changed, 50 insertions(+), 21 deletions(-) diff --git a/java/openapi/src/com/intellij/codeInsight/AnnotationUtil.java b/java/openapi/src/com/intellij/codeInsight/AnnotationUtil.java index 4038ba353f..90a565f152 100644 --- a/java/openapi/src/com/intellij/codeInsight/AnnotationUtil.java +++ b/java/openapi/src/com/intellij/codeInsight/AnnotationUtil.java @@ -149,28 +149,47 @@ public class AnnotationUtil { public static PsiAnnotation findAnnotationInHierarchy(PsiModifierListOwner listOwner, Set annotationNames) { PsiAnnotation directAnnotation = findAnnotation(listOwner, annotationNames); if (directAnnotation != null) return directAnnotation; + if (listOwner instanceof PsiMethod) { + PsiMethod method = (PsiMethod)listOwner; + PsiClass aClass = method.getContainingClass(); + if (aClass == null) return null; + HierarchicalMethodSignature methodSignature = method.getHierarchicalMethodSignature(); + return findAnnotationInHierarchy(methodSignature, annotationNames, method, null); + } else if (listOwner instanceof PsiClass) { + return findAnnotationInHierarchy(((PsiClass)listOwner), annotationNames, null); + } + return null; + } - if (!(listOwner instanceof PsiMethod)) { - return null; + @Nullable + private static PsiAnnotation findAnnotationInHierarchy(final @NotNull PsiClass psiClass, final Set annotationNames, Set processed) { + final PsiClass[] superClasses = psiClass.getSupers(); + for (final PsiClass superClass : superClasses) { + if (processed == null) processed = new THashSet(); + if (!processed.add(superClass)) return null; + final PsiAnnotation annotation = findAnnotation(superClass, annotationNames); + if (annotation != null) return annotation; + final PsiAnnotation annotationInHierarchy = findAnnotationInHierarchy(superClass, annotationNames, processed); + if (annotationInHierarchy != null) return annotationInHierarchy; } - PsiMethod method = (PsiMethod)listOwner; - PsiClass aClass = method.getContainingClass(); - if (aClass == null) return null; - HierarchicalMethodSignature methodSignature = method.getHierarchicalMethodSignature(); - return findAnnotationInHierarchy(methodSignature, annotationNames, method); + return null; } + @Nullable private static PsiAnnotation findAnnotationInHierarchy(HierarchicalMethodSignature signature, Set annotationNames, - PsiElement place) { - List superSignatures = signature.getSuperSignatures(); - PsiResolveHelper resolveHelper = JavaPsiFacade.getInstance(place.getProject()).getResolveHelper(); - for (HierarchicalMethodSignature superSignature : superSignatures) { - PsiMethod superMethod = superSignature.getMethod(); + PsiElement place, + Set processed) { + final List superSignatures = signature.getSuperSignatures(); + final PsiResolveHelper resolveHelper = JavaPsiFacade.getInstance(place.getProject()).getResolveHelper(); + for (final HierarchicalMethodSignature superSignature : superSignatures) { + final PsiMethod superMethod = superSignature.getMethod(); + if (processed == null) processed = new THashSet(); + if (!processed.add(superMethod)) continue; if (!resolveHelper.isAccessible(superMethod, place, null)) continue; PsiAnnotation direct = findAnnotation(superMethod, annotationNames); if (direct != null) return direct; - PsiAnnotation superResult = findAnnotationInHierarchy(superSignature, annotationNames, place); + PsiAnnotation superResult = findAnnotationInHierarchy(superSignature, annotationNames, place, processed); if (superResult != null) return superResult; } @@ -195,7 +214,7 @@ public class AnnotationUtil { private static boolean isAnnotated(@NotNull PsiModifierListOwner listOwner, @NonNls String annotationFQN, - boolean checkHierarchy, final boolean skipExternal, Set processed) { + boolean checkHierarchy, final boolean skipExternal, Set processed) { if (!listOwner.isValid()) return false; final PsiModifierList modifierList = listOwner.getModifierList(); if (modifierList == null) return false; @@ -204,13 +223,23 @@ public class AnnotationUtil { if (!skipExternal && ExternalAnnotationsManager.getInstance(listOwner.getProject()).findExternalAnnotation(listOwner, annotationFQN) != null) { return true; } - if (checkHierarchy && listOwner instanceof PsiMethod) { - PsiMethod method = (PsiMethod)listOwner; - if (processed == null) processed = new THashSet(); - if (!processed.add(method)) return false; - final PsiMethod[] superMethods = method.findSuperMethods(); - for (PsiMethod superMethod : superMethods) { - if (isAnnotated(superMethod, annotationFQN, checkHierarchy, skipExternal, processed)) return true; + if (checkHierarchy) { + if (listOwner instanceof PsiMethod) { + PsiMethod method = (PsiMethod)listOwner; + if (processed == null) processed = new THashSet(); + if (!processed.add(method)) return false; + final PsiMethod[] superMethods = method.findSuperMethods(); + for (PsiMethod superMethod : superMethods) { + if (isAnnotated(superMethod, annotationFQN, checkHierarchy, skipExternal, processed)) return true; + } + } else if (listOwner instanceof PsiClass) { + final PsiClass clazz = (PsiClass)listOwner; + if (processed == null) processed = new THashSet(); + if (!processed.add(clazz)) return false; + final PsiClass[] superClasses = clazz.getSupers(); + for (PsiClass superClass : superClasses) { + if (isAnnotated(superClass, annotationFQN, checkHierarchy, skipExternal, processed)) return true; + } } } return false; -- 2.11.4.GIT