/** * Split a block into sub methods. * * @param block Block or function to split. * @return new weight for the resulting block. */ private Block splitBlock(final Block block, final FunctionNode function) { final List<Statement> splits = new ArrayList<>(); List<Statement> statements = new ArrayList<>(); long statementsWeight = 0; for (final Statement statement : block.getStatements()) { final long weight = WeighNodes.weigh(statement, weightCache); if (statementsWeight + weight >= SPLIT_THRESHOLD || statement.isTerminal()) { if (!statements.isEmpty()) { splits.add(createBlockSplitNode(block, function, statements, statementsWeight)); statements = new ArrayList<>(); statementsWeight = 0; } } if (statement.isTerminal()) { splits.add(statement); } else { statements.add(statement); statementsWeight += weight; } } if (!statements.isEmpty()) { splits.add(createBlockSplitNode(block, function, statements, statementsWeight)); } return block.setStatements(lc, splits); }