Java Code Examples for spoon.reflect.code.CtLoop

The following examples show how to use spoon.reflect.code.CtLoop. 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
/**
 * Stop Do loop on 3 or 100 Rounds
 */
@Override
public void process(CtLoop candidate) {
	thisIndex++;
	String constanteName = "_doExpressionMetaMutator"+thisIndex+"_Constante";
	String expression = "int "+ constanteName +" = 1";
	CtCodeSnippetStatement DeclareRoundStatement = getFactory().Core()
			.createCodeSnippetStatement();
	DeclareRoundStatement.setValue(expression);
	
	
	
	
	String expression2 = "if((" + PREFIX + thisIndex + ".is("+  NbRound.Rounds3.getClass().getCanonicalName() + '.' + NbRound.Rounds3.toString() + ")) && "+ constanteName +" == 3) "
						+ "{" + this.breakOrReturn(candidate) + "}"
						+ "else if((" + PREFIX + thisIndex + ".is("+NbRound.NoRound.getClass().getCanonicalName() + '.' + NbRound.NoRound.toString() + "))) "
						+ "{" + this.breakOrReturn(candidate) + "}"
						+ "else if("+ constanteName +" == 100)"
						+ "{" + this.breakOrReturn(candidate) + "}"
						+ " "+ constanteName +"++";
	CtCodeSnippetStatement ifRoundStatement = getFactory().Core()
			.createCodeSnippetStatement();
	ifRoundStatement.setValue(expression2);
	
	candidate.insertBefore(DeclareRoundStatement);
	candidate.getBody().insertAfter(ifRoundStatement);
	Selector.generateSelector(candidate, NbRound.NoRound, thisIndex, roundsSet, PREFIX);
}
 
Example 2
public String breakOrReturn(CtLoop candidate) {
	Filter<CtCFlowBreak> filter = new ReturnOrThrowFilter();
	if(candidate.getBody().getElements(filter).size() > 0) {
		return candidate.getBody().getElements(filter).get(0).toString() + ";";
	}else {
		return "break;";
	}
}
 
Example 3
Source Project: nopol   Source File: SpoonLoopLibrary.java    License: GNU General Public License v2.0 5 votes vote down vote up
public static Collection<CtBreak> breakStatementsIn(CtLoop loop) {
    List<CtBreak> breaksOfLoop = MetaList.newArrayList();
    List<CtBreak> allBreaks = allChildrenOf(loop, CtBreak.class);
    for (CtBreak candidateBreak : allBreaks) {
        if (isBreakingFrom(loop, candidateBreak)) {
            breaksOfLoop.add(candidateBreak);
        }
    }
    return breaksOfLoop;
}
 
Example 4
Source Project: nopol   Source File: SpoonLoopLibrary.java    License: GNU General Public License v2.0 5 votes vote down vote up
public static Collection<CtReturn<?>> returnStatementsIn(CtLoop loop) {
    List<CtReturn<?>> returnsOfLoop = MetaList.newArrayList();
    List<CtReturn<?>> allReturns = (List) allChildrenOf(loop, CtReturn.class);
    for (CtReturn<?> candidateReturn : allReturns) {
        if (isReturningFrom(loop, candidateReturn)) {
            returnsOfLoop.add(candidateReturn);
        }
    }
    return returnsOfLoop;
}
 
Example 5
@Override
public void process(CtLoop element) {
	if(element instanceof CtFor){
		addExpressionAndSubexpressions(((CtFor)element).getExpression());
	}
	if(element instanceof CtWhile){
		addExpressionAndSubexpressions(((CtWhile)element).getLoopingExpression());
	}
	if(element instanceof CtDo){
		addExpressionAndSubexpressions(((CtDo)element).getLoopingExpression());
	}
	
}
 
Example 6
Source Project: nopol   Source File: SpoonLoopLibrary.java    License: GNU General Public License v2.0 4 votes vote down vote up
public static boolean isBreakingFrom(CtLoop loop, CtBreak breakStatement) {
    if (breakStatement.getParent(CtLoop.class) == loop) {
        return breakStatement.getParent(CtSwitch.class) == loop.getParent(CtSwitch.class);
    }
    return false;
}
 
Example 7
Source Project: nopol   Source File: SpoonLoopLibrary.java    License: GNU General Public License v2.0 4 votes vote down vote up
public static boolean isReturningFrom(CtLoop loop, CtReturn<?> returnStatement) {
    if (returnStatement.getParent(CtLoop.class) == loop) {
        return returnStatement.getParent(CtMethod.class) == loop.getParent(CtMethod.class);
    }
    return false;
}
 
