spoon.reflect.cu.position.NoSourcePosition Java Examples

The following examples show how to use spoon.reflect.cu.position.NoSourcePosition. 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: PatchGenerator.java    From nopol with GNU General Public License v2.0 5 votes vote down vote up
private CtElement getTarget() {
	CtType type = factory.Type().get(patch.getSourceLocation().getRootClassName());
	EarlyTerminatingScanner<CtElement> targetFinder = new EarlyTerminatingScanner<CtElement>() {
		@Override
		protected void enter(CtElement e) {
			if (e.getPosition() instanceof NoSourcePosition) {
				return;
			}
			if (e.getPosition().getSourceStart() == patch.getSourceLocation().getBeginSource()
					&& e.getPosition().getSourceEnd() == patch.getSourceLocation().getEndSource() && e.isImplicit() == false) {
				if (patch.getType() == RepairType.CONDITIONAL && e instanceof CtIf) {
					setResult(((CtIf) e).getCondition());
				} else {
					setResult(e);
				}
				terminate();
				return;
			}
			if (e.getPosition().getSourceStart() <= patch.getSourceLocation().getBeginSource()
					&& e.getPosition().getSourceEnd() >= patch.getSourceLocation().getEndSource()) {
				super.enter(e);
			}
		}
	};
	type.accept(targetFinder);
	return targetFinder.getResult();
}
 
Example #2
Source File: VariablesInSuspiciousCollector.java    From nopol with GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean isToBeProcessed(CtTypedElement<?> candidate) {
    if (candidate.getPosition() == null || candidate.getPosition() instanceof NoSourcePosition) {
        return false;
    }
    return candidate.getPosition().getLine() == location.getLineNumber();
}
 
Example #3
Source File: InitialisationProcessor.java    From nopol with GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean isToBeProcessed(CtStatement statement) {
    if (statement.getPosition() == null || statement.getPosition() instanceof NoSourcePosition) {
        return false;
    }
    return (statement.getPosition().getLine() == target().getPosition().getLine()) &&
            (statement.getPosition().getColumn() == target().getPosition().getColumn()) &&
            (FileLibrary.isSameFile(target().getPosition().getFile(), statement.getPosition().getFile()));
}
 
Example #4
Source File: NopolProcessorBuilder.java    From nopol with GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean isToBeProcessed(CtStatement candidate) {
    SourcePosition position = candidate.getPosition();
    if (position == null || position instanceof NoSourcePosition) {
        return false;
    }
    if (!new LineFilter().matches(candidate)) {
        return false;
    }
    boolean isSameFile = FileLibrary.isSameFile(file, position.getFile());
    boolean isSameLine = position.getLine() == this.line;
    return isSameLine && isSameFile && super.isToBeProcessed(candidate);
}
 
Example #5
Source File: NopolProcessor.java    From nopol with GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean isToBeProcessed(CtStatement statement) {
    if (statement.getPosition() instanceof NoSourcePosition || target.getPosition() instanceof NoSourcePosition) {
        return false;
    }
    return target.getPosition().getSourceStart() == statement.getPosition().getSourceStart() && target.getPosition().getSourceEnd() == statement.getPosition().getSourceEnd() && statement.equals(target);
}
 
Example #6
Source File: CodeSnippetFilter.java    From nopol with GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean matches(CtElement element) {
    SourcePosition position = element.getPosition();
    if (position != null && !(position instanceof NoSourcePosition)) {
        return FileLibrary.isSameFile(classSourceFile(), position.getFile()) && codeSnippet().equals(element.toString());
    }
    return false;
}
 
