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

The following examples show how to use spoon.reflect.code.CtBinaryOperator#getKind() . 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: LogicalExpressionMetaMutator.java    From metamutator with GNU General Public License v3.0 6 votes vote down vote up
public void process(CtBinaryOperator<Boolean> binaryOperator) {
	BinaryOperatorKind kind = binaryOperator.getKind();
	
	if (LOGICAL_OPERATORS.contains(kind)) {
		mutateOperator(binaryOperator, LOGICAL_OPERATORS);
	} else if (COMPARISON_OPERATORS.contains(kind)) {
		if (isNumber(binaryOperator.getLeftHandOperand())
		 && isNumber(binaryOperator.getRightHandOperand()))
		{
			mutateOperator(binaryOperator, COMPARISON_OPERATORS);
		}
		 else {
			 EnumSet<BinaryOperatorKind> clone = REDUCED_COMPARISON_OPERATORS.clone();
			 clone.add(kind);
			 mutateOperator(binaryOperator, clone);
		 }
	}
}
 
Example 2
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 3
Source File: LogicalExpressionMetaMutator.java    From metamutator with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Converts "a op b" bean op one of "<", "<=", "==", ">=", "!=" to:
 *    (  (op(1, 0, "<")  && (a < b))
 *    || (op(1, 1, "<=") && (a <= b))
 *    || (op(1, 2, "==") && (a == b))
 *    || (op(1, 3, ">=") && (a >= b))
 *    || (op(1, 4, ">")  && (a > b))
 *    )
 *	 */
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 newExpression = operators
			.stream()
			.map(kind -> {
				expression.setKind(kind);
				return String.format("("+ PREFIX + "%s.is(%s) && (%s))",
						thisIndex, kind.getDeclaringClass().getName()+"."+kind.name(), expression);
			}).collect(Collectors.joining(" || "));

	CtCodeSnippetExpression<Boolean> codeSnippet = getFactory().Core()
			.createCodeSnippetExpression();
	codeSnippet.setValue('(' + newExpression + ')');

	expression.replace(codeSnippet);
	expression.replace(expression);
	Selector.generateSelector(expression, originalKind, thisIndex, operators, PREFIX);

	hostSpots.add(expression);

}
 
Example 4
Source File: ArithmeticOperatorMetaMutator.java    From metamutator with GNU General Public License v3.0 5 votes vote down vote up
public void process(CtBinaryOperator<Boolean> binaryOperator) {
	BinaryOperatorKind kind = binaryOperator.getKind();
	if(ARITHMETIC_OPERATORS.contains(kind)){
		if(binaryOperator.getLeftHandOperand().getType() != null && binaryOperator.getRightHandOperand().getType() != null)
			if ( isNumber(binaryOperator.getLeftHandOperand())
			&& isNumber(binaryOperator.getRightHandOperand()))
			{
				mutateOperator(binaryOperator, ARITHMETIC_OPERATORS);
			}
	}
}
 
Example 5
Source File: BinaryOperatorAnalyzer.java    From coming with MIT License 5 votes vote down vote up
private void analyzeBinaryLogicalOperator(CtBinaryOperator operatorunderstudy, int operatorindex, Cntx<Object> context) {
	
	boolean whethercontainnotoperator = false;
	
	BinaryOperatorKind operatorkind = operatorunderstudy.getKind();

	if(logicalOperator.contains(operatorkind)) {
		
		CtExpression leftexpression = operatorunderstudy.getLeftHandOperand();
		CtExpression rightexpression = operatorunderstudy.getRightHandOperand();
				
		List<CtBinaryOperator> logicalOperatorLeft = leftexpression.getElements(
		  e -> e instanceof CtBinaryOperator && logicalOperator.contains(((CtBinaryOperator) e).getKind()));
		
		List<CtBinaryOperator> logicalOperatorRight = rightexpression.getElements(
				  e -> e instanceof CtBinaryOperator && logicalOperator.contains(((CtBinaryOperator) e).getKind()));
		
		if(logicalOperatorLeft.size() == 0) {	
			if(scannotoperator(leftexpression))
				whethercontainnotoperator=true;
		}
			
		if(!whethercontainnotoperator && logicalOperatorRight.size() == 0)	{
			if(scannotoperator(rightexpression))
				whethercontainnotoperator=true;
		}
	}
	
	writeGroupedInfo(context, Integer.toString(operatorindex)+"_"+operatorunderstudy, CodeFeatures.O2_LOGICAL_CONTAIN_NOT, 
			whethercontainnotoperator, "FEATURES_BINARYOPERATOR");
	
}
 
