Java Code Examples for spoon.reflect.code.CtReturn

The following examples show how to use spoon.reflect.code.CtReturn. These examples are extracted from open source projects. 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 Project: gumtree-spoon-ast-diff   Source File: AstComparatorTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void test_t_204225() throws Exception {
	AstComparator diff = new AstComparator();
	// meld
	// src/test/resources/examples/t_204225/left_UMLModelElementStereotypeComboBoxModel_1.3.java
	// src/test/resources/examples/t_204225/right_UMLModelElementStereotypeComboBoxModel_1.4.java
	File fl = new File("src/test/resources/examples/t_204225/left_UMLModelElementStereotypeComboBoxModel_1.3.java");
	File fr = new File(
			"src/test/resources/examples/t_204225/right_UMLModelElementStereotypeComboBoxModel_1.4.java");
	Diff result = diff.compare(fl, fr);

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

	List<Operation> actions = result.getRootOperations();
	// result.debugInformation();
	assertEquals(actions.size(), 2);
	assertTrue(result.containsOperation(OperationKind.Insert, "BinaryOperator", "OR"));
	assertTrue(result.containsOperation(OperationKind.Move, "BinaryOperator", "AND"));

}
 
Example 2
Source Project: gumtree-spoon-ast-diff   Source File: AstComparatorTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void test_t_221295() throws Exception {
	AstComparator diff = new AstComparator();
	// meld src/test/resources/examples/t_221295/left_Board_1.5.java
	// src/test/resources/examples/t_221295/right_Board_1.6.java
	File fl = new File("src/test/resources/examples/t_221295/left_Board_1.5.java");
	File fr = new File("src/test/resources/examples/t_221295/right_Board_1.6.java");
	Diff result = diff.compare(fl, fr);

	List<Operation> actions = result.getRootOperations();
	result.debugInformation();
	assertEquals(actions.size(), 1);
	assertTrue(result.containsOperation(OperationKind.Update, "BinaryOperator", "GT"));

	CtElement elem = actions.get(0).getNode();
	assertNotNull(elem);
	assertNotNull(elem.getParent(CtReturn.class));

}
 
Example 3
Source Project: gumtree-spoon-ast-diff   Source File: AstComparatorTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void test_t_221958() throws Exception {
	AstComparator diff = new AstComparator();
	// meld src/test/resources/examples/t_221958/left_TilesetManager_1.22.java
	// src/test/resources/examples/t_221958/right_TilesetManager_1.23.java
	File fl = new File("src/test/resources/examples/t_221958/left_TilesetManager_1.22.java");
	File fr = new File("src/test/resources/examples/t_221958/right_TilesetManager_1.23.java");
	Diff result = diff.compare(fl, fr);

	List<Operation> actions = result.getRootOperations();
	result.debugInformation();
	assertEquals(actions.size(), 1);
	assertTrue(result.containsOperation(OperationKind.Insert, "Literal", "null"));

	CtElement elem = actions.get(0).getNode();
	assertNotNull(elem);
	assertNotNull(elem.getParent(CtReturn.class));

}
 
Example 4
@Override
public boolean isToBeProcessed(CtReturn element) {
	try {
		Selector.getTopLevelClass(element);
	} catch (NullPointerException e) {
		return false;
	}
	return (element.getReturnedExpression() != null && !element.getReturnedExpression().getType().getSimpleName().equals("Boolean"));
}
 
Example 5
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 6
Source Project: nopol   Source File: SpoonMethodLibrary.java    License: GNU General Public License v2.0 5 votes vote down vote up
public static boolean isGetter(CtMethod<?> method) {
    if (hasNoArguments(method) && numberOfStatements(method) == 1) {
        CtStatement statement = lastStatementOf(method);
        return isReturnStatement(statement) && isFieldAccess(((CtReturn<?>) statement).getReturnedExpression());
    }
    return false;
}
 
Example 7
Source Project: nopol   Source File: SpoonLoopLibrary.java    License: GNU General Public License v2.0 5 votes vote down vote up
public static Collection<CtReturn<?>> returnStatementsIn(CtLoop loop) {
    List<CtReturn<?>> returnsOfLoop = MetaList.newArrayList();
    List<CtReturn<?>> allReturns = (List) allChildrenOf(loop, CtReturn.class);
    for (CtReturn<?> candidateReturn : allReturns) {
        if (isReturningFrom(loop, candidateReturn)) {
            returnsOfLoop.add(candidateReturn);
        }
    }
    return returnsOfLoop;
}
 
Example 8
Source Project: astor   Source File: ReplaceReturnOp.java    License: 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 9
Source Project: astor   Source File: InsertAfterOp.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean canBeAppliedToPoint(ModificationPoint point) {

	boolean apply = super.canBeAppliedToPoint(point);
	if (!apply)
		return apply;

	// do not insert after a return
	if (point.getCodeElement() instanceof CtReturn) {
		return false;
	}

	// Otherwise, accept the element
	return true;
}
 
Example 10
Source Project: astor   Source File: RemoveOp.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean canBeAppliedToPoint(ModificationPoint point) {
	if (!(point.getCodeElement() instanceof CtStatement))
		return false;
	// Do not remove local declaration
	if (point.getCodeElement() instanceof CtLocalVariable) {
		CtLocalVariable lv = (CtLocalVariable) point.getCodeElement();
		boolean shadow = false;
		CtClass parentC = point.getCodeElement().getParent(CtClass.class);
		List<CtField> ff = parentC.getFields();
		for (CtField<?> f : ff) {
			if (f.getSimpleName().equals(lv.getSimpleName()))
				shadow = true;
		}
		if (!shadow)
			return false;
	}
	// do not remove the last statement
	CtMethod parentMethd = point.getCodeElement().getParent(CtMethod.class);
	if (point.getCodeElement() instanceof CtReturn
			&& parentMethd.getBody().getLastStatement().equals(point.getCodeElement())) {
		return false;
	}

	// Otherwise, accept the element
	return true;
}
 
Example 11
Source Project: astor   Source File: ReturnExpresionMutOp.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean canBeAppliedToPoint(ModificationPoint point) {

	return (point.getCodeElement() instanceof CtReturn)
			&& (point.getCodeElement().getElements(new TypeFilter(CtBinaryOperator.class)).size() > 0
					|| point.getCodeElement().getElements(new TypeFilter(CtUnaryOperator.class)).size() > 0);

}
 
Example 12
Source Project: astor   Source File: ReturnExpresionMutOp.java    License: GNU General Public License v2.0 5 votes vote down vote up
protected OperatorInstance createModificationInstance(ModificationPoint point, MutantCtElement fix)
		throws IllegalAccessException {
	CtReturn targetIF = (CtReturn) point.getCodeElement();
	OperatorInstance operation = new OperatorInstance();
	operation.setOriginal(targetIF.getReturnedExpression());
	operation.setOperationApplied(this);
	operation.setModificationPoint(point);
	operation.setModified(fix.getElement());

	return operation;
}
 
Example 13
Source Project: astor   Source File: ReturnExpresionMutOp.java    License: 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) {

	CtReturn targetIF = (CtReturn) element;
	List<MutantCtElement> mutations = null;
	mutations = this.mutatorBinary.execute(targetIF.getReturnedExpression());
	return mutations;
}
 
Example 14
Source Project: astor   Source File: ConditionAddTransform.java    License: 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 15
Source Project: astor   Source File: TransformStrategy.java    License: 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 16
Source Project: astor   Source File: TransformStrategy.java    License: GNU General Public License v2.0 5 votes vote down vote up
@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public <R> void visitCtReturn(CtReturn<R> returnStatement) {
	super.visitCtReturn(returnStatement);
	
	CtExpression exper=returnStatement.getReturnedExpression();
	if (candidates.containsKey(exper)) {
		returnStatement.setReturnedExpression(candidates.get(exper));
		saveSketchAndSynthesize();
		returnStatement.setReturnedExpression(exper);
		resoreDiskFile();
	}
}
 
Example 17
@Override
public boolean canBeAppliedToPoint(ModificationPoint point) {

	if (!(point.getCodeElement() instanceof CtStatement))
		return false;

	// do not insert after a return
	if (point.getCodeElement() instanceof CtConstructorCall || point.getCodeElement() instanceof CtReturn) {
		return false;
	}

	// Otherwise, accept the element
	return true;
}
 
Example 18
@Override
public boolean canBeAppliedToPoint(ModificationPoint point) {

	if (!(point.getCodeElement() instanceof CtStatement))
		return false;

	// do not insert after a return
	if (point.getCodeElement() instanceof CtConstructorCall || point.getCodeElement() instanceof CtReturn) {
		return false;
	}

	// Otherwise, accept the element
	return true;
}
 
Example 19
Source Project: nopol   Source File: SpoonLoopLibrary.java    License: GNU General Public License v2.0 4 votes vote down vote up
public static boolean isReturningFrom(CtLoop loop, CtReturn<?> returnStatement) {
    if (returnStatement.getParent(CtLoop.class) == loop) {
        return returnStatement.getParent(CtMethod.class) == loop.getParent(CtMethod.class);
    }
    return false;
}
 
Example 20
@Override
public void process(CtReturn element) {
	super.add(element);
}
 
Example 21
@Test
public void test_doomy_Multi_1_hierarchy_with_target() throws Exception {

	String dep = new File("./examples/libs/junit-4.4.jar").getAbsolutePath();

	File out = new File(ConfigurationProperties.getProperty("workingDirectory"));

	CommandSummary command = new CommandSummary();
	command.command.put("-location", new File("./examples/testMultiMet/testMulti1").getAbsolutePath());
	command.command.put("-mode", "custom");
	command.command.put("-customengine", MultiMetaEvalTOSApproach.class.getName());
	command.command.put("-javacompliancelevel", "7");
	command.command.put("-maxtime", "120");
	command.command.put("-seed", "0");
	command.command.put("-stopfirst", "false");
	command.command.put("-maxgen", "0");
	command.command.put("-population", "1");
	command.command.put("-scope", "local");
	command.command.put("-srcjavafolder", "src/main/java/");
	command.command.put("-srctestfolder", "src/test/java/");
	command.command.put("-binjavafolder", "target/classes/");
	command.command.put("-bintestfolder", "target/test-classes/");
	command.command.put("-id", "test-mr1a");
	command.command.put("-out", out.getAbsolutePath());
	command.command.put("-dependencies", dep);
	command.command.put("-loglevel", "INFO");
	command.command.put("-flthreshold", "0.24");

	AstorMain main1 = new AstorMain();
	main1.execute(command.flat());

	MultiMetaEvalTOSApproach.MAX_GENERATIONS = 1000;

	MultiMetaEvalTOSApproach approach = (MultiMetaEvalTOSApproach) main1.getEngine();

	ModificationPoint mp24 = approach.getVariants().get(0).getModificationPoints().stream()
			.filter(e -> (e.getCodeElement().getPosition().getLine() == 24
					&& e.getCodeElement().getPosition().getFile().getName().equals("MyBuggy.java")))
			.findAny().get();
	assertNotNull(mp24);

	Prediction prediction = new Prediction();

	approach.setPredictor(new IPredictor() {

		@Override
		public PredictionResult computePredictionsForModificationPoint(ModificationPoint iModifPoint) {
			// No prediction
			return null;
		}
	});

	CtElement binary = mp24.getCodeElement().getElements(e -> e.toString().equals("i2 > i1")).get(0);
	assertNotNull(binary);

	CtReturn rnt = (CtReturn) mp24.getCodeElement().getElements(e -> e.toString().equals("return i1")).get(0);
	assertNotNull(rnt);

	prediction.add(new PredictionElement(1, binary), new OperatorReplacementOp());
	prediction.add(new PredictionElement(2, rnt.getReturnedExpression()), new VarReplacementByAnotherVarOp());

	boolean isSolution = approach.analyzePrediction(approach.getVariants().get(0), mp24, prediction);

	assertTrue(isSolution);

	approach.atEnd();

	List<ProgramVariant> solutions = approach.getSolutions();

	assertTrue(solutions.size() > 0);
	ProgramVariant solutions0 = solutions.get(0);
	assertEquals(2, solutions0.getAllOperations().size());

}
 
