public Void visitMethodDeclaration()

in paimon-common/src/main/java/org/apache/paimon/codegen/codesplit/BlockStatementRewriter.java [152:249]


        public Void visitMethodDeclaration(JavaParser.MethodDeclarationContext ctx) {

            if (!"void".equals(ctx.typeTypeOrVoid().getText())) {
                return null;
            }

            // function real parameters
            LinkedHashSet<String> declarationContext = new LinkedHashSet<>();
            new JavaParserBaseVisitor<Void>() {
                @Override
                public Void visitFormalParameter(JavaParser.FormalParameterContext ctx) {
                    declarationContext.add(ctx.variableDeclaratorId().getText());
                    return null;
                }
            }.visit(ctx);

            String type = CodeSplitUtil.getContextString(ctx.typeTypeOrVoid());
            String functionName = ctx.IDENTIFIER().getText();
            String parameters = CodeSplitUtil.getContextString(ctx.formalParameters());

            String methodQualifier = "";
            if (ctx.THROWS() != null) {
                methodQualifier =
                        " throws " + CodeSplitUtil.getContextString(ctx.qualifiedNameList());
            }

            int counter = 0;
            for (JavaParser.BlockStatementContext blockStatementContext :
                    ctx.methodBody().block().blockStatement()) {

                StatementContext statement = blockStatementContext.statement();
                if (statement != null
                        && statement.getText().length() > maxMethodLength
                        && (statement.IF() != null
                                || statement.ELSE() != null
                                || statement.WHILE() != null)) {

                    BlockStatementSplitter splitter =
                            new BlockStatementSplitter(
                                    CodeSplitUtil.getContextString(statement),
                                    String.join(", ", declarationContext));

                    // create rewrite context for every block that will be rewritten. This is for
                    // case
                    // when we can have many IF/ELSE/WHILE blocks in single method and
                    String context = String.format(functionName + "_%d", counter++);
                    // Rewrite function's body to include calls to extracted methods.
                    String blockRewrittenBody = splitter.rewriteBlock(context);

                    // Get extract methods from block's original body.
                    Map<String, List<String>> newMethods = splitter.extractBlocks();

                    // Try to group statements in function's new body
                    StringBuilder functionGroupedBody = new StringBuilder();
                    BlockStatementGrouper statementGrouper =
                            new BlockStatementGrouper(
                                    blockRewrittenBody,
                                    maxMethodLength,
                                    String.join(", ", declarationContext));

                    RewriteGroupedCode groupedCode = statementGrouper.rewrite(context);
                    // add new methods, representing extracted groups.
                    newMethods.putAll(groupedCode.getGroups());

                    functionGroupedBody.append("\n").append(groupedCode.getRewriteCode());

                    // replace original functions body with new, rewritten and grouped body.
                    rewriter.replace(statement.start, statement.stop, functionGroupedBody);

                    // build new method definitions.
                    StringJoiner newMethodDefinitions =
                            new StringJoiner(System.lineSeparator() + System.lineSeparator());
                    for (Entry<String, List<String>> entry : newMethods.entrySet()) {
                        StringBuilder sb =
                                new StringBuilder(type)
                                        .append(" ")
                                        .append(entry.getKey())
                                        .append(parameters)
                                        .append(methodQualifier)
                                        .append(" {")
                                        .append(System.lineSeparator());

                        StringJoiner bodyLines = new StringJoiner(System.lineSeparator());
                        for (String bodyLine : entry.getValue()) {
                            bodyLines.add(bodyLine);
                        }

                        sb.append(bodyLines).append(System.lineSeparator()).append("}");
                        newMethodDefinitions.add(sb.toString());
                    }

                    // add new method definitions to class
                    rewriter.insertAfter(
                            ctx.getParent().stop, "\n\n" + newMethodDefinitions + "\n");
                }
            }
            return null;
        }