Skip to content

Commit

Permalink
[FLINK-35334][flink-table] Split constructor of generated code if it …
Browse files Browse the repository at this point in the history
…is too large
  • Loading branch information
littleeleventhwolf authored Nov 26, 2024
1 parent 98d1705 commit 35a58f7
Show file tree
Hide file tree
Showing 4 changed files with 243 additions and 110 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,11 @@ public Void visitFieldDeclaration(JavaParser.FieldDeclarationContext ctx) {
return visitChildren(ctx);
}

@Override
public Void visitConstructorDeclaration(JavaParser.ConstructorDeclarationContext ctx) {
return visitFunctionBody(ctx.constructorBody);
}

@Override
public Void visitMethodDeclaration(JavaParser.MethodDeclarationContext ctx) {
if ("void".equals(ctx.typeTypeOrVoid().getText())) {
Expand All @@ -142,17 +147,21 @@ public Void visitMethodDeclaration(JavaParser.MethodDeclarationContext ctx) {

@Override
public Void visitMethodBody(JavaParser.MethodBodyContext ctx) {
return visitFunctionBody(ctx.block());
}

public Void visitFunctionBody(JavaParser.BlockContext blockContext) {
// this condition must be the same with the condition in
// FunctionSplitter::visitMethodDeclaration
if (CodeSplitUtil.getContextTextLength(ctx.block()) <= maxMethodLength) {
if (CodeSplitUtil.getContextTextLength(blockContext) <= maxMethodLength) {
return null;
}

hasRewrite = true;
InnerBlockStatementExtractor extractor = new InnerBlockStatementExtractor();
if (ctx.block() != null && ctx.block().blockStatement() != null) {
if (blockContext != null && blockContext.blockStatement() != null) {
for (JavaParser.BlockStatementContext blockStatementContext :
ctx.block().blockStatement()) {
blockContext.blockStatement()) {
extractor.visitBlockStatement(blockStatementContext);
}
newFields.peek().append(extractor.getNewLocalVariables());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.TokenStreamRewriter;
import org.antlr.v4.runtime.atn.PredictionMode;

Expand Down Expand Up @@ -142,21 +143,51 @@ public Void visitClassBody(JavaParser.ClassBodyContext ctx) {
return visitChildren(ctx);
}

@Override
public Void visitConstructorDeclaration(JavaParser.ConstructorDeclarationContext ctx) {
return doFunctionSplit(
ctx,
ctx.constructorBody,
"void",
ctx.IDENTIFIER().getText(),
CodeSplitUtil.getContextString(ctx.formalParameters()),
ctx.THROWS() != null,
CodeSplitUtil.getContextString(ctx.qualifiedNameList()));
}

@Override
public Void visitMethodDeclaration(JavaParser.MethodDeclarationContext ctx) {

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

long methodBodyLength = CodeSplitUtil.getContextTextLength(ctx.methodBody().block());
return doFunctionSplit(
ctx,
ctx.methodBody().block(),
CodeSplitUtil.getContextString(ctx.typeTypeOrVoid()),
ctx.IDENTIFIER().getText(),
CodeSplitUtil.getContextString(ctx.formalParameters()),
ctx.THROWS() != null,
CodeSplitUtil.getContextString(ctx.qualifiedNameList()));
}

private Void doFunctionSplit(
ParserRuleContext ctx,
JavaParser.BlockContext blockContext,
String returnType,
String functionName,
String parameters,
boolean hasThrowable,
String throwables) {
long methodBodyLength = CodeSplitUtil.getContextTextLength(blockContext);

if (methodBodyLength < maxMethodLength) {
return null;
}

if (ctx.methodBody().block().blockStatement() == null
|| ctx.methodBody().block().blockStatement().size() <= 1) {
if (blockContext.blockStatement() == null
|| blockContext.blockStatement().size() <= 1) {
return null;
}

Expand All @@ -173,13 +204,8 @@ public Void visitFormalParameter(JavaParser.FormalParameterContext ctx) {
}
}.visit(ctx);

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

for (JavaParser.BlockStatementContext blockStatementContext :
ctx.methodBody().block().blockStatement()) {
blockContext.blockStatement()) {
blockStatementContexts.add(blockStatementContext);
splitFuncBodies.add(CodeSplitUtil.getContextString(blockStatementContext));
}
Expand All @@ -189,24 +215,22 @@ public Void visitFormalParameter(JavaParser.FormalParameterContext ctx) {
List<String> newSplitMethodCalls = new ArrayList<>();

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

String hasReturnedVarName = boolVarNames.get(classCount).get(functionName + parameters);
if (hasReturnedVarName != null) {
rewriter.insertAfter(
ctx.methodBody().block().start,
String.format("\n%s = false;", hasReturnedVarName));
blockContext.start, String.format("\n%s = false;", hasReturnedVarName));
}

for (String methodBody : mergedCodeBlocks) {
long counter = CodeSplitUtil.getCounter().getAndIncrement();

// void f_splitXX(int x, String y)
String splitMethodDef =
type
returnType
+ " "
+ functionName
+ "_split"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,26 @@ public class TestSplitJavaCode {

public TestSplitJavaCode(int[] b) {
this.b = b;
int b0 = this.b[0] + b[0];
System.out.println("b0 = " + b0);
int b1 = this.b[1] + b[1];
System.out.println("b1 = " + b1);
int b2 = this.b[2] + b[2];
System.out.println("b2 = " + b2);
int b3 = this.b[3] + b[3];
System.out.println("b3 = " + b3);
int b4 = this.b[4] + b[4];
System.out.println("b4 = " + b4);
int b5 = this.b[5] + b[5];
System.out.println("b5 = " + b5);
int b6 = this.b[6] + b[6];
System.out.println("b6 = " + b6);
int b7 = this.b[7] + b[7];
System.out.println("b7 = " + b7);
int b8 = this.b[8] + b[8];
System.out.println("b8 = " + b8);
int b9 = this.b[9] + b[9];
System.out.println("b9 = " + b9);
}

public void myFun1(int a) {
Expand Down
Loading

0 comments on commit 35a58f7

Please sign in to comment.