PsiElementVisitor (@NotNull ProblemsHolder holder, boolean isOnTheFly) { PsiFile file = holder.getFile(); if (!PsiUtil.isLanguageLevel9OrHigher(file)) { return PsiElementVisitor.EMPTY_VISITOR; } return new JavaElementVisitor() { @Override public void visitIfStatement(@NotNull PsiIfStatement ifStatement) { NotNullContext context = NotNullContext.from(ifStatement); if(context == null) return; String method = getMethodWithClass(context.myExpressionToReplace, context.myIsStream); PsiStatement nextToDelete = context.myNextToDelete; int maybeImplicitElseLength = nextToDelete != null ? nextToDelete.getTextLength() : 0; boolean isInfoLevel = noWarningReplacementBigger && ifStatement.getTextLength() + maybeImplicitElseLength - context.getLenAfterReplace() < MINIMAL_WARN_DELTA_SIZE; ProblemHighlightType highlight = getHighlight(context, isInfoLevel); if (!isOnTheFly && highlight == ProblemHighlightType.INFORMATION) return; holder.registerProblem(ifStatement.getFirstChild(), JavaBundle.message("inspection.require.non.null.message", 1, method), highlight, new ReplaceWithRequireNonNullFix(method, false)); } @NotNull private static ProblemHighlightType getHighlight(NotNullContext context, boolean isInfoLevel) { if(context.myIsStream) { return ProblemHighlightType.GENERIC_ERROR_OR_WARNING; } if (isInfoLevel) { return ProblemHighlightType.INFORMATION; } return ProblemHighlightType.GENERIC_ERROR_OR_WARNING; } @Override public void visitConditionalExpression(@NotNull PsiConditionalExpression ternary) { TernaryNotNullContext context = TernaryNotNullContext.from(ternary); if(context == null) return; String method = getMethodWithClass(context.myNullExpr, false); String name = context.myReferenceExpression.getText(); boolean replacementShorter = name != null && context.myNullExpr.getTextLength() + method.length() + name.length() < context.myTernary.getTextLength() + MINIMAL_WARN_DELTA_SIZE; boolean isInfoLevel = noWarningReplacementBigger && replacementShorter; ProblemHighlightType highlightType = isInfoLevel ? ProblemHighlightType.INFORMATION : ProblemHighlightType.GENERIC_ERROR_OR_WARNING; if (!isOnTheFly && highlightType == ProblemHighlightType.INFORMATION) return; holder.registerProblem(ternary, JavaBundle.message("inspection.require.non.null.message", 2, method), highlightType, new ReplaceWithRequireNonNullFix(method, true)); } }; }