Search in sources :

Example 1 with CtUnaryOperator

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;
}
Also used : CtLiteral(spoon.reflect.code.CtLiteral) CtUnaryOperator(spoon.reflect.code.CtUnaryOperator) CtMethod(spoon.reflect.declaration.CtMethod)

Example 2 with CtUnaryOperator

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);
}
Also used : CtLiteral(spoon.reflect.code.CtLiteral) CtExpression(spoon.reflect.code.CtExpression) CtStatement(spoon.reflect.code.CtStatement) CtTypeReference(spoon.reflect.reference.CtTypeReference) CtElement(spoon.reflect.declaration.CtElement) Factory(spoon.reflect.factory.Factory) CtUnaryOperator(spoon.reflect.code.CtUnaryOperator) CtStatementList(spoon.reflect.code.CtStatementList) CtLocalVariable(spoon.reflect.code.CtLocalVariable) CtVariableRead(spoon.reflect.code.CtVariableRead)

Example 3 with CtUnaryOperator

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);
}
Also used : CtUnaryOperator(spoon.reflect.code.CtUnaryOperator)

Example 4 with CtUnaryOperator

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());
}
Also used : Panini(spoon.test.fieldaccesses.testclasses.Panini) CtUnaryOperator(spoon.reflect.code.CtUnaryOperator) CtFieldReference(spoon.reflect.reference.CtFieldReference) Test(org.junit.Test)

Example 5 with CtUnaryOperator

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;
}
Also used : Ex2Amplifier(eu.stamp.project.ex2amplifier.amplifier.Ex2Amplifier) Utils(eu.stamp.project.ex2amplifier.Utils) CtParameterReference(spoon.reflect.reference.CtParameterReference) HashMap(java.util.HashMap) Factory(spoon.reflect.factory.Factory) CtUnaryOperator(spoon.reflect.code.CtUnaryOperator) ArrayList(java.util.ArrayList) List(java.util.List) AssertionRemover(fr.inria.diversify.dspot.assertGenerator.AssertionRemover) Map(java.util.Map) CtAbstractInvocation(spoon.reflect.code.CtAbstractInvocation) CtVariableAccess(spoon.reflect.code.CtVariableAccess) CtParameter(spoon.reflect.declaration.CtParameter) CtMethod(spoon.reflect.declaration.CtMethod) HashMap(java.util.HashMap) CtAbstractInvocation(spoon.reflect.code.CtAbstractInvocation) Factory(spoon.reflect.factory.Factory) CtParameter(spoon.reflect.declaration.CtParameter) AssertionRemover(fr.inria.diversify.dspot.assertGenerator.AssertionRemover) CtUnaryOperator(spoon.reflect.code.CtUnaryOperator) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

CtUnaryOperator (spoon.reflect.code.CtUnaryOperator)5 CtLiteral (spoon.reflect.code.CtLiteral)2 CtMethod (spoon.reflect.declaration.CtMethod)2 Factory (spoon.reflect.factory.Factory)2 Utils (eu.stamp.project.ex2amplifier.Utils)1 Ex2Amplifier (eu.stamp.project.ex2amplifier.amplifier.Ex2Amplifier)1 AssertionRemover (fr.inria.diversify.dspot.assertGenerator.AssertionRemover)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Test (org.junit.Test)1 CtAbstractInvocation (spoon.reflect.code.CtAbstractInvocation)1 CtExpression (spoon.reflect.code.CtExpression)1 CtLocalVariable (spoon.reflect.code.CtLocalVariable)1 CtStatement (spoon.reflect.code.CtStatement)1 CtStatementList (spoon.reflect.code.CtStatementList)1 CtVariableAccess (spoon.reflect.code.CtVariableAccess)1 CtVariableRead (spoon.reflect.code.CtVariableRead)1 CtElement (spoon.reflect.declaration.CtElement)1