Example #7
Source File: TreeTest.java    From gumtree-spoon-ast-diff with Apache License 2.0 5 votes vote down vote up
@Test
public void test_bug_Possition() throws Exception {
	AstComparator comparator = new AstComparator();
	File fl = new File("src/test/resources/examples/roots/test8/left_QuickNotepad_1.13.java");
	File fr = new File("src/test/resources/examples/roots/test8/right_QuickNotepad_1.14.java");

	CtType<?> astLeft = comparator.getCtType(fl);

	assertNotNull(astLeft);

	CtType<?> astRight = comparator.getCtType(fr);
	assertNotNull(astRight);

	Diff diffResult = comparator.compare(astLeft, astRight);
	List<Operation> rootOperations = diffResult.getRootOperations();
	getPaths(rootOperations);
	List<Operation> allOperations = diffResult.getAllOperations();
	getPaths(allOperations);

	assertEquals(1, rootOperations.size());

	SourcePosition position = rootOperations.get(0).getSrcNode().getPosition();
	assertTrue(position.getLine() > 0);
	assertEquals(113, position.getLine());

	assertTrue(!(position instanceof NoSourcePosition));

}
 
Example #8
Source File: WrapwithIfNullCheck.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Override
	public List<MetaOperatorInstance> createMetaOperatorInstances(ModificationPoint modificationPoint) {

		//
		CtElement codeElement = (targetElement == null) ? modificationPoint.getCodeElement() : targetElement;

		List<CtVariableAccess> varAccessInModificationPoints = VariableResolver.collectVariableAccess(codeElement);

		List<Ingredient> ingredients = this.computeIngredientsNullCheck(modificationPoint,
				varAccessInModificationPoints);

		// The parameters to be included in the new method
		List<CtVariableAccess> varsToBeParameters = varAccessInModificationPoints;

		// List of parameters
		List<CtParameter<?>> parameters = new ArrayList<>();
		List<CtExpression<?>> realParameters = new ArrayList<>();
		for (CtVariableAccess ctVariableAccess : varsToBeParameters) {
			// the parent is null, it is setter latter
			CtParameter pari = MutationSupporter.getFactory().createParameter(null, ctVariableAccess.getType(),
					ctVariableAccess.getVariable().getSimpleName());
			parameters.add(pari);
			realParameters.add(ctVariableAccess.clone().setPositions(new NoSourcePosition()));
		}
///
		// let's start with one, and let's keep the Zero for the default (all ifs are
		// false)
		int candidateNumber = 0;
		CtTypeReference returnType = MutationSupporter.getFactory().createCtTypeReference(Boolean.class);

		MetaOperatorInstance megaOp = MetaGenerator.createMetaStatementReplacement(modificationPoint, codeElement,
				MutationSupporter.getFactory().createCodeSnippetExpression("true"), candidateNumber, ingredients,
				parameters, realParameters, this, returnType);
		List<MetaOperatorInstance> opsMega = new ArrayList();
		opsMega.add(megaOp);

		return opsMega;
	}
 
Example #9
Source File: WrapwithIfOp.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Override
public List<MetaOperatorInstance> createMetaOperatorInstances(ModificationPoint modificationPoint,
		List<IngredientFromDyna> ingredients) {

	// The parameters to be included in the new method
	List<CtVariableAccess> varsToBeParameters = SupportOperators.collectAllVarsFromDynaIngredients(ingredients,
			modificationPoint);

	// List of parameters
	List<CtParameter<?>> parameters = new ArrayList<>();
	List<CtExpression<?>> realParameters = new ArrayList<>();
	for (CtVariableAccess ctVariableAccess : varsToBeParameters) {
		// the parent is null, it is setter latter
		CtParameter pari = MutationSupporter.getFactory().createParameter(null, ctVariableAccess.getType(),
				ctVariableAccess.getVariable().getSimpleName());
		parameters.add(pari);
		realParameters.add(ctVariableAccess.clone().setPositions(new NoSourcePosition()));
	}

	// let's start with one, and let's keep the Zero for the default (all ifs are
	// false)
	int candidateNumber = 0;
	CtTypeReference returnType = MutationSupporter.getFactory().createCtTypeReference(Boolean.class);

	CtElement codeElement = (targetElement == null) ? modificationPoint.getCodeElement() : targetElement;

	// let's create the meta

	MetaOperatorInstance megaOp = MetaGenerator.createMetaStatementReplacement(modificationPoint, codeElement,
			MutationSupporter.getFactory().createCodeSnippetExpression("true"), candidateNumber,
			ingredients.stream().map(Ingredient.class::cast).collect(Collectors.toList())//
			, parameters, realParameters, this, returnType);
	List<MetaOperatorInstance> opsMega = new ArrayList();
	opsMega.add(megaOp);

	return opsMega;
}
 
