From 1ef5040c8c875647f1fe9c469116913d4e3b9c86 Mon Sep 17 00:00:00 2001 From: Mario Fusco Date: Tue, 30 Dec 2014 18:24:12 +0100 Subject: [PATCH] refactoring --- .../org/jboss/forge/roaster/model/Block.java | 15 +++----- .../roaster/model/source/MethodSource.java | 11 ++++-- .../model/statements/BlockStatement.java | 4 +- .../forge/roaster/model/impl/BlockImpl.java | 34 +++++++++++++++-- .../forge/roaster/model/impl/MethodImpl.java | 38 ++++++++++--------- .../impl/statements/JdtStatementWrapper.java | 5 ++- .../statements/ModifyMethodBodyTest.java | 28 ++++++++++++++ 7 files changed, 97 insertions(+), 38 deletions(-) create mode 100644 impl/src/test/java/org/jboss/forge/test/roaster/model/statements/ModifyMethodBodyTest.java diff --git a/api/src/main/java/org/jboss/forge/roaster/model/Block.java b/api/src/main/java/org/jboss/forge/roaster/model/Block.java index aaa498cc..1fc5f9d7 100644 --- a/api/src/main/java/org/jboss/forge/roaster/model/Block.java +++ b/api/src/main/java/org/jboss/forge/roaster/model/Block.java @@ -8,22 +8,17 @@ package org.jboss.forge.roaster.model; import org.jboss.forge.roaster.Origin; +import org.jboss.forge.roaster.model.expressions.Expression; import org.jboss.forge.roaster.model.source.BlockHolder; import org.jboss.forge.roaster.model.source.JavaSource; -import org.jboss.forge.roaster.model.source.MethodSource; -import org.jboss.forge.roaster.model.statements.AssignStatement; -import org.jboss.forge.roaster.model.statements.BlockStatement; -import org.jboss.forge.roaster.model.statements.DeclareStatement; -import org.jboss.forge.roaster.model.statements.ForEachStatement; -import org.jboss.forge.roaster.model.statements.ForStatement; -import org.jboss.forge.roaster.model.statements.IfStatement; -import org.jboss.forge.roaster.model.statements.InvokeStatement; -import org.jboss.forge.roaster.model.statements.ReturnStatement; import org.jboss.forge.roaster.model.statements.Statement; -import org.jboss.forge.roaster.model.statements.WhileStatement; public interface Block, T extends BlockHolder> extends Origin, BlockHolder> { + Block addStatement( Statement statement ); + + Block addStatement( Expression statement ); + } diff --git a/api/src/main/java/org/jboss/forge/roaster/model/source/MethodSource.java b/api/src/main/java/org/jboss/forge/roaster/model/source/MethodSource.java index a77b7480..846ca5c8 100644 --- a/api/src/main/java/org/jboss/forge/roaster/model/source/MethodSource.java +++ b/api/src/main/java/org/jboss/forge/roaster/model/source/MethodSource.java @@ -7,15 +7,14 @@ package org.jboss.forge.roaster.model.source; -import java.util.List; - -import org.jboss.forge.roaster.model.Block; import org.jboss.forge.roaster.model.JavaClass; import org.jboss.forge.roaster.model.JavaType; import org.jboss.forge.roaster.model.Method; import org.jboss.forge.roaster.model.expressions.Expression; import org.jboss.forge.roaster.model.statements.Statement; +import java.util.List; + /** * Represents a Java Method in source form. * @@ -136,4 +135,10 @@ public interface MethodSource> extends Method setBody( Expression expr ); + /** + * Returns the body of this method as a Roaster Block + * @return + */ + org.jboss.forge.roaster.model.Block getBodyAsBlock(); + } \ No newline at end of file diff --git a/api/src/main/java/org/jboss/forge/roaster/model/statements/BlockStatement.java b/api/src/main/java/org/jboss/forge/roaster/model/statements/BlockStatement.java index be8290b9..1c594b87 100644 --- a/api/src/main/java/org/jboss/forge/roaster/model/statements/BlockStatement.java +++ b/api/src/main/java/org/jboss/forge/roaster/model/statements/BlockStatement.java @@ -16,8 +16,8 @@ public interface BlockStatement, T extends Block>, Block { - BlockStatement addStatement( Statement statement ); + BlockStatement addStatement( Statement statement ); - BlockStatement addStatement( Expression statement ); + BlockStatement addStatement( Expression statement ); } diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/BlockImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/BlockImpl.java index 3a9ac27a..0926872d 100644 --- a/impl/src/main/java/org/jboss/forge/roaster/model/impl/BlockImpl.java +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/BlockImpl.java @@ -9,10 +9,16 @@ import org.eclipse.jdt.core.dom.AST; import org.jboss.forge.roaster.model.Block; +import org.jboss.forge.roaster.model.expressions.Expression; +import org.jboss.forge.roaster.model.impl.statements.ExpressionStatementImpl; import org.jboss.forge.roaster.model.impl.statements.JdtStatementWrapper; import org.jboss.forge.roaster.model.source.BlockHolder; import org.jboss.forge.roaster.model.source.JavaSource; -import org.jboss.forge.roaster.model.statements.BlockStatement; +import org.jboss.forge.roaster.model.statements.Statement; + +import java.util.Collections; +import java.util.LinkedList; +import java.util.List; public class BlockImpl, T extends BlockHolder> extends NodeImpl @@ -20,15 +26,35 @@ public class BlockImpl, T extends BlockHolder> JdtStatementWrapper { private org.eclipse.jdt.core.dom.Block block; + private List> statements = Collections.EMPTY_LIST; - private BlockStatement body; + @Override + public Block addStatement( Statement statement ) { + if ( statements.isEmpty() ) { + statements = new LinkedList>(); + } + statements.add( statement ); + return this; + } - public void setBlock( BlockStatement body ) { - this.body = body; + @Override + public Block addStatement( Expression expression ) { + return addStatement( new ExpressionStatementImpl>( expression ) ); } @Override public org.eclipse.jdt.core.dom.Block materialize( AST ast ) { + if (block != null) { + return block; + } + + block = ast.newBlock(); + + for ( Statement statement : statements ) { + block.statements().add( wireAndGetStatement( statement, this, ast ) ); + } + return block; } + } diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/MethodImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/MethodImpl.java index 262f4328..1831225f 100644 --- a/impl/src/main/java/org/jboss/forge/roaster/model/impl/MethodImpl.java +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/MethodImpl.java @@ -6,11 +6,6 @@ */ package org.jboss.forge.roaster.model.impl; -import java.util.ArrayList; -import java.util.Collections; -import java.util.Iterator; -import java.util.List; - import org.eclipse.jdt.core.dom.AST; import org.eclipse.jdt.core.dom.ASTNode; import org.eclipse.jdt.core.dom.Block; @@ -26,7 +21,6 @@ import org.jboss.forge.roaster.Roaster; import org.jboss.forge.roaster.model.Annotation; import org.jboss.forge.roaster.model.JavaType; -import org.jboss.forge.roaster.model.Method; import org.jboss.forge.roaster.model.Type; import org.jboss.forge.roaster.model.TypeVariable; import org.jboss.forge.roaster.model.Visibility; @@ -35,7 +29,6 @@ import org.jboss.forge.roaster.model.expressions.Expression; import org.jboss.forge.roaster.model.impl.statements.ExpressionStatementImpl; import org.jboss.forge.roaster.model.impl.statements.JdtStatementWrapper; -import org.jboss.forge.roaster.model.impl.statements.StatementImpl; import org.jboss.forge.roaster.model.source.AnnotationSource; import org.jboss.forge.roaster.model.source.BlockHolder; import org.jboss.forge.roaster.model.source.JavaClassSource; @@ -45,10 +38,14 @@ import org.jboss.forge.roaster.model.source.ParameterSource; import org.jboss.forge.roaster.model.source.TypeVariableSource; import org.jboss.forge.roaster.model.statements.BlockStatement; -import org.jboss.forge.roaster.model.statements.Statements; import org.jboss.forge.roaster.model.util.Strings; import org.jboss.forge.roaster.model.util.Types; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Iterator; +import java.util.List; + /** * @author Lincoln Baxter, III */ @@ -62,6 +59,8 @@ public class MethodImpl> implements MethodSource, Blo private CompilationUnit cu = null; private final MethodDeclaration method; + private org.jboss.forge.roaster.model.Block body; + private void init(final O parent) { this.parent = parent; @@ -798,10 +797,15 @@ public JavaDocSource> getJavaDoc() @Override public MethodSource setBody( org.jboss.forge.roaster.model.statements.Statement statement ) { - BlockImpl> block = new BlockImpl>(); + if (statement instanceof org.jboss.forge.roaster.model.Block) { + this.body = ((org.jboss.forge.roaster.model.Block) statement); + } else { + this.body = new BlockImpl>(); + body.addStatement(statement); + } - Statement jdtStatement = ((BlockImpl) block).wireAndGetStatement( statement.wrap(), block, cu.getAST() ); - method.setBody( (Block) jdtStatement ); + Statement jdtStatement = ((JdtStatementWrapper) body).wireAndGetStatement(statement.wrap(), this, cu.getAST()); + method.setBody((Block) jdtStatement); return this; } @@ -809,13 +813,11 @@ public MethodSource setBody( org.jboss.forge.roaster.model.statements.Stateme @Override public MethodSource setBody( Expression expr ) { - ExpressionStatementImpl> expressionStatement = new ExpressionStatementImpl>( expr ); - BlockImpl> block = new BlockImpl>(); - - Statement jdtStatement = ((BlockImpl) block).wireAndGetStatement( expressionStatement.wrap(), block, cu.getAST() ); - method.setBody( (Block) jdtStatement ); - - return this; + return setBody( new ExpressionStatementImpl>( expr ) ); } + public org.jboss.forge.roaster.model.Block getBodyAsBlock() + { + return body; + } } diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/JdtStatementWrapper.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/JdtStatementWrapper.java index 9412189b..ebbad5a0 100644 --- a/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/JdtStatementWrapper.java +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/JdtStatementWrapper.java @@ -7,11 +7,14 @@ package org.jboss.forge.roaster.model.impl.statements; +import org.eclipse.jdt.core.dom.AST; import org.eclipse.jdt.core.dom.Statement; import org.jboss.forge.roaster.model.ASTNode; import org.jboss.forge.roaster.model.source.JavaSource; public interface JdtStatementWrapper extends ASTNode { - public void setOrigin( T origin ); + void setOrigin( T origin ); + + org.eclipse.jdt.core.dom.Statement wireAndGetStatement( org.jboss.forge.roaster.model.statements.Statement statement, P parent, AST ast ); } diff --git a/impl/src/test/java/org/jboss/forge/test/roaster/model/statements/ModifyMethodBodyTest.java b/impl/src/test/java/org/jboss/forge/test/roaster/model/statements/ModifyMethodBodyTest.java new file mode 100644 index 00000000..db04c76e --- /dev/null +++ b/impl/src/test/java/org/jboss/forge/test/roaster/model/statements/ModifyMethodBodyTest.java @@ -0,0 +1,28 @@ +package org.jboss.forge.test.roaster.model.statements; + +import org.jboss.forge.roaster.Roaster; +import org.jboss.forge.roaster.model.Block; +import org.jboss.forge.roaster.model.source.JavaClassSource; +import org.jboss.forge.roaster.model.source.MethodSource; +import org.junit.Test; + +import static org.jboss.forge.roaster.model.expressions.Expressions.var; +import static org.jboss.forge.roaster.model.statements.Statements.newReturn; +import static org.junit.Assert.assertEquals; + +public class ModifyMethodBodyTest { + @Test + public void testReturnArg() throws Exception + { + String target = "return x;"; + MethodSource method = Roaster.create(JavaClassSource.class).addMethod( "public String echo( String x )" ); + + method.setBody( newReturn().setReturn( var( "x" ) ) ); + + assertEquals( target, method.getBody().trim() ); + + Block body = method.getBodyAsBlock(); + System.out.println(body); + } + +}