use of spoon.reflect.code.CtVariableRead in project spoon by INRIA.
the class ReplaceTest method testReplaceAParameterReferenceToFieldReference.
@Test
public void testReplaceAParameterReferenceToFieldReference() throws Exception {
// contract: replace a parameter reference to a field reference.
final Factory factory = build(Tacos.class);
final CtType<Tacos> aTacos = factory.Type().get(Tacos.class);
final CtInvocation inv = aTacos.getMethodsByName("m3").get(0).getElements(new TypeFilter<>(CtInvocation.class)).get(0);
final CtVariableRead<?> variableRead = (CtVariableRead<?>) inv.getArguments().get(0);
final CtParameterReference<?> aParameterReference = (CtParameterReference<?>) variableRead.getVariable();
final CtFieldReference<?> aFieldReference = aTacos.getField("field").getReference();
assertEquals(aParameterReference, variableRead.getVariable());
assertEquals("java.lang.System.err.println(param)", inv.toString());
aParameterReference.replace(aFieldReference);
assertEquals(aFieldReference, variableRead.getVariable());
assertEquals("java.lang.System.err.println(field)", inv.toString());
}
use of spoon.reflect.code.CtVariableRead in project spoon by INRIA.
the class ParentTest method testParentOfCtVariableReference.
@Test
public void testParentOfCtVariableReference() throws Exception {
// contract: parent of a variable reference is the element which call getVariable().
final Factory factory = build(Tacos.class);
final CtType<Tacos> aTacos = factory.Type().get(Tacos.class);
final CtInvocation inv = aTacos.getMethodsByName("m3").get(0).getElements(new TypeFilter<>(CtInvocation.class)).get(0);
final CtVariableRead<?> variableRead = (CtVariableRead<?>) inv.getArguments().get(0);
final CtParameterReference<?> aParameterReference = (CtParameterReference<?>) variableRead.getVariable();
assertNotNull(aParameterReference.getParent());
assertEquals(variableRead, aParameterReference.getParent());
}
use of spoon.reflect.code.CtVariableRead in project spoon by INRIA.
the class AccessTest method testCanVisitVariableAccessAndSubClasses.
@Test
public void testCanVisitVariableAccessAndSubClasses() throws Exception {
final Factory factory = build(VariableAccessSample.class);
final List<CtVariableRead<?>> variablesRead = Query.getElements(factory, new AbstractFilter<CtVariableRead<?>>(CtVariableRead.class) {
@Override
public boolean matches(CtVariableRead<?> element) {
return super.matches(element);
}
});
// System.out and s
assertEquals(2, variablesRead.size());
final List<CtVariableWrite<?>> variablesWrite = Query.getElements(factory, new AbstractFilter<CtVariableWrite<?>>(CtVariableWrite.class) {
@Override
public boolean matches(CtVariableWrite<?> element) {
return super.matches(element);
}
});
assertEquals(1, variablesWrite.size());
final List<CtVariableAccess<?>> variablesAccess = Query.getElements(factory, new AbstractFilter<CtVariableAccess<?>>(CtVariableAccess.class) {
@Override
public boolean matches(CtVariableAccess<?> element) {
return super.matches(element);
}
});
assertEquals(3, variablesAccess.size());
}
use of spoon.reflect.code.CtVariableRead in project dspot by STAMP-project.
the class AssertionRemover method removeAssertion.
/**
* Replaces an invocation with its arguments.
*
* @param invocation Invocation
*/
public void removeAssertion(CtInvocation<?> invocation) {
final Factory factory = invocation.getFactory();
invocation.getArguments().forEach(argument -> {
CtExpression clone = ((CtExpression) argument).clone();
if (clone instanceof CtUnaryOperator) {
clone = ((CtUnaryOperator) clone).getOperand();
}
if (clone instanceof CtStatement) {
clone.getTypeCasts().clear();
invocation.insertBefore((CtStatement) clone);
} else if (!(clone instanceof CtLiteral || clone instanceof CtVariableRead)) {
CtTypeReference<?> typeOfParameter = clone.getType();
if (clone.getType().equals(factory.Type().NULL_TYPE)) {
typeOfParameter = factory.Type().createReference(Object.class);
}
final CtLocalVariable localVariable = factory.createLocalVariable(typeOfParameter, typeOfParameter.getSimpleName() + "_" + counter[0]++, clone);
invocation.insertBefore(localVariable);
}
});
CtElement currentParent = invocation;
while (!(currentParent.getParent() instanceof CtStatementList)) {
currentParent = currentParent.getParent();
}
((CtStatementList) currentParent.getParent()).removeStatement((CtStatement) currentParent);
}
use of spoon.reflect.code.CtVariableRead in project dspot by STAMP-project.
the class GeneralMinimizer method removeUselessDuplicateAssertions.
private void removeUselessDuplicateAssertions(CtMethod<?> amplifiedTestToBeMinimized, CtInvocation<?> duplicatesAssertion, List<CtStatement> statements) {
final CtVariableReference variable = ((CtVariableRead<?>) duplicatesAssertion.filterChildren(new TypeFilter<CtVariableRead<?>>(CtVariableRead.class)).first()).getVariable();
boolean canBeRemoved = true;
for (int i = statements.indexOf(duplicatesAssertion) + 1; i < statements.lastIndexOf(duplicatesAssertion); i++) {
if (!AmplificationChecker.isAssert(statements.get(i))) {
final CtVariableRead<?> first = (CtVariableRead<?>) statements.get(i).filterChildren(new TypeFilter<CtVariableRead<?>>(CtVariableRead.class) {
@Override
public boolean matches(CtVariableRead<?> element) {
return element.getVariable().equals(variable);
}
}).first();
if (first != null) {
canBeRemoved = false;
break;
}
}
}
if (canBeRemoved) {
amplifiedTestToBeMinimized.getBody().getStatements().remove(statements.lastIndexOf(duplicatesAssertion));
}
}
Aggregations