Java Code Examples for spoon.reflect.code.CtExpression#replace()

The following examples show how to use spoon.reflect.code.CtExpression#replace() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LiteralReplacer.java    From nopol with GNU General Public License v2.0 6 votes vote down vote up
private void replaceDouble(CtExpression ctElement) {
    if (getValue() == null) {
        CtLocalVariable<Double> evaluation = newLocalVariableDeclaration(
                ctElement.getFactory(), double.class, "guess_fix",
                Debug.class.getCanonicalName() + ".makeSymbolicReal(\"guess_fix\")");

        CtStatement firstStatement = getFirstStatement(ctElement);
        if (firstStatement == null) {
            return;
        }
        SpoonStatementLibrary.insertBeforeUnderSameParent(evaluation, firstStatement);
        // SpoonStatementLibrary.insertAfterUnderSameParent(getFactory().Code().createCodeSnippetStatement("System.out.println(\"guess_fix: \" + guess_fix)"),
        // getFirstStatement(ctElement));
        ctElement.replace(getFactory().Code().createCodeSnippetExpression("guess_fix"));
    } else {
        ctElement.replace(getFactory().Code().createCodeSnippetExpression(getValue()));
    }
}
 
Example 2
Source File: LiteralReplacer.java    From nopol with GNU General Public License v2.0 6 votes vote down vote up
private void replaceInteger(CtExpression ctElement) {
    if (getValue() == null) {
        CtLocalVariable<Integer> evaluation = newLocalVariableDeclaration(
                ctElement.getFactory(), int.class, "guess_fix",
                Debug.class.getCanonicalName()
                        + ".makeSymbolicInteger(\"guess_fix\")");

        CtStatement firstStatement = getFirstStatement(ctElement);
        if (firstStatement == null) {
            return;
        }
        SpoonStatementLibrary.insertBeforeUnderSameParent(evaluation, firstStatement);
        // SpoonStatementLibrary.insertAfterUnderSameParent(getFactory().Code().createCodeSnippetStatement("System.out.println(\"guess_fix: \" + guess_fix)"),
        // getFirstStatement(ctElement));

        ctElement.replace(getFactory().Code().createCodeSnippetExpression("guess_fix"));
    } else {
        ctElement.replace(getFactory().Code().createCodeSnippetExpression(getValue()));
    }
}
 
Example 3
Source File: jMutRepairEvolutionary.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Apply a given Mutation to the node referenced by the operation
 * 
 * @param operation
 * @throws IllegalAccessException
 */
@Override
protected void applyNewMutationOperationToSpoonElement(OperatorInstance operation) throws IllegalAccessException {

	boolean successful = false;
	CtExpression ctst = (CtExpression) operation.getOriginal();
	CtExpression fix = (CtExpression) operation.getModified();
	//
	try {
		ctst.replace((CtExpression) fix);
		successful = true;
		operation.setSuccessfulyApplied((successful));
	} catch (Exception ex) {
		log.error("Error applying an operation, exception: " + ex.getMessage());
		operation.setExceptionAtApplied(ex);
		operation.setSuccessfulyApplied(false);
	}

}
 
Example 4
Source File: ExpresionMutOp.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Override
public boolean applyChangesInModel(OperatorInstance operation, ProgramVariant p) {
	boolean successful = false;
	CtExpression rightTerm = null, leftTerm = null;
	try {

		CtExpression ctst = (CtExpression) operation.getOriginal();
		CtExpression fix = (CtExpression) operation.getModified();

		ctst.replace((CtExpression) fix);
		successful = true;
		operation.setSuccessfulyApplied((successful));

		log.debug(" applied: " + ctst.getParent().toString());

	} catch (Exception ex) {
		log.error("Error applying an operation, exception: " + ex.getMessage());
		operation.setExceptionAtApplied(ex);
		operation.setSuccessfulyApplied(false);
	}
	return true;
}
 
Example 5
Source File: ExpressionTransformer.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public static boolean doReplace(OperatorInstance operation) {
	boolean successful = false;

	try {

		CtExpression ctst = (CtExpression) operation.getOriginal();
		CtExpression fix = (CtExpression) operation.getModified();

		ctst.replace((CtExpression) fix);
		successful = true;
		operation.setSuccessfulyApplied((successful));

	} catch (Exception ex) {
		log.error("Error applying an operation, exception: " + ex.getMessage());
		operation.setExceptionAtApplied(ex);
		operation.setSuccessfulyApplied(false);
	}
	return successful;
}
 
Example 6
Source File: VariabletoNullMetaMutator.java    From metamutator with GNU General Public License v3.0 5 votes vote down vote up
/**
 *
 * @param expression
 * @param operators
 */
