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 14, 2016
1 parent 9cfbe84 commit 39b3d1a
Show file tree
Hide file tree
Showing 12 changed files with 68 additions and 15 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);
}
6 changes: 4 additions & 2 deletions src/main/java/spoon/reflect/code/CtCatch.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,14 @@ public interface CtCatch extends CtCodeElement, CtBodyHolder {
/**
* Gets the catch's body.
*/
@Override
CtBlock<?> getBody();

/**
* Sets the catch's body.
* Sets the catch's body. The instance of CtBlock makes sense too
*/
<T extends CtCatch> T setBody(CtBlock<?> body);
@Override
<T extends CtBodyHolder> T setBody(CtStatement body);

@Override
CtCatch clone();
Expand Down
6 changes: 4 additions & 2 deletions 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.
* Sets the body of this loop. The instance of CtBlock makes sense too
*/
<T extends CtLoop> T setBody(CtStatement body);
@Override
<T extends CtBodyHolder> T setBody(CtStatement body);

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

/**
* Sets the tried body.
* Sets the tried body. The instance of CtBlock makes sense too
*/
<T extends CtTry> T setBody(CtBlock<?> body);
@Override
<T extends CtBodyHolder> T setBody(CtStatement body);

/**
* Gets the <i>finalizer</i> block of this <code>try</code> (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@
*/
package spoon.reflect.declaration;

import spoon.reflect.code.CtBlock;
import spoon.reflect.code.CtBodyHolder;
import spoon.reflect.code.CtExpression;
import spoon.reflect.code.CtStatement;
import spoon.reflect.reference.CtTypeReference;
import spoon.support.UnsettableProperty;

Expand All @@ -43,7 +44,7 @@ public interface CtAnnotationMethod<T> extends CtMethod<T> {

@Override
@UnsettableProperty
<B extends T, T1 extends CtExecutable<T>> T1 setBody(CtBlock<B> body);
<T1 extends CtBodyHolder> T1 setBody(CtStatement body);

@Override
@UnsettableProperty
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/spoon/reflect/declaration/CtExecutable.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import spoon.reflect.code.CtBlock;
import spoon.reflect.code.CtBodyHolder;
import spoon.reflect.code.CtStatement;
import spoon.reflect.reference.CtExecutableReference;
import spoon.reflect.reference.CtTypeReference;
import spoon.support.DerivedProperty;
Expand Down Expand Up @@ -47,12 +48,14 @@ public interface CtExecutable<R> extends CtNamedElement, CtTypedElement<R>, CtBo
/**
* Gets the body expression.
*/
@Override
CtBlock<R> getBody();

/**
* Sets the body expression.
* Sets the body expression. The instance of CtBlock makes sense too
*/
<B extends R, T extends CtExecutable<R>> T setBody(CtBlock<B> body);
@Override
<T extends CtBodyHolder> T setBody(CtStatement body);

/**
* Gets the parameters list.
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/spoon/reflect/factory/CodeFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,25 @@ public <T extends CtStatement> CtBlock<?> createCtBlock(T element) {
return factory.Core().createBlock().addStatement(element);
}

/**
* Accepts instance of CtStatement or CtBlock.
* If element is CtStatement, then it creates wrapping CtBlock, which contains the element
* If element is CtBlock, then it directly returns that element
* If element is null, then it returns null.
* note: It must not create empty CtBlock - as expected in CtCatch, CtExecutable, CtLoop and CtTry setBody implementations
* @param element
* @return CtBlock instance
*/
public <T extends CtStatement> CtBlock<?> getOrCreateCtBlock(T element) {
if (element == null) {
return null;
}
if (element instanceof CtBlock<?>) {
return (CtBlock<?>) element;
}
return this.createCtBlock(element);
}

/**
* Creates a throw.
*
Expand Down
5 changes: 4 additions & 1 deletion 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 @@ -44,7 +46,8 @@ public CtCatchVariable<? extends Throwable> getParameter() {
}

@Override
public <T extends CtCatch> T setBody(CtBlock<?> body) {
public <T extends CtBodyHolder> T setBody(CtStatement statement) {
CtBlock<?> body = getFactory().Code().getOrCreateCtBlock(statement);
if (body != null) {
body.setParent(this);
}
Expand Down
5 changes: 4 additions & 1 deletion 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 @@ -65,7 +67,8 @@ public CtBlock<T> getBody() {
}

@Override
public <B extends T, C extends CtExecutable<T>> C setBody(CtBlock<B> body) {
public <C extends CtBodyHolder> C setBody(CtStatement statement) {
CtBlock<?> body = getFactory().Code().getOrCreateCtBlock(statement);
if (expression != null && body != null) {
throw new SpoonException("A lambda can't have two bodys.");
}
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
10 changes: 9 additions & 1 deletion 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 @@ -99,7 +101,13 @@ public CtBlock<?> getBody() {
}

@Override
public <T extends CtTry> T setBody(CtBlock<?> body) {
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);
}
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 @@ -55,7 +57,8 @@ public CtBlock<R> getBody() {
}

@Override
public <B extends R, T extends CtExecutable<R>> T setBody(CtBlock<B> body) {
public <T extends CtBodyHolder> T setBody(CtStatement statement) {
CtBlock<?> body = getFactory().Code().getOrCreateCtBlock(statement);
if (body != null) {
body.setParent(this);
}
Expand Down

0 comments on commit 39b3d1a

Please sign in to comment.