Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
mariofusco authored and sotty committed Jan 8, 2015
1 parent 8ab770d commit 1ef5040
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 38 deletions.
15 changes: 5 additions & 10 deletions api/src/main/java/org/jboss/forge/roaster/model/Block.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<O extends JavaSource<O>, T extends BlockHolder<O,?>>
extends Origin<T>,
BlockHolder<O,Block<O,T>> {

Block<O, T> addStatement( Statement<?,?,?> statement );

Block<O, T> addStatement( Expression<?,?> statement );

}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -136,4 +135,10 @@ public interface MethodSource<O extends JavaSource<O>> extends Method<O, MethodS
*/
MethodSource<O> setBody( Expression expr );

/**
* Returns the body of this method as a Roaster Block
* @return
*/
org.jboss.forge.roaster.model.Block getBodyAsBlock();

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ public interface BlockStatement<O extends JavaSource<O>, T extends Block<O, ? ex
extends Statement<O,T,BlockStatement<O,T>>,
Block<O, T> {

BlockStatement<O,T> addStatement( Statement<?,?,?> statement );
BlockStatement<O, T> addStatement( Statement<?,?,?> statement );

BlockStatement<O, T> addStatement( Expression<?,?> statement );
BlockStatement<O, T> addStatement( Expression<?,?> statement );

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,52 @@

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<O extends JavaSource<O>, T extends BlockHolder<O,T>>
extends NodeImpl<O,T,org.eclipse.jdt.core.dom.Block>
implements Block<O,T>,
JdtStatementWrapper<O,T, org.eclipse.jdt.core.dom.Block> {

private org.eclipse.jdt.core.dom.Block block;
private List<Statement<?,?,?>> statements = Collections.EMPTY_LIST;

private BlockStatement body;
@Override
public Block<O, T> addStatement( Statement<?,?,?> statement ) {
if ( statements.isEmpty() ) {
statements = new LinkedList<Statement<?,?,?>>();
}
statements.add( statement );
return this;
}

public void setBlock( BlockStatement body ) {
this.body = body;
@Override
public Block<O, T> addStatement( Expression<?,?> expression ) {
return addStatement( new ExpressionStatementImpl<O, Block<O, T>>( 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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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 <a href="mailto:[email protected]">Lincoln Baxter, III</a>
*/
Expand All @@ -62,6 +59,8 @@ public class MethodImpl<O extends JavaSource<O>> implements MethodSource<O>, 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;
Expand Down Expand Up @@ -798,24 +797,27 @@ public JavaDocSource<MethodSource<O>> getJavaDoc()
@Override
public MethodSource<O> setBody( org.jboss.forge.roaster.model.statements.Statement statement )
{
BlockImpl<O,MethodSource<O>> block = new BlockImpl<O,MethodSource<O>>();
if (statement instanceof org.jboss.forge.roaster.model.Block) {
this.body = ((org.jboss.forge.roaster.model.Block) statement);
} else {
this.body = new BlockImpl<O,MethodSource<O>>();
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;
}

@Override
public MethodSource<O> setBody( Expression expr )
{
ExpressionStatementImpl<O,BlockStatement<O,?>> expressionStatement = new ExpressionStatementImpl<O, BlockStatement<O,?>>( expr );
BlockImpl<O,MethodSource<O>> block = new BlockImpl<O,MethodSource<O>>();

Statement jdtStatement = ((BlockImpl) block).wireAndGetStatement( expressionStatement.wrap(), block, cu.getAST() );
method.setBody( (Block) jdtStatement );

return this;
return setBody( new ExpressionStatementImpl<O, BlockStatement<O,?>>( expr ) );
}

public org.jboss.forge.roaster.model.Block getBodyAsBlock()
{
return body;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<O extends JavaSource, T, J extends Statement> extends ASTNode<J> {

public void setOrigin( T origin );
void setOrigin( T origin );

<K extends org.eclipse.jdt.core.dom.Statement, P> org.eclipse.jdt.core.dom.Statement wireAndGetStatement( org.jboss.forge.roaster.model.statements.Statement statement, P parent, AST ast );
}
Original file line number Diff line number Diff line change
@@ -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<JavaClassSource> 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);
}

}

0 comments on commit 1ef5040

Please sign in to comment.