![JAR search and dependency download from the Maven repository](/logo.png)
org.dinky.shaded.paimon.codegen.codesplit.FunctionSplitter Maven / Gradle / Ivy
The newest version!
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to you under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.dinky.shaded.paimon.codegen.codesplit;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.TokenStreamRewriter;
import org.antlr.v4.runtime.atn.PredictionMode;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
/**
* Split long functions into several smaller functions.
*
* This rewriter only deals with functions without return values. Functions with return values
* should have been converted by {@link org.dinky.shaded.paimon.codegen.codesplit.ReturnValueRewriter}.
* For functions with
* return
statements, this rewriter will add a check for early returns with the help of
* {@link org.dinky.shaded.paimon.codegen.codesplit.AddBoolBeforeReturnRewriter}.
*
*
Before
*
*
* public class Example {
* public void myFun(int a, int b) {
* a += b;
* b += a;
* if (a > 0) {
* return;
* }
* a *= 2;
* b *= 2;
* System.out.println(a);
* System.out.println(b);
* }
* }
*
*
* After
*
*
* public class Example {
* boolean myFunHasReturned$0;
*
* public void myFun(int a, int b) {
* myFunHasReturned$0 = false;
* myFun_split1(a, b);
*
* myFun_split2(a, b);
* if (myFunHasReturned$0) {
* return;
* }
*
* myFun_split3(a, b);
* }
*
* void myFun_split1(int a, int b) {
* a += b;
* b += a;
* }
*
* void myFun_split2(int a, int b) {
* if (a > 0) {
* {
* myFunHasReturned$0 = true;
* return;
* }
* }
* }
*
* void myFun_split3(int a, int b) {
* a *= 2;
* b *= 2;
* System.out.println(a);
* System.out.println(b);
* }
* }
*
*/
public class FunctionSplitter implements CodeRewriter {
private String code;
private final int maxMethodLength;
public FunctionSplitter(String code, int maxMethodLength) {
this.code = code;
this.maxMethodLength = maxMethodLength;
}
public String rewrite() {
AddBoolBeforeReturnRewriter boolRewriter =
new AddBoolBeforeReturnRewriter(this.code, maxMethodLength);
code = boolRewriter.rewrite();
FunctionSplitVisitor visitor = new FunctionSplitVisitor(boolRewriter.getBoolVarNames());
JavaParser javaParser = new JavaParser(visitor.tokenStream);
javaParser.getInterpreter().setPredictionMode(PredictionMode.SLL);
visitor.visit(javaParser.compilationUnit());
return visitor.rewriter.getText();
}
private class FunctionSplitVisitor extends JavaParserBaseVisitor {
private final CommonTokenStream tokenStream;
private final TokenStreamRewriter rewriter;
private final List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy