Java Code Examples for spoon.reflect.code.CtBinaryOperator

The following examples show how to use spoon.reflect.code.CtBinaryOperator. 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
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
@Override
public boolean isToBeProcessed(CtBinaryOperator<Boolean> element) {
	try {
		Selector.getTopLevelClass(element);
	} catch (NullPointerException e) {
		return false;
	}

	// not in constructors because we use static fields
	if (element.getParent(CtConstructor.class) != null) {
		return false;
	}

	// not in fields declaration because we use static fields
	if (element.getParent(CtField.class) != null) {
		return false;
	}

	return (ARITHMETIC_OPERATORS.contains(element.getKind()))
			&& (element.getParent(CtAnonymousExecutable.class) == null);
}
 
Example 3
Source Project: coming   Source File: AbstractCodeAnalyzer.java    License: MIT License 6 votes vote down vote up
/**
 * Return if the Condition is a guard
 * 
 * @param condition
 * @return
 */
// we want the condition not to be null related check
public boolean checkNormalGuardCondition(CtExpression condition) {
	if (condition != null) {
		List<CtBinaryOperator> binOp = condition.getElements(new TypeFilter<>(CtBinaryOperator.class));
		if (binOp != null && binOp.size() > 0) {

			for (CtBinaryOperator ctBinaryOperator : binOp) {
				if (ctBinaryOperator.getRightHandOperand().toString().equals("null")
						|| ctBinaryOperator.getLeftHandOperand().toString().equals("null")) {

					return false;
				}
			}
		}

		return true;
	}
	return false;
}
 
Example 4
Source Project: coming   Source File: AbstractCodeAnalyzer.java    License: MIT License 6 votes vote down vote up
public boolean checkNullCheckGuardCondition(CtExpression condition) {
	if (condition != null) {
		List<CtBinaryOperator> binOp = condition.getElements(new TypeFilter<>(CtBinaryOperator.class));
		if (binOp != null && binOp.size() > 0) {

			for (CtBinaryOperator ctBinaryOperator : binOp) {
				if (!ctBinaryOperator.getRightHandOperand().toString().equals("null")
						&& !ctBinaryOperator.getLeftHandOperand().toString().equals("null")) {

					return false;
				}
			}

			return true;
		}

		return false;
	}
	return false;
}
 
Example 5
Source Project: coming   Source File: LogicalExpressionAnalyzer.java    License: MIT License 6 votes vote down vote up
public boolean isLogicalExpression(CtElement currentElement) {
	if (currentElement == null)
		return false;
	if ((currentElement instanceof CtBinaryOperator)) {
		CtBinaryOperator binOp = (CtBinaryOperator) currentElement;
					
		if(binOp.getKind().equals(BinaryOperatorKind.AND) || binOp.getKind().equals(BinaryOperatorKind.OR)
			|| binOp.getKind().equals(BinaryOperatorKind.EQ)
			|| binOp.getKind().equals(BinaryOperatorKind.GE)
			|| binOp.getKind().equals(BinaryOperatorKind.GT)
			|| binOp.getKind().equals(BinaryOperatorKind.INSTANCEOF)
			|| binOp.getKind().equals(BinaryOperatorKind.LE)
			|| binOp.getKind().equals(BinaryOperatorKind.LT)
			|| binOp.getKind().equals(BinaryOperatorKind.NE))
			   return true;
	}
	
	return false;
}
 
