public static void insertAfter(CtStatement target, CtStatementList statements) throws ParentNotInitializedException { CtElement e = target.getParent(); if (e instanceof CtExecutable) { throw new RuntimeException("cannot insert in this context (use insertEnd?)"); } new InsertVisitor(target, statements, InsertType.AFTER).scan(e); }
@Override public <T extends CtStatementList> T insertAfter(Filter<? extends CtStatement> insertionPoints, CtStatementList statements) { for (CtStatement e : Query.getElements(this, insertionPoints)) { e.insertAfter(statements); } return (T) this; }
@Override public <T extends CtStatementList> T insertBefore(Filter<? extends CtStatement> insertionPoints, CtStatement statement) { for (CtStatement e : Query.getElements(this, insertionPoints)) { e.insertBefore(statement); } return (T) this; }
/** write all non-implicit parts of a block, with special care for indentation */ public void writeIfOrLoopBlock(CtStatement block) { if (block != null) { if (!block.isImplicit() && (block instanceof CtBlock || block instanceof CtIf)) { printer.writeSpace(); } if (!(block instanceof CtBlock) && !(block instanceof CtIf)) { printer.incTab(); printer.writeln(); } writeStatement(block); if (!(block instanceof CtBlock) && !(block instanceof CtIf)) { printer.decTab().writeln(); } if (!block.isImplicit()) { if (!block.isParentInitialized() || (!(block.getParent() instanceof CtFor) && !(block.getParent() instanceof CtForEach) && !(block.getParent() instanceof CtIf))) { printer.writeSpace(); } } } else { printer.writeSeparator(";"); } } /**
if (thenStatement != null) { if (!(thenStatement instanceof CtBlock)) { if (comment.getPosition().getSourceEnd() <= thenStatement.getPosition().getSourceStart()) { thenStatement.addComment(comment); return; SourcePosition thenPosition = thenStatement.getPosition(); if (!thenPosition.isValidPosition() && thenStatement instanceof CtBlock) { CtStatement thenExpression = ((CtBlock) thenStatement).getStatement(0); thenPosition = thenExpression.getPosition(); SourcePosition elsePosition = elseStatement.getPosition(); if (!elsePosition.isValidPosition() && elseStatement instanceof CtBlock) { CtStatement elseExpression = ((CtBlock) elseStatement).getStatement(0); elsePosition = elseExpression.getPosition(); elseStatement.addComment(comment);
private static CtStatement internalCompileStatement(CtElement st, CtTypeReference returnType) { Factory f = st.getFactory(); String contents = createWrapperContent(st, f, returnType); build(f, contents); CtType<?> c = f.Type().get(WRAPPER_CLASS_NAME); // Get the part we want CtMethod<?> wrapper = c.getMethod(WRAPPER_METHOD_NAME); List<CtStatement> statements = wrapper.getBody().getStatements(); CtStatement ret = statements.get(statements.size() - 1); // Clean up c.getPackage().removeType(c); //disconnect element from the parent, so it can be added to another model ret.delete(); ret.setParent(null); if (ret instanceof CtClass) { CtClass klass = (CtClass) ret; ret.getFactory().Package().getRootPackage().addType(klass); klass.setSimpleName(klass.getSimpleName().replaceAll("^[0-9]*", "")); } return ret; }
public boolean applyChangesInModel(OperatorInstance operation, ProgramVariant p) { StatementOperatorInstance stmtoperator = (StatementOperatorInstance) operation; boolean successful = false; CtStatement ctst = (CtStatement) operation.getOriginal(); CtStatement fix = (CtStatement) operation.getModified(); CtBlock parentBlock = stmtoperator.getParentBlock(); if (parentBlock != null) { try { ctst.replace((CtStatement) fix); fix.setParent(parentBlock); successful = true; operation.setSuccessfulyApplied(successful); } catch (Exception ex) { log.error("Error applying an operation, exception: " + ex.getMessage()); operation.setExceptionAtApplied(ex); operation.setSuccessfulyApplied(false); } } else { log.error("Operation not applied. Parent null "); } return successful; }
public void process(CtStatement element) { logger.debug("##### {} ##### Before:\n{}", element, element.getParent()); CtElement parent = element.getParent(); CtIf newIf = element.getFactory().Core().createIf(); CtCodeSnippetExpression<Boolean> condition; if (getValue() != null) { switch (getValue()) { case "1": condition = element.getFactory().Code() .createCodeSnippetExpression("true"); break; case "0": condition = element.getFactory().Code() .createCodeSnippetExpression("false"); break; default: condition = element.getFactory().Code() .createCodeSnippetExpression(getValue()); .getFactory() .Code() .createCodeSnippetExpression( element.replace(newIf); newIf.getThenStatement().setParent(newIf); logger.debug("##### {} ##### After:\n{}", element, element.getParent().getParent());
private void replaceInvocationByLocalVariable(CtStatement invocationToBeReplaced, CtLocalVariable localVariable) { if (invocationToBeReplaced.getParent() instanceof CtBlock) { invocationToBeReplaced.replace(localVariable); } else { CtElement parent = invocationToBeReplaced.getParent(); while (!(parent.getParent() instanceof CtBlock)) { parent = invocationToBeReplaced.getParent(); } ((CtStatement) parent).insertBefore(localVariable); invocationToBeReplaced.replace(localVariable.getReference()); } }
@Override public <T extends CtStatementList> T insertBegin(CtStatement statement) { ensureModifiableStatementsList(); statement.setParent(this); this.addStatement(0, statement); if (isImplicit() && this.statements.size() > 1) { setImplicit(false); } return (T) this; }
public static void insertAfter(CtStatement target, CtStatement statement) throws ParentNotInitializedException { CtStatementList sts = target.getFactory().Core().createStatementList(); sts.addStatement(statement); insertAfter(target, sts); }
private void addAtCorrectPlace(String id, CtStatement lastStmt, CtStatement assertStatement, CtStatement statementToBeAsserted) { if (id.endsWith("end")) { statementToBeAsserted.getParent(CtBlock.class).insertEnd(assertStatement); } else { lastStmt.insertAfter(assertStatement); } } }
@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; } }
@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) { fix.replace((CtStatement) ctst); return true; } return false; }
statementToBeAsserted.getParent() instanceof CtBlock) { CtInvocation invocationToBeReplaced = (CtInvocation) statementToBeAsserted.clone(); final CtLocalVariable localVariable = factory.createLocalVariable( invocationToBeReplaced.getType(), "o_" + id.split("___")[0], invocationToBeReplaced ); statementToBeAsserted.replace(localVariable); DSpotUtils.addComment(localVariable, "AssertGenerator create local variable with return value of invocation", CtComment.CommentType.INLINE); localVariable.setParent(statementToBeAsserted.getParent()); addAtCorrectPlace(id, localVariable, assertStatement, statementToBeAsserted); statements.remove(line);
public static void insertBeforeUnderSameParent(CtStatement toBeInserted, CtStatement insertionPoint) { CtElement parent; if (isBlock(insertionPoint)) { CtBlock<?> block = (CtBlock<?>) insertionPoint; block.insertBegin(toBeInserted); parent = block; } else { insertionPoint.insertBefore(toBeInserted); parent = insertionPoint.getParent(); } setParent(parent, toBeInserted); }
.getLastStatement(); if (lastStatement instanceof CtReturn) { lastStatement.insertBefore(list_call); } else { lastStatement.insertAfter(list_call);
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(),
@Override public boolean applyChangesInModel(OperatorInstance operation, ProgramVariant p) { boolean successful = false; CtStatement ctst = (CtStatement) operation.getOriginal(); CtStatement fix = (CtStatement) operation.getModified(); StatementOperatorInstance stmtoperator = (StatementOperatorInstance) operation; CtBlock parentBlock = stmtoperator.getParentBlock(); if (parentBlock != null) { ctst.insertBefore((CtStatement) fix); fix.setParent(parentBlock); successful = true; operation.setSuccessfulyApplied(successful); StatementSupporter.updateBlockImplicitly(parentBlock, true); } else { log.error("Operation not applied. Parent null "); } return successful; }
@Override public boolean applyChangesInModel(OperatorInstance operation, ProgramVariant p) { StatementOperatorInstance stmtoperator = (StatementOperatorInstance) operation; boolean successful = false; CtStatement ctst = (CtStatement) operation.getOriginal(); CtStatement fix = (CtStatement) operation.getModified(); CtBlock parentBlock = stmtoperator.getParentBlock(); if (parentBlock != null) { ctst.insertAfter((CtStatement) fix); fix.setParent(parentBlock); successful = true; operation.setSuccessfulyApplied(successful); StatementSupporter.updateBlockImplicitly(parentBlock, true); } else { log.error("Operation not applied. Parent null"); } return successful; }