use of spoon.reflect.code.CtUnaryOperator in project Ex2Amplifier by STAMP-project.
the class JBSEAmplifier method generateNewTestMethod.
private CtMethod<?> generateNewTestMethod(CtMethod<?> testMethod, Map<String, List<String>> conditionForParameter) {
final CtMethod clone = AmplificationHelper.cloneTestMethodForAmp(testMethod, "_Ex2_JBSE");
final List<?> solutions = SMTSolver.solve(conditionForParameter);
final Iterator<?> iterator = solutions.iterator();
final List<CtLiteral> originalLiterals = clone.getElements(new TypeFilter<>(CtLiteral.class));
conditionForParameter.keySet().forEach(s -> {
try {
final int indexOfLit = Integer.parseInt(s.substring("param".length()));
final CtLiteral literalToBeReplaced = originalLiterals.get(indexOfLit);
final CtLiteral<?> newLiteral = testMethod.getFactory().createLiteral(iterator.next());
if (!this.intermediateAmplification.containsKey(literalToBeReplaced)) {
this.intermediateAmplification.put(literalToBeReplaced, new ArrayList<>());
}
this.intermediateAmplification.get(literalToBeReplaced).add(newLiteral);
if (literalToBeReplaced.getParent() instanceof CtUnaryOperator) {
literalToBeReplaced.getParent().replace(newLiteral);
} else {
literalToBeReplaced.replace(newLiteral);
}
} catch (Exception e) {
LOGGER.warn("Error when trying to generate a value for {}", s);
}
});
return clone;
}
use of spoon.reflect.code.CtUnaryOperator 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.CtUnaryOperator in project spoon by INRIA.
the class EqualsChecker method visitCtUnaryOperator.
@Override
public <T> void visitCtUnaryOperator(CtUnaryOperator<T> e) {
final CtUnaryOperator peek = (CtUnaryOperator) this.other;
if (e.getKind() == null) {
if (peek.getKind() != null) {
isNotEqual = true;
return;
}
} else if (peek.getKind() == null) {
isNotEqual = true;
return;
} else if (!e.getKind().equals(peek.getKind())) {
isNotEqual = true;
return;
}
super.visitCtUnaryOperator(e);
}
use of spoon.reflect.code.CtUnaryOperator in project spoon by INRIA.
the class FieldAccessTest method testIncrementationOnAVarIsAUnaryOperator.
@Test
public void testIncrementationOnAVarIsAUnaryOperator() throws Exception {
// contract: When we use var++, the variable is a read access with an unary operator.
final CtType<Panini> aMole = buildClass(Panini.class);
final CtMethod<?> make = aMole.getMethodsByName("make").get(0);
final List<CtUnaryOperator<?>> unaryOperators = make.getElements(new TypeFilter<CtUnaryOperator<?>>(CtUnaryOperator.class));
final CtFieldWrite<Object> fieldRead = aMole.getFactory().Core().createFieldWrite();
fieldRead.setTarget(aMole.getFactory().Code().createThisAccess(aMole.getReference(), true));
final CtFieldReference fieldReference = aMole.getField("i").getReference();
fieldRead.setVariable(fieldReference);
assertEquals(2, unaryOperators.size());
final CtUnaryOperator<?> first = unaryOperators.get(0);
assertEquals(UnaryOperatorKind.POSTINC, first.getKind());
assertEquals(fieldRead, first.getOperand());
assertEquals("(i)++", first.toString());
final CtUnaryOperator<?> second = unaryOperators.get(1);
assertEquals(UnaryOperatorKind.PREINC, second.getKind());
assertEquals(fieldRead, second.getOperand());
assertEquals("++(i)", second.toString());
}
use of spoon.reflect.code.CtUnaryOperator in project Ex2Amplifier by STAMP-project.
the class ArgumentsExtractor method performExtraction.
public static CtMethod<?> performExtraction(CtMethod<?> ctMethod) {
final CtMethod<?> ctMethodWithoutAssertion = new AssertionRemover().removeAssertion(ctMethod);
final Factory factory = ctMethodWithoutAssertion.getFactory();
final CtMethod<?> extractedMethod = ctMethodWithoutAssertion.clone();
extractedMethod.setSimpleName("extract_" + ctMethodWithoutAssertion.getSimpleName());
new ArrayList<>(extractedMethod.getThrownTypes()).forEach(extractedMethod::removeThrownType);
ctMethodWithoutAssertion.getAnnotations().forEach(extractedMethod::removeAnnotation);
final int[] count = new int[1];
final Map<CtAbstractInvocation<?>, List<CtVariableAccess>> parametersPerInvocation = new HashMap<>();
extractedMethod.getElements(Ex2Amplifier.CT_LITERAL_TYPE_FILTER).stream().filter(literal -> !(literal.getValue() instanceof String)).forEach(ctLiteral -> {
final CtParameter parameter = factory.createParameter(extractedMethod, Utils.getRealTypeOfLiteral(ctLiteral), "lit" + count[0]++);
final CtParameterReference<?> parameterReference = factory.createParameterReference();
parameterReference.setSimpleName(parameter.getSimpleName());
parameterReference.setType(parameter.getType());
final CtVariableAccess<?> variableRead = factory.createVariableRead(parameterReference, false);
final CtAbstractInvocation invocation = ctLiteral.getParent(CtAbstractInvocation.class);
if (invocation != null) {
if (!parametersPerInvocation.containsKey(invocation)) {
parametersPerInvocation.put(invocation, new ArrayList<>(invocation.getArguments()));
}
if (ctLiteral.getParent() instanceof CtUnaryOperator) {
ctLiteral.getParent().replace(variableRead);
} else {
ctLiteral.replace(variableRead);
}
} else {
ctLiteral.replace(variableRead);
}
});
extractedMethod.setThrownTypes(ctMethod.getThrownTypes());
return extractedMethod;
}
Aggregations