Skip to content

Commit

Permalink
test(replacevisitor): add a strong test and fix many bugs accordingly
Browse files Browse the repository at this point in the history
Close PR #957
  • Loading branch information
monperrus authored and tdurieux committed Nov 18, 2016
1 parent df6f639 commit 69b8a8a
Show file tree
Hide file tree
Showing 34 changed files with 334 additions and 151 deletions.
6 changes: 1 addition & 5 deletions src/main/java/spoon/generating/replace/ReplaceScanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
import spoon.reflect.visitor.ReferenceFilter;

import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
Expand All @@ -51,7 +50,6 @@ public class ReplaceScanner extends CtScanner {
public static final String GENERATING_REPLACE_PACKAGE = "spoon.generating.replace";
public static final String GENERATING_REPLACE_VISITOR = GENERATING_REPLACE_PACKAGE + ".ReplacementVisitor";

private final List<String> excludes = Collections.singletonList("spoon.reflect.code.CtLiteral#getValue()");
private final Map<String, CtClass> listeners = new HashMap<>();
private final CtClass<Object> target;
private final CtExecutableReference<?> element;
Expand Down Expand Up @@ -80,16 +78,14 @@ public <T> void visitCtMethod(CtMethod<T> element) {
for (int i = 1; i < element.getBody().getStatements().size() - 1; i++) {
CtInvocation inv = element.getBody().getStatement(i);
CtInvocation getter = (CtInvocation) inv.getArguments().get(0);

if (clone.getComments().size() == 0) {
// Add auto-generated comment.
final CtComment comment = factory.Core().createComment();
comment.setCommentType(CtComment.CommentType.INLINE);
comment.setContent("auto-generated, see " + ReplacementVisitorGenerator.class.getName());
clone.addComment(comment);
}
if (excludes.contains(getter.getExecutable().toString())) {
continue;
}
Class actualClass = getter.getType().getActualClass();
CtInvocation<?> invocation = createInvocation(factory, element, inv, getter, actualClass);
clone.getBody().addStatement(invocation);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,5 @@ public interface CtCodeSnippetExpression<T> extends CtExpression<T>, CtCodeSnipp

@Override
CtCodeSnippetExpression<T> clone();

}
4 changes: 4 additions & 0 deletions src/main/java/spoon/reflect/code/CtConstructorCall.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,8 @@ public interface CtConstructorCall<T> extends CtTargetedExpression<T, CtExpressi

@Override
CtConstructorCall<T> clone();

@Override
@DerivedProperty
CtTypeReference<T> getType();
}
2 changes: 1 addition & 1 deletion src/main/java/spoon/reflect/code/CtFieldAccess.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
* @param <T>
* Type of this field
*/
public interface CtFieldAccess<T> extends CtVariableRead<T>, CtTargetedExpression<T, CtExpression<?>> {
public interface CtFieldAccess<T> extends CtVariableAccess<T>, CtTargetedExpression<T, CtExpression<?>> {
CtFieldReference<T> getVariable();

@Override
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/spoon/reflect/code/CtFieldRead.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
* @param <T>
* type of the field
*/
public interface CtFieldRead<T> extends CtFieldAccess<T> {
public interface CtFieldRead<T> extends CtFieldAccess<T>, CtVariableRead<T> {
@Override
CtFieldRead<T> clone();
}
2 changes: 1 addition & 1 deletion src/main/java/spoon/reflect/code/CtFieldWrite.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
* @param <T>
* type of the field
*/
public interface CtFieldWrite<T> extends CtFieldAccess<T> {
public interface CtFieldWrite<T> extends CtFieldAccess<T>, CtVariableWrite<T> {
@Override
CtFieldWrite<T> clone();
}
2 changes: 0 additions & 2 deletions src/main/java/spoon/reflect/code/CtRHSReceiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package spoon.reflect.code;

import spoon.reflect.declaration.CtField;
import spoon.support.DerivedProperty;

/**
* Represents the right hand side of an assignment
Expand All @@ -28,7 +27,6 @@ public interface CtRHSReceiver<A> {
/**
* Returns the right-hand side of the "=" operator.
*/
@DerivedProperty
CtExpression<A> getAssignment();

/**
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/spoon/reflect/code/CtSuperAccess.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
*/
package spoon.reflect.code;

import spoon.reflect.reference.CtTypeReference;
import spoon.support.DerivedProperty;

/**
* This code element defines an access to super.
*
Expand All @@ -37,4 +40,8 @@
public interface CtSuperAccess<T> extends CtVariableRead<T>, CtTargetedExpression<T, CtExpression<?>> {
@Override
CtSuperAccess<T> clone();

@Override
@DerivedProperty
CtTypeReference<T> getType();
}
6 changes: 6 additions & 0 deletions src/main/java/spoon/reflect/code/CtVariableAccess.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
*/
package spoon.reflect.code;

import spoon.reflect.reference.CtTypeReference;
import spoon.reflect.reference.CtVariableReference;
import spoon.support.DerivedProperty;

/**
* This code element defines an access to a variable (read and write).
Expand All @@ -39,4 +41,8 @@ public interface CtVariableAccess<T> extends CtExpression<T> {

@Override
CtVariableAccess<T> clone();

@Override
@DerivedProperty
CtTypeReference<T> getType();
}
2 changes: 0 additions & 2 deletions src/main/java/spoon/reflect/declaration/CtTypedElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package spoon.reflect.declaration;

import spoon.reflect.reference.CtTypeReference;
import spoon.support.DerivedProperty;

/**
* This abstract element defines a typed element.
Expand All @@ -26,7 +25,6 @@ public interface CtTypedElement<T> extends CtElement {
/**
* Gets this element's type.
*/
@DerivedProperty
CtTypeReference<T> getType();

/**
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/spoon/reflect/reference/CtTypeReference.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,13 @@ public interface CtTypeReference<T> extends CtReference, CtActualTypeContainer,
* @return the declaring type if this references an inner class; null in
* other cases
*/
@DerivedProperty
CtTypeReference<?> getDeclaringType();

/**
* Gets the package of the referenced type.
*
* @return the declaring package or null if this if a inner class
*/
@DerivedProperty
CtPackageReference getPackage();

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ public interface CtVariableReference<T> extends CtReference {
/**
* Gets the type of the variable.
*/
@DerivedProperty
CtTypeReference<T> getType();

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ public void scan(CtElement element) {
return;
}
if (parent != null && element.getParent() != parent) {
throw new IllegalStateException(toDebugString(element)
+ "is set as child of\n" + toDebugString(element.getParent())
throw new IllegalStateException(element.toString()
+ " is set as child of\n" + toDebugString(element.getParent())
+ "however it is visited as a child of\n" + toDebugString(parent));
}
CtElement parent = this.parent;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/spoon/reflect/visitor/CtInheritanceScanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -304,15 +304,15 @@ public <T> void scanCtVariableAccess(CtVariableAccess<T> variableAccess) {

@Override
public <T> void visitCtFieldRead(CtFieldRead<T> fieldRead) {
scanCtFieldAccess(fieldRead);
visitCtVariableRead(fieldRead);
scanCtFieldAccess(fieldRead);
scanCtTargetedExpression(fieldRead);
}

@Override
public <T> void visitCtFieldWrite(CtFieldWrite<T> fieldWrite) {
visitCtVariableWrite(fieldWrite);
scanCtFieldAccess(fieldWrite);
visitCtVariableRead(fieldWrite);
scanCtTargetedExpression(fieldWrite);
}

Expand Down
18 changes: 13 additions & 5 deletions src/main/java/spoon/reflect/visitor/CtScanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ public void scan(CtElement element) {
public <A extends Annotation> void visitCtAnnotation(
final CtAnnotation<A> annotation) {
enter(annotation);
scan(annotation.getType());
scan(annotation.getComments());
scan(annotation.getAnnotationType());
scan(annotation.getAnnotations());
Expand Down Expand Up @@ -220,6 +221,7 @@ public <T> void visitCtArrayWrite(final CtArrayWrite<T> arrayWrite) {
public <T> void visitCtArrayTypeReference(final CtArrayTypeReference<T> reference) {
enter(reference);
scan(reference.getComments());
scan(reference.getPackage());
scan(reference.getDeclaringType());
scan(reference.getComponentType());
scan(reference.getActualTypeArguments());
Expand Down Expand Up @@ -314,6 +316,7 @@ public void visitCtTypeParameter(CtTypeParameter typeParameter) {

public <T> void visitCtConditional(final CtConditional<T> conditional) {
enter(conditional);
scan(conditional.getType());
scan(conditional.getAnnotations());
scan(conditional.getCondition());
scan(conditional.getThenExpression());
Expand Down Expand Up @@ -408,11 +411,9 @@ public <T> void visitCtAnnotationFieldAccess(
enter(annotationFieldAccess);
scan(annotationFieldAccess.getComments());
scan(annotationFieldAccess.getAnnotations());
scan(annotationFieldAccess.getType());
scan(annotationFieldAccess.getTypeCasts());
scan(annotationFieldAccess.getTarget());
scan(annotationFieldAccess.getType());
scan(annotationFieldAccess.getVariable());
scan(annotationFieldAccess.getComments());
exit(annotationFieldAccess);
}

Expand Down Expand Up @@ -480,7 +481,6 @@ public <T> void visitCtLiteral(final CtLiteral<T> literal) {
enter(literal);
scan(literal.getAnnotations());
scan(literal.getType());
scan(literal.getValue());
scan(literal.getTypeCasts());
scan(literal.getComments());
exit(literal);
Expand All @@ -507,6 +507,7 @@ public <T> void visitCtCatchVariable(final CtCatchVariable<T> catchVariable) {
enter(catchVariable);
scan(catchVariable.getComments());
scan(catchVariable.getAnnotations());
scan(catchVariable.getDefaultExpression());
scan(catchVariable.getType());
scan(catchVariable.getMultiTypes());
exit(catchVariable);
Expand Down Expand Up @@ -584,6 +585,7 @@ public <T> void visitCtLambda(final CtLambda<T> lambda) {
scan(lambda.getType());
scan(lambda.getTypeCasts());
scan(lambda.getParameters());
scan(lambda.getThrownTypes());
scan(lambda.getBody());
scan(lambda.getExpression());
scan(lambda.getComments());
Expand Down Expand Up @@ -722,6 +724,8 @@ public void visitCtTypeParameterReference(final CtTypeParameterReference ref) {
@Override
public void visitCtWildcardReference(CtWildcardReference wildcardReference) {
enter(wildcardReference);
scan(wildcardReference.getPackage());
scan(wildcardReference.getDeclaringType());
scan(wildcardReference.getAnnotations());
scan(wildcardReference.getBoundingType());
exit(wildcardReference);
Expand All @@ -730,6 +734,8 @@ public void visitCtWildcardReference(CtWildcardReference wildcardReference) {
@Override
public <T> void visitCtIntersectionTypeReference(final CtIntersectionTypeReference<T> reference) {
enter(reference);
scan(reference.getPackage());
scan(reference.getDeclaringType());
scan(reference.getAnnotations());
scan(reference.getBounds());
exit(reference);
Expand Down Expand Up @@ -796,6 +802,7 @@ public void visitCtWhile(final CtWhile whileLoop) {

public <T> void visitCtCodeSnippetExpression(final CtCodeSnippetExpression<T> expression) {
enter(expression);
scan(expression.getType());
scan(expression.getComments());
scan(expression.getAnnotations());
scan(expression.getTypeCasts());
Expand All @@ -811,6 +818,7 @@ public void visitCtCodeSnippetStatement(final CtCodeSnippetStatement statement)

public <T> void visitCtUnboundVariableReference(final CtUnboundVariableReference<T> reference) {
enter(reference);
scan(reference.getType());
exit(reference);
}

Expand Down Expand Up @@ -839,9 +847,9 @@ public <T> void visitCtFieldWrite(final CtFieldWrite<T> fieldWrite) {
@Override
public <T> void visitCtSuperAccess(final CtSuperAccess<T> f) {
enter(f);
scan(f.getType());
scan(f.getComments());
scan(f.getAnnotations());
scan(f.getType());
scan(f.getTypeCasts());
scan(f.getTarget());
scan(f.getVariable());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import spoon.reflect.reference.CtCatchVariableReference;
import spoon.reflect.reference.CtTypeReference;
import spoon.reflect.visitor.CtVisitor;
import spoon.support.DerivedProperty;
import spoon.support.UnsettableProperty;
import spoon.support.reflect.declaration.CtElementImpl;

Expand All @@ -40,7 +41,7 @@
public class CtCatchVariableImpl<T> extends CtCodeElementImpl implements CtCatchVariable<T> {
private static final long serialVersionUID = 1L;

String name;
String name = "";

CtTypeReference<T> type;

Expand All @@ -54,6 +55,7 @@ public void accept(CtVisitor visitor) {
}

@Override
@DerivedProperty
public CtExpression<T> getDefaultExpression() {
return null;
}
Expand Down
13 changes: 11 additions & 2 deletions src/main/java/spoon/support/reflect/code/CtFieldAccessImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import spoon.reflect.code.CtTargetedExpression;
import spoon.reflect.reference.CtFieldReference;

public abstract class CtFieldAccessImpl<T> extends CtVariableReadImpl<T> implements CtFieldAccess<T> {
public abstract class CtFieldAccessImpl<T> extends CtVariableAccessImpl<T> implements CtFieldAccess<T> {
private static final long serialVersionUID = 1L;

CtExpression<?> target;
Expand All @@ -43,11 +43,20 @@ public <C extends CtTargetedExpression<T, CtExpression<?>>> C setTarget(

@Override
public CtFieldReference<T> getVariable() {
return (CtFieldReference<T>) super.getVariable();
if (variable != null) {
return (CtFieldReference<T>) variable;
}
if (getFactory() != null) {
CtFieldReference<Object> fieldReference = getFactory().Core().createFieldReference();
fieldReference.setParent(this);
return (CtFieldReference<T>) fieldReference;
}
return null;
}

@Override
public CtFieldAccess<T> clone() {
return (CtFieldAccess<T>) super.clone();
}

}
2 changes: 1 addition & 1 deletion src/main/java/spoon/support/reflect/code/CtLambdaImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
import static spoon.reflect.ModelElementContainerDefaultCapacities.PARAMETERS_CONTAINER_DEFAULT_CAPACITY;

public class CtLambdaImpl<T> extends CtExpressionImpl<T> implements CtLambda<T> {
String simpleName;
String simpleName = "";
CtExpression<T> expression;
CtBlock<?> body;
List<CtParameter<?>> parameters = emptyList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class CtLocalVariableImpl<T> extends CtStatementImpl implements CtLocalVa

CtExpression<T> defaultExpression;

String name;
String name = "";

CtTypeReference<T> type;

Expand Down
Loading

0 comments on commit 69b8a8a

Please sign in to comment.