Example 22
/**
 * Two changes, one in a hierarchy of the other (but not inside). We do not
 * specify the target.
 * 
 * @throws Exception
 */
@Test
public void test_doomy_Multi_1_hierarchy_without_target() throws Exception {

	String dep = new File("./examples/libs/junit-4.4.jar").getAbsolutePath();

	File out = new File(ConfigurationProperties.getProperty("workingDirectory"));

	CommandSummary command = new CommandSummary();
	command.command.put("-location", new File("./examples/testMultiMet/testMulti1").getAbsolutePath());
	command.command.put("-mode", "custom");
	command.command.put("-customengine", MultiMetaEvalTOSApproach.class.getName());
	command.command.put("-javacompliancelevel", "7");
	command.command.put("-maxtime", "120");
	command.command.put("-seed", "0");
	command.command.put("-stopfirst", "false");
	command.command.put("-maxgen", "0");
	command.command.put("-population", "1");
	command.command.put("-scope", "local");
	command.command.put("-srcjavafolder", "src/main/java/");
	command.command.put("-srctestfolder", "src/test/java/");
	command.command.put("-binjavafolder", "target/classes/");
	command.command.put("-bintestfolder", "target/test-classes/");
	command.command.put("-id", "test-mr1b");
	command.command.put("-out", out.getAbsolutePath());
	command.command.put("-dependencies", dep);
	command.command.put("-loglevel", "INFO");
	command.command.put("-flthreshold", "0.24");
	command.command.put("-saveall", "");

	AstorMain main1 = new AstorMain();
	main1.execute(command.flat());

	MultiMetaEvalTOSApproach.MAX_GENERATIONS = 1000;

	MultiMetaEvalTOSApproach approach = (MultiMetaEvalTOSApproach) main1.getEngine();

	ModificationPoint mp24 = approach.getVariants().get(0).getModificationPoints().stream()
			.filter(e -> (e.getCodeElement().getPosition().getLine() == 24
					&& e.getCodeElement().getPosition().getFile().getName().equals("MyBuggy.java")))
			.findAny().get();
	assertNotNull(mp24);

	Prediction prediction = new Prediction();

	CtElement binary = mp24.getCodeElement().getElements(e -> e.toString().equals("i2 > i1")).get(0);
	assertNotNull(binary);

	CtReturn rnt = (CtReturn) mp24.getCodeElement().getElements(e -> e.toString().equals("return i1")).get(0);
	assertNotNull(rnt);

	prediction.add(new PredictionElement(1, binary), new OperatorReplacementOp());
	prediction.add(new PredictionElement(2, rnt.getReturnedExpression()), new VarReplacementByAnotherVarOp());

	boolean isSolution = approach.analyzePrediction(approach.getVariants().get(0), mp24, prediction);

	assertTrue(isSolution);

	approach.atEnd();
}
 
Example 23
/**
 * Two changes, one in a hierarchy of the other (but not inside). We specify the
 * target.
 * 
 * @throws Exception
 */
@Test
public void test_doomy_Multi_2_hierarchy_2_target() throws Exception {

	String dep = new File("./examples/libs/junit-4.4.jar").getAbsolutePath();

	File out = new File(ConfigurationProperties.getProperty("workingDirectory"));

	CommandSummary command = new CommandSummary();
	command.command.put("-location", new File("./examples/testMultiMet/testMulti2").getAbsolutePath());
	command.command.put("-mode", "custom");
	command.command.put("-customengine", MultiMetaEvalTOSApproach.class.getName());
	command.command.put("-javacompliancelevel", "7");
	command.command.put("-maxtime", "120");
	command.command.put("-seed", "0");
	command.command.put("-stopfirst", "false");
	command.command.put("-maxgen", "0");
	command.command.put("-population", "1");
	command.command.put("-scope", "local");
	command.command.put("-srcjavafolder", "src/main/java/");
	command.command.put("-srctestfolder", "src/test/java/");
	command.command.put("-binjavafolder", "target/classes/");
	command.command.put("-bintestfolder", "target/test-classes/");
	command.command.put("-id", "test-mr2a");
	command.command.put("-out", out.getAbsolutePath());
	command.command.put("-dependencies", dep);
	command.command.put("-loglevel", "INFO");
	command.command.put("-flthreshold", "0.24");
	command.command.put("-saveall", "");

	AstorMain main1 = new AstorMain();
	main1.execute(command.flat());

	MultiMetaEvalTOSApproach.MAX_GENERATIONS = 1000;

	MultiMetaEvalTOSApproach approach = (MultiMetaEvalTOSApproach) main1.getEngine();

	ModificationPoint mp24 = approach.getVariants().get(0).getModificationPoints().stream()
			.filter(e -> (e.getCodeElement().getPosition().getLine() == 36
					&& e.getCodeElement().getPosition().getFile().getName().equals("MyBuggy.java")))
			.findAny().get();
	assertNotNull(mp24);
	assertTrue(mp24.getCodeElement() instanceof CtReturn);

	Prediction prediction = new Prediction();

	approach.setPredictor(new IPredictor() {

		@Override
		public PredictionResult computePredictionsForModificationPoint(ModificationPoint iModifPoint) {
			// No prediction
			return null;
		}
	});

	CtElement invocation = mp24.getCodeElement().getElements(e -> e.toString().equals("myinst.toNegative(i1)"))
			.get(0);
	assertNotNull(invocation);
	assertTrue(invocation instanceof CtInvocation);
	// The var inside invocation
	CtElement vacc = invocation.getElements(e -> e.toString().equals("i1")).get(0);
	assertNotNull(vacc);

	prediction.add(new PredictionElement(1, vacc), new VarReplacementByAnotherVarOp());
	prediction.add(new PredictionElement(2, invocation), new MethodXMethodReplacementDiffNameOp());

	boolean isSolution = approach.analyzePrediction(approach.getVariants().get(0), mp24, prediction);

	assertTrue(isSolution);

	approach.atEnd();

	List<ProgramVariant> solutions = approach.getSolutions();

	assertTrue(solutions.size() > 0);
	assertEquals(1, solutions.size());
	ProgramVariant solutions0 = solutions.get(0);
	assertEquals(2, solutions0.getAllOperations().size());

	assertTrue(solutions0.getAllOperations().stream()
			.filter(e -> e.getModified().toString().startsWith("myinst.toPositive(i2)")).findFirst().isPresent());

	assertTrue(solutions0.getPatchDiff().getOriginalStatementAlignmentDiff()
			.contains("-			return myinst.toPositive(i1) * myinst.toNegative(i1)"));

	assertTrue(solutions0.getPatchDiff().getOriginalStatementAlignmentDiff()
			.contains("+			return myinst.toPositive(i1) * myinst.toPositive(i2)"));

}
 
