Skip to content

Commit

Permalink
refactor: CtBodyHolder.setBody(body) implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
pvojtechovsky committed Nov 9, 2016
1 parent ef20e87 commit 927fc95
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/main/java/spoon/reflect/code/CtBodyHolder.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,9 @@ public interface CtBodyHolder extends CtElement {
* Gets the body of this element
*/
CtStatement getBody();

/**
* Sets the body of this element.
*/
<T extends CtBodyHolder> T setBody(CtStatement body);
}
1 change: 1 addition & 0 deletions src/main/java/spoon/reflect/code/CtCatch.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public interface CtCatch extends CtCodeElement, CtBodyHolder {
/**
* Gets the catch's body.
*/
@Override
CtBlock<?> getBody();

/**
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/spoon/reflect/code/CtLoop.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@ public interface CtLoop extends CtStatement, TemplateParameter<Void>, CtBodyHold
/**
* Gets the body of this loop.
*/
@Override
CtStatement getBody();

/**
* Sets the body of this loop.
*/
<T extends CtLoop> T setBody(CtStatement body);
@Override
<T extends CtBodyHolder> T setBody(CtStatement body);

@Override
CtLoop clone();
Expand Down
1 change: 1 addition & 0 deletions src/main/java/spoon/reflect/code/CtTry.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public interface CtTry extends CtStatement, TemplateParameter<Void>, CtBodyHolde
/**
* Sets the tried body.
*/
@Override
CtBlock<?> getBody();

/**
Expand Down
1 change: 1 addition & 0 deletions src/main/java/spoon/reflect/declaration/CtExecutable.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public interface CtExecutable<R> extends CtNamedElement, CtTypedElement<R>, CtBo
/**
* Gets the body expression.
*/
@Override
CtBlock<R> getBody();

/**
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/spoon/support/reflect/code/CtCatchImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
package spoon.support.reflect.code;

import spoon.reflect.code.CtBlock;
import spoon.reflect.code.CtBodyHolder;
import spoon.reflect.code.CtCatch;
import spoon.reflect.code.CtCatchVariable;
import spoon.reflect.code.CtStatement;
import spoon.reflect.visitor.CtVisitor;

public class CtCatchImpl extends CtCodeElementImpl implements CtCatch {
Expand All @@ -43,6 +45,17 @@ public CtCatchVariable<? extends Throwable> getParameter() {
return parameter;
}

@Override
public <T extends CtBodyHolder> T setBody(CtStatement statement) {
CtBlock<?> block;
if (statement instanceof CtBlock<?>) {
block = (CtBlock<?>) statement;
} else {
block = getFactory().Code().createCtBlock(statement);
}
return setBody(block);
}

@Override
public <T extends CtCatch> T setBody(CtBlock<?> body) {
if (body != null) {
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/spoon/support/reflect/code/CtLambdaImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@

import spoon.SpoonException;
import spoon.reflect.code.CtBlock;
import spoon.reflect.code.CtBodyHolder;
import spoon.reflect.code.CtExpression;
import spoon.reflect.code.CtLambda;
import spoon.reflect.code.CtStatement;
import spoon.reflect.declaration.CtExecutable;
import spoon.reflect.declaration.CtNamedElement;
import spoon.reflect.declaration.CtParameter;
Expand Down Expand Up @@ -64,6 +66,17 @@ public CtBlock<T> getBody() {
return (CtBlock<T>) body;
}

@Override
public <T extends CtBodyHolder> T setBody(CtStatement statement) {
CtBlock<?> block;
if (statement instanceof CtBlock<?>) {
block = (CtBlock<?>) statement;
} else {
block = getFactory().Code().createCtBlock(statement);
}
return setBody(block);
}

@Override
public <B extends T, C extends CtExecutable<T>> C setBody(CtBlock<B> body) {
if (expression != null && body != null) {
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/spoon/support/reflect/code/CtLoopImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package spoon.support.reflect.code;

import spoon.reflect.code.CtBodyHolder;
import spoon.reflect.code.CtCodeElement;
import spoon.reflect.code.CtLoop;
import spoon.reflect.code.CtStatement;
Expand All @@ -31,8 +32,9 @@ public CtStatement getBody() {
return body;
}

@SuppressWarnings("unchecked")
@Override
public <T extends CtLoop> T setBody(CtStatement body) {
public <T extends CtBodyHolder> T setBody(CtStatement body) {
if (body != null) {
body.setParent(this);
}
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/spoon/support/reflect/code/CtTryImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
package spoon.support.reflect.code;

import spoon.reflect.code.CtBlock;
import spoon.reflect.code.CtBodyHolder;
import spoon.reflect.code.CtCatch;
import spoon.reflect.code.CtCodeElement;
import spoon.reflect.code.CtStatement;
import spoon.reflect.code.CtTry;
import spoon.reflect.declaration.CtType;
import spoon.reflect.visitor.CtVisitor;
Expand Down Expand Up @@ -98,6 +100,17 @@ public CtBlock<?> getBody() {
return body;
}

@Override
public <T extends CtBodyHolder> T setBody(CtStatement statement) {
CtBlock<?> block;
if (statement instanceof CtBlock<?>) {
block = (CtBlock<?>) statement;
} else {
block = getFactory().Code().createCtBlock(statement);
}
return setBody(block);
}

@Override
public <T extends CtTry> T setBody(CtBlock<?> body) {
if (body != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import java.util.Set;

import spoon.reflect.code.CtBlock;
import spoon.reflect.code.CtBodyHolder;
import spoon.reflect.code.CtStatement;
import spoon.reflect.declaration.CtExecutable;
import spoon.reflect.declaration.CtParameter;
import spoon.reflect.reference.CtExecutableReference;
Expand Down Expand Up @@ -54,6 +56,17 @@ public CtBlock<R> getBody() {
return (CtBlock<R>) body;
}

@Override
public <T extends CtBodyHolder> T setBody(CtStatement statement) {
CtBlock<?> block;
if (statement instanceof CtBlock<?>) {
block = (CtBlock<?>) statement;
} else {
block = getFactory().Code().createCtBlock(statement);
}
return setBody(block);
}

@Override
public <B extends R, T extends CtExecutable<R>> T setBody(CtBlock<B> body) {
if (body != null) {
Expand Down

0 comments on commit 927fc95

Please sign in to comment.