PsiElement (@NotNull PsiExpression runnableExpr, @NotNull PsiClass trackedClass) { if (runnableExpr instanceof PsiMethodReferenceExpression methodReference) { if (PsiMethodReferenceUtil.isStaticallyReferenced(methodReference)) return null; PsiElement qualifier = methodReference.getQualifier(); if (qualifier instanceof PsiThisExpression) { PsiClass thisClass = PsiUtil.resolveClassInType(((PsiThisExpression)qualifier).getType()); if (thisClass != trackedClass) return null; return qualifier; } return null; } if (runnableExpr instanceof PsiLambdaExpression lambda) { if (!lambda.getParameterList().isEmpty()) return null; PsiElement lambdaBody = lambda.getBody(); if (lambdaBody == null) return null; return getLambdaOrInnerClassElementCapturingThis(lambdaBody, trackedClass); } if (runnableExpr instanceof PsiNewExpression newExpression) { if (newExpression.getAnonymousClass() != null) { if (PsiUtil.isLanguageLevel18OrHigher(trackedClass)) { PsiElement elementCapturingThis = getLambdaOrInnerClassElementCapturingThis(newExpression, trackedClass); if (elementCapturingThis != null) { return elementCapturingThis; } } else { return newExpression; } } PsiJavaCodeReferenceElement classReference = newExpression.getClassReference(); if (classReference == null) return null; PsiClass aClass = tryCast(classReference.resolve(), PsiClass.class); if (aClass == null) return null; if (aClass.getContainingClass() != trackedClass) return null; if (aClass.hasModifierProperty(PsiModifier.STATIC)) return null; if (PsiUtil.isLanguageLevel18OrHigher(trackedClass) && getLambdaOrInnerClassElementCapturingThis(newExpression, trackedClass) == null) return null; return classReference; } return null; }