PsiElement (@NotNull PsiElement element) { if (PsiTreeUtil.getParentOfType(element, PsiParameterList.class) != null) { return PsiTreeUtil.getParentOfType(element, PsiMethod.class); } PsiRecordHeader header = PsiTreeUtil.getParentOfType(element, PsiRecordHeader.class); if (header != null) { PsiClass aClass = header.getContainingClass(); if (aClass != null) { return JavaPsiRecordUtil.findCanonicalConstructor(aClass); } return null; } final PsiTypeParameterList typeParameterList = PsiTreeUtil.getParentOfType(element, PsiTypeParameterList.class); if (typeParameterList != null) { return PsiTreeUtil.getParentOfType(typeParameterList, PsiMember.class); } final PsiElement elementParent = element.getParent(); if (elementParent instanceof PsiMethod && ((PsiMethod)elementParent).getNameIdentifier()==element) { final PsiClass containingClass = ((PsiMethod)elementParent).getContainingClass(); if (containingClass != null && containingClass.isAnnotationType()) { return null; } return elementParent; } if (elementParent instanceof PsiClass psiClass && psiClass.getNameIdentifier() == element) { if (psiClass.isAnnotationType() || psiClass.isEnum()) { return null; } if (psiClass.isRecord()) { return JavaPsiRecordUtil.findCanonicalConstructor(psiClass); } return elementParent; } final PsiCallExpression expression = PsiTreeUtil.getParentOfType(element, PsiCallExpression.class); if (expression != null) { final PsiExpression qualifierExpression; if (expression instanceof PsiMethodCallExpression) { qualifierExpression = ((PsiMethodCallExpression)expression).getMethodExpression().getQualifierExpression(); } else if (expression instanceof PsiNewExpression) { qualifierExpression = ((PsiNewExpression)expression).getQualifier(); } else { qualifierExpression = null; } if (PsiTreeUtil.isAncestor(qualifierExpression, element, false)) { final PsiExpressionList expressionList = PsiTreeUtil.getParentOfType(qualifierExpression, PsiExpressionList.class); if (expressionList != null) { final PsiElement parent = expressionList.getParent(); if (parent instanceof PsiCallExpression) { return ((PsiCallExpression)parent).resolveMethod(); } } } else { return expression.resolveMethod(); } } final PsiReferenceParameterList referenceParameterList = PsiTreeUtil.getParentOfType(element, PsiReferenceParameterList.class); if (referenceParameterList != null) { final PsiJavaCodeReferenceElement referenceElement = PsiTreeUtil.getParentOfType(referenceParameterList, PsiJavaCodeReferenceElement.class); if (referenceElement != null) { final PsiElement resolved = referenceElement.resolve(); if (resolved instanceof PsiClass) { return resolved; } else if (resolved instanceof PsiMethod) { return resolved; } } } return null; }