Example 6
Source File: BinaryOperatorAnalyzer.java    From coming with MIT License 5 votes vote down vote up
private void analyzeBinaryOperatorKind(CtBinaryOperator operatorunderstudy, int operatorindex, Cntx<Object> context) {
	
	BinaryOperatorKind operatorkind = operatorunderstudy.getKind();
	
	String operatorstring="";
	
	if(logicalOperator.contains(operatorkind)) {
		operatorstring="logical";
	} else if (bitOperator.contains(operatorkind)) {
		operatorstring="bit";
	} else if (compareOperator.contains(operatorkind)) {
		operatorstring="compare";
	} else if (shiftOperator.contains(operatorkind)) {
		operatorstring="shift";
	} else if (mathOperator.contains(operatorkind)) {
		operatorstring="math";
	} else operatorstring="others";
	
	for(int index=0; index<binoperatortype.size(); index++) {
		CodeFeatures cerainfeature = binoperatortype.get(index);
		
		if(cerainfeature.toString().endsWith(operatorstring.toUpperCase()))
			writeGroupedInfo(context,  Integer.toString(operatorindex)+"_"+operatorunderstudy, cerainfeature, 
						true, "FEATURES_BINARYOPERATOR");
		else writeGroupedInfo(context,  Integer.toString(operatorindex)+"_"+operatorunderstudy, cerainfeature, 
				false, "FEATURES_BINARYOPERATOR");
	}	
}
 
Example 7
Source File: ConditionMutationTransform.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Override
public <T> void visitCtBinaryOperator(CtBinaryOperator<T> operator) {
	super.visitCtBinaryOperator(operator);
	
	if(operator.getKind()==BinaryOperatorKind.AND||operator.getKind()==BinaryOperatorKind.OR) {
		fetchCOND(operator);
	}
}
 
Example 8
Source File: OperatorTransform.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Override
public <T> void visitCtBinaryOperator(CtBinaryOperator<T> operator) {
	super.visitCtBinaryOperator(operator);
	@SuppressWarnings("rawtypes")
	CtExpression left = operator.getLeftHandOperand();
	
	String typelefthand=left.getType().getQualifiedName();
	String typeoperator=operator.getType().getQualifiedName();
	typelefthand = typelefthand.replaceAll("\\d","");
	typeoperator = typeoperator.replaceAll("\\d","");
	@SuppressWarnings("rawtypes")
	CtExpression exp = null;
	
	BinaryOperatorKind kind=operator.getKind();
	if(rop.contains(kind))
		exp = OperatorGenerator.fetchROP(operator, this.mutSupporter, this.modificationPoint, typelefthand, "ROP");
	else if(aop.contains(kind))
		exp = OperatorGenerator.fetchROP(operator, this.mutSupporter, this.modificationPoint, typeoperator, "AOP");
			
	if (exp != null)
		candidates.put(operator, exp);
	
	if (candidates.containsKey(left)) {
		operator.setLeftHandOperand(candidates.get(left));
		saveSketchAndSynthesize();
		operator.setLeftHandOperand(left);
		resoreDiskFile();
	}
	
	@SuppressWarnings("rawtypes")
	CtExpression right = operator.getRightHandOperand();
	if (candidates.containsKey(right)) {
		operator.setRightHandOperand(candidates.get(right));
		saveSketchAndSynthesize();
		operator.setRightHandOperand(right);
		resoreDiskFile();
	}
}
 
