@java.lang.Override public void set(spoon.reflect.code.CtStatement replace) { this.element.setThenStatement(replace); } }
@Override public <T, U> void setValue(T element, U value) { castTarget(element).setThenStatement(castValue(value)); } }
@Override public void visitCtIf(CtIf e) { super.visitCtIf(e); boolean inThen = true; CtStatement stat = e.getThenStatement(); if (stat != target) { stat = e.getElseStatement(); inThen = false; } if (stat != target) { throw new IllegalArgumentException("should not happen"); } if (stat instanceof CtBlock) { insertType.insert((CtBlock<?>) stat, statementsToBeInserted); } else { CtBlock<?> block = insertNewBlock(stat); if (inThen) { e.setThenStatement(block); } else { e.setElseStatement(block); } } }
@Override public <T> void generateInlineTargets(DefaultGenerator generator, ResultHolder<T> result, ImmutableMap parameters) { Factory f = generator.getFactory(); CoreFactory cf = f.Core(); CtBlock<?> block = cf.createBlock(); if (statement != null) { block.setStatements(generator.generateTargets(statement, parameters, CtStatement.class)); } if (vrOfExpression != null) { //There is if expression CtIf ifStmt = cf.createIf(); ifStmt.setCondition(generator.generateSingleTarget(vrOfExpression, parameters, CtExpression.class)); ifStmt.setThenStatement(block); result.addResult((T) ifStmt); } else { //There is no expression. It represents the last else block result.addResult((T) block); } } }
@Override public void visitCtIf(CtIf ifElement) { if (ifElement.getCondition() == null && child instanceof CtExpression) { ifElement.setCondition((CtExpression<Boolean>) child); return; } else if (child instanceof CtStatement) { CtStatement child = (CtStatement) this.child; // we create implicit blocks everywhere for facilitating transformation if (!(this.child instanceof CtBlock)) { child = jdtTreeBuilder.getFactory().Code().createCtBlock(child); child.setImplicit(true); child.setPosition(this.child.getPosition()); } IfStatement ifJDT = (IfStatement) this.parentJDT; if (ifJDT.thenStatement == this.childJDT) { //we are visiting `then` of `if` ifElement.setThenStatement(child); return; } else if (ifJDT.elseStatement == this.childJDT) { //we are visiting `else` of `if` ifElement.setElseStatement(child); return; } else { throw new SpoonException("Unexpected call of ParentExiter on CtIf"); } } super.visitCtIf(ifElement); }
public void visitCtIf(final spoon.reflect.code.CtIf ifElement) { spoon.reflect.code.CtIf aCtIf = ifElement.getFactory().Core().createIf(); this.builder.copy(ifElement, aCtIf); aCtIf.setAnnotations(this.cloneHelper.clone(ifElement.getAnnotations())); aCtIf.setCondition(this.cloneHelper.clone(ifElement.getCondition())); aCtIf.setThenStatement(this.cloneHelper.clone(((spoon.reflect.code.CtStatement) (ifElement.getThenStatement())))); aCtIf.setElseStatement(this.cloneHelper.clone(((spoon.reflect.code.CtStatement) (ifElement.getElseStatement())))); aCtIf.setComments(this.cloneHelper.clone(ifElement.getComments())); this.cloneHelper.tailor(ifElement, aCtIf); this.other = aCtIf; }
boolean thenEnded = false; boolean elseEnded = false; ifRes.setThenStatement((CtStatement) evaluate(ifElement.getThenStatement())); if (flowEnded) { thenEnded = true;
@SuppressWarnings({ "rawtypes", "unchecked" }) @Override public List<OperatorInstance> createOperatorInstances(ModificationPoint modificationPoint, Ingredient ingredient, IngredientTransformationStrategy transformationStrategy) { List<OperatorInstance> instances = new ArrayList<>(); List<Ingredient> ingredientsTransformed = transformationStrategy.transform(modificationPoint, ingredient); for (Ingredient iIngredient : ingredientsTransformed) { CtExpression condition = (CtExpression) iIngredient.getCode(); CtIf precondition = MutationSupporter.factory.createIf(); precondition.setCondition(condition); precondition.setThenStatement((CtStatement) modificationPoint.getCodeElement().clone()); OperatorInstance operatorInstance = new StatementOperatorInstance(modificationPoint, this, precondition); instances.add(operatorInstance); } return instances; }
public static CtIf newIf(Factory factory, CtExpression<Boolean> condition, CtStatement thenBranch) { CtIf newIf = factory.Core().createIf(); thenBranch = asBlock(thenBranch, newIf); setParent(newIf, condition, thenBranch); newIf.setCondition(condition); newIf.setThenStatement(thenBranch); return newIf; }
@Override public OperatorInstance getConcreteOperatorInstance(MetaOperatorInstance operatorInstance, int metaIdentifier) { // We retrieve the information from the operator instance Ingredient ingredient = operatorInstance.getAllIngredients().get(metaIdentifier); ModificationPoint modificationPoint = operatorInstance.getModificationPoint(); // We create the new operator CtIf ifNew = MutationSupporter.getFactory().createIf(); CtStatement statementPointed = (CtStatement) modificationPoint.getCodeElement(); CtStatement statementPointedCloned = statementPointed.clone(); MutationSupporter.clearPosition(statementPointedCloned); ifNew.setThenStatement(statementPointedCloned); // as difference with the meta, here we put the ingredient evaluated in the // meta. ifNew.setCondition((CtExpression<Boolean>) ingredient.getCode()); // Let's create the operations List<OperatorInstance> opsOfVariant = new ArrayList(); OperatorInstance opInstace = new StatementOperatorInstance(modificationPoint, this, statementPointed, ifNew); opsOfVariant.add(opInstace); return opInstace; }
@Override public OperatorInstance getConcreteOperatorInstance(MetaOperatorInstance operatorInstance, int metaIdentifier) { // We retrieve the information from the operator instance Ingredient ingredient = operatorInstance.getAllIngredients().get(metaIdentifier); ModificationPoint modificationPoint = operatorInstance.getModificationPoint(); // We create the new operator CtIf ifNew = MutationSupporter.getFactory().createIf(); CtStatement statementPointed = (CtStatement) modificationPoint.getCodeElement(); CtStatement statementPointedCloned = statementPointed.clone(); MutationSupporter.clearPosition(statementPointedCloned); ifNew.setThenStatement(statementPointedCloned); // as difference with the meta, here we put the ingredient evaluated in the // meta. ifNew.setCondition((CtExpression<Boolean>) ingredient.getCode()); // Let's create the operations List<OperatorInstance> opsOfVariant = new ArrayList(); OperatorInstance opInstace = new StatementOperatorInstance(modificationPoint, this/* new ReplaceOp() */, statementPointed, ifNew); opsOfVariant.add(opInstace); return opInstace; }
@Override public void visitCtIf(CtIf ifElement) { if (ifElement.getCondition() == null && child instanceof CtExpression) { ifElement.setCondition((CtExpression<Boolean>) child); return; } else if (child instanceof CtStatement) { if (ifElement.getThenStatement() == null) { ifElement.setThenStatement((CtStatement) child); return; } else if (ifElement.getElseStatement() == null) { ifElement.setElseStatement((CtStatement) child); return; } } super.visitCtIf(ifElement); }
@SuppressWarnings({ "rawtypes", "unchecked" }) @Override public List<OperatorInstance> createOperatorInstances(ModificationPoint modificationPoint) { List<OperatorInstance> instances = new ArrayList<>(); CtInvocation invocation = (CtInvocation) modificationPoint.getCodeElement(); CtExpression targetExpresionOfMI = invocation.getTarget(); if (targetExpresionOfMI instanceof CtVariableAccess) { CtStatement parentStatement = modificationPoint.getCodeElement().getParent(CtStatement.class); CtExpression ifcondition = MutationSupporter.factory.createBinaryOperator(targetExpresionOfMI, MutationSupporter.factory.createCodeSnippetExpression("null").compile(), BinaryOperatorKind.NE); CtIf precodition = MutationSupporter.factory.createIf(); precodition.setCondition(ifcondition); precodition.setThenStatement(parentStatement); OperatorInstance operatorInstance = new StatementOperatorInstance(modificationPoint, this, parentStatement, precodition); instances.add(operatorInstance); } return instances; };
@SuppressWarnings({ "rawtypes", "unchecked" }) @Override public List<OperatorInstance> createOperatorInstances(ModificationPoint modificationPoint) { List<OperatorInstance> instances = new ArrayList<>(); List<CtVariableAccess> varAccess = VariableResolver.collectVariableAccess(modificationPoint.getCodeElement()); for (CtVariableAccess iVariableAccess : varAccess) { if (!iVariableAccess.getType().isPrimitive()) { CtIf precodition = MutationSupporter.factory.createIf(); CtExpression ifcondition = MutationSupporter.factory.createBinaryOperator(iVariableAccess, MutationSupporter.factory.createCodeSnippetExpression("null").compile(), BinaryOperatorKind.NE); precodition.setCondition(ifcondition); precodition.setThenStatement((CtStatement) modificationPoint.getCodeElement().clone()); OperatorInstance operatorInstance = new StatementOperatorInstance(modificationPoint, this, precodition); instances.add(operatorInstance); } } return instances; };
/** * replace assertNotNull * * @param ctInvocation * @return */ private CtStatement replaceAssertNotNull(CtInvocation<?> ctInvocation) { List<?> arguments = ctInvocation.getArguments(); CtIf newIf = ctInvocation.getFactory().Core().createIf(); Object elem1 = arguments.get(0); CtExpression<Boolean> condition = ctInvocation.getFactory().Code() .createCodeSnippetExpression("(" + elem1 + ") != null"); newIf.setCondition(condition); // newIf.setThenStatement(getFactory().Code().createCodeSnippetStatement( // Debug.class.getCanonicalName() + ".printPC(\"Path Condition: \")")); /* * CtBlock<Object> thenStatement = ctInvocation.getFactory().Core() * .createBlock(); thenStatement .addStatement((getFactory().Code() * .createCodeSnippetStatement("System.out.println(\"Then...\")"))); * thenStatement.addStatement((getFactory().Code() * .createCodeSnippetStatement("System.out.println(" + * Debug.class.getCanonicalName() + ".getSolvedPC())"))); * newIf.setThenStatement(thenStatement); */ newIf.setThenStatement(createThen(ctInvocation)); return newIf; }
ifstmt.setThenStatement(returnstmt);
@SuppressWarnings({ "static-access", "unchecked", "rawtypes" }) private void writeCondition(String type, CtBlock parent, int id) { CtExpression conditionExp = ExpressionGenerator.fetchEXP(this.mutSupporter, this.modificationPoint, type, querytype); CtIf ifStmt = this.mutSupporter.getFactory().Core().createIf(); ifStmt.setCondition(conditionExp); ifStmt.setThenStatement((CtStatement)this.modificationPoint.getCodeElement()); parent.addStatement(id, ifStmt); parent.removeStatement((CtStatement)this.modificationPoint.getCodeElement()); saveSketchAndSynthesize(); parent.addStatement(id, (CtStatement)this.modificationPoint.getCodeElement()); parent.removeStatement(ifStmt); resoreDiskFile(); }
@Override public void visitCtIf(CtIf element) { super.visitCtIf(element); if(!(element.getThenStatement() instanceof CtBlock)){ CtStatement c = element.getThenStatement() ; CtBlock nBlock = MutationSupporter.getFactory().Core().createBlock(); nBlock.addStatement(c); element.setThenStatement(nBlock); } if( element.getElseStatement() != null && !(element.getElseStatement() instanceof CtBlock)){ CtStatement c = element.getElseStatement() ; CtBlock nBlock = MutationSupporter.getFactory().Core().createBlock(); nBlock.addStatement(c); element.setElseStatement(nBlock); } }
@Override public CtIf processCondition(CtStatement element, String newCondition) { //logger.debug("##### {} ##### Before:\n{}", element, element.getParent()); // if the element is not a line if (!new LineFilter().matches(element)) { element = element.getParent(new LineFilter()); } CtElement parent = element.getParent(); CtIf newIf = element.getFactory().Core().createIf(); CtCodeSnippetExpression<Boolean> condition = element.getFactory().Core().createCodeSnippetExpression(); condition.setValue(newCondition); newIf.setCondition(condition); // Fix : warning: ignoring inconsistent parent for [CtElem1] ( [CtElem2] != [CtElem3] ) newIf.setParent(parent); element.replace(newIf); // this should be after the replace to avoid an StackOverflowException caused by the circular reference. newIf.setThenStatement(element); //logger.debug("##### {} ##### After:\n{}", element, element.getParent().getParent()); return newIf; } }
private <T> void addGuardingPrologue(CtMethod<T> proxy) { if (noArgInitStageMethod != null) { CtIf ctIf = createNotInitIf(); ctIf.setThenStatement(f().Code().createInvocation(thisAccess(), noArgInitStageMethod.getReference())); proxy.getBody().addStatement(ctIf); } else { CtAssert<String> ctAssert = f().Core().createAssert(); ctAssert.setAssertExpression(createStageInitInvocation()); ctAssert.setExpression(f().Code().createLiteral(name + " should be init")); proxy.getBody().addStatement(ctAssert); } }