Example #10
Source File: TreeTest.java    From gumtree-spoon-ast-diff with Apache License 2.0 4 votes vote down vote up
@Test
public void test_bug_Possition_from_String() {
	String c1 = "" + "class X {\n" + "public void foo() {\n" + " int x = 0;\n" + "}" + "};";

	String c2 = "" + "class X {\n" + "public void foo() {\n" + " int x = 1;\n" + "}" + "};";

	AstComparator diff = new AstComparator();
	Diff editScript = diff.compare(c1, c2);
	assertTrue(editScript.getRootOperations().size() == 1);

	List<Operation> rootOperations = editScript.getRootOperations();

	assertEquals(1, rootOperations.size());

	SourcePosition position = rootOperations.get(0).getSrcNode().getPosition();
	assertTrue(!(position instanceof NoSourcePosition));

	assertTrue(position.getLine() > 0);
	assertEquals(3, position.getLine());

}
 
Example #11
Source File: MutationSupporter.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public static void clearPosition(CtElement expCloned) {
	expCloned.setPosition(new NoSourcePosition());
	expCloned.getElements(e -> true).stream().forEach(e -> e.setPosition(new NoSourcePosition()));
}
 
Example #12
Source File: InitialConceptMetEngine.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public void clearPosition(CtExpression expCloned) {
	expCloned.getElements(e -> true).stream().forEach(e -> e.setPosition(new NoSourcePosition()));
}
 
Example #13
Source File: MetaGenerator.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public static MetaOperatorInstance createMetaStatementReplacement(ModificationPoint modificationPoint,
		CtElement elementSource, CtExpression defaultReturnElement, int variableCounter,
		List<Ingredient> ingredients, List<CtParameter<?>> parameters, List<CtExpression<?>> realParameters,
		AstorOperator parentOperator, CtTypeReference returnType) {

	MetaOperatorInstance opMega = new MetaOperatorInstance((MetaOperator) parentOperator,
			MetaGenerator.getNewIdentifier());

	List<OperatorInstance> opsOfVariant = new ArrayList();
	Map<Integer, Ingredient> ingredientOfMapped = new HashMap<>();

	realParameters = filterParameter(realParameters);
	// Creation of mega method
	CtMethod<?> megaMethod = createMegaMethod(opMega, modificationPoint, defaultReturnElement, variableCounter,
			ingredients, parameters, ingredientOfMapped, returnType);

	CtInvocation newInvocationToMega = creationInvocationToMega(modificationPoint, realParameters, megaMethod);

	// Now the if to be inserted:

	CtIf ifNew = MutationSupporter.getFactory().createIf();

	CtStatement statementPointed = (CtStatement) modificationPoint.getCodeElement();
	CtStatement statementPointedCloned = statementPointed.clone();
	statementPointedCloned.setPosition(new NoSourcePosition());
	MutationSupporter.clearPosition(statementPointedCloned);

	ifNew.setThenStatement(statementPointedCloned);
	ifNew.setCondition(newInvocationToMega);

	// Let's create the operations
	OperatorInstance opInstace = new StatementOperatorInstance(modificationPoint, parentOperator, statementPointed,
			ifNew);
	opsOfVariant.add(opInstace);

	// The meta method to be added
	OperatorInstance opMethodAdd = new OperatorInstance();
	opMethodAdd.setOperationApplied(new InsertMethodOperator());
	opMethodAdd.setOriginal(modificationPoint.getCodeElement());
	opMethodAdd.setModified(megaMethod);
	opMethodAdd.setModificationPoint(modificationPoint);
	opsOfVariant.add(opMethodAdd);

	//
	log.debug("method: \n" + megaMethod);

	log.debug("invocation: \n" + newInvocationToMega);

	opMega.setOperatorInstances(opsOfVariant);
	opMega.setAllIngredients(ingredientOfMapped);
	opMega.setOperationApplied(parentOperator);
	opMega.setOriginal(modificationPoint.getCodeElement());
	opMega.setModificationPoint(modificationPoint);

	return opMega;
}
 