Example 6
Source Project: coming   Source File: BinaryOperatorAnalyzer.java    License: MIT License 6 votes vote down vote up
private void analyzeBinaryOperatorInvolve01 (CtBinaryOperator operatorunderstudy, int operatorindex, Cntx<Object> context) {
	
	boolean whethercontain01 = false; 
			
	CtExpression leftexpression = operatorunderstudy.getLeftHandOperand();
	CtExpression rightexpression = operatorunderstudy.getRightHandOperand();
	
	if(leftexpression.toString().trim().equals("0") || leftexpression.toString().trim().equals("0.0") ||
			leftexpression.toString().trim().equals("1.0") || leftexpression.toString().trim().equals("1")
			|| rightexpression.toString().trim().equals("0") || rightexpression.toString().trim().equals("0.0") ||
			rightexpression.toString().trim().equals("1.0") || rightexpression.toString().trim().equals("1")
			|| leftexpression.toString().trim().endsWith("1") || rightexpression.toString().trim().endsWith("1"))
		whethercontain01 = true;
	
	writeGroupedInfo(context, Integer.toString(operatorindex)+"_"+operatorunderstudy, CodeFeatures.O3_CONTAIN_01, 
			whethercontain01, "FEATURES_BINARYOPERATOR");
}
 
Example 7
Source Project: coming   Source File: CodeElementInfo.java    License: MIT License 6 votes vote down vote up
public CodeElementInfo(CtElement elementoriginal, List<CtExpression> allExpressions,
		List<CtExpression> allrootlogicalexpers, List<CtBinaryOperator> allBinOperators) {

	this.element = elementoriginal;
	this.desirableExpressions = allExpressions;
	this.logicalExpressions = allrootlogicalexpers;
	this.binoperators = allBinOperators;

	setContext();
	setVarsInScope();
	setParentClass();
	setStatementList();
	setMethodList();
	setInvocationsFromClass();
	setConstructorcallsFromClass();
	setElementToStudy();
	setVarsAffected();
	setInvocations();
	setConstructorcalls();
	setLiteralsFromFaultyLine();
	setTypeaccess();

}
 
Example 8
Source Project: coming   Source File: JMutRepair.java    License: MIT License 6 votes vote down vote up
/**
 * Certain patterns/characteristics of search-space of a repair tool can't be represented by ChangePatternSpecification
 * This filter is supposed to delete/remove such instances from the results given by PatternInstanceAnalyser.
 *
 * @param patternInstance
 * @param revision
 * @param diff
 * @return
 */
@Override
public boolean filter(ChangePatternInstance patternInstance, IRevision revision, Diff diff) {

    String patternType = patternInstance.getPattern().getName().split(File.pathSeparator)[1];
    if (patternType.startsWith("binary")) {

        Operation upd = patternInstance.getActions().get(0);
        CtBinaryOperator src = (CtBinaryOperator) upd.getSrcNode();
        CtBinaryOperator dst = (CtBinaryOperator) upd.getDstNode();

        return src.getLeftHandOperand().equals(dst.getLeftHandOperand())
                && src.getRightHandOperand().equals(dst.getRightHandOperand());
    }

    return true;
}
 
Example 9
Source Project: astor   Source File: BinaryOperatorMutator.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Creates alternatives Expressions (Binary Operations) given a list of Binary
 * operators kind operators. The original kind (that one included in the
 * original expression is not analyzed)
 * 
 * @param result
 * @param op
 * @param kind
 * @param operators
 */
protected void addRemainings(List<CtExpression> result, CtBinaryOperator<?> op, BinaryOperatorKind kind,
		List<BinaryOperatorKind> operators) {
	// TODO: recursive?
	if (operators.contains(kind)) {
		for (BinaryOperatorKind binaryOperatorKind : operators) {
			if (binaryOperatorKind != kind) {
				// Cloning
				CtExpression right = factory.Core().clone(op.getRightHandOperand());
				CtExpression left = factory.Core().clone(op.getLeftHandOperand());

				CtBinaryOperator binaryOp = factory.Code().createBinaryOperator(left, right, binaryOperatorKind);
				// Set parent
				right.setParent(binaryOp);
				left.setParent(binaryOp);

				result.add(binaryOp);
			}
		}
	}
}
 
