LengthTruncateJoiningTerminal (@NotNull TerminalBlock terminalBlock, @Nullable List<PsiVariable> nonFinalVariables) { if (nonFinalVariables != null && !nonFinalVariables.isEmpty()) return null; List<PsiStatement> statements = Arrays.asList(terminalBlock.getStatements()); if (statements.size() < 1) return null; PsiVariable targetBuilder = extractStringBuilder(statements.get(0)); if(!(targetBuilder instanceof PsiLocalVariable)) return null; List<PsiExpression> joinParts = extractJoinParts(statements); if (joinParts == null) return null; JoinData joinData = JoinData.extractRightDelimiter(joinParts); List<PsiExpression> mainJoinParts = joinData.getMainJoinParts(); List<PsiExpression> delimiterJoinParts = joinData.getDelimiterJoinParts(); PsiStatement loop = terminalBlock.getStreamSourceStatement(); PsiIfStatement ifStatement = tryCast(PsiTreeUtil.skipWhitespacesAndCommentsForward(loop), PsiIfStatement.class); if(ifStatement == null) return null; PsiExpression condition = ifStatement.getCondition(); if (condition == null) return null; Integer conditionPrefixLength = extractConditionPrefixLength(condition, targetBuilder); if (conditionPrefixLength == null) return null; PsiMethodCallExpression truncateCall = extractTruncateCall(targetBuilder, ifStatement); if (truncateCall == null) return null; Integer truncateSize = tryExtractTruncationSize(targetBuilder, truncateCall); String delimiter = joinData.getDelimiter(); if(delimiter == null) return null; if (truncateSize == null || truncateSize != delimiter.length()) return null; PrefixSuffixContext context = PrefixSuffixContext .extractAndVerifyRefs(ifStatement, loop, targetBuilder, terminalBlock, emptyList(), new HashSet<>(singletonList(truncateCall))); if (context == null) return null; String prefix = computeConstant(context.getPrefixJoinParts()); if (prefix == null || prefix.length() != conditionPrefixLength) return null; PsiVariable loopVariable = terminalBlock.getVariable(); return new LengthTruncateJoiningTerminal(targetBuilder, loopVariable, mainJoinParts, context, delimiterJoinParts, ifStatement, terminalBlock); }