Example #14
Source File: MethodXMethodReplacementOp.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Override
public List<MetaOperatorInstance> createMetaOperatorInstances(ModificationPoint modificationPoint) {

	List<MetaOperatorInstance> opsMega = new ArrayList();

	MapList<CtInvocation, Ingredient> ingredientsPerInvocation = this
			.retrieveInvocationIngredient(modificationPoint);
	if (ingredientsPerInvocation.isEmpty()) {
		log.debug("Any ingredient (other invocations) for modif point " + modificationPoint.identified);
		// Nothing to replace
		return opsMega;
	}

	log.debug("\nMethodInvoReplacement: \n" + modificationPoint);

	// let's start with one, and let's keep the Zero for the default (all ifs are
	// false)

	// As difference with var replacement, a metamutant for each invocation
	for (CtInvocation invocationToReplace : ingredientsPerInvocation.keySet()) {

		int invocationCounter = 0;

		List<Ingredient> ingredients = ingredientsPerInvocation.get(invocationToReplace);

		List<CtVariableAccess> varsToBeParameters = new ArrayList<>();

		// The parameters to be included in the new method
		for (Ingredient ingredient : ingredients) {
			SupportOperators.putVarsNotDuplicated(ingredient.getCode(), varsToBeParameters);

		}

		// The variable from the existing invocation must also be a parameter
		SupportOperators.putVarsNotDuplicated(modificationPoint.getCodeElement(), varsToBeParameters);

		// List of parameters
		List<CtParameter<?>> parameters = new ArrayList<>();
		List<CtExpression<?>> realParameters = new ArrayList<>();
		for (CtVariableAccess ctVariableAccess : varsToBeParameters) {
			// the parent is null, it is setter latter
			CtParameter pari = MutationSupporter.getFactory().createParameter(null, ctVariableAccess.getType(),
					ctVariableAccess.getVariable().getSimpleName());
			parameters.add(pari);
			realParameters.add(ctVariableAccess.clone().setPositions(new NoSourcePosition()));
		}

		invocationCounter++;

		/// Let's start creating the body of the new method.
		// first the main try
		CtTypeReference returnTypeOfInvocation = invocationToReplace.getType();

		MetaOperatorInstance megaOp = MetaGenerator.createMetaFineGrainedReplacement(modificationPoint,
				invocationToReplace, invocationCounter, ingredients, parameters, realParameters, this,
				returnTypeOfInvocation);
		opsMega.add(megaOp);

	} // End invocation

	return opsMega;
}
 
