Java Code Examples for spoon.reflect.code.CtBinaryOperator#setKind()

The following examples show how to use spoon.reflect.code.CtBinaryOperator#setKind() . 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: WrapwithIfNullCheck.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
private List<Ingredient> computeIngredientsNullCheck(ModificationPoint modificationPoint,
		List<CtVariableAccess> varAccessInModificationPoints) {

	List<Ingredient> ingredients = new ArrayList();

	for (CtVariableAccess iVariableAccess : varAccessInModificationPoints) {

		// Let's check the type, if primitive discard it
		if (iVariableAccess.getVariable() != null && iVariableAccess.getVariable().getType() != null
				&& iVariableAccess.getVariable().getType().isPrimitive())
			continue;

		CtVariableAccess iVariableAccessC = iVariableAccess.clone();
		MutationSupporter.clearPosition(iVariableAccessC);

		CtBinaryOperator<Boolean> binaryOp = new CtBinaryOperatorImpl<>();
		binaryOp.setLeftHandOperand(iVariableAccessC);
		binaryOp.setRightHandOperand(MutationSupporter.getFactory().createCodeSnippetExpression("null"));
		binaryOp.setKind(BinaryOperatorKind.NE);

		ingredients.add(new Ingredient(binaryOp));

	}

	return ingredients;
}
 
Example 2
Source File: SingleOperatorChangeOperator.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Override
public boolean applyModification() {
	CtBinaryOperator newBinaryOperator = new CtBinaryOperatorImpl<>();
	newBinaryOperator.setKind(operator);

	CtExpression leftOriginal2 = (CtExpression) MetaGenerator.geOriginalElement(leftOriginal);
	newBinaryOperator.setLeftHandOperand(leftOriginal2.clone());

	CtExpression rightOriginal2 = (CtExpression) MetaGenerator.geOriginalElement(rightOriginal);
	newBinaryOperator.setRightHandOperand(rightOriginal2.clone());

	//
	newBinaryOperator.setFactory(MutationSupporter.getFactory());
	newBinaryOperator.setParent(original.getParent());
	//

	super.setOriginal(original);
	super.setModified(newBinaryOperator);

	return super.applyModification();
}
 
Example 3
Source File: SingleLogicExpOperator.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Override
public boolean applyModification() {
	CtBinaryOperator binaryOperator = new CtBinaryOperatorImpl<>();
	binaryOperator.setKind(operator);

	CtExpression leftOriginal2 = (CtExpression) MetaGenerator.geOriginalElement(leftOriginal);
	binaryOperator.setLeftHandOperand(leftOriginal2.clone());

	CtExpression newRightExpression = rightNew;
	binaryOperator.setRightHandOperand(newRightExpression);

	//
	binaryOperator.setFactory(MutationSupporter.getFactory());
	binaryOperator.setParent(leftOriginal2.getParent());
	//

	super.setOriginal(leftOriginal2);
	super.setModified(binaryOperator);

	return super.applyModification();
}
 
Example 4
Source File: ConditionRemoveTransform.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@SuppressWarnings({ "rawtypes", "static-access" })
@Override
public <T> void visitCtBinaryOperator(CtBinaryOperator<T> operator) {
	super.visitCtBinaryOperator(operator);
	
	if(operator.getKind()==BinaryOperatorKind.AND||operator.getKind()==BinaryOperatorKind.OR) {
		CtExpression right = operator.getRightHandOperand();
		operator.setKind(BinaryOperatorKind.AND);
		CtLiteral<Boolean> literalvalue = this.mutSupporter.getFactory().Core().createLiteral();
		Boolean bval=true;
		literalvalue.setValue(bval);
		operator.setRightHandOperand(literalvalue);
		saveSketchAndSynthesize();
		operator.setRightHandOperand(right);
		resoreDiskFile();
		
		CtExpression left = operator.getLeftHandOperand();
		operator.setKind(BinaryOperatorKind.AND);
		operator.setLeftHandOperand(literalvalue);
		saveSketchAndSynthesize();
		operator.setLeftHandOperand(left);
		resoreDiskFile();
	}
}
 
Example 5
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 6
Source File: BinaryOperatorMutator.java    From spoon-examples with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void process(CtElement candidate) {
	if (!(candidate instanceof CtBinaryOperator)) {
		return;
	}
	CtBinaryOperator op = (CtBinaryOperator)candidate;
	op.setKind(BinaryOperatorKind.MINUS);
}
 
