Search in sources :

Example 1 with ObjectLog

use of fr.inria.diversify.compare.ObjectLog in project dspot by STAMP-project.

the class AssertGeneratorHelper method addLogStmt.

// This method will add a log statement at the given statement AND at the end of the test.
@SuppressWarnings("unchecked")
private static void addLogStmt(CtStatement stmt, String id) {
    if (stmt instanceof CtLocalVariable && ((CtLocalVariable) stmt).getDefaultExpression() == null) {
        return;
    }
    final CtTypeAccess<ObjectLog> typeAccess = stmt.getFactory().createTypeAccess(stmt.getFactory().Type().createReference(ObjectLog.class));
    final CtExecutableReference objectLogExecRef = stmt.getFactory().createExecutableReference().setStatic(true).setDeclaringType(stmt.getFactory().Type().createReference(ObjectLog.class)).setSimpleName("log");
    objectLogExecRef.setType(stmt.getFactory().Type().voidPrimitiveType());
    final CtInvocation invocationToObjectLog = stmt.getFactory().createInvocation(typeAccess, objectLogExecRef);
    CtStatement insertAfter;
    if (stmt instanceof CtVariableWrite) {
        // TODO
        CtVariableWrite varWrite = (CtVariableWrite) stmt;
        insertAfter = stmt;
    } else if (stmt instanceof CtLocalVariable) {
        CtLocalVariable localVar = (CtLocalVariable) stmt;
        // TODO checks static
        final CtVariableAccess variableRead = stmt.getFactory().createVariableRead(localVar.getReference(), false);
        invocationToObjectLog.addArgument(variableRead);
        invocationToObjectLog.addArgument(stmt.getFactory().createLiteral(localVar.getSimpleName()));
        insertAfter = stmt;
    } else if (stmt instanceof CtAssignment) {
        CtAssignment localVar = (CtAssignment) stmt;
        invocationToObjectLog.addArgument(localVar.getAssigned());
        invocationToObjectLog.addArgument(stmt.getFactory().createLiteral(localVar.getAssigned().toString()));
        insertAfter = stmt;
    } else if (stmt instanceof CtInvocation) {
        CtInvocation invocation = (CtInvocation) stmt;
        if (isVoidReturn(invocation)) {
            invocationToObjectLog.addArgument(invocation.getTarget());
            invocationToObjectLog.addArgument(stmt.getFactory().createLiteral(invocation.getTarget().toString().replace("\"", "\\\"")));
            insertAfter = invocation;
        } else {
            final CtLocalVariable localVariable = stmt.getFactory().createLocalVariable(invocation.getType(), "o_" + id, invocation.clone());
            try {
                stmt.replace(localVariable);
            } catch (ClassCastException e) {
                throw new RuntimeException(e);
            }
            invocationToObjectLog.addArgument(stmt.getFactory().createVariableRead(localVariable.getReference(), false));
            invocationToObjectLog.addArgument(stmt.getFactory().createLiteral("o_" + id));
            insertAfter = localVariable;
        }
    } else {
        throw new RuntimeException("Could not find the proper type to add log statement" + stmt.toString());
    }
    // clone the statement invocation for add it to the end of the tests
    CtInvocation invocationToObjectLogAtTheEnd = invocationToObjectLog.clone();
    invocationToObjectLogAtTheEnd.addArgument(stmt.getFactory().createLiteral(id + "___" + "end"));
    invocationToObjectLog.addArgument(stmt.getFactory().createLiteral(id));
    // TODO checks this if this condition is ok.
    if (getSize(stmt.getParent(CtMethod.class).getBody()) + 1 < 65535) {
        insertAfter.insertAfter(invocationToObjectLog);
    }
    // if between the two log statements there is only log statement, we do not add the log end statement
    if (shouldAddLogEndStatement.test(invocationToObjectLog) && getSize(stmt.getParent(CtMethod.class).getBody()) + 1 < 65535) {
        stmt.getParent(CtBlock.class).insertEnd(invocationToObjectLogAtTheEnd);
    }
}
Also used : ObjectLog(fr.inria.diversify.compare.ObjectLog) CtExecutableReference(spoon.reflect.reference.CtExecutableReference) CtMethod(spoon.reflect.declaration.CtMethod)

Aggregations

ObjectLog (fr.inria.diversify.compare.ObjectLog)1 CtMethod (spoon.reflect.declaration.CtMethod)1 CtExecutableReference (spoon.reflect.reference.CtExecutableReference)1