PsiModifierListOwner (@Nullable PsiElement element, LookupType type) { while (element != null) { if (type == LookupType.PREFER_DECLARATION || type == LookupType.DECLARATION_ONLY) { if (element instanceof PsiReferenceExpression) { final PsiElement e = ((PsiReferenceExpression)element).resolve(); if (e instanceof PsiModifierListOwner) { return (PsiModifierListOwner)e; } if (type == LookupType.DECLARATION_ONLY) { return null; } } } element = ContextComputationProcessor.getTopLevelInjectionTarget(element); final PsiElement parent = element.getParent(); if (element instanceof PsiAssignmentExpression && ((PsiAssignmentExpression)element).getOperationTokenType() == JavaTokenType.PLUSEQ) { element = ((PsiAssignmentExpression)element).getLExpression(); continue; } else if (parent instanceof PsiAssignmentExpression p) { if (p.getRExpression() == element) { element = p.getLExpression(); continue; } } else if (parent instanceof PsiReturnStatement) { final PsiMethod m = PsiTreeUtil.getParentOfType(parent, PsiMethod.class); if (m != null) { return m; } } else if (parent instanceof PsiModifierListOwner) { return (PsiModifierListOwner)parent; } else if (parent instanceof PsiArrayInitializerMemberValue value) { final PsiElement pair = value.getParent(); if (pair instanceof PsiNameValuePair) { return AnnotationUtil.getAnnotationMethod((PsiNameValuePair)pair); } } else if (parent instanceof PsiNameValuePair) { return AnnotationUtil.getAnnotationMethod((PsiNameValuePair)parent); } else { return PsiUtilEx.getParameterForArgument(element); } // If no annotation has been found through the usage context, check if the element // (i.e., the element the reference refers to) is annotated itself if (type != LookupType.DECLARATION_ONLY) { if (element instanceof PsiReferenceExpression) { final PsiElement e = ((PsiReferenceExpression)element).resolve(); if (e instanceof PsiModifierListOwner) { return (PsiModifierListOwner)e; } } } return null; } return null; }