Example 7
Source File: LogicExpOperator.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private List<Ingredient> computeIngredientsFromExpressionExplansion(ModificationPoint modificationPoint,
		CtExpression previousExpression, List<IngredientFromDyna> ingredientsDynamoth,
		BinaryOperatorKind operatorKind2) {

	List<Ingredient> ingredientsNewBinaryExpressions = new ArrayList();

	for (IngredientFromDyna ingredientFromDyna : ingredientsDynamoth) {

		CtBinaryOperator binaryOperator = new CtBinaryOperatorImpl<>();
		binaryOperator.setKind(operatorKind2);

		CtExpression previousExpressionCloned = previousExpression.clone();
		MutationSupporter.clearPosition(previousExpressionCloned);
		binaryOperator.setLeftHandOperand(previousExpressionCloned);

		CtExpression newRightExpression = MutationSupporter.getFactory()
				.createCodeSnippetExpression(ingredientFromDyna.getDynmothExpression().toString());
		binaryOperator.setRightHandOperand(newRightExpression);

		//
		binaryOperator.setFactory(MutationSupporter.getFactory());
		binaryOperator.setParent(previousExpression.getParent());

		Ingredient newIngredientExtended = new Ingredient(binaryOperator);
		// Updated: new code
		newIngredientExtended.getMetadata().put("operator", operatorKind2);
		newIngredientExtended.getMetadata().put("right", newRightExpression);
		newIngredientExtended.getMetadata().put("leftoriginal", previousExpression);
		//

		ingredientsNewBinaryExpressions.add(newIngredientExtended);
	}

	return ingredientsNewBinaryExpressions;
}
 
Example 8
Source File: ArithmeticOperatorMetaMutator.java    From metamutator with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Converts "a op b" bean op one of "-", "+", "*", "/"
 *    (  (op(1, 0, "-") && (a - b))
 *    || (op(1, 1, "+") && (a + b))
 *    || (op(1, 2, "*") && (a * b))
 *    || (op(1, 3, "/") && (a / b))
 *    )
 *
 * @param expression
 * @param operators
 */
private void mutateOperator(final CtBinaryOperator<Boolean> expression, EnumSet<BinaryOperatorKind> operators) {
	
	if (!operators.contains(expression.getKind())) {
		throw new IllegalArgumentException("not consistent");
	}

	if (alreadyInHotsSpot(expression)
			|| expression.toString().contains(".is(\"")) {
		System.out
				.println(String
						.format("Expression '%s' ignored because it is included in previous hot spot",
								expression));
		return;
	}

	int thisIndex = ++index;
	BinaryOperatorKind originalKind = expression.getKind();
	String originalExpression = expression.toString();
	
	String newExpression = "";
	
	int cpt = 0;
	BinaryOperatorKind tmp = null;
	for(BinaryOperatorKind op : ARITHMETIC_OPERATORS){
			expression.setKind(op);
			newExpression += "(" + PREFIX + thisIndex + ".is(" + op.getClass().getCanonicalName()+"."+op.toString() + ")) ? (" + expression + ")";
			newExpression += " : ";
	}

	newExpression += "(" + originalExpression + ")"; 
	CtCodeSnippetExpression<Boolean> codeSnippet = getFactory().Core()
			.createCodeSnippetExpression();
	codeSnippet.setValue('(' + newExpression + ')');
	expression.replace(codeSnippet);
	expression.replace(expression);
	Selector.generateSelector(expression, originalKind, thisIndex, operators, PREFIX);
	System.out.println("nb mutants " +index);

	hostSpots.add(expression);
}
 
Example 9
Source File: OnTheFlyTransfoTest.java    From spoon-examples with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void example() throws Exception {
 Launcher l = new Launcher();
 
 // required for having IFoo.class in the classpath in Maven
 l.setArgs(new String[] {"--source-classpath","target/test-classes"});
 
 l.addInputResource("src/test/resources/transformation/");
 l.buildModel();
 
 CtClass foo = l.getFactory().Package().getRootPackage().getElements(new NamedElementFilter<>(CtClass.class, "Foo1")).get(0);

 // compiling and testing the initial class
 Class<?> fooClass = InMemoryJavaCompiler.newInstance().compile(foo.getQualifiedName(), "package "+foo.getPackage().getQualifiedName()+";"+foo.toString());
 IFoo x = (IFoo) fooClass.newInstance();
 // testing its behavior
 assertEquals(5, x.m());

 // now we apply a transformation
 // we replace "+" by "-"
 for(Object e : foo.getElements(new TypeFilter(CtBinaryOperator.class))) {
  CtBinaryOperator op = (CtBinaryOperator)e;
  if (op.getKind()==BinaryOperatorKind.PLUS) {
	  op.setKind(BinaryOperatorKind.MINUS);
  }
 }
 
 // first assertion on the results of the transfo
 
 // there are no more additions in the code
 assertEquals(0, foo.getElements(new Filter<CtBinaryOperator<?>>() {
@Override
public boolean matches(CtBinaryOperator<?> arg0) {
	return arg0.getKind()==BinaryOperatorKind.PLUS;
}		  
 }).size());

 // second assertions on the behavior of the transformed code
 
 // compiling and testing the transformed class
 fooClass = InMemoryJavaCompiler.newInstance().compile(foo.getQualifiedName(), "package "+foo.getPackage().getQualifiedName()+";"+foo.toString());
 IFoo y = (IFoo) fooClass.newInstance();
 // testing its behavior with subtraction
 assertEquals(1, y.m());
 
 System.out.println("yes y.m()="+y.m());
}