PsiElementVisitor (@NotNull final ProblemsHolder holder, boolean isOnTheFly) { if (!JavaFeature.THREAD_LOCAL_WITH_INITIAL.isFeatureSupported(holder.getFile())) { return PsiElementVisitor.EMPTY_VISITOR; } return new JavaElementVisitor() { @Override public void visitAnonymousClass(final @NotNull PsiAnonymousClass aClass) { super.visitAnonymousClass(aClass); PsiExpressionList argumentList = aClass.getArgumentList(); if (AnonymousCanBeLambdaInspection.isLambdaForm(aClass, Collections.emptySet()) && argumentList != null && argumentList.isEmpty()) { PsiMethod method = aClass.getMethods()[0]; PsiClassType type = aClass.getBaseClassType(); AnonymousLambdaAlternative alternative = getAlternative(type.resolve(), method); if(alternative != null) { final PsiElement lBrace = aClass.getLBrace(); LOG.assertTrue(lBrace != null); final TextRange rangeInElement = new TextRange(0, lBrace.getStartOffsetInParent() + aClass.getStartOffsetInParent() - 1); holder.registerProblem(aClass.getParent(), rangeInElement, JavaAnalysisBundle.message("anonymous.ref.loc.can.be.replaced.with.0", alternative.myReplacementMessage), new ReplaceWithLambdaAlternativeFix(alternative)); } } } @Contract("null, _ -> null") private static AnonymousLambdaAlternative getAlternative(PsiClass type, PsiMethod method) { if(type == null) return null; for(AnonymousLambdaAlternative alternative : ALTERNATIVES) { if(alternative.myClassName.equals(type.getQualifiedName()) && alternative.myMethodName.equals(method.getName())) { return alternative; } } return null; } }; }