Example 10
Source Project: astor   Source File: WrapwithIfNullCheck.java    License: 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 11
@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 12
Source Project: astor   Source File: SingleLogicExpOperator.java    License: 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 13
Source Project: astor   Source File: OperatorReplacementOp.java    License: GNU General Public License v2.0 6 votes vote down vote up
public List<Ingredient> getNewBinary(CtBinaryOperator oldBinary, List<BinaryOperatorKind> ops) {
	List<Ingredient> mutationAll = new ArrayList<>();

	if (ops.contains(oldBinary.getKind())) {

		for (BinaryOperatorKind binaryOperatorKind2 : ops) {

			if (binaryOperatorKind2.equals(oldBinary.getKind()))
				continue;

			CtBinaryOperator binaryOp = MutationSupporter.getFactory().Code().createBinaryOperator(
					oldBinary.getLeftHandOperand().clone(), oldBinary.getRightHandOperand(), binaryOperatorKind2);
			MutationSupporter.clearPosition(binaryOp);
			Ingredient newIngredientExtended = new Ingredient(binaryOp);
			newIngredientExtended.setDerivedFrom(oldBinary);
			mutationAll.add(newIngredientExtended);
			newIngredientExtended.getMetadata().put("left_original", oldBinary.getLeftHandOperand());
			newIngredientExtended.getMetadata().put("right_original", oldBinary.getRightHandOperand());
			newIngredientExtended.getMetadata().put("operator", binaryOperatorKind2);
		}
	}

	return mutationAll;
}
 
