-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8ab770d
commit 1ef5040
Showing
7 changed files
with
97 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 <a href="mailto:[email protected]">Lincoln Baxter, III</a> | ||
*/ | ||
|
@@ -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; | ||
|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
impl/src/test/java/org/jboss/forge/test/roaster/model/statements/ModifyMethodBodyTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |