spoon.reflect.code.CtIf Java Examples

The following examples show how to use spoon.reflect.code.CtIf. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ExtendedRepairGenerator.java    From coming with MIT License 6 votes vote down vote up
private void genAddIfGuard(CtStatement n) {
    CtLiteral<Boolean> placeholder = factory.createLiteral();
    placeholder.setValue(true); // consider the placeholder, should this be more concrete?
    CtUnaryOperator<Boolean> guardCondition = factory.createUnaryOperator();
    guardCondition.setKind(UnaryOperatorKind.NOT);
    guardCondition.setOperand(placeholder);

    CtIf guardIf = factory.createIf();
    guardIf.setParent(n.getParent());
    guardIf.setCondition(guardCondition);
    guardIf.setThenStatement(n.clone());

    Repair repair = new Repair();
    repair.kind = RepairKind.GuardKind;
    repair.isReplace = true;
    repair.srcElem = n;
    repair.dstElem = guardIf;
    repair.atoms.addAll(repairAnalyzer.getCondCandidateVars(n));
    repairs.add(repair);
    // we do not consider the case of if statement as special at all
}
 
Example #2
Source File: VariableResolver.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public static List<CtVariableAccess> collectVariableReadIgnoringBlocks(CtElement element) {

		if (element instanceof CtIf) {
			return collectVariableRead(((CtIf) element).getCondition());
		}
		if (element instanceof CtWhile) {
			return collectVariableRead(((CtWhile) element).getLoopingExpression());
		}

		if (element instanceof CtFor) {
			return collectVariableRead(((CtFor) element).getExpression());
		}

		return collectVariableRead(element);

	}
 
Example #3
Source File: VariableResolver.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public static List<CtVariableAccess> collectVariableAccessIgnoringBlocks(CtElement element) {

		if (element instanceof CtIf) {
			return collectVariableAccess(((CtIf) element).getCondition());
		}
		if (element instanceof CtWhile) {
			return collectVariableAccess(((CtWhile) element).getLoopingExpression());
		}

		if (element instanceof CtFor) {
			return collectVariableAccess(((CtFor) element).getExpression());
		}

		return collectVariableAccess(element);

	}
 
Example #4
Source File: SpecialStatementFixSpaceProcessor.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void process(CtStatement element) {

	if (element instanceof CtIf) {
		add(((CtIf) element).getCondition());
	} else if (element instanceof CtFor) {
		add(((CtFor) element).getExpression());
	} else if (element instanceof CtWhile) {
		add(((CtWhile) element).getLoopingExpression());
	} else if (element instanceof CtDo) {
		add(((CtDo) element).getLoopingExpression());
	} else if (element instanceof CtThrow) {
		add(((CtThrow) element).getThrownExpression());
	} else if (element instanceof CtInvocation && (element.getParent() instanceof CtBlock)) {
		add(element);
	} else if (element instanceof CtAssignment || element instanceof CtConstructorCall
			|| element instanceof CtCFlowBreak || element instanceof CtLocalVariable) {
		add(element);
	}

}
 
Example #5
Source File: PatchGenerator.java    From nopol with GNU General Public License v2.0 6 votes vote down vote up
private boolean isElseIf(CtStatement original, CtStatement parentLine) {
	if (parentLine.getParent() instanceof CtIf) {
		CtStatement elseStatement = ((CtIf) parentLine.getParent()).getElseStatement();
		if (elseStatement == original) {
			return true;
		} else if (elseStatement instanceof CtBlock) {
			CtBlock block = (CtBlock) elseStatement;
			if (block.isImplicit() && block.getStatement(0) == original) {
				return true;
			}
		}
	}
	if (parentLine.getParent() instanceof CtBlock) {
		return isElseIf(original, (CtStatement) parentLine.getParent());
	}
	return false;
}
 
Example #6
Source File: AstComparatorTest.java    From gumtree-spoon-ast-diff with Apache License 2.0 6 votes vote down vote up
@Test
public void test_t_222894() throws Exception {
	AstComparator diff = new AstComparator();
	// meld src/test/resources/examples/t_222894/left_Client_1.150.java
	// src/test/resources/examples/t_222894/right_Client_1.151.java
	File fl = new File("src/test/resources/examples/t_222894/left_Client_1.150.java");
	File fr = new File("src/test/resources/examples/t_222894/right_Client_1.151.java");
	Diff result = diff.compare(fl, fr);

	CtElement ancestor = result.commonAncestor();
	assertTrue(ancestor instanceof CtIf);

	result.getRootOperations();
	result.debugInformation();
	assertTrue(result.containsOperation(OperationKind.Insert, "BinaryOperator", "AND"));

	// TODO there is a move that is not detected but should be
	// assertTrue(result.containsOperation(OperationKind.Move, VariableRead",
	// "Settings.keepServerlog"));
	// this is the case if gumtree.match.gt.minh" = "0" (but bad for other tests)
}
 
Example #7
Source File: ConditionalAdder.java    From nopol with GNU General Public License v2.0 6 votes vote down vote up
@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;
}
 
Example #8
Source File: ConditionRemoveTransform.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@SuppressWarnings({ "static-access", "rawtypes", "unchecked" })
public void visitCtIf(CtIf ifElement) {
	 
	super.visitCtIf(ifElement);
	CtExpression  cond = ifElement.getCondition();
	
	CtLiteral<Boolean> literalvalue = this.mutSupporter.getFactory().Core().createLiteral();
	Boolean bval=false;
	literalvalue.setValue(bval);
	
	CtBinaryOperator<?> newcond = this.mutSupporter.getFactory().Core().createBinaryOperator();
	newcond.setKind(BinaryOperatorKind.AND);
	newcond.setRightHandOperand(literalvalue);
	newcond.setLeftHandOperand(cond);
	
	ifElement.setCondition((CtExpression<Boolean>) newcond);
	saveSketchAndSynthesize();
	ifElement.setCondition(cond);
	resoreDiskFile();
}
 
Example #9
Source File: BoundProcessor.java    From spoon-examples with GNU General Public License v2.0 6 votes vote down vote up
public void process(Bound annotation, CtParameter<?> element) {
	final CtMethod parent = element.getParent(CtMethod.class);

	// Build if check for min.
	CtIf anIf = getFactory().Core().createIf();
	anIf.setCondition(getFactory().Code().<Boolean>createCodeSnippetExpression(element.getSimpleName() + " < " + annotation.min()));
	CtThrow throwStmt = getFactory().Core().createThrow();
	throwStmt.setThrownExpression((CtExpression<? extends Throwable>) getFactory().Core().createCodeSnippetExpression().setValue("new RuntimeException(\"out of min bound (\" + " + element.getSimpleName() + " + \" < " + annotation.min() + "\")"));
	anIf.setThenStatement(throwStmt);
	parent.getBody().insertBegin(anIf);
	anIf.setParent(parent);

	// Build if check for max.
	anIf = getFactory().Core().createIf();
	anIf.setCondition(getFactory().Code().<Boolean>createCodeSnippetExpression(element.getSimpleName() + " > " + annotation.max()));
	anIf.setThenStatement(getFactory().Code().createCtThrow("new RuntimeException(\"out of max bound (\" + " + element.getSimpleName() + " + \" > " + annotation.max() + "\")"));
	parent.getBody().insertBegin(anIf);
	anIf.setParent(parent);
}
 
Example #10
Source File: BoundTest.java    From spoon-examples with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testBoundTemplate() throws Exception {
	SpoonAPI launcher = new Launcher();
	launcher.getEnvironment().setNoClasspath(true);
	launcher.addInputResource("./src/main/java");
	launcher.setSourceOutputDirectory("./target/spoon-template");
	launcher.addProcessor(new BoundTemplateProcessor());
	launcher.run();

	final CtType<Main> target = launcher.getFactory().Type().get(Main.class);
	final CtMethod<?> m = target.getMethodsByName("m").get(0);

	assertTrue(m.getBody().getStatements().size() >= 2);
	assertTrue(m.getBody().getStatement(0) instanceof CtIf);
	assertTrue(m.getBody().getStatement(1) instanceof CtIf);
}
 
Example #11
Source File: AbstractCodeAnalyzer.java    From coming with MIT License 6 votes vote down vote up
public static boolean whethereffectiveguard(CtIf ifstatement, CtStatement targetstatement) {
	CtBlock thenBlock = ifstatement.getThenStatement();
	CtBlock elseBlock = ifstatement.getElseStatement();

	if (thenBlock != null) {
		List<CtStatement> thenstatements = thenBlock.getStatements();
		if (thenstatements.size() > 0 && thenstatements.get(0) == targetstatement)
			return true;
	}

	if (elseBlock != null) {
		List<CtStatement> elsestatements = elseBlock.getStatements();
		if (elsestatements.size() > 0 && elsestatements.get(0) == targetstatement)
			return true;
	}

	return false;
}
 
Example #12
Source File: AbstractCodeAnalyzer.java    From coming with MIT License 6 votes vote down vote up
public CtElement retrieveElementToStudy(CtElement element) {

		if (element instanceof CtIf) {
			return (((CtIf) element).getCondition());
		} else if (element instanceof CtWhile) {
			return (((CtWhile) element).getLoopingExpression());
		} else if (element instanceof CtFor) {
			return (((CtFor) element).getExpression());
		} else if (element instanceof CtDo) {
			return (((CtDo) element).getLoopingExpression());
		} else if (element instanceof CtForEach) {
			return (((CtForEach) element).getExpression());
		} else if (element instanceof CtSwitch) {
			return (((CtSwitch) element).getSelector());
		} else
			return (element);
	}
 
Example #13
Source File: CodeElementInfo.java    From coming with MIT License 6 votes vote down vote up
private CtElement retrieveElementToStudy(CtElement element) {

		if (element instanceof CtIf) {
			return (((CtIf) element).getCondition());
		} else if (element instanceof CtWhile) {
			return (((CtWhile) element).getLoopingExpression());
		} else if (element instanceof CtFor) {
			return (((CtFor) element).getExpression());
		} else if (element instanceof CtDo) {
			return (((CtDo) element).getLoopingExpression());
		} else if (element instanceof CtForEach) {
			return (((CtForEach) element).getExpression());
		} else if (element instanceof CtSwitch) {
			return (((CtSwitch) element).getSelector());
		} else
			return (element);
	}
 
Example #14
Source File: VariableResolver.java    From coming with MIT License 6 votes vote down vote up
public static List<CtVariableAccess> collectVariableReadIgnoringBlocks(CtElement element) {

		if (element instanceof CtIf) {
			return collectVariableRead(((CtIf) element).getCondition());
		}
		if (element instanceof CtWhile) {
			return collectVariableRead(((CtWhile) element).getLoopingExpression());
		}

		if (element instanceof CtFor) {
			return collectVariableRead(((CtFor) element).getExpression());
		}

		return collectVariableRead(element);

	}
 
Example #15
Source File: VariableResolver.java    From coming with MIT License 6 votes vote down vote up
public static List<CtVariableAccess> collectVariableAccessIgnoringBlocks(CtElement element) {

		if (element instanceof CtIf) {
			return collectVariableAccess(((CtIf) element).getCondition());
		}
		if (element instanceof CtWhile) {
			return collectVariableAccess(((CtWhile) element).getLoopingExpression());
		}

		if (element instanceof CtFor) {
			return collectVariableAccess(((CtFor) element).getExpression());
		}

		return collectVariableAccess(element);

	}
 
Example #16
Source File: ExtendedRepairGenerator.java    From coming with MIT License 6 votes vote down vote up
private Set<CtElement> fuzzyLocator(CtElement statement) {
    Set<CtElement> locations = new HashSet<>();
    if (statement instanceof CtMethod || statement instanceof CtClass || statement instanceof CtIf || statement instanceof CtStatementList) {
        locations.add(statement);
    } else {
        // "int a;" is not CtStatement?
        CtElement parent = statement.getParent();
        if (parent != null) {
            List<CtElement> statements = parent.getElements(new TypeFilter<>(CtElement.class));
            if (parent instanceof CtStatement) {
                statements = statements.subList(1, statements.size());
            }
            int idx = statements.indexOf(statement);
            if (idx >= 0) {
                if (idx > 0)
                    locations.add(statements.get(idx - 1));
                locations.add(statements.get(idx));
                if (idx < statements.size() - 1)
                    locations.add(statements.get(idx + 1));
            }
        }
    }
    return locations;
}
 
Example #17
Source File: ExtendedRepairGenerator.java    From coming with MIT License 6 votes vote down vote up
private void genTightCondition(CtIf n) {
    CtExpression<Boolean> oldCondition = n.getCondition();
    CtLiteral<Boolean> placeholder = factory.createLiteral();
    placeholder.setValue(true); // consider the placeholder, should this be more concrete?
    CtUnaryOperator<Boolean> tmpCondition = factory.createUnaryOperator();
    tmpCondition.setKind(UnaryOperatorKind.NOT);
    tmpCondition.setOperand(placeholder);
    CtBinaryOperator<Boolean> newCondition = factory.createBinaryOperator();
    newCondition.setKind(BinaryOperatorKind.AND);
    newCondition.setLeftHandOperand(oldCondition);
    newCondition.setRightHandOperand(placeholder);

    CtIf S = n.clone();
    S.setParent(n.getParent());
    S.setCondition(newCondition);

    Repair repair = new Repair();
    repair.kind = RepairKind.TightenConditionKind;
    repair.isReplace = true;
    repair.srcElem = n;
    repair.dstElem = S;
    repair.atoms.addAll(repairAnalyzer.getCondCandidateVars(n));
    repairs.add(repair);
    // we do not consider the case of short-circuit evaluation at all
}
 
Example #18
Source File: ExtendedRepairGenerator.java    From coming with MIT License 6 votes vote down vote up
private void genLooseCondition(CtIf n) {
    CtExpression<Boolean> oldCondition = n.getCondition();
    CtLiteral<Boolean> placeholder = factory.createLiteral();
    placeholder.setValue(true); // consider the placeholder, should this be more concrete?
    CtBinaryOperator<Boolean> newCondition = factory.createBinaryOperator();
    newCondition.setKind(BinaryOperatorKind.OR);
    newCondition.setLeftHandOperand(oldCondition);
    newCondition.setRightHandOperand(placeholder);

    CtIf S = n.clone();
    S.setParent(n.getParent());
    S.setCondition(newCondition);

    Repair repair = new Repair();
    repair.kind = RepairKind.LoosenConditionKind;
    repair.isReplace = true;
    repair.srcElem = n;
    repair.dstElem = S;
    repair.atoms.addAll(repairAnalyzer.getCondCandidateVars(n));
    repairs.add(repair);
    // we do not consider the case of short-circuit evaluation at all
}
 
Example #19
Source File: BoundTest.java    From spoon-examples with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testBoundProcessor() throws Exception {
	SpoonAPI launcher = new Launcher();
	launcher.getEnvironment().setNoClasspath(true);
	launcher.addInputResource("./src/main/java");
	launcher.setSourceOutputDirectory("./target/spoon-processor");
	launcher.addProcessor(new BoundProcessor());
	launcher.run();

	final CtType<Main> target = launcher.getFactory().Type().get(Main.class);
	final CtMethod<?> m = target.getMethodsByName("m").get(0);

	assertTrue(m.getBody().getStatements().size() >= 2);
	assertTrue(m.getBody().getStatement(0) instanceof CtIf);
	assertTrue(m.getBody().getStatement(1) instanceof CtIf);
}
 
Example #20
Source File: SingleWrapIfOperator.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Override
public boolean applyModification() {

	CtStatement original = (CtStatement) MetaGenerator.geOriginalElement(statementToWrap);
	this.setParentBlock(original.getParent(CtBlock.class));
	//
	CtIf ifNew = MutationSupporter.getFactory().createIf();
	ifNew.setParent(original.getParent());
	CtStatement originalCloned = original.clone();
	MutationSupporter.clearPosition(originalCloned);
	ifNew.setThenStatement(originalCloned);

	// as difference with the meta, here we put the ingredient evaluated in the
	// meta.
	ifNew.setCondition((CtExpression<Boolean>) ifcondition);

	//

	super.setOriginal(original);
	super.setModified(ifNew);
	//

	return super.applyModification();
}
 
Example #21
Source File: IfExpresionMutOp.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/** Return the list of CtElements Mutanted */
@Override
public List<MutantCtElement> getMutants(CtElement element) {

	CtIf targetIF = (CtIf) element;
	List<MutantCtElement> mutations = null;
	mutations = this.mutatorBinary.execute(targetIF.getCondition());
	return mutations;
}
 