Example #15
Source File: VarReplacementByAnotherVarOp.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Override
public List<MetaOperatorInstance> createMetaOperatorInstances(ModificationPoint modificationPoint) {

	MetaOperatorInstance opMega = new MetaOperatorInstance(this, MetaGenerator.getNewIdentifier());

	List<OperatorInstance> opsOfVariant = new ArrayList();

	Map<Integer, Ingredient> ingredientOfMapped = new HashMap<>();

	//
	List<CtVariableAccess> varAccessInModificationPoints = null;

	if (targetElement == null) {
		varAccessInModificationPoints = VariableResolver.collectVariableAccess(modificationPoint.getCodeElement(),
				// it must be true because, even we have vars with different names, they are
				// different access.
				true);
	} else {
		varAccessInModificationPoints = new ArrayList<>();
		varAccessInModificationPoints.add((CtVariableAccess) targetElement);
	}

	log.debug("\nModifcationPoint: \n" + modificationPoint);

	// let's start with one, and let's keep the Zero for the default (all ifs are
	// false)
	// TODO: we only can activate one mutant

	int variableCounter = 0;
	for (CtVariableAccess variableAccessToReplace : varAccessInModificationPoints) {

		// The return type of the new method correspond to the type of variable to
		// change
		CtTypeReference returnType = variableAccessToReplace.getType();

		List<Ingredient> ingredients = this.computeIngredientsFromVarToReplace(modificationPoint,
				variableAccessToReplace);

		if (ingredients.isEmpty()) {
			// Nothing to replace
			continue;
		}

		// The parameters to be included in the new method
		List<CtVariableAccess> varsToBeParameters = ingredients.stream().map(e -> e.getCode())
				.map(CtVariableAccess.class::cast).collect(Collectors.toList());
		// The variable to be replaced must also be a parameter
		varsToBeParameters.add(variableAccessToReplace);

		// List of parameters
		List<CtParameter<?>> parameters = new ArrayList<>();
		List<CtExpression<?>> realParameters = new ArrayList<>();
		for (CtVariableAccess ctVariableAccess : varsToBeParameters) {
			// the parent is null, it is setter latter
			CtParameter pari = MutationSupporter.getFactory().createParameter(null, ctVariableAccess.getType(),
					ctVariableAccess.getVariable().getSimpleName());
			parameters.add(pari);
			realParameters.add(ctVariableAccess.clone().setPositions(new NoSourcePosition()));
		}

		variableCounter++;

		MetaGenerator.createMetaForSingleElement(opMega, modificationPoint, variableAccessToReplace,
				variableCounter, ingredients, parameters, realParameters, returnType, opsOfVariant,
				ingredientOfMapped);

	} // End variable

	opMega.setOperatorInstances(opsOfVariant);
	opMega.setAllIngredients(ingredientOfMapped);
	opMega.setOperationApplied(this);
	opMega.setOriginal(modificationPoint.getCodeElement());
	opMega.setModificationPoint(modificationPoint);

	List<MetaOperatorInstance> opsMega = new ArrayList();
	opsMega.add(opMega);

	return opsMega;
}
 
