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 10, 2016
1 parent 47f3c23 commit 9923a95
Show file tree
Hide file tree
Showing 10 changed files with 82 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
17 changes: 17 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,21 @@ public CtCatchVariable<? extends Throwable> getParameter() {
return parameter;
}

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

@Override
public <T extends CtCatch> T setBody(CtBlock<?> body) {
if (body != null) {
Expand Down
17 changes: 17 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,21 @@ public CtBlock<T> getBody() {
return (CtBlock<T>) body;
}

@Override
public <C extends CtBodyHolder> C setBody(CtStatement statement) {
CtBlock<?> body = null;
if (statement instanceof CtBlock<?>) {
body = (CtBlock<?>) statement;
} else if (statement != null) {
body = getFactory().Code().createCtBlock(statement);
}
if (body != null) {
body.setParent(this);
}
this.body = body;
return (C) this;
}

@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
17 changes: 17 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,21 @@ public CtBlock<?> getBody() {
return body;
}

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

@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,21 @@ public CtBlock<R> getBody() {
return (CtBlock<R>) body;
}

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

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

0 comments on commit 9923a95

Please sign in to comment.