Example 8
Source Project: coming   Source File: VariableAnalyzer.java    License: MIT License 4 votes vote down vote up
private void analyzeV15_LastthreeVariableIntroduction (List<CtVariableAccess> varsAffected, CtElement element,
		Cntx<Object> context) {
	try {
		
		CtExecutable methodParent = element.getParent(CtExecutable.class);

		if (methodParent == null)
			// the element is not in a method.
			return;
		
		List<CtStatement> statements=methodParent.getElements(new LineFilter());

		// For each variable affected
		for (CtVariableAccess variableAffected : varsAffected) {

			List<CtStatement> statementbefore = new ArrayList<>();
			
			boolean lastthreesametypeloc=false;

			for (CtStatement aStatement : statements) {

				CtStatement parent = variableAffected.getParent(new LineFilter());
									
				if (!isElementBeforeVariable(variableAffected, aStatement))
					continue;
				
				if (isStatementInControl(parent, aStatement) || parent==aStatement)
					continue;
				
				if(aStatement instanceof CtIf || aStatement instanceof CtLoop) 
					continue;
				
				statementbefore.add(aStatement);
			}
			
			List<CtStatement> statinterest = new ArrayList<>();
			
			if(statementbefore.size()<=4)
				statinterest=statementbefore;
			else {
				statinterest.add(statementbefore.get(statementbefore.size()-1));
				statinterest.add(statementbefore.get(statementbefore.size()-2));
				statinterest.add(statementbefore.get(statementbefore.size()-3));
				statinterest.add(statementbefore.get(statementbefore.size()-4));
			}

			for (int index=0; index< statinterest.size(); index++) {
				if(statinterest.get(index) instanceof CtLocalVariable) {
					CtLocalVariable ctLocalVariable=(CtLocalVariable)statinterest.get(index);

					if (!ctLocalVariable.getReference().getSimpleName()
							.equals(variableAffected.getVariable().getSimpleName()) 
							&& compareTypes(ctLocalVariable.getType(), variableAffected.getType())) {
						lastthreesametypeloc = true;
						break;
					}
				}
			}
			
			writeGroupedInfo(context, adjustIdentifyInJson(variableAffected), 
					CodeFeatures.V15_VAR_LAST_THREE_SAME_TYPE_LOC,
					(lastthreesametypeloc), "FEATURES_VARS");
		}
	} catch (Throwable e) {
		e.printStackTrace();
	}
}
 
Example 9
Source Project: coming   Source File: AbstractCodeAnalyzer.java    License: MIT License 4 votes vote down vote up
public boolean[] analyze_SametypewithGuard(List<CtVariableAccess> varsAffected, CtElement element,
			CtClass parentClass, List<CtStatement> statements) {

		boolean hasPrimitiveSimilarTypeWithNormalGuard = false;
		boolean hasObjectSimilarTypeWithNormalGuard = false;
		boolean hasPrimitiveSimilarTypeWithNullGuard = false;
		boolean hasObjectSimilarTypeWithNullGuard = false;

		try {
			CtExecutable faultyMethodParent = element.getParent(CtExecutable.class);

			if (faultyMethodParent == null)
				// the element is not in a method.
				return null;

			// For each variable affected
			for (CtVariableAccess variableAffected : varsAffected) {
				// for (CtStatement aStatement : statements) {
				CtStatement parent = variableAffected.getParent(new LineFilter());

				if (isNormalGuard(variableAffected, (parent)) || isNullCheckGuard(variableAffected, (parent)))
					continue;

				// For each statement in the method (it includes the statements inside the
				// blocks (then, while)!)
				for (CtStatement aStatement : statements) {
					// for (CtVariableAccess variableAffected : varsAffected) {

					if (parent == aStatement)
						continue;

					List<CtVariableAccess> varsFromStatement;

					if (aStatement instanceof CtIf || aStatement instanceof CtLoop) {
						varsFromStatement = VariableResolver.collectVariableRead(retrieveElementToStudy(aStatement));
					} else
						varsFromStatement = VariableResolver.collectVariableRead(aStatement);
//
//					// let's collect the var access in the statement
//					List<CtVariableAccess> varsFromStatement = VariableResolver
//							.collectVariableReadIgnoringBlocks(aStatement);
					// if the var access is the same that the affected
					for (CtVariableAccess varInStatement : varsFromStatement) {
						// Has similar type but different name
						if (compareTypes(variableAffected.getVariable().getType(),
								varInStatement.getVariable().getType())) {
							// && !hasSameName(variableAffected, varInStatement)) {
							// Now, let's check if the parent is a guard
							// if (isGuard(getParentNotBlock(aStatement))) {
							if (isNormalGuard(varInStatement, (aStatement))) {

								// it's ok, now let's check the type
								if (variableAffected.getType() != null) {
									// for primitive type variables, we require it to be the same global variable
									if (variableAffected.getType().isPrimitive() && varInStatement.getVariable()
											.getSimpleName().equals(variableAffected.getVariable().getSimpleName()))
										hasPrimitiveSimilarTypeWithNormalGuard = true;
									else
										hasObjectSimilarTypeWithNormalGuard = true;
								}
							}

							if (isNullCheckGuard(varInStatement, (aStatement))) {

								// it's ok, now let's check the type
								if (variableAffected.getType() != null) {

									if (variableAffected.getType().isPrimitive() && varInStatement.getVariable()
											.getSimpleName().equals(variableAffected.getVariable().getSimpleName()))
										hasPrimitiveSimilarTypeWithNullGuard = true;
									else
										hasObjectSimilarTypeWithNullGuard = true;
								}
							}

						}
					}
					// If we find both cases, we can stop
					if (hasPrimitiveSimilarTypeWithNormalGuard && hasObjectSimilarTypeWithNormalGuard
							&& hasPrimitiveSimilarTypeWithNullGuard && hasObjectSimilarTypeWithNullGuard)
						break;
				}
			}

		} catch (Throwable e) {
			e.printStackTrace();
		}

		boolean[] expressionvalue = new boolean[4];
		expressionvalue[0] = hasObjectSimilarTypeWithNormalGuard;
		expressionvalue[1] = hasPrimitiveSimilarTypeWithNormalGuard;
		expressionvalue[2] = hasObjectSimilarTypeWithNullGuard;
		expressionvalue[3] = hasPrimitiveSimilarTypeWithNullGuard;

		return expressionvalue;

	}