Example 9
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 10
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());
}
 
Example 11
Source File: BinaryOperatorAnalyzer.java    From coming with MIT License 4 votes vote down vote up
private void analyzeBinaryWhetehrMathRoot (CtBinaryOperator operatorunderstudy, int operatorindex, Cntx<Object> context) {
	
	boolean whethermathroot = false;
	
	BinaryOperatorKind operatorkind = operatorunderstudy.getKind();

	if(mathOperator.contains(operatorkind)) {
		
		whethermathroot = true;
		
		CtElement parent = operatorunderstudy.getParent(CtBinaryOperator.class);
		
		if(parent!=null && mathOperator.contains(((CtBinaryOperator)parent).getKind()))
			whethermathroot =false;
	}
	
	writeGroupedInfo(context, Integer.toString(operatorindex)+"_"+operatorunderstudy, CodeFeatures.O5_IS_MATH_ROOT, 
			whethermathroot, "FEATURES_BINARYOPERATOR");	
}
 
Example 12
Source File: BinaryOperatorAnalyzer.java    From coming with MIT License 4 votes vote down vote up
private void analyzeBinaryOperatorCompareInCondition (CtElement wholeoriginal, CtBinaryOperator operatorunderstudy, int operatorindex, Cntx<Object> context) {
	
	boolean whethercompareincondition = false; 
	
       if(wholeoriginal instanceof CtIf || wholeoriginal instanceof CtWhile || wholeoriginal instanceof CtFor 
       	|| wholeoriginal instanceof CtDo || wholeoriginal instanceof CtForEach || wholeoriginal instanceof CtSwitch) {
       	
   		BinaryOperatorKind operatorkind = operatorunderstudy.getKind();

   		if (compareOperator.contains(operatorkind))
   			whethercompareincondition = true;
       }
	
       writeGroupedInfo(context, Integer.toString(operatorindex)+"_"+operatorunderstudy, CodeFeatures.O4_COMPARE_IN_CONDITION, 
			whethercompareincondition, "FEATURES_BINARYOPERATOR");
}
 
Example 13
Source File: DataCombinerSpoon.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@SuppressWarnings("rawtypes")
public boolean isExpressionMakeSense(CtBinaryOperator binop) {

	CtTypeReference param1 = binop.getLeftHandOperand().getType().box();
	CtTypeReference param2 = binop.getRightHandOperand().getType().box();

	// check the compatibility with the operator
	if (!param1.isSubtypeOf(param2) || !param2.isSubtypeOf(param1)) {
		return false;
	}

	switch (binop.getKind()) {
	case EQ:
		if (binop.getLeftHandOperand().equals(binop.getRightHandOperand())) {
			return false;
		}

		break;
	case NE:

		if (binop.getLeftHandOperand().equals(binop.getRightHandOperand())) {
			return false;
		}

		break;
	case AND:
	case OR:
		if (binop.getLeftHandOperand().equals(binop.getRightHandOperand())) {
			return false;
		}
		break;
	case LE:
	case LT:
		if (binop.getLeftHandOperand().equals(binop.getRightHandOperand())) {
			return false;
		}
		break;
	case PLUS:
		if (binop.getLeftHandOperand().toString().equals("0")
				|| (binop.getRightHandOperand().toString().equals("0"))) {
			return false;
		}
		break;
	case MINUS:
		if (binop.getLeftHandOperand().equals(binop.getRightHandOperand())
				|| (binop.getRightHandOperand().toString().equals("0"))) {
			return false;
		}
		break;
	case MUL:
		if (binop.getLeftHandOperand().toString().equals("0")
				|| (binop.getRightHandOperand().toString().equals("0"))) {
			return false;
		}

	case DIV:
		if (binop.getLeftHandOperand().equals(binop.getRightHandOperand())
				|| (binop.getRightHandOperand().toString().equals("0"))) {
			return false;
		}
		break;
	}
	return true;
}