@Override public void process(CtIf element) { // On récupère le body actuel : CtBlock fi; this.init(); int addLoop = 1 + (int) (Math.random() * ((4 - 1))); CtBlock dummyIf = null; switch (addLoop) { case 1: dummyIf = getDummyIf(); break; case 2: dummyIf = getDummyStringIf(); break; case 3: dummyIf = getDummyAndIf(); break; case 4: dummyIf = getDummyOrIf(); break; default: } fi = (CtBlock) element.getThenStatement(); fi = (CtBlock) element.getThenStatement(); element.setThenStatement(dummyIf); ((CtIf) element.getThenStatement().getElements(new TypeFilter(CtIf.class)).get(0)) .setThenStatement(fi); CtExpression cdt = element.getCondition(); CtBinaryOperator condition = getFactory().Core().createBinaryOperator(); condition.setKind(BinaryOperatorKind.AND); condition.setLeftHandOperand(cdt); condition.setRightHandOperand( ((CtIf) getDummyAndIf().getElements(new TypeFilter(CtIf.class)).get(0)).getCondition()); element.setCondition(condition); }
@Override public void visitCtIf(CtIf ifElement) { // super.visitCtIf(ifElement); CtStatement ctThen = ifElement.getThenStatement(); CtStatement ctElse = ifElement.getElseStatement(); Mutability condMut = mutability(ifElement.getCondition()); if (ctThen != null) ifElement.getThenStatement().accept(this); if (ctElse != null) ifElement.getElseStatement().accept(this); if (condMut == Mutability.ERASABLE && isEmpty(ctThen)) { // if ( - ) { } else { } <- Remove the whole if if (ctElse == null || isEmpty(ctElse)) remove(ifElement); // else if case: if ( - ) { } else if { doSomething() } <-- pull the else if element up else if (ctElse instanceof CtIf) ctElse.setParent(ifElement.getParent()); } }
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; }
@Override public void process(CtConditional ctConditional) { CtStatement parent = ctConditional.getParent(CtStatement.class); while (!(parent.getParent() instanceof CtStatementList)) { parent = parent.getParent(CtStatement.class); } CtExpression condition = ctConditional.getCondition(); CtIf anIf = getFactory().Core().createIf(); anIf.setPosition(ctConditional.getPosition()); if (parent instanceof CtReturn) { if (!((CtReturn) parent).getReturnedExpression().equals(ctConditional)) { return; } CtReturn returnThen = (CtReturn) parent.clone(); CtReturn returnElse = (CtReturn) parent.clone(); returnThen.setReturnedExpression(ctConditional.getThenExpression()); returnElse.setReturnedExpression(ctConditional.getElseExpression()); List<CtTypeReference> typeCasts = ctConditional.getTypeCasts(); for (int i = 0; i < typeCasts.size(); i++) { CtTypeReference ctTypeReference = typeCasts.get(i); returnThen.getReturnedExpression().addTypeCast(ctTypeReference.clone()); returnElse.getReturnedExpression().addTypeCast(ctTypeReference.clone()); } anIf.setElseStatement(getFactory().Code().createCtBlock(returnElse)); anIf.setThenStatement(getFactory().Code().createCtBlock(returnThen)); } else if (parent instanceof CtAssignment) { CtAssignment assignment = (CtAssignment) parent; CtExpression ctExpression = assignment.getAssignment(); if (!ctExpression.equals(ctConditional)) { if (ctExpression instanceof CtBinaryOperator) { CtBinaryOperator ctBinaryOperator = (CtBinaryOperator) ctExpression; createAssignment(ctConditional, anIf, assignment); CtBinaryOperator cloneThen = ctBinaryOperator.clone(); CtBinaryOperator cloneElse = ctBinaryOperator.clone(); if (ctBinaryOperator.getLeftHandOperand().equals(ctConditional)) { cloneThen.setLeftHandOperand(ctConditional.getThenExpression()); ctConditional.getThenExpression().setParent(cloneThen); cloneElse.setLeftHandOperand(ctConditional.getElseExpression()); } else if (ctBinaryOperator.getRightHandOperand().equals(ctConditional)) { cloneThen.setRightHandOperand(ctConditional.getThenExpression()); cloneElse.setRightHandOperand(ctConditional.getElseExpression()); } cloneThen.getLeftHandOperand().setParent(cloneThen); cloneElse.getLeftHandOperand().setParent(cloneElse); ((CtAssignment) ((CtBlock) anIf.getThenStatement()).getStatement(0)) .setAssignment(cloneThen); ((CtAssignment) ((CtBlock) anIf.getElseStatement()).getStatement(0)) .setAssignment(cloneElse); } else { return; } } else { createAssignment(ctConditional, anIf, assignment); } } else if (parent instanceof CtLocalVariable) { CtLocalVariable localVariable = (CtLocalVariable) parent; if (!localVariable.getDefaultExpression().equals(ctConditional)) { return; } CtLocalVariable clone = localVariable.clone(); clone.setDefaultExpression(null); localVariable.insertBefore(clone); CtAssignment variableAssignment = getFactory() .Code() .createVariableAssignment(localVariable.getReference(), false, ctConditional); variableAssignment.setType(localVariable.getType().clone()); variableAssignment.setPosition(ctConditional.getPosition()); createAssignment(ctConditional, anIf, variableAssignment); } else if (parent instanceof CtInvocation) { CtInvocation invocation = (CtInvocation) parent; CtInvocation cloneThen = invocation.clone(); CtInvocation cloneElse = invocation.clone(); List arguments = cloneThen.getArguments(); boolean found = false; for (int i = 0; i < arguments.size(); i++) { Object o = arguments.get(i); if (o.equals(ctConditional)) { ctConditional.getThenExpression().setParent(invocation); arguments.set(i, ctConditional.getThenExpression()); ctConditional.getElseExpression().setParent(invocation); cloneElse.getArguments().set(i, ctConditional.getElseExpression()); found = true; break; } } if (!found) { return; } cloneThen.setParent(anIf); cloneElse.setParent(anIf); anIf.setElseStatement(getFactory().Code().createCtBlock(cloneElse)); anIf.setThenStatement(getFactory().Code().createCtBlock(cloneThen)); } else if (parent instanceof CtConstructorCall) { CtConstructorCall invocation = (CtConstructorCall) parent; CtConstructorCall cloneThen = invocation.clone(); CtConstructorCall cloneElse = invocation.clone(); List arguments = cloneThen.getArguments(); boolean found = false; for (int i = 0; i < arguments.size(); i++) { Object o = arguments.get(i); if (o.equals(ctConditional)) { arguments.set(i, ctConditional.getThenExpression()); cloneElse.getArguments().set(i, ctConditional.getElseExpression()); found = true; break; } } if (!found) { return; } cloneThen.setParent(anIf); cloneElse.setParent(anIf); anIf.setElseStatement(getFactory().Code().createCtBlock(cloneElse)); anIf.setThenStatement(getFactory().Code().createCtBlock(cloneThen)); } else if (parent instanceof CtIf) { CtIf elem = (CtIf) parent; if (!elem.getCondition().equals(ctConditional)) { return; } CtIf cloneThen = elem.clone(); cloneThen.setParent(anIf); CtIf cloneElse = elem.clone(); cloneElse.setParent(anIf); cloneThen.setCondition(ctConditional.getThenExpression()); ctConditional.getThenExpression().setParent(cloneThen); cloneElse.setCondition(ctConditional.getElseExpression()); ctConditional.getElseExpression().setParent(cloneElse); anIf.setElseStatement(getFactory().Code().createCtBlock(cloneElse)); anIf.setThenStatement(getFactory().Code().createCtBlock(cloneThen)); } else if (parent instanceof CtThrow) { return; } else if (parent instanceof CtLoop) { return; } else if (parent instanceof CtUnaryOperator) { return; } else { System.err.println(parent); throw new RuntimeException("Other " + parent.getClass()); } /*if(ctConditional.getThenExpression().getTypeCasts() == null || ctConditional.getThenExpression().getTypeCasts().isEmpty()) { ((CtExpression)anIf.getThenStatement()).setTypeCasts(ctConditional.getTypeCasts()); } if(ctConditional.getElseExpression().getTypeCasts() == null || ctConditional.getElseExpression().getTypeCasts().isEmpty()) { ((CtExpression) anIf.getElseStatement()) .setTypeCasts(ctConditional.getTypeCasts()); }*/ anIf.setCondition(condition); condition.setParent(anIf); parent.replace(anIf); }