From 975f26bcc6324de748d2f307aed80e0950cd7f19 Mon Sep 17 00:00:00 2001 From: Emil Lundberg Date: Thu, 10 Jan 2019 04:37:32 +0100 Subject: Extract methods generateSingularMethod{Parameters,Statements} --- .../handlers/singulars/JavacGuavaSingularizer.java | 24 ++++++++++++++-------- .../JavacJavaUtilListSetSingularizer.java | 13 +++++++++--- .../singulars/JavacJavaUtilMapSingularizer.java | 17 +++++++++++---- 3 files changed, 39 insertions(+), 15 deletions(-) (limited to 'src/core/lombok/javac') diff --git a/src/core/lombok/javac/handlers/singulars/JavacGuavaSingularizer.java b/src/core/lombok/javac/handlers/singulars/JavacGuavaSingularizer.java index bf2e1cbc..64345742 100644 --- a/src/core/lombok/javac/handlers/singulars/JavacGuavaSingularizer.java +++ b/src/core/lombok/javac/handlers/singulars/JavacGuavaSingularizer.java @@ -39,7 +39,6 @@ import lombok.javac.handlers.JavacSingularsRecipes.StatementMaker; import com.sun.tools.javac.code.Flags; import com.sun.tools.javac.tree.JCTree; -import com.sun.tools.javac.tree.JCTree.JCAnnotation; import com.sun.tools.javac.tree.JCTree.JCBlock; import com.sun.tools.javac.tree.JCTree.JCExpression; import com.sun.tools.javac.tree.JCTree.JCModifiers; @@ -82,9 +81,24 @@ abstract class JavacGuavaSingularizer extends JavacSingularizer { @Override protected void generateSingularMethod(boolean deprecate, JavacTreeMaker maker, JCExpression returnType, JCStatement returnStatement, SingularData data, JavacNode builderType, JCTree source, boolean fluent) { + ListBuffer statements = generateSingularMethodStatements(maker, data, builderType, source); + List params = generateSingularMethodParameters(maker, data, builderType, source); + finishAndInjectSingularMethod(maker, returnType, returnStatement, data, builderType, source, fluent, deprecate, statements, params, getAddMethodName()); + } + + private List generateSingularMethodParameters(JavacTreeMaker maker, SingularData data, JavacNode builderType, JCTree source) { Name[] names = generateSingularMethodParameterNames(data, builderType); + ListBuffer params = new ListBuffer(); + for (int i = 0; i < names.length; i++) { + params.append(generateSingularMethodParameter(i, maker, data, builderType, source, names[i])); + } + return params.toList(); + } + private ListBuffer generateSingularMethodStatements(JavacTreeMaker maker, SingularData data, JavacNode builderType, JCTree source) { ListBuffer statements = new ListBuffer(); + Name[] names = generateSingularMethodParameterNames(data, builderType); + statements.append(createConstructBuilderVarIfNeeded(maker, data, builderType, source)); JCExpression thisDotFieldDotAdd = chainDots(builderType, "this", data.getPluralName().toString(), getAddMethodName()); ListBuffer invokeAddExprBuilder = new ListBuffer(); @@ -95,13 +109,7 @@ abstract class JavacGuavaSingularizer extends JavacSingularizer { JCExpression invokeAdd = maker.Apply(List.nil(), thisDotFieldDotAdd, invokeAddExpr); JCStatement st = maker.Exec(invokeAdd); statements.append(st); - - ListBuffer params = new ListBuffer(); - for (int i = 0; i < names.length; i++) { - params.append(generateSingularMethodParameter(i, maker, data, builderType, source, names[i])); - } - - finishAndInjectSingularMethod(maker, returnType, returnStatement, data, builderType, source, fluent, deprecate, statements, params.toList(), getAddMethodName()); + return statements; } private Name[] generateSingularMethodParameterNames(SingularData data, JavacNode builderType) { diff --git a/src/core/lombok/javac/handlers/singulars/JavacJavaUtilListSetSingularizer.java b/src/core/lombok/javac/handlers/singulars/JavacJavaUtilListSetSingularizer.java index 182bf78a..90fdb0cb 100644 --- a/src/core/lombok/javac/handlers/singulars/JavacJavaUtilListSetSingularizer.java +++ b/src/core/lombok/javac/handlers/singulars/JavacJavaUtilListSetSingularizer.java @@ -98,14 +98,21 @@ abstract class JavacJavaUtilListSetSingularizer extends JavacJavaUtilSingularize @Override protected void generateSingularMethod(boolean deprecate, JavacTreeMaker maker, JCExpression returnType, JCStatement returnStatement, SingularData data, JavacNode builderType, JCTree source, boolean fluent) { + ListBuffer statements = generateSingularMethodStatements(maker, data, builderType, source); + List params = generateSingularMethodParameters(maker, data, builderType, source); + finishAndInjectSingularMethod(maker, returnType, returnStatement, data, builderType, source, fluent, deprecate, statements, params, "add"); + } + + private ListBuffer generateSingularMethodStatements(JavacTreeMaker maker, SingularData data, JavacNode builderType, JCTree source) { ListBuffer statements = new ListBuffer(); statements.append(createConstructBuilderVarIfNeeded(maker, data, builderType, false, source)); statements.append(generateSingularMethodAddStatement(maker, builderType, data.getSingularName(), data.getPluralName().toString())); + return statements; + } + private List generateSingularMethodParameters(JavacTreeMaker maker, SingularData data, JavacNode builderType, JCTree source) { JCVariableDecl param = generateSingularMethodParameter(0, maker, data, builderType, source, data.getSingularName()); - List params = List.of(param); - - finishAndInjectSingularMethod(maker, returnType, returnStatement, data, builderType, source, fluent, deprecate, statements, params, "add"); + return List.of(param); } @Override diff --git a/src/core/lombok/javac/handlers/singulars/JavacJavaUtilMapSingularizer.java b/src/core/lombok/javac/handlers/singulars/JavacJavaUtilMapSingularizer.java index 255bb63a..cc6b47f9 100644 --- a/src/core/lombok/javac/handlers/singulars/JavacJavaUtilMapSingularizer.java +++ b/src/core/lombok/javac/handlers/singulars/JavacJavaUtilMapSingularizer.java @@ -125,21 +125,30 @@ public class JavacJavaUtilMapSingularizer extends JavacJavaUtilSingularizer { @Override protected void generateSingularMethod(boolean deprecate, JavacTreeMaker maker, JCExpression returnType, JCStatement returnStatement, SingularData data, JavacNode builderType, JCTree source, boolean fluent) { + ListBuffer statements = generateSingularMethodStatements(maker, data, builderType, source); + List params = generateSingularMethodParameters(maker, data, builderType, source); + finishAndInjectSingularMethod(maker, returnType, returnStatement, data, builderType, source, fluent, deprecate, statements, params, "put"); + } + + private ListBuffer generateSingularMethodStatements(JavacTreeMaker maker, SingularData data, JavacNode builderType, JCTree source) { ListBuffer statements = new ListBuffer(); - statements.append(createConstructBuilderVarIfNeeded(maker, data, builderType, true, source)); Name keyName = builderType.toName(data.getSingularName().toString() + "Key"); Name valueName = builderType.toName(data.getSingularName().toString() + "Value"); + statements.append(createConstructBuilderVarIfNeeded(maker, data, builderType, true, source)); /* Generates: this.pluralname$key.add(singularnameKey); */ statements.append(generateSingularMethodAddStatement(maker, builderType, keyName, data.getPluralName() + "$key")); /* Generates: this.pluralname$value.add(singularnameValue); */ statements.append(generateSingularMethodAddStatement(maker, builderType, valueName, data.getPluralName() + "$value")); + return statements; + } + private List generateSingularMethodParameters(JavacTreeMaker maker, SingularData data, JavacNode builderType, JCTree source) { + Name keyName = builderType.toName(data.getSingularName().toString() + "Key"); + Name valueName = builderType.toName(data.getSingularName().toString() + "Value"); JCVariableDecl paramKey = generateSingularMethodParameter(0, maker, data, builderType, source, keyName); JCVariableDecl paramValue = generateSingularMethodParameter(1, maker, data, builderType, source, valueName); - List params = List.of(paramKey, paramValue); - - finishAndInjectSingularMethod(maker, returnType, returnStatement, data, builderType, source, fluent, deprecate, statements, params, "put"); + return List.of(paramKey, paramValue); } @Override -- cgit