@Override public void visitCtIf(CtIf ifElement) { CtExpression<Boolean> r = evaluate(ifElement.getCondition()); if (r instanceof CtLiteral) { CtLiteral<Boolean> l = (CtLiteral<Boolean>) r; if (l.getValue()) { setResult(evaluate(ifElement.getThenStatement())); } else { if (ifElement.getElseStatement() != null) { setResult(evaluate(ifElement.getElseStatement())); } else { setResult(ifElement.getFactory().Code().createComment("if removed", CtComment.CommentType.INLINE)); CtIf ifRes = ifElement.getFactory().Core().createIf(); ifRes.setCondition(r); boolean thenEnded = false; boolean elseEnded = false; ifRes.setThenStatement((CtStatement) evaluate(ifElement.getThenStatement())); if (flowEnded) { thenEnded = true; flowEnded = false; if (ifElement.getElseStatement() != null) { ifRes.setElseStatement((CtStatement) evaluate(ifElement.getElseStatement()));
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; }
@Override public CtIf createIf() { CtIf e = new CtIfImpl(); e.setFactory(getMainFactory()); return e; }
public void visitCtIf(final spoon.reflect.code.CtIf ifElement) { spoon.reflect.code.CtIf other = ((spoon.reflect.code.CtIf) (this.stack.peek())); enter(ifElement); biScan(spoon.reflect.path.CtRole.ANNOTATION, ifElement.getAnnotations(), other.getAnnotations()); biScan(spoon.reflect.path.CtRole.CONDITION, ifElement.getCondition(), other.getCondition()); biScan(spoon.reflect.path.CtRole.THEN, ((spoon.reflect.code.CtStatement) (ifElement.getThenStatement())), other.getThenStatement()); biScan(spoon.reflect.path.CtRole.ELSE, ((spoon.reflect.code.CtStatement) (ifElement.getElseStatement())), other.getElseStatement()); biScan(spoon.reflect.path.CtRole.COMMENT, ifElement.getComments(), other.getComments()); exit(ifElement); }
/** * calls function once for each expression/then block and at the end calls function for last else block. * * @param ifElement * @param consumer * @return true if all function calls returns true or if there is no function call */ private void forEachIfCase(CtIf ifElement, BiConsumer<CtExpression<Boolean>, CtStatement> consumer) { consumer.accept(ifElement.getCondition(), ifElement.getThenStatement()); CtStatement elseStmt = getElseIfStatement(ifElement.getElseStatement()); if (elseStmt instanceof CtIf) { //another else if case forEachIfCase((CtIf) elseStmt, consumer); } else if (elseStmt != null) { //last else consumer.accept(null, elseStmt); } }
@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 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 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); }
@SuppressWarnings("unchecked") @Override public <T, U> U getValue(T element) { return ((U) ((Object) (castTarget(element).getCondition()))); }
@SuppressWarnings("unchecked") @Override public void visitCtIf(CtIf ifElement) { super.visitCtIf(ifElement); @SuppressWarnings("rawtypes") CtExpression exper=ifElement.getCondition(); if (candidates.containsKey(exper)) { ifElement.setCondition(candidates.get(exper)); saveSketchAndSynthesize(); ifElement.setCondition(exper); resoreDiskFile(); } }
+ ".makeSymbolicBoolean(\"guess_fix\")"); newIf.setCondition(condition); newIf.setParent(parent); element.replace(newIf); newIf.setThenStatement(element); newIf.getThenStatement().setParent(newIf); logger.debug("##### {} ##### After:\n{}", element, element.getParent().getParent());
return element.equals(anIf.getThenStatement()) || element.equals(anIf.getElseStatement());
@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; } }
@java.lang.Override public void set(spoon.reflect.code.CtExpression replace) { this.element.setCondition(replace); } }
@Override public void visitCtIf(CtIf e) { CtStatement thenStatement = e.getThenStatement(); if (thenStatement != null) { if (!(thenStatement instanceof CtBlock)) { CtStatement elseStatement = e.getElseStatement(); if (elseStatement != null && thenStatement != null) { SourcePosition thenPosition = thenStatement.getPosition(); comment.getParent(); } catch (ParentNotInitializedException ex) { e.addComment(comment);
@java.lang.Override public void set(spoon.reflect.code.CtStatement replace) { this.element.setThenStatement(replace); } }
private static CtIf originalLoopReplacement(Factory factory, LoopMonitor monitor, While loop) { CtExpression<Boolean> monitoredCondition = newExpressionFromSnippet(factory, conditionName(monitor), Boolean.class); CtIf newIf = newIf(factory, monitoredCondition, loop.loopBody(), newBreak(factory)); CtStatement increment = newStatementFromSnippet(factory, format("%s += 1", counterName(monitor))); insertBeforeUnderSameParent(increment, newIf.getThenStatement()); if (loop.isUnbreakable()) { newIf.setElseStatement(null); } return newIf; }
@java.lang.Override public void set(spoon.reflect.code.CtStatement replace) { this.element.setElseStatement(replace); } }
@SuppressWarnings("unchecked") @Override public <T, U> U getValue(T element) { return ((U) ((Object) (castTarget(element).getThenStatement()))); }
@SuppressWarnings("unchecked") @Override public <T, U> U getValue(T element) { return ((U) ((Object) (castTarget(element).getElseStatement()))); }