Example #16
Source File: VarReplacementByMethodCallOp.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Override
public List<MetaOperatorInstance> createMetaOperatorInstances(ModificationPoint modificationPoint) {

	MetaOperatorInstance opMega = new MetaOperatorInstance(this, MetaGenerator.getNewIdentifier());

	// Let's create one meta per modif point
	List<OperatorInstance> metaOperations = new ArrayList();
	List<MetaOperatorInstance> opsMega = new ArrayList();

	// Map that allows to trace the mutant id with the ingredient used
	Map<Integer, Ingredient> ingredientOfMapped = new HashMap<>();

	//
	List<CtVariableAccess> varAccessInModificationPoints = null;

	if (targetElement == null) {
		varAccessInModificationPoints = VariableResolver.collectVariableAccess(modificationPoint.getCodeElement(),
				false);
	} else {
		varAccessInModificationPoints = new ArrayList<>();
		varAccessInModificationPoints.add((CtVariableAccess) targetElement);
	}

	log.debug("\nModifcationPoint: \n" + modificationPoint);

	MapList<CtTypeReference, CtInvocation> cacheIngredientsPerType = new MapList<>();

	int variableCounter = 0;
	for (CtVariableAccess variableAccessToReplace : varAccessInModificationPoints) {

		List<Ingredient> ingredients = this.computeIngredientsFromVarToReplace(modificationPoint,
				variableAccessToReplace, cacheIngredientsPerType);

		if (ingredients.isEmpty()) {
			continue;
		}

		// The parameters to be included in the new method
		List<CtVariableAccess> varsToBeParametersTemp = ingredients.stream()
				.map(e -> e.getCode().getElements(new TypeFilter<>(CtVariableAccess.class))).flatMap(List::stream)
				.map(CtVariableAccess.class::cast).distinct().collect(Collectors.toList());

		// The variable to be replaced must also be a parameter

		varsToBeParametersTemp.add(variableAccessToReplace);
		// Let's check the names (the previous filter does not filter all duplicates, so
		// to avoid problems we do it manually):

		List<CtVariableAccess> varsToBeParameters = new ArrayList();
		for (CtVariableAccess parameterFound : varsToBeParametersTemp) {
			boolean hasname = false;
			for (CtVariableAccess parameterConsiderer : varsToBeParameters) {
				if (parameterConsiderer.getVariable().getSimpleName()
						.equals(parameterFound.getVariable().getSimpleName())) {
					hasname = true;
					break;
				}
			}
			// any variable with that name, so we add in parameters
			if (!hasname) {
				varsToBeParameters.add(parameterFound);
			}
		}

		// List of parameters
		List<CtParameter<?>> parameters = new ArrayList<>();
		List<CtExpression<?>> realParameters = new ArrayList<>();
		for (CtVariableAccess ctVariableAccess : varsToBeParameters) {
			// the parent is null, it is setter latter
			CtParameter pari = MutationSupporter.getFactory().createParameter(null, ctVariableAccess.getType(),
					ctVariableAccess.getVariable().getSimpleName());
			parameters.add(pari);
			realParameters.add(ctVariableAccess.clone().setPositions(new NoSourcePosition()));
		}

		variableCounter++;

		CtTypeReference returnType = variableAccessToReplace.getType();

		MetaGenerator.createMetaForSingleElement(opMega, modificationPoint, variableAccessToReplace,
				variableCounter, ingredients, parameters, realParameters, returnType, metaOperations,
				ingredientOfMapped);

	} // End variable
	opMega.setOperatorInstances(metaOperations);
	opsMega.add(opMega);
	opMega.setAllIngredients(ingredientOfMapped);
	opMega.setOperationApplied(this);
	opMega.setOriginal(modificationPoint.getCodeElement());
	opMega.setModificationPoint(modificationPoint);

	return opsMega;
}
 
Example #17
Source File: ConstReplacementOp.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Override
public List<MetaOperatorInstance> createMetaOperatorInstances(ModificationPoint modificationPoint) {

	MetaOperatorInstance opMega = new MetaOperatorInstance(this, MetaGenerator.getNewIdentifier());

	List<OperatorInstance> opsOfVariant = new ArrayList();

	List<MetaOperatorInstance> opsMega = new ArrayList();

	Map<Integer, Ingredient> ingredientOfMapped = new HashMap<>();

	List<CtLiteral> literalsInModificationPoints = null;

	if (targetElement == null) {
		literalsInModificationPoints = modificationPoint.getCodeElement()
				.getElements(new TypeFilter<>(CtLiteral.class));
	} else {
		literalsInModificationPoints = new ArrayList<>();
		literalsInModificationPoints.add((CtLiteral) targetElement);
	}

	List<CtLiteral> getAllLiteralsFromClass = modificationPoint.getCtClass()
			.getElements(new TypeFilter<>(CtLiteral.class)).stream().distinct().collect(Collectors.toList());

	log.debug("\nModifcationPoint: \n" + modificationPoint);

	int variableCounter = 0;
	for (CtLiteral variableAccessToReplace : literalsInModificationPoints) {

		// The return type of the new method correspond to the type of variable to
		// change
		CtTypeReference returnType = variableAccessToReplace.getType();

		List<Ingredient> ingredients = this.computeIngredientsFromLiteralsToReplace(modificationPoint,
				variableAccessToReplace, getAllLiteralsFromClass);

		if (ingredients.isEmpty()) {
			// Nothing to replace
			continue;
		}

		// The parameters to be included in the new method
		List<CtVariableAccess> varsToBeParameters = ingredients.stream()
				.filter(e -> e.getCode() instanceof CtVariableAccess).map(e -> e.getCode())
				.map(CtVariableAccess.class::cast).collect(Collectors.toList());
		// The variable to be replaced must also be a parameter

		// List of parameters
		List<CtParameter<?>> parameters = new ArrayList<>();
		List<CtExpression<?>> realParameters = new ArrayList<>();
		for (CtVariableAccess ctVariableAccess : varsToBeParameters) {
			// the parent is null, it is setter latter
			CtParameter pari = MutationSupporter.getFactory().createParameter(null, ctVariableAccess.getType(),
					ctVariableAccess.getVariable().getSimpleName());
			parameters.add(pari);
			realParameters.add(ctVariableAccess.clone().setPositions(new NoSourcePosition()));
		}

		variableCounter++;

		MetaGenerator.createMetaForSingleElement(opMega, modificationPoint, variableAccessToReplace,
				variableCounter, ingredients, parameters, realParameters, returnType, opsOfVariant,
				ingredientOfMapped);

	} // End variable

	opMega.setOperatorInstances(opsOfVariant);
	opMega.setAllIngredients(ingredientOfMapped);
	opMega.setOperationApplied(this);
	opMega.setOriginal(modificationPoint.getCodeElement());
	opMega.setModificationPoint(modificationPoint);

	opsMega.add(opMega);

	return opsMega;
}
 
