static List<? extends CtElement> bodyToStatements(CtStatement statementOrBlock) { if (statementOrBlock instanceof CtBlock) { return ((CtBlock<?>) statementOrBlock).getStatements(); } return Collections.singletonList(statementOrBlock); }
@Override public <R> void visitCtBlock(CtBlock<R> block) { if (child instanceof CtStatement) { block.addStatement((CtStatement) child); return; } super.visitCtBlock(block); }
ASTPair pair = context.stack.peek(); CtBlock<?> block = (CtBlock<?>) pair.element; if (block.getStatements().size() == 1) { CtStatement childStmt = block.getStatement(0); if (childStmt.getLabel() == null) { childStmt.setLabel(block.getLabel()); SourcePosition oldPos = childStmt.getPosition(); int newSourceStart = Math.min(oldPos.getSourceStart(), block.getPosition().getSourceStart()); if (newSourceStart != oldPos.getSourceStart()) { childStmt.setPosition(block.getFactory().Core().createSourcePosition( oldPos.getCompilationUnit(), newSourceStart, oldPos.getSourceEnd(), CtElement parent = block.getParent(); block.replace(childStmt);
private CtStatement getElseIfStatement(CtStatement elseStmt) { if (elseStmt instanceof CtBlock<?>) { CtBlock<?> block = (CtBlock<?>) elseStmt; if (block.isImplicit()) { List<CtStatement> stmts = block.getStatements(); if (stmts.size() == 1) { if (stmts.get(0) instanceof CtIf) { return stmts.get(0); } } } } return elseStmt; }
@Override void insertFromFirstStatement(CtBlock<?> block, CtStatement target, CtStatementList statementsToBeInserted) { final List<CtStatement> copy = new ArrayList<>(block.getStatements()); int indexOfTargetElement = indexOfReference(block.getStatements(), target); for (CtStatement ctStatement : statementsToBeInserted) { copy.add(indexOfTargetElement++, ctStatement); } //remove statements from the `statementsToBeInserted` before they are added to spoon model //note: one element MUST NOT be part of two models. statementsToBeInserted.setStatements(null); block.setStatements(copy); }
public <R> void visitCtBlock(final spoon.reflect.code.CtBlock<R> block) { spoon.reflect.code.CtBlock other = ((spoon.reflect.code.CtBlock) (this.stack.peek())); enter(block); biScan(spoon.reflect.path.CtRole.ANNOTATION, block.getAnnotations(), other.getAnnotations()); biScan(spoon.reflect.path.CtRole.STATEMENT, block.getStatements(), other.getStatements()); biScan(spoon.reflect.path.CtRole.COMMENT, block.getComments(), other.getComments()); exit(block); }
@Override public <R> void visitCtBlock(CtBlock<R> block) { CtBlock<?> b = block.getFactory().Core().createBlock(); for (CtStatement s : block.getStatements()) { CtElement res = evaluate(s); if (res != null) { if (res instanceof CtStatement) { b.addStatement((CtStatement) res); } else { //the context expects statement. We cannot simplify in this case b.addStatement(s.clone()); } } // do not copy unreachable statements if (flowEnded) { break; } } setResult(b); }
public <R> void visitCtBlock(final spoon.reflect.code.CtBlock<R> block) { spoon.reflect.code.CtBlock<R> aCtBlock = block.getFactory().Core().createBlock(); this.builder.copy(block, aCtBlock); aCtBlock.setAnnotations(this.cloneHelper.clone(block.getAnnotations())); aCtBlock.setStatements(this.cloneHelper.clone(block.getStatements())); aCtBlock.setComments(this.cloneHelper.clone(block.getComments())); this.cloneHelper.tailor(block, aCtBlock); this.other = aCtBlock; }
private void removeRedundantAssertions(CtMethod<?> amplifiedTestToBeMinimized) { amplifiedTestToBeMinimized.getElements(new TypeFilter<>(CtBlock.class)) .forEach(block -> { final List<CtInvocation<?>> assertions = block.getElements(new TypeFilter<CtInvocation<?>>(CtInvocation.class) { @Override public boolean matches(CtInvocation<?> element) { return element.getParent(CtBlock.class).equals(block) && TestFramework.get().isAssert(element); } }); final List<CtInvocation<?>> duplicatesAssertions = findDuplicates(assertions); // One of them might be removed final List<CtStatement> statements = block.getStatements(); duplicatesAssertions.forEach(duplicatesAssertion -> { DSpotUtils.printProgress(duplicatesAssertions.indexOf(duplicatesAssertion), duplicatesAssertions.size()); removeUselessDuplicateAssertions( block, duplicatesAssertion, statements ); }); }); }
static void addTryCatchFailAssertion(Launcher spoon, CtMethod<?> testCaseToBeFix, Failure failure) { final Factory factory = spoon.getFactory(); final CtTry aTry = factory.createTry(); final CtCatch aCatch = factory.createCatch(); aTry.addCatcher(aCatch); String fullQualifiedNameException = failure.fullQualifiedNameOfException; CtTypeReference reference = testCaseToBeFix.getFactory().Type().createReference(fullQualifiedNameException); final String[] splittedNameException = fullQualifiedNameException.split("\\."); String exceptionName = splittedNameException[splittedNameException.length - 1]; if (isAnonymous.test(failure.fullQualifiedNameOfException)) { reference = testCaseToBeFix.getFactory().Type().createReference(fullQualifiedNameException).getSuperclass(); } final CtCatchVariable<? extends Throwable> catchVariable = testCaseToBeFix.getFactory().createCatchVariable( reference, PREFIX_NAME_EXPECTED_EXCEPTION + exceptionName ); aCatch.setParameter(catchVariable); aCatch.setBody(factory.createCodeSnippetStatement( "org.junit.Assert.assertTrue(true)" )); aTry.setBody(testCaseToBeFix.getBody().getStatement(0)); for (int i = 1; i < testCaseToBeFix.getBody().getStatements().size(); i++) { aTry.getBody().addStatement(testCaseToBeFix.getBody().getStatement(i)); } aTry.getBody().addStatement(factory .createCodeSnippetStatement("org.junit.Assert." + NAME_FAIL_METHOD + "(\"" + PREFIX_MESSAGE_EXPECTED_EXCEPTION + exceptionName + "\")") ); testCaseToBeFix.setBody(aTry); }
((CtBlock<?>) stat).insertBegin(statementsToBeInserted); return; } else { CtBlock<?> block = target.getFactory().Core().createBlock(); target.setParent(block); block.addStatement(stat); if (inThen) { ((CtIf) targetParent).setThenStatement(block); if (stat instanceof CtBlock) { parentBlock = (CtBlock<?>) stat; ((CtBlock<?>) stat).insertBegin(statementsToBeInserted); return; } else { CtBlock<?> block = target.getFactory().Core().createBlock(); block.getStatements().add(stat); ((CtLoop) targetParent).setBody(block); parentBlock = block; for (CtStatement s : parentBlock.getStatements()) { if (s == target) { break; parentBlock.getStatements().add(indexOfTargetElement++, s);
private static <T> void processConstructor(CtConstructor<T> c, CtClass<T> toMerge) { CtStatement firstStmt = c.getBody().getStatements().get(0); if (firstStmt instanceof CtInvocation) { CtInvocation<?> superConstructorCall = (CtInvocation) firstStmt; CtBlock superConstructorBody = c.getFactory().Core().clone(superConstructor.getBody()); superConstructorBody.accept(new CtScanner() { @Override public <T> void visitCtParameterReference( c.getBody().removeStatement(firstStmt); List<CtStatement> superConstructorBodyStatements = superConstructorBody.getStatements(); for (int i = superConstructorBodyStatements.size() - 1; i >= 0; i--) { c.getBody().insertBegin(superConstructorBodyStatements.get(i));
@Override public boolean undoChangesInModel(OperatorInstance operation, ProgramVariant p) { StatementOperatorInstance stmtoperator = (StatementOperatorInstance) operation; CtStatement ctst = (CtStatement) operation.getOriginal(); CtStatement fix = (CtStatement) operation.getModified(); CtBlock<?> parentBlock = stmtoperator.getParentBlock(); if (parentBlock != null) { if (fix instanceof CtBlock) { CtBlock movedBlock = (CtBlock) fix; CtStatement firstFromBlock = movedBlock.getStatement(0); firstFromBlock.replace((CtStatement) ctst); for (CtStatement s : movedBlock.getStatements()) { parentBlock.removeStatement(s); } return true; } else { fix.replace((CtStatement) ctst); return true; } } return false; }
@Override public <T extends CtSynchronized> T setBlock(CtBlock<?> block) { if (block != null) { block.setParent(this); } getFactory().getEnvironment().getModelChangeListener().onObjectUpdate(this, BODY, block, this.block); this.block = block; return (T) this; }
call.setParent(block); elem.getThenStatement().setParent(block); block.addStatement(call); block.addStatement(elem.getThenStatement()); block.setParent(elem); elem.setThenStatement(block); } else { CtBlock<?> block = (CtBlock<?>) elem.getThenStatement(); block.insertBegin(call); call.setParent(block); elem.getElseStatement().setParent(block); block.addStatement(call); block.addStatement(elem.getElseStatement()); block.setParent(elem); elem.setElseStatement(block); } else { CtBlock<?> block = (CtBlock<?>) elem.getElseStatement(); block.insertBegin(call); snippet.toString()); CtBlock<?> block = getFactory().Core().createBlock(); block.addStatement(call); elem.setElseStatement(block);
@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(); }
public static boolean remove(CtBlock parentBlock, CtStatement fixStatement, int pos) { CtStatement s = parentBlock.getStatement(pos); // To be sure that the position has the element we // want to remove if (fixStatement.equals(s)) { parentBlock.getStatements().remove(pos); return true; } else { System.out.println("\n fx: " + fixStatement + "\n" + (s)); throw new IllegalStateException("Undo: Not valid fix position"); } } }
statementToRemove.replace(movedBlock.getStatements()); movedBlock.setParent(parentBlock); for (CtStatement newStmtBlock : movedBlock.getStatements()) { newStmtBlock.setParent(parentBlock);
localVariable = ValueCreator.createRandomLocalVar(parameter.getType(), parameter.getSimpleName()); body.insertBegin(localVariable); arguments.add(factory.createVariableRead(localVariable.getReference(), false)); } catch (Exception e) { CtInvocation newInvocation = factory.Code().createInvocation(targetClone, methodToInvokeToAdd.getReference(), arguments); body.insertEnd(newInvocation); return methodClone;
public CtStatementList getSubstitution(CtType<?> targetType) { CtClass<?> c; CtBlock<?> b; c = targetType.getFactory().Class().get(this.getClass()); if (c == null) { c = targetType.getFactory().Class().get(this.getClass()); } CtStatementList l = targetType.getFactory().Core().createStatementList(); if (this instanceof Template) { b = Substitution.substitute(targetType, (Template<?>) this, c.getMethod("statements").getBody()); } else { b = c.getMethod("statements").getBody().clone(); } l.setStatements(b.getStatements()); return l; }