Example #22
Source File: StatementDeletionMetaMutator.java    From metamutator with GNU General Public License v3.0 5 votes vote down vote up
private void SetElseStatementWithReturn(CtIf ifStatement){
	//search the first parent method
	CtMethod parentMethod = ifStatement.getParent(CtMethod.class);
	//we go out of while with null of a CtMethod. If null, return without method in parents...?
	if(parentMethod == null){
		return;
	}

	
	//create returned expression from template.
	CtLiteral returnedExpression = null;
	Class classOfReturn = parentMethod.getType().getActualClass();
	
	if(PrimitiveTemplateExpressions.containsKey(classOfReturn)){
		CtLiteral templateExpression = PrimitiveTemplateExpressions.get(classOfReturn);
		returnedExpression = getFactory().Core().clone(templateExpression);
	}else{
		returnedExpression = new CtLiteralImpl().setValue(null);
	}
	
	
	CtReturn theReturn = getFactory().Core().createReturn();
	theReturn.setReturnedExpression(returnedExpression);
	CtBlock elseBlock = ifStatement.getElseStatement();
	if(elseBlock == null){
		elseBlock = getFactory().Core().createBlock();
	}
	elseBlock.addStatement(theReturn);
	ifStatement.setElseStatement(elseBlock);
}
 
Example #23
Source File: TransformStrategy.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@SuppressWarnings({ "unchecked", "rawtypes" })
public List<String> transform () {
	CtStatement targetStmt = (CtStatement) this.modificationPoint.getCodeElement();
	if (targetStmt instanceof CtInvocation)
		this.visitCtInvocation((CtInvocation) targetStmt);
	else if (targetStmt instanceof CtConstructorCall)
		this.visitCtConstructorCall((CtConstructorCall) targetStmt);
	else if (targetStmt instanceof CtIf)
		this.visitCtIf ((CtIf)targetStmt);
	else if (targetStmt instanceof CtReturn)
		this.visitCtReturn((CtReturn) targetStmt);
	else if (targetStmt instanceof CtSwitch)
		this.visitCtSwitch((CtSwitch) targetStmt);
	else if (targetStmt instanceof CtCase)
		this.visitCtCase((CtCase) targetStmt);
	else if (targetStmt instanceof CtAssignment)
		this.visitCtAssignment((CtAssignment) targetStmt);
	else if (targetStmt instanceof CtAssert)
		this.visitCtAssert((CtAssert) targetStmt);
	else if (targetStmt instanceof CtFor)
		this.visitCtFor((CtFor) targetStmt);
	else if (targetStmt instanceof CtForEach)
		this.visitCtForEach((CtForEach) targetStmt);
	else if (targetStmt instanceof CtWhile)
		this.visitCtWhile((CtWhile) targetStmt);
	else if (targetStmt instanceof CtUnaryOperator)
		this.visitCtUnaryOperator((CtUnaryOperator) targetStmt);
	else if (targetStmt instanceof CtSynchronized)
		this.visitCtSynchronized((CtSynchronized) targetStmt);

	return list;
}
 
Example #24
Source File: ConditionAddTransform.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@SuppressWarnings({ "rawtypes", "static-access", "unchecked" })
private void writeIfReturn(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);
	
	CtElement parentmethod=parent;
	
	do {
		parentmethod=parentmethod.getParent();
	} while (!(parentmethod instanceof CtMethod));
	
	String returntype="void";
	returntype=((CtMethod)parentmethod).getType().getQualifiedName();
	returntype = returntype.replaceAll("\\d","");
	
	CtReturn returnstmt = this.mutSupporter.getFactory().Core().createReturn();

	if (!returntype.toLowerCase().equals("void")) {
		CtExpression returnexp = ExpressionGenerator.fetchEXP(this.mutSupporter, this.modificationPoint, returntype, "EXP");
		returnstmt.setReturnedExpression(returnexp);
	}
	
	ifstmt.setThenStatement(returnstmt);
	
	parent.addStatement(id, ifstmt);
	saveSketchAndSynthesize();
	parent.removeStatement(ifstmt);
	resoreDiskFile();
}
 