Example #18
Source File: MethodXVariableReplacementOp.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Override
public List<MetaOperatorInstance> createMetaOperatorInstances(ModificationPoint modificationPoint) {

	MetaOperatorInstance opMega = new MetaOperatorInstance(this, MetaGenerator.getNewIdentifier());

	List<OperatorInstance> opsOfVariant = new ArrayList();

	Map<Integer, Ingredient> ingredientOfMapped = new HashMap<>();

	//
	List<CtInvocation> invocationsFromModifPoints = getInvocations(modificationPoint.getCodeElement());

	log.debug("\nModifcationPoint: \n" + modificationPoint);

	// let's start with one, and let's keep the Zero for the default (all ifs are
	// false)
	// TODO: we only can activate one mutant

	int variableCounter = 0;
	for (CtInvocation invocationToReplace : invocationsFromModifPoints) {

		// The return type of the new method correspond to the type of variable to
		// change
		CtTypeReference returnType = invocationToReplace.getType();

		List<Ingredient> ingredients = this.computeIngredientsFromMInvokToReplace(modificationPoint,
				invocationToReplace);

		if (ingredients.isEmpty()) {
			// Nothing to replace
			continue;
		}

		// The parameters to be included in the new method
		List<CtVariableAccess> varsToBeParameters = ingredients.stream().map(e -> e.getCode())
				.map(CtVariableAccess.class::cast).collect(Collectors.toList());
		// The variable from the existing invocation must also be a parameter
		SupportOperators.putVarsNotDuplicated(modificationPoint.getCodeElement(), varsToBeParameters);

		// List of parameters
		List<CtParameter<?>> parameters = new ArrayList<>();
		List<CtExpression<?>> realParameters = new ArrayList<>();
		for (CtVariableAccess ctVariableAccess : varsToBeParameters) {
			// the parent is null, it is setter latter
			CtParameter pari = MutationSupporter.getFactory().createParameter(null, ctVariableAccess.getType(),
					ctVariableAccess.getVariable().getSimpleName());
			parameters.add(pari);
			realParameters.add(ctVariableAccess.clone().setPositions(new NoSourcePosition()));
		}

		variableCounter++;

		MetaGenerator.createMetaForSingleElement(opMega, modificationPoint, invocationToReplace, variableCounter,
				ingredients, parameters, realParameters, returnType, opsOfVariant, ingredientOfMapped);

	} // End variable

	opMega.setOperatorInstances(opsOfVariant);
	opMega.setAllIngredients(ingredientOfMapped);
	opMega.setOperationApplied(this);
	opMega.setOriginal(modificationPoint.getCodeElement());
	opMega.setModificationPoint(modificationPoint);

	List<MetaOperatorInstance> opsMega = new ArrayList();
	opsMega.add(opMega);

	return opsMega;
}