spoon.reflect.code.CtForEach Java Examples

The following examples show how to use spoon.reflect.code.CtForEach. 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: AbstractCodeAnalyzer.java    From coming with MIT License 6 votes vote down vote up
public CtElement retrieveElementToStudy(CtElement element) {

		if (element instanceof CtIf) {
			return (((CtIf) element).getCondition());
		} else if (element instanceof CtWhile) {
			return (((CtWhile) element).getLoopingExpression());
		} else if (element instanceof CtFor) {
			return (((CtFor) element).getExpression());
		} else if (element instanceof CtDo) {
			return (((CtDo) element).getLoopingExpression());
		} else if (element instanceof CtForEach) {
			return (((CtForEach) element).getExpression());
		} else if (element instanceof CtSwitch) {
			return (((CtSwitch) element).getSelector());
		} else
			return (element);
	}
 
Example #2
Source File: CodeElementInfo.java    From coming with MIT License 6 votes vote down vote up
private CtElement retrieveElementToStudy(CtElement element) {

		if (element instanceof CtIf) {
			return (((CtIf) element).getCondition());
		} else if (element instanceof CtWhile) {
			return (((CtWhile) element).getLoopingExpression());
		} else if (element instanceof CtFor) {
			return (((CtFor) element).getExpression());
		} else if (element instanceof CtDo) {
			return (((CtDo) element).getLoopingExpression());
		} else if (element instanceof CtForEach) {
			return (((CtForEach) element).getExpression());
		} else if (element instanceof CtSwitch) {
			return (((CtSwitch) element).getSelector());
		} else
			return (element);
	}
 
Example #3
Source File: VariableAnalyzer.java    From coming with MIT License 5 votes vote down vote up
private boolean whetherConditionalStat(CtElement input) {
	
	if(input instanceof CtIf || input instanceof CtWhile || input instanceof CtFor || input
			instanceof CtForEach)
		return true;
	else return false;	
}
 
Example #4
Source File: VariableAnalyzer.java    From coming with MIT License 5 votes vote down vote up
public CtElement retrieveExpressionToStudy(CtElement element) {

			if (element instanceof CtIf) {
				return (((CtIf) element).getCondition());
			} else if (element instanceof CtWhile) {
				return (((CtWhile) element).getLoopingExpression());
			} else if (element instanceof CtFor) {
				return (((CtFor) element).getExpression());
			} else if (element instanceof CtForEach) {
				return (((CtForEach) element).getExpression());
			}  else
				return (element);
		}
 
Example #5
Source File: TransformStrategy.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@SuppressWarnings({ "unchecked", "rawtypes" })
public List<String> transform () {
	CtStatement targetStmt = (CtStatement) this.modificationPoint.getCodeElement();
	if (targetStmt instanceof CtInvocation)
		this.visitCtInvocation((CtInvocation) targetStmt);
	else if (targetStmt instanceof CtConstructorCall)
		this.visitCtConstructorCall((CtConstructorCall) targetStmt);
	else if (targetStmt instanceof CtIf)
		this.visitCtIf ((CtIf)targetStmt);
	else if (targetStmt instanceof CtReturn)
		this.visitCtReturn((CtReturn) targetStmt);
	else if (targetStmt instanceof CtSwitch)
		this.visitCtSwitch((CtSwitch) targetStmt);
	else if (targetStmt instanceof CtCase)
		this.visitCtCase((CtCase) targetStmt);
	else if (targetStmt instanceof CtAssignment)
		this.visitCtAssignment((CtAssignment) targetStmt);
	else if (targetStmt instanceof CtAssert)
		this.visitCtAssert((CtAssert) targetStmt);
	else if (targetStmt instanceof CtFor)
		this.visitCtFor((CtFor) targetStmt);
	else if (targetStmt instanceof CtForEach)
		this.visitCtForEach((CtForEach) targetStmt);
	else if (targetStmt instanceof CtWhile)
		this.visitCtWhile((CtWhile) targetStmt);
	else if (targetStmt instanceof CtUnaryOperator)
		this.visitCtUnaryOperator((CtUnaryOperator) targetStmt);
	else if (targetStmt instanceof CtSynchronized)
		this.visitCtSynchronized((CtSynchronized) targetStmt);

	return list;
}
 
Example #6
Source File: TransformStrategy.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void visitCtForEach(CtForEach foreach) {
	super.visitCtForEach(foreach);
	
	@SuppressWarnings("rawtypes")
	CtExpression exper=foreach.getExpression();
	if (candidates.containsKey(exper)) {
		foreach.setExpression(candidates.get(exper));
		saveSketchAndSynthesize();
		foreach.setExpression(exper);
		resoreDiskFile();
	}
}
 
Example #7
Source File: VariableResolver.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private static List<CtLocalVariable> getVarsInForEach(CtElement element) {
	List<CtLocalVariable> variables = new ArrayList<CtLocalVariable>();
	CtElement currentElement = element;
	CtForEach ff = currentElement.getParent(CtForEach.class);
	while (ff != null) {
		variables.add((CtLocalVariable) ff.getVariable());
		ff = ff.getParent(CtForEach.class);

	}
	return variables;
}
 
Example #8
Source File: StatementDeletionMetaMutator.java    From metamutator with GNU General Public License v3.0 4 votes vote down vote up
private void mutateOperator(final CtStatement expression) {
	
	
	/*if (alreadyInHotsSpot(expression)) {
		System.out
				.println(String
						.format("Expression '%s' ignored because it is included in previous hot spot",
								expression));
		return;
	}*/
	int thisIndex = ++selectorIndex;
	
	ACTIVABLE kind = ACTIVABLE.ENABLED;
	String expressionContent =  String.format("("+ PREFIX + "%s.is(%s))",
			thisIndex, kind.getClass().getCanonicalName()+"."+kind.name());
	
	//create IfChoice with right condition
	CtIf ifChoice = getFactory().Core().createIf();
	CtCodeSnippetExpression expIf = getFactory().Code().createCodeSnippetExpression(expressionContent);
	ifChoice.setCondition(expIf);
		
	
	
	//create block from a clone of expression
	CtStatement exp2 = getFactory().Core().clone(expression);
	CtBlock thenBlock = getFactory().Code().createCtBlock(exp2);
	
	//set if and replace the expression with the new if
	ifChoice.setThenStatement(thenBlock);
	expression.replace(ifChoice);
	
	//to be sure
	ifChoice.getParent().updateAllParentsBelow();
	
	//if there are return or throws, set else with value of return.
	Filter<CtCFlowBreak> filterReturn = new ReturnOrThrowFilter();
	if(!thenBlock.getElements(filterReturn).isEmpty()){
		SetElseStatementWithReturn(ifChoice);
	}

	
	//to avoid to delete assignement in statement, we assign a default value to all local variable.
	Filter<CtLocalVariable> filterLocalVariable =  new TypeFilter<CtLocalVariable>(CtLocalVariable.class);
	CtMethod method = ifChoice.getParent(CtMethod.class);
	if(method != null && !method.getElements(filterLocalVariable).isEmpty()){
		for(CtLocalVariable var : method.getElements(filterLocalVariable)){
			if(var.getAssignment() == null){
				//create right side expression from template.
				Class classOfAssignment = var.getType().getActualClass();
				CtLiteral rightHand = null;
				
				//Particular case of ForEach (int x : numbers) can't be (int x = 0 : numbers)
				if(var.getParent() instanceof CtForEach){
					continue;
				}
				if(PrimitiveTemplateExpressions.containsKey(classOfAssignment)){
					CtLiteral templateExpression = PrimitiveTemplateExpressions.get(classOfAssignment);
					rightHand = getFactory().Core().clone(templateExpression);
				}else{
					rightHand = getFactory().createLiteral().setValue(null);
				}
				var.setAssignment(rightHand);
			}
		}
	}
	
	Selector.generateSelector(expression, ACTIVABLE.ENABLED, thisIndex, ActivableSet, PREFIX);
	//hotSpots.add(expression);

}
 
Example #9
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");
}