Example 14
@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 15
@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 16
Source Project: astor   Source File: ExpressionTransform.java    License: GNU General Public License v2.0 6 votes vote down vote up
@Override
public <T> void visitCtBinaryOperator(CtBinaryOperator<T> operator) {
	super.visitCtBinaryOperator(operator);
	@SuppressWarnings("rawtypes")
	CtExpression left = operator.getLeftHandOperand();
	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 17
Source Project: astor   Source File: DataCombinerSpoon.java    License: GNU General Public License v2.0 6 votes vote down vote up
private void add(final List<CtExpression> result, CtExpression binaryExpression) {
	boolean makesence = true;
	try {
		if (binaryExpression instanceof CtBinaryOperator) {
			makesence = isExpressionMakeSense((CtBinaryOperator) binaryExpression);
		}

		if (makesence) {
			this.callListener(binaryExpression);
			result.add(binaryExpression);
		}
	} catch (Exception e) {
		// the hash is different, so it throws an exception
		// System.err.println("error checking");
		// System.err.println(binaryExpression);
		// e.printStackTrace();
	}
}
 
Example 18
Source Project: astor   Source File: ExpressionRevolver.java    License: GNU General Public License v2.0 6 votes vote down vote up
public static List<CtExpression<Boolean>> getExpressions(CtExpression<Boolean> element) {
	List<CtExpression<Boolean>> expsRetrieved = new ArrayList<CtExpression<Boolean>>();

	if (element instanceof CtUnaryOperator) {
		expsRetrieved.add(element);
		element = ((CtUnaryOperator) element).getOperand();
	}

	if (element instanceof CtBinaryOperator) {
		expsRetrieved.add(element);
		CtBinaryOperator bin = (CtBinaryOperator) element;
		if (bin.getKind().equals(BinaryOperatorKind.AND) || bin.getKind().equals(BinaryOperatorKind.OR)) {
			expsRetrieved.addAll(getExpressions(bin.getLeftHandOperand()));
			expsRetrieved.addAll(getExpressions(bin.getRightHandOperand()));
		}
	} else {
		if (element instanceof CtInvocation
				&& element.getType().getSimpleName().equals(boolean.class.getSimpleName())) {
			expsRetrieved.add(element);
		}
	}
	return expsRetrieved;
}
 
Example 19
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_214116() throws Exception {
	AstComparator diff = new AstComparator();
	// meld src/test/resources/examples/t_214116/left_Modeller_1.134.java
	// src/test/resources/examples/t_214116/right_Modeller_1.135.java
	File fl = new File("src/test/resources/examples/t_214116/left_Modeller_1.134.java");
	File fr = new File("src/test/resources/examples/t_214116/right_Modeller_1.135.java");
	Diff result = diff.compare(fl, fr);

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

	List<Operation> actions = result.getRootOperations();
	// result.debugInformation();
	assertEquals(actions.size(), 2);
	assertTrue(result.containsOperation(OperationKind.Update, "Literal", "\" \""));

	// the change is in a throw
	CtElement elem = actions.get(0).getNode();
	assertNotNull(elem);
	assertNotNull(elem.getParent(CtThrow.class));

}
 
Example 20
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_224512() throws Exception {
	AstComparator diff = new AstComparator();
	// meld src/test/resources/examples/t_224512/left_Server_1.925.java
	// src/test/resources/examples/t_224512/right_Server_1.926.java
	File fl = new File("src/test/resources/examples/t_224512/left_Server_1.925.java");
	File fr = new File("src/test/resources/examples/t_224512/right_Server_1.926.java");
	Diff result = diff.compare(fl, fr);

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

	List<Operation> actions = result.getRootOperations();
	result.debugInformation();
	assertEquals(actions.size(), 2);
	assertTrue(result.containsOperation(OperationKind.Insert, "BinaryOperator", "AND"));
	assertTrue(result.containsOperation(OperationKind.Move, "BinaryOperator", "AND"));
}
 
Example 21
@Override
public boolean isToBeProcessed(CtBinaryOperator<Boolean> element) {
	// if (element.getParent(CtAnonymousExecutable.class)!=null) {
	// System.out.println(element.getParent(CtAnonymousExecutable.class));
	// }

	try {
		Selector.getTopLevelClass(element);
	} catch (NullPointerException e) {
		return false;
	}

	// not in constructors because we use static fields
	if (element.getParent(CtConstructor.class) != null) {
		return false;
	}

	// not in fields declaration because we use static fields
	if (element.getParent(CtField.class) != null) {
		return false;
	}
	
	return (LOGICAL_OPERATORS.contains(element.getKind()) || COMPARISON_OPERATORS
			.contains(element.getKind()))
			&& (element.getParent(CtAnonymousExecutable.class) == null) // not
																		// in
																		// static
																		// block
	;
}
 
Example 22
/**
 * 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 23
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 24
@Override
public void process(CtElement candidate) {
	if (!(candidate instanceof CtBinaryOperator)) {
		return;
	}
	CtBinaryOperator op = (CtBinaryOperator)candidate;
	op.setKind(BinaryOperatorKind.MINUS);
}
 
Example 25
Source Project: coming   Source File: LogicalExpressionAnalyzer.java    License: MIT License 5 votes vote down vote up
/**
 * Whether the number of boolean expressions in the logical expression is larger
 * than 1
 * 
 * @param binOps
 * @param tostudy
 */

private boolean wheteherCompundBoolExper(List<CtBinaryOperator> binOps, CtExpression tostudy) {
	
	boolean whethercompound=false;
	for(int index=0; index<binOps.size(); index++) {
		if(binOps.get(index).equals(tostudy)) {
			whethercompound=true;
			break;
		}
	}
	return whethercompound;
}
 
Example 26
Source Project: coming   Source File: BinaryOperatorAnalyzer.java    License: 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 27
Source Project: coming   Source File: BinaryOperatorAnalyzer.java    License: 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 28
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 29
Source Project: astor   Source File: BinaryOperatorMutator.java    License: GNU General Public License v2.0 5 votes vote down vote up
private CtBinaryOperator createBinaryOp(CtBinaryOperator<?> op, BinaryOperatorKind binaryOperatorKind,
		CtExpression left_i, CtExpression right_i) {
	CtExpression right_c = factory.Core().clone(right_i);
	CtExpression left_c = factory.Core().clone(left_i);

	CtBinaryOperator binaryOp = factory.Code().createBinaryOperator(left_c, right_c, binaryOperatorKind);
	// Set parent
	right_c.setParent(binaryOp);
	left_c.setParent(binaryOp);
	return binaryOp;

}
 
Example 30
Source Project: astor   Source File: LogicExpOperator.java    License: 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;
}