Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to assign null AST childs after non-null #259

Merged
merged 1 commit into from
Jun 24, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/main/java/spoon/support/reflect/code/CtAssertImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ public CtExpression<T> getExpression() {
}

public void setExpression(CtExpression<T> value) {
value.setParent(this);
if (value != null)
value.setParent(this);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An expression of an assertion can be null? For me, an assertion must have an expression so I propose to change for something like:

public void setExpression(CtExpression<T> value) {
    if (value == null) {
        throw new SpoonException("An assertion must have an expression.");
    }
    value.setParent(this);
    this.value = value;
}

What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is an "assertionExpression" and just "expression". Null expression corresponds to the statement assert assertionExpression;. Or it might be assert assertionExpression : expression;

this.value = value;
}

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/spoon/support/reflect/code/CtContinueImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ public CtStatement getLabelledStatement() {
}

public void setLabelledStatement(CtStatement labelledStatement) {
labelledStatement.setParent(this);
if (labelledStatement != null)
labelledStatement.setParent(this);
this.labelledStatement = labelledStatement;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ public void setVariable(CtVariableReference<T> variable) {
}

public void setTarget(CtExpression<?> target) {
target.setParent(this);
if (target != null)
target.setParent(this);
this.target = target;
}
}
3 changes: 2 additions & 1 deletion src/main/java/spoon/support/reflect/code/CtForImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ public List<CtStatement> getForUpdate() {
}

public void setExpression(CtExpression<Boolean> expression) {
expression.setParent(this);
if (expression != null)
expression.setParent(this);
this.expression = expression;
}

Expand Down
7 changes: 5 additions & 2 deletions src/main/java/spoon/support/reflect/code/CtIfImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,15 @@ public void setCondition(CtExpression<Boolean> condition) {
}

public void setElseStatement(CtStatement elseStatement) {
elseStatement.setParent(this);
if (elseStatement != null)
elseStatement.setParent(this);
this.elseStatement = elseStatement;
}

public void setThenStatement(CtStatement thenStatement) {
thenStatement.setParent(this);
// then branch might be null: `if (condition) ;`
if (thenStatement != null)
thenStatement.setParent(this);
this.thenStatement = thenStatement;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,9 @@ public CtTypeReference<T> getType() {
}

public void setDefaultExpression(CtExpression<T> defaultExpression) {
if (defaultExpression != null)
defaultExpression.setParent(this);
this.defaultExpression = defaultExpression;
this.defaultExpression.setParent(this);
}

public void setSimpleName(String simpleName) {
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/spoon/support/reflect/code/CtLoopImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ public Void S() {
}

public void setBody(CtStatement body) {
body.setParent(this);
if (body != null)
body.setParent(this);
this.body = body;
}
}
3 changes: 2 additions & 1 deletion src/main/java/spoon/support/reflect/code/CtReturnImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ public CtExpression<R> getReturnedExpression() {
}

public void setReturnedExpression(CtExpression<R> expression) {
expression.setParent(this);
if (expression != null)
expression.setParent(this);
this.returnedExpression = expression;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ public CtExpression<?> getTarget() {
}

public void setTarget(CtExpression<?> target) {
target.setParent(this);
if (target != null)
target.setParent(this);
this.target = target;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ public T getTarget() {
}

public void setTarget(T target) {
target.setParent(this);
if (target != null)
target.setParent(this);
this.target = target;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ public CtTypeReference<T> getType() {
}

public void setDefaultExpression(CtExpression<T> defaultExpression) {
defaultExpression.setParent(this);
if (defaultExpression != null)
defaultExpression.setParent(this);
this.defaultExpression = defaultExpression;
}

Expand Down
18 changes: 18 additions & 0 deletions src/test/java/spoon/test/targeted/TargetedExpressionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@
import spoon.reflect.code.CtSuperAccess;
import spoon.reflect.declaration.CtClass;
import spoon.reflect.declaration.CtConstructor;
import spoon.reflect.declaration.CtField;
import spoon.reflect.declaration.CtMethod;
import spoon.reflect.declaration.CtType;
import spoon.reflect.factory.Factory;
import spoon.reflect.reference.CtFieldReference;
import spoon.reflect.visitor.filter.AbstractFilter;
import spoon.reflect.visitor.filter.NameFilter;
import spoon.reflect.visitor.filter.TypeFilter;
import spoon.support.reflect.code.CtThisAccessImpl;
import spoon.test.targeted.testclasses.Foo;
import spoon.test.targeted.testclasses.InternalSuperCall;

import java.util.List;
Expand Down Expand Up @@ -74,4 +77,19 @@ public void testTargetOfFieldAccess() throws Exception {
assertEquals("Target is CtThisAccessImpl if there is a 'this' explicit.", CtThisAccessImpl.class, elements.get(0).getTarget().getClass());
assertNull("Targets is null if there isn't a 'this' explicit.", elements.get(1).getTarget());
}

@Test
public void testNotTargetedExpression() throws Exception {
Factory factory = build(Foo.class);
CtClass<Object> fooClass = factory.Class().get(Foo.class);
CtField<?> iField = fooClass.getField("i");
CtFieldAccess<?> fieldAccess = factory.Core().createFieldRead();
fieldAccess.setVariable((CtFieldReference) iField.getReference());
fieldAccess.setTarget(factory.Code().createThisAccess(fooClass.getReference()));
assertEquals("this.i", fieldAccess.toString());
// this test is made for this line. Check that we can setTarget(null)
// without NPE
fieldAccess.setTarget(null);
assertEquals("i", fieldAccess.toString());
}
}