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

The following examples show how to use spoon.reflect.code.CtExpression#getParent() . 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 5 votes vote down vote up
private CtStatement getFirstStatement(CtExpression ctElement) {
    CtElement ctParent = ctElement.getParent();
    while (!(ctParent instanceof CtStatement) && ctParent != null) {
        ctParent = ctParent.getParent();
    }
    if (ctParent == null) {
        return null;
    }
    return (CtStatement) ctParent;
}
 
Example 2
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;
}