Example #25
Source File: ReplaceReturnOp.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@SuppressWarnings({ "rawtypes", "unchecked", "static-access" })
private CtElement createReturn(CtElement rootElement) {
	CtMethod method = rootElement.getParent(CtMethod.class);

	if (method == null) {
		log.info("Element without method parent");
		return null;
	}
	// We create the "if(true)"{}
	CtIf ifReturn = MutationSupporter.getFactory().Core().createIf();
	CtExpression ifTrueExpression = MutationSupporter.getFactory().Code().createCodeSnippetExpression("true");
	ifReturn.setCondition(ifTrueExpression);

	// Now we create the return statement
	CtReturn<?> returnStatement = null;
	CtTypeReference typeR = method.getType();
	if (typeR == null || "void".equals(typeR.getSimpleName())) {
		returnStatement = MutationSupporter.getFactory().Core().createReturn();
	} else {
		String codeExpression = "";
		if (prim.contains(typeR.getSimpleName())) {
			codeExpression = getZeroValue(typeR.getSimpleName().toLowerCase());
		} else if (typeR.getSimpleName().toLowerCase().equals("boolean")) {
			codeExpression = "false";
		} else {
			codeExpression = "null";
		}
		CtExpression returnExpression = MutationSupporter.getFactory().Code()
				.createCodeSnippetExpression(codeExpression);
		returnStatement = MutationSupporter.getFactory().Core().createReturn();
		returnStatement.setReturnedExpression(returnExpression);
	}
	// Now, we associate if(true){return [...]}
	ifReturn.setThenStatement(returnStatement);
	return ifReturn;

}
 
Example #26
Source File: IFExpressionFixSpaceProcessor.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void process(CtIf element) {
	List<CtExpression<Boolean>> ctExp = ExpressionRevolver.getExpressions(element.getCondition());
	for (CtExpression ctExpression : ctExp) {
		super.add(ctExpression);
	}

}
 
Example #27
Source File: IFCollectorProcessor.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void process(CtIf element) {
	
	CtType simpleType = getSimpleType(element);
	/*ifs.add(element);
	ifClasses.put(element, simpleType);*/
	super.add(element);
	
}
 
Example #28
Source File: AstComparatorTest.java    From gumtree-spoon-ast-diff with Apache License 2.0 5 votes vote down vote up
@Test
public void test_t_222399() throws Exception {
	AstComparator diff = new AstComparator();
	// meld src/test/resources/examples/t_222399/left_TdbFile_1.7.java
	// src/test/resources/examples/t_222399/right_TdbFile_1.8.java
	File fl = new File("src/test/resources/examples/t_222399/left_TdbFile_1.7.java");
	File fr = new File("src/test/resources/examples/t_222399/right_TdbFile_1.8.java");
	Diff result = diff.compare(fl, fr);

	CtElement ancestor = result.commonAncestor();
	assertTrue(ancestor instanceof CtIf);
	assertEquals(229, ancestor.getPosition().getLine());

	List<Operation> actions = result.getRootOperations();
	result.debugInformation();
	assertEquals(3, actions.size());
	assertEquals(229, ancestor.getPosition().getLine());

	assertTrue(result.containsOperation(OperationKind.Update, "Invocation", "equals"));
	assertTrue(result.containsOperation(OperationKind.Insert, "BinaryOperator", "NE"));
	assertTrue(result.containsOperation(OperationKind.Move, "Invocation", "equals"));

	// updated the if condition
	CtElement elem = actions.get(0).getNode();
	assertNotNull(elem);
	assertNotNull(elem.getParent(CtIf.class));

}
 
Example #29
Source File: ConditionRemoveTransform.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Override
public List<String> transform() {
	CtStatement targetStmt = (CtStatement) this.modificationPoint.getCodeElement();
	if (targetStmt instanceof CtIf)
		this.visitCtIf ((CtIf)targetStmt);
	else if (targetStmt instanceof CtWhile)
		this.visitCtWhile((CtWhile) targetStmt);
	return list;
}
 
Example #30
Source File: ReplaceIfBooleanOp.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates a new if from that one passed as parammeter. The next if has a
 * condition expression expression true or false according to the variable
 * <b>thenBranch</b>
 * 
 * @param ifElement
 * @param thenBranch
 * @return
 */
@SuppressWarnings({ "rawtypes", "unchecked", })
private CtIf createIf(CtIf ifElement, boolean thenBranch) {

	CtIf clonedIf = MutationSupporter.getFactory().Core().clone(ifElement);
	CtExpression ifExpression = MutationSupporter.getFactory().Code()
			.createCodeSnippetExpression(Boolean.toString(thenBranch));

	clonedIf.setCondition(ifExpression);

	return clonedIf;
}