private void mutateOperator(final CtExpression expression, EnumSet<Null> operators) {
			
	if (alreadyInHotsSpot(expression)
			|| expression.toString().contains(".is(\"")) {
		System.out
		.println(String
				.format("Expression '%s' ignored because it is included in previous hot spot",
						expression));
		return;
	}		

	int thisIndex = ++index;
	
	String actualExpression = expression.toString();
	
	String newExpression = String.format("(%s%s.is(%s))?"+actualExpression+":null",PREFIX,thisIndex,"metamutator.Null.NO");
	
	CtCodeSnippetExpression codeSnippet = getFactory().Core()
			.createCodeSnippetExpression();
	codeSnippet.setValue('(' + newExpression + ')');
	
	expression.replace(codeSnippet);
	expression.replace(expression);
	
	Selector.generateSelector(expression, Null.NO, thisIndex, operators, PREFIX);

	hostSpots.add(expression);

}
 
Example 7
Source File: ConditionalReplacer.java    From nopol with GNU General Public License v2.0 5 votes vote down vote up
@Override
public CtIf processCondition(CtStatement element, String newCondition) {
    CtCodeSnippetExpression<Boolean> snippet = element.getFactory().Core().createCodeSnippetExpression();
    snippet.setValue(newCondition);
    CtExpression<Boolean> condition = getCondition(element);
    condition.replace(snippet);
    return (CtIf) element;
}
 
Example 8
Source File: LiteralReplacer.java    From nopol with GNU General Public License v2.0 5 votes vote down vote up
private void replaceBoolean(CtExpression ctElement) {
    if (getValue() == null) {
        CtLocalVariable<Boolean> evaluation = newLocalVariableDeclaration(
                ctElement.getFactory(), boolean.class, "guess_fix",
                Debug.class.getCanonicalName() + ".makeSymbolicBoolean(\"guess_fix\")");
        CtStatement firstStatement = getFirstStatement(ctElement);
        if (firstStatement == null) {
            return;
        }
        SpoonStatementLibrary.insertBeforeUnderSameParent(evaluation,
                firstStatement);
        ctElement.replace(getFactory().Code().createCodeSnippetExpression(
                "guess_fix"));
    } else {
        switch (getValue()) {
            case "1":
                ctElement.replace(getFactory().Code().createCodeSnippetExpression("true"));
                break;
            case "0":
                ctElement.replace(getFactory().Code().createCodeSnippetExpression("false"));
                break;
            default:
                ctElement.replace(getFactory().Code().createCodeSnippetExpression(getValue()));
                break;
        }
    }
}
 
Example 9
Source File: FineGrainedExpressionReplaceOperator.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@SuppressWarnings("rawtypes")
@Override
public boolean applyChangesInModel(OperatorInstance opInstance, ProgramVariant p) {

	CtExpression elementToModify = (CtExpression) opInstance.getOriginal();
	CtExpression elementOriginalCloned = (CtExpression) MutationSupporter.clone(elementToModify);

	CtElement elFixIngredient = opInstance.getModified();

	MetaGenerator.getSourceTarget().put(elementToModify, elFixIngredient);

	// MetaGenerator.targetSource.put(elementToModify, elFixIngredient);

	this.originalParent = elementToModify.getParent();
	// we transform the Spoon model
	try {
		elementToModify.replace(elFixIngredient);
	} catch (Exception e) {
		log.error("error to modify " + elementOriginalCloned + " to " + elFixIngredient);
		log.error(e);
		e.printStackTrace();
		opInstance.setExceptionAtApplied(e);
		return false;
	}
	opInstance.setOriginal(elementToModify);

	boolean change = !opInstance.getModificationPoint().getCodeElement().toString()
			.equals(elementOriginalCloned.toString());

	if (!change)
		log.error("Replacement does not work for  modify " + elementOriginalCloned + " to " + elFixIngredient);

	return true;
}
 
Example 10
Source File: jMutRepairEvolutionary.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@SuppressWarnings("rawtypes")
public void undoOperationToSpoonElement(OperatorInstance operation) {
	CtExpression ctst = (CtExpression) operation.getOriginal();
	CtExpression fix = (CtExpression) operation.getModified();
	try {
		fix.replace(ctst);
	} catch (Throwable tr) {
		operation.setExceptionAtApplied((Exception) tr);
	}
}
 
Example 11
Source File: ExpresionMutOp.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean undoChangesInModel(OperatorInstance opInstance, ProgramVariant p) {
	try {
		CtExpression ctst = (CtExpression) opInstance.getOriginal();
		CtExpression fix = (CtExpression) opInstance.getModified();
		fix.replace(ctst);

		return true;
	} catch (Exception e) {
		return false;
	}
}
 
Example 12
Source File: ExpressionTransformer.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public static boolean undoReplace(OperatorInstance operation) {
	try {
		CtExpression ctst = (CtExpression) operation.getOriginal();
		CtExpression fix = (CtExpression) operation.getModified();
		fix.replace(ctst);
		return true;

	} catch (Exception ex) {
		log.error("Error applying an operation, exception: " + ex.getMessage());
		return false;
	}

}