All Downloads are FREE. Search and download functionalities are using the official Maven repository.

fr.inria.coming.codefeatures.codeanalyze.BinaryOperatorAnalyzer Maven / Gradle / Ivy

The newest version!
package fr.inria.coming.codefeatures.codeanalyze;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import fr.inria.coming.codefeatures.Cntx;
import fr.inria.coming.codefeatures.CodeElementInfo;
import fr.inria.coming.codefeatures.CodeFeatures;
import spoon.reflect.code.BinaryOperatorKind;
import spoon.reflect.code.CtBinaryOperator;
import spoon.reflect.code.CtDo;
import spoon.reflect.code.CtExpression;
import spoon.reflect.code.CtFor;
import spoon.reflect.code.CtForEach;
import spoon.reflect.code.CtIf;
import spoon.reflect.code.CtSwitch;
import spoon.reflect.code.CtUnaryOperator;
import spoon.reflect.code.CtWhile;
import spoon.reflect.code.UnaryOperatorKind;
import spoon.reflect.declaration.CtElement;
import spoon.reflect.visitor.CtScanner;

public class BinaryOperatorAnalyzer extends AbstractCodeAnalyzer {
	
    List logicalOperator = Arrays.asList(BinaryOperatorKind.OR, BinaryOperatorKind.AND);
	
	List bitOperator = Arrays.asList(BinaryOperatorKind.BITOR, BinaryOperatorKind.BITXOR,
			BinaryOperatorKind.BITAND);
	
	List compareOperator = Arrays.asList(BinaryOperatorKind.EQ, BinaryOperatorKind.NE,
			BinaryOperatorKind.LT, BinaryOperatorKind.GT, BinaryOperatorKind.LE, BinaryOperatorKind.GE);
	
	List shiftOperator = Arrays.asList(BinaryOperatorKind.SL, BinaryOperatorKind.SR,
			BinaryOperatorKind.USR);
	
	List mathOperator = Arrays.asList(BinaryOperatorKind.PLUS, BinaryOperatorKind.MINUS,
			BinaryOperatorKind.MUL, BinaryOperatorKind.DIV, BinaryOperatorKind.MOD);
	
	List binoperatortype = Arrays.asList(CodeFeatures.O1_IS_LOGICAL, CodeFeatures.O1_IS_BIT,
			CodeFeatures.O1_IS_COMPARE, CodeFeatures.O1_IS_SHIFT, CodeFeatures.O1_IS_MATH, CodeFeatures.O1_IS_OTHERS);
	
	public BinaryOperatorAnalyzer (CodeElementInfo inputinfo) {
		super(inputinfo);
	}
	
	@Override
	public void analyze() {

		analyzeBinary_BinarySides(elementinfo.element, elementinfo.elementToStudy, elementinfo.context, 
				elementinfo.binoperators);
	}

	private void analyzeBinary_BinarySides(CtElement wholeoriginal, CtElement elementtostudy, Cntx context, 
			List allbinaryoperators) {

		List binaryOperatorsFromFaultyLine = allbinaryoperators;
		
		for(int index=0; index 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, "binaryoperator_"+Integer.toString(operatorindex), CodeFeatures.O5_IS_MATH_ROOT, 
				whethermathroot, "FEATURES_BINARYOPERATOR");	
	}
	
	// 765534, 1503331
	private void analyzeBinaryLogicalOperator(CtBinaryOperator operatorunderstudy, int operatorindex, Cntx context) {
		
		boolean whethercontainnotoperator = false;
		
		BinaryOperatorKind operatorkind = operatorunderstudy.getKind();

		if(logicalOperator.contains(operatorkind)) {
			
			CtExpression leftexpression = operatorunderstudy.getLeftHandOperand();
			CtExpression rightexpression = operatorunderstudy.getRightHandOperand();
					
			List logicalOperatorLeft = leftexpression.getElements(
			  e -> e instanceof CtBinaryOperator && logicalOperator.contains(((CtBinaryOperator) e).getKind()));
			
			List 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, "binaryoperator_"+Integer.toString(operatorindex), CodeFeatures.O2_LOGICAL_CONTAIN_NOT, 
				whethercontainnotoperator, "FEATURES_BINARYOPERATOR");
		
	}
	
	private boolean scannotoperator (CtExpression expressiontostudy) {
		
		List unaryOps = new ArrayList();
		
		CtScanner scanner = new CtScanner() {

			@Override
			public  void visitCtUnaryOperator(CtUnaryOperator operator) {

				super.visitCtUnaryOperator(operator);
				unaryOps.add(operator.getKind().toString());
			}
		};
		
		scanner.scan(expressiontostudy);
		
		return unaryOps.contains(UnaryOperatorKind.NOT.toString());
	}
	
	private void analyzeBinaryOperatorKind(CtBinaryOperator operatorunderstudy, int operatorindex, Cntx 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 context) {
			
			boolean whethercontainnull = false; 
					
			CtExpression leftexpression = operatorunderstudy.getLeftHandOperand();
			CtExpression rightexpression = operatorunderstudy.getRightHandOperand();
			
			if(leftexpression.toString().trim().equals("null") || rightexpression.toString().trim().equals("null"))
				whethercontainnull = true;
			
			writeGroupedInfo(context, "binaryoperator_"+Integer.toString(operatorindex), CodeFeatures.O3_CONTAIN_NULL, 
					whethercontainnull, "FEATURES_BINARYOPERATOR");
			
		}
	   
	   // ICSE15: 965894, 822931, 699512, 1187333, 1199874, 1163939, 1207508, 933249, 1301541, 785697, 949382, 1081884, 527944
	   // 1547911, 1508451, 1301466, 430825, 723149, 1132421, 1532321, 1151753, 149923, 545454, 306822, 908744, 1097097
	   private void analyzeBinaryOperatorInvolve01 (CtBinaryOperator operatorunderstudy, int operatorindex, Cntx 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, "binaryoperator_"+Integer.toString(operatorindex), CodeFeatures.O3_CONTAIN_01, 
					whethercontain01, "FEATURES_BINARYOPERATOR");
		}
	   
	   private void analyzeBinaryOperatorCompareInCondition (CtElement wholeoriginal, CtBinaryOperator operatorunderstudy, int operatorindex, Cntx 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, "binaryoperator_"+Integer.toString(operatorindex), CodeFeatures.O4_COMPARE_IN_CONDITION, 
					whethercompareincondition, "FEATURES_BINARYOPERATOR");
		}	
}