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; } }
@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; } }
@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); } }
@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; }
@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(); // 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; }
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; }
/** * 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); } }
@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; } }
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())); } }
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())); } }
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; } } } }
@Override public void process(CtStatement element) { String oldStatement = element.toString(); CtCodeSnippetExpression<Boolean> snippet = null; if (getValue() != null) { if (getValue().equals("1")) { snippet = element.getFactory().Code() .createCodeSnippetExpression("true"); } else if (getValue().equals("0")) { snippet = element.getFactory().Code() .createCodeSnippetExpression("false"); } else { snippet = element.getFactory().Code() .createCodeSnippetExpression(getValue()); } } else { snippet = element .getFactory() .Code() .createCodeSnippetExpression( Debug.class.getCanonicalName() + ".makeSymbolicBoolean(\"guess_fix\")"); } CtExpression<Boolean> condition = getCondition(element); condition.replace(snippet); logger.debug("Replacing:\n{}\nby:\n{}", oldStatement, element); }
toReplace.replace(factory.Code() .createClassAccess(factory.Type().createReference(((Class<?>) value).getName()))); } else if (value instanceof Enum) { CtTypeReference<?> enumType = factory.Type().createReference(value.getClass()); toReplace.replace(factory.Code().createVariableRead( factory.Field().createReference(enumType, enumType, ((Enum<?>) value).name()), true)); } else if (value instanceof List) { toReplace.replace(factory.Code().createLiteralArray((Object[]) value)); } else { toReplace.replace(factory.Code().createLiteral(value));