List<PsiClass> (PsiElement element) { PsiClass psiClass = null; PsiExpression qualifier = null; if (element instanceof PsiNameValuePair) { final PsiAnnotation annotation = PsiTreeUtil.getParentOfType(element, PsiAnnotation.class); if (annotation != null) { PsiJavaCodeReferenceElement nameRef = annotation.getNameReferenceElement(); if (nameRef == null) { return Collections.emptyList(); } else { final PsiElement resolve = nameRef.resolve(); if (resolve instanceof PsiClass) { return Collections.singletonList((PsiClass)resolve); } else { return Collections.emptyList(); } } } } if (element instanceof PsiNewExpression newExpression) { PsiJavaCodeReferenceElement ref = newExpression.getClassOrAnonymousClassReference(); if (ref != null) { PsiElement refElement = ref.resolve(); if (refElement instanceof PsiClass cls) { psiClass = cls; } else if (ref.getQualifier() instanceof PsiJavaCodeReferenceElement refQualifier) { refElement = refQualifier.resolve(); if (refElement instanceof PsiClass cls) { psiClass = cls; } } } qualifier = newExpression.getQualifier(); } else if (element instanceof PsiReferenceExpression) { qualifier = ((PsiReferenceExpression)element).getQualifierExpression(); if (qualifier == null && element instanceof PsiMethodReferenceExpression) { final PsiTypeElement qualifierTypeElement = ((PsiMethodReferenceExpression)element).getQualifierType(); if (qualifierTypeElement != null) { psiClass = PsiUtil.resolveClassInType(qualifierTypeElement.getType()); } } else if (qualifier == null) { final PsiElement parent = element.getParent(); if (parent instanceof PsiSwitchLabelStatement) { final PsiSwitchStatement switchStatement = PsiTreeUtil.getParentOfType(parent, PsiSwitchStatement.class); if (switchStatement != null) { final PsiExpression expression = switchStatement.getExpression(); if (expression != null) { psiClass = PsiUtil.resolveClassInClassTypeOnly(expression.getType()); } } } } } else if (element instanceof PsiMethodCallExpression) { final PsiReferenceExpression methodExpression = ((PsiMethodCallExpression)element).getMethodExpression(); qualifier = methodExpression.getQualifierExpression(); @NonNls final String referenceName = methodExpression.getReferenceName(); if (referenceName == null) return Collections.emptyList(); } boolean allowOuterClasses = false; if (qualifier != null) { PsiType type = qualifier.getType(); if (type instanceof PsiClassType) { psiClass = ((PsiClassType)type).resolve(); } if (qualifier instanceof PsiJavaCodeReferenceElement) { final PsiElement resolved = ((PsiJavaCodeReferenceElement)qualifier).resolve(); if (resolved instanceof PsiClass) { if (psiClass == null) psiClass = (PsiClass)resolved; } } } else if (psiClass == null) { psiClass = PsiTreeUtil.getParentOfType(element, PsiClass.class); allowOuterClasses = true; } if (psiClass instanceof PsiTypeParameter) { PsiClass[] supers = psiClass.getSupers(); List<PsiClass> filtered = new ArrayList<>(); for (PsiClass aSuper : supers) { if (!canModify(aSuper)) continue; if (!(aSuper instanceof PsiTypeParameter)) filtered.add(aSuper); } return filtered; } else { if (psiClass == null || !canModify(psiClass)) { return Collections.emptyList(); } if (!allowOuterClasses || !isAllowOuterTargetClass()) { final ArrayList<PsiClass> classes = new ArrayList<>(); collectSupers(psiClass, classes); return classes; } List<PsiClass> result = new ArrayList<>(); while (psiClass != null) { result.add(psiClass); if (psiClass.hasModifierProperty(PsiModifier.STATIC)) break; psiClass = PsiTreeUtil.getParentOfType(psiClass, PsiClass.class); } return result; } }