boolean (PsiLambdaExpression lambda) { PsiElement body = lambda.getBody(); if (body == null) return false; class CapturingLambdaVisitor extends JavaRecursiveElementWalkingVisitor { boolean capturing; @Override public void visitReferenceExpression(@NotNull PsiReferenceExpression expression) { super.visitReferenceExpression(expression); if (expression instanceof PsiMethodReferenceExpression) return; if (expression.getParent() instanceof PsiMethodCallExpression && expression.getQualifierExpression() != null) return; PsiElement target = expression.resolve(); // variable/parameter/field/method/class are always PsiModifierListOwners if (target instanceof PsiModifierListOwner && !((PsiModifierListOwner)target).hasModifierProperty(PsiModifier.STATIC) && !PsiTreeUtil.isAncestor(lambda, target, true)) { if (target instanceof PsiClass && ((PsiClass)target).getContainingClass() == null) return; capturing = true; stopWalking(); } } @Override public void visitSuperExpression(@NotNull PsiSuperExpression expression) { capturing = true; stopWalking(); } @Override public void visitThisExpression(@NotNull PsiThisExpression expression) { capturing = true; stopWalking(); } } CapturingLambdaVisitor visitor = new CapturingLambdaVisitor(); body.accept(visitor); return visitor.capturing; }