PsiElementVisitor (@NotNull ProblemsHolder holder, boolean isOnTheFly) { if (!PsiUtil.isLanguageLevel8OrHigher(holder.getFile())) { return PsiElementVisitor.EMPTY_VISITOR; } return new JavaElementVisitor() { @Override public void visitMethodCallExpression(@NotNull PsiMethodCallExpression methodCall) { PsiElement nameElement = methodCall.getMethodExpression().getReferenceNameElement(); if (nameElement == null) return; CALL_TO_FIX_MAPPER.mapAll(methodCall) .forEach( simplification -> holder.registerProblem(nameElement, simplification.getMessage(), new SimplifyCallChainFix(simplification))); if (STREAM_COLLECT.test(methodCall)) { handleStreamCollect(methodCall); } else if (OPTIONAL_IS_PRESENT.test(methodCall)) { handleOptionalIsPresent(methodCall); } } private void handleOptionalIsPresent(PsiMethodCallExpression methodCall) { PsiMethodCallExpression optionalQualifier = getQualifierMethodCall(methodCall); if (!STREAM_FIND.test(optionalQualifier)) return; PsiMethodCallExpression streamQualifier = getQualifierMethodCall(optionalQualifier); if (!STREAM_FILTER.test(streamQualifier)) return; ReplaceOptionalIsPresentChainFix fix = new ReplaceOptionalIsPresentChainFix(optionalQualifier.getMethodExpression().getReferenceName()); holder.registerProblem(methodCall, getCallChainRange(methodCall, streamQualifier), fix.getMessage(), new SimplifyCallChainFix(fix)); } private void handleStreamCollect(PsiMethodCallExpression methodCall) { PsiElement parameter = methodCall.getArgumentList().getExpressions()[0]; if(parameter instanceof PsiMethodCallExpression collectorCall) { ReplaceCollectorFix fix = ReplaceCollectorFix.COLLECTOR_TO_FIX_MAPPER.mapFirst(collectorCall); if (fix != null) { TextRange range = methodCall.getTextRange(); PsiElement nameElement = methodCall.getMethodExpression().getReferenceNameElement(); if (nameElement != null) { range = new TextRange(nameElement.getTextOffset(), range.getEndOffset()); } holder.registerProblem(methodCall, range.shiftRight(-methodCall.getTextOffset()), fix.getMessage(), new SimplifyCallChainFix(fix)); } else { if(!(PsiUtil.resolveClassInClassTypeOnly(methodCall.getType()) instanceof PsiTypeParameter)) { String replacement = SimplifyCollectionCreationFix.COLLECTOR_TO_CLASS_MAPPER.mapFirst(collectorCall); if (replacement != null) { PsiMethodCallExpression qualifier = getQualifierMethodCall(methodCall); if (COLLECTION_STREAM.test(qualifier)) { PsiElement startElement = qualifier.getMethodExpression().getReferenceNameElement(); if (startElement != null) { holder.registerProblem(methodCall, new TextRange(startElement.getTextOffset() - methodCall.getTextOffset(), methodCall.getTextLength()), JavaBundle.message("inspection.message.can.be.replaced.with.0.constructor", replacement), new SimplifyCallChainFix(new SimplifyCollectionCreationFix(replacement))); } } } } } } } }; }