Example 24
@Test
public void test_composed_repair_WMVMcase3() throws Exception {

	String dep = new File("./examples/libs/junit-4.4.jar").getAbsolutePath();

	File out = new File(ConfigurationProperties.getProperty("workingDirectory"));

	CommandSummary command = new CommandSummary();
	command.command.put("-location", new File("./examples/testMultiMet/testWMVMRcase3").getAbsolutePath());
	command.command.put("-mode", "custom");
	command.command.put("-customengine", MultiMetaEvalTOSApproach.class.getName());
	command.command.put("-javacompliancelevel", "7");
	command.command.put("-maxtime", "120");
	command.command.put("-seed", "0");
	command.command.put("-stopfirst", "false");
	command.command.put("-maxgen", "0");
	command.command.put("-population", "1");
	command.command.put("-scope", "local");
	command.command.put("-srcjavafolder", "src/main/java/");
	command.command.put("-srctestfolder", "src/test/java/");
	command.command.put("-binjavafolder", "target/classes/");
	command.command.put("-bintestfolder", "target/test-classes/");
	command.command.put("-id", "test-wmr3");
	command.command.put("-out", out.getAbsolutePath());
	command.command.put("-dependencies", dep);
	command.command.put("-loglevel", "INFO");
	command.command.put("-flthreshold", "0.24");
	command.command.put("-saveall", "true");

	AstorMain main1 = new AstorMain();
	main1.execute(command.flat());

	MultiMetaEvalTOSApproach.MAX_GENERATIONS = 1000;

	MultiMetaEvalTOSApproach approach = (MultiMetaEvalTOSApproach) main1.getEngine();

	approach.getVariants().get(0).getModificationPoints()
			.removeIf(e -> !((e.getCodeElement().getPosition().getLine() == 36
					&& e.getCodeElement().getPosition().getFile().getName().equals("MyBuggy.java"))));

	ModificationPoint mp36 = approach.getVariants().get(0).getModificationPoints().get(0);

	assertTrue(mp36.getCodeElement() instanceof CtReturn);

	Prediction prediction = new Prediction();

	approach.setPredictor(new IPredictor() {

		@Override
		public PredictionResult computePredictionsForModificationPoint(ModificationPoint iModifPoint) {
			// No prediction
			return null;
		}
	});

	CtElement invocationToReplace = mp36.getCodeElement()
			.getElements(e -> (e.toString().equals("myinst.toNegative(i2, 1)") && e instanceof CtInvocation))
			.get(0);
	assertNotNull(invocationToReplace);
	assertTrue(invocationToReplace instanceof CtInvocation);
	// The var inside invocation
	CtElement varWrong = invocationToReplace
			.getElements(e -> (e.toString().contains("i2") && e instanceof CtVariableAccess)).get(0);
	assertNotNull(varWrong);

	prediction.add(new PredictionElement(1, varWrong), new VarReplacementByAnotherVarOp());
	prediction.add(new PredictionElement(2, invocationToReplace), new MethodXMethodReplacementDiffNameOp());

	boolean isSolution = approach.analyzePrediction(approach.getVariants().get(0), mp36, prediction);

	assertTrue(isSolution);
	// check redo
	assertEquals("return myinst.toNegative(i2, 1) * myinst.toPositive(i2)", mp36.getCodeElement().toString());
	approach.atEnd();

	assertTrue(main1.getEngine().getSolutions().size() > 0);

	List<ProgramVariant> solutionVarByVar1 = main1.getEngine().getSolutions();

	assertTrue(solutionVarByVar1.size() > 0);

	Optional<ProgramVariant> solution0 = solutionVarByVar1.stream().filter(soli -> soli.getAllOperations().stream()
			.filter(e -> e.getModified().toString().equals("myinst.toPositive(i1, 1)")
			// && e.getOriginal().toString().equals("(myinst.toPositive(\"1\"))" )
			).findFirst().isPresent()).findFirst();
	assertTrue(solution0.isPresent());

	assertTrue(solution0.get().getPatchDiff().getOriginalStatementAlignmentDiff()
			.contains("+			return myinst.toPositive(i1, 1) * myinst.toPositive(i2)"));
	// check redo
	assertEquals("return myinst.toNegative(i2, 1) * myinst.toPositive(i2)", mp36.getCodeElement().toString());

}
 
Example 25
@Test
public void test_single_testWMRcase2a() throws Exception {

	String dep = new File("./examples/libs/junit-4.4.jar").getAbsolutePath();

	File out = new File(ConfigurationProperties.getProperty("workingDirectory"));

	CommandSummary command = new CommandSummary();
	command.command.put("-location", new File("./examples/testMet/testWMRcase2a").getAbsolutePath());
	command.command.put("-mode", "custom");
	command.command.put("-customengine", MultiMetaEvalTOSApproach.class.getName());
	command.command.put("-javacompliancelevel", "7");
	command.command.put("-maxtime", "120");
	command.command.put("-seed", "0");
	command.command.put("-stopfirst", "false");
	command.command.put("-maxgen", "0");
	command.command.put("-population", "1");
	command.command.put("-scope", "local");
	command.command.put("-srcjavafolder", "src/main/java/");
	command.command.put("-srctestfolder", "src/test/java/");
	command.command.put("-binjavafolder", "target/classes/");
	command.command.put("-bintestfolder", "target/test-classes/");
	command.command.put("-id", "test-wmr1c2");
	command.command.put("-out", out.getAbsolutePath());
	command.command.put("-dependencies", dep);
	command.command.put("-loglevel", "INFO");
	command.command.put("-flthreshold", "0.24");
	command.command.put("-saveall", "true");

	AstorMain main1 = new AstorMain();
	main1.execute(command.flat());

	MultiMetaEvalTOSApproach.MAX_GENERATIONS = 1000;

	MultiMetaEvalTOSApproach approach = (MultiMetaEvalTOSApproach) main1.getEngine();

	approach.getOperatorSpace().getOperators().removeIf(e -> !(e instanceof CompositeMethodXMethodReplacementOp));

	approach.getVariants().get(0).getModificationPoints()
			.removeIf(e -> !((e.getCodeElement().getPosition().getLine() == 33
					&& e.getCodeElement().getPosition().getFile().getName().equals("MyBuggy.java"))));

	ModificationPoint mp36 = approach.getVariants().get(0).getModificationPoints().get(0);

	assertTrue(mp36.getCodeElement() instanceof CtReturn);

	////

	approach.startEvolution();

	approach.atEnd();

	///
	List<ProgramVariant> solutionVarByVar1 = main1.getEngine().getSolutions();

	assertTrue(solutionVarByVar1.size() > 0);

	assertTrue(solutionVarByVar1.get(0).getAllOperations().get(0).getModificationPoint().getCodeElement().toString()
			.contains("return toPositive(i1) * toNegative(i2)"));

	assertTrue("No solution with the target operator", solutionVarByVar1.size() > 0);

	Optional<ProgramVariant> solution0 = solutionVarByVar1.stream()
			.filter(soli -> soli.getAllOperations().stream()
					.filter(e -> TestHelper.equalsNoParentesis(e.getModified().toString(), "(toPositive(i2))")
							&& TestHelper.equalsNoParentesis(e.getOriginal().toString(), "(toNegative(i2))"))
					.findFirst().isPresent())
			.findFirst();
	assertTrue(solution0.isPresent());
	assertTrue(solution0.get().getPatchDiff().getOriginalStatementAlignmentDiff()
			.contains("-			return toPositive(i1) * toNegative(i2);"));
	assertTrue(solution0.get().getPatchDiff().getOriginalStatementAlignmentDiff()
			.contains("+			return toPositive(i1) * toPositive(i2);"));

}
 
Example 26
@Test
public void test_composed_Try_VR_1() throws Exception {

	String dep = new File("./examples/libs/junit-4.4.jar").getAbsolutePath();

	File out = new File(ConfigurationProperties.getProperty("workingDirectory"));

	CommandSummary command = new CommandSummary();
	command.command.put("-location", new File("./examples/testMultiMet/testTryVR").getAbsolutePath());
	command.command.put("-mode", "custom");
	command.command.put("-customengine", MultiMetaEvalTOSApproach.class.getName());
	command.command.put("-javacompliancelevel", "7");
	command.command.put("-maxtime", "120");
	command.command.put("-seed", "0");
	command.command.put("-stopfirst", "false");
	command.command.put("-maxgen", "0");
	command.command.put("-population", "1");
	command.command.put("-scope", "local");
	command.command.put("-srcjavafolder", "src/main/java/");
	command.command.put("-srctestfolder", "src/test/java/");
	command.command.put("-binjavafolder", "target/classes/");
	command.command.put("-bintestfolder", "target/test-classes/");
	command.command.put("-id", "test-try");
	command.command.put("-out", out.getAbsolutePath());
	command.command.put("-dependencies", dep);
	command.command.put("-loglevel", "DEBUG");
	command.command.put("-flthreshold", "0.24");
	command.command.put("-saveall", "true");

	//
	AstorMain main1 = new AstorMain();

	main1.execute(command.flat());

	MultiMetaEvalTOSApproach.MAX_GENERATIONS = 1000;

	MultiMetaEvalTOSApproach approach = (MultiMetaEvalTOSApproach) main1.getEngine();

	approach.getOperatorSpace().getOperators().removeIf(e -> !(e instanceof WrapwithTrySingleStatementOp));

	approach.getVariants().get(0).getModificationPoints()
			.removeIf(e -> !((e.getCodeElement().getPosition().getLine() == 13
					&& e.getCodeElement().getPosition().getFile().getName().equals("MyBuggy.java"))));

	ModificationPoint modificationPoint13 = approach.getVariants().get(0).getModificationPoints().get(0);
	assertTrue(modificationPoint13.getCodeElement().toString().contains("return i1 + i1"));

	//

	Prediction prediction = new Prediction();

	approach.setPredictor(new IPredictor() {

		@Override
		public PredictionResult computePredictionsForModificationPoint(ModificationPoint iModifPoint) {
			// No prediction
			return null;
		}
	});
	ModificationPoint mp13 = modificationPoint13;

	CtElement assignmentToWrap = mp13.getCodeElement();
	assertNotNull(assignmentToWrap);
	assertTrue(assignmentToWrap instanceof CtReturn);
	// The var inside invocation
	CtElement varWrong = assignmentToWrap
			.getElements(e -> (e.toString().contains("i1") && e instanceof CtVariableAccess)).get(1);
	assertNotNull(varWrong);

	// We suppose that the var from assignment needs to be changed from i2 = i2
	prediction.add(new PredictionElement(1, varWrong), new VarReplacementByAnotherVarOp());
	prediction.add(new PredictionElement(2, assignmentToWrap), new WrapwithTrySingleStatementOp());

	boolean isSolution = approach.analyzePrediction(approach.getVariants().get(0), mp13, prediction);
	assertTrue(isSolution);
	///
	approach.atEnd();

	//

	List<ProgramVariant> solutionTry = main1.getEngine().getSolutions();

	ProgramVariant solution1 = solutionTry.get(0);

	assertTrue(solution1.getAllOperations().stream().filter(e -> e.getModified().toString().startsWith("try {"))
			.findFirst().isPresent());

	assertTrue(solution1.getPatchDiff().getOriginalStatementAlignmentDiff().contains("+			try {"));
	assertTrue(solution1.getPatchDiff().getOriginalStatementAlignmentDiff().contains("-			return i1 + i1;"));
	assertTrue(solution1.getPatchDiff().getOriginalStatementAlignmentDiff()
			.contains("+				return i1 + i2;"));
	assertTrue(solution1.getPatchDiff().getOriginalStatementAlignmentDiff()
			.contains("catch (java.lang.Exception e) {}"));

}
 
Example 27
Source Project: gumtree-spoon-ast-diff   Source File: LabelFinder.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public <R> void visitCtReturn(CtReturn<R> e) {
	label = "return";
}