spoon.reflect.code.CtFor Java Examples

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

		if (element instanceof CtIf) {
			return collectVariableRead(((CtIf) element).getCondition());
		}
		if (element instanceof CtWhile) {
			return collectVariableRead(((CtWhile) element).getLoopingExpression());
		}

		if (element instanceof CtFor) {
			return collectVariableRead(((CtFor) element).getExpression());
		}

		return collectVariableRead(element);

	}
 
Example #4
Source File: VariableResolver.java    From coming with MIT License 6 votes vote down vote up
public static List<CtVariableAccess> collectVariableAccessIgnoringBlocks(CtElement element) {

		if (element instanceof CtIf) {
			return collectVariableAccess(((CtIf) element).getCondition());
		}
		if (element instanceof CtWhile) {
			return collectVariableAccess(((CtWhile) element).getLoopingExpression());
		}

		if (element instanceof CtFor) {
			return collectVariableAccess(((CtFor) element).getExpression());
		}

		return collectVariableAccess(element);

	}
 
Example #5
Source File: VariableResolver.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public static List<CtVariableAccess> collectVariableReadIgnoringBlocks(CtElement element) {

		if (element instanceof CtIf) {
			return collectVariableRead(((CtIf) element).getCondition());
		}
		if (element instanceof CtWhile) {
			return collectVariableRead(((CtWhile) element).getLoopingExpression());
		}

		if (element instanceof CtFor) {
			return collectVariableRead(((CtFor) element).getExpression());
		}

		return collectVariableRead(element);

	}
 
Example #6
Source File: VariableResolver.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public static List<CtVariableAccess> collectVariableAccessIgnoringBlocks(CtElement element) {

		if (element instanceof CtIf) {
			return collectVariableAccess(((CtIf) element).getCondition());
		}
		if (element instanceof CtWhile) {
			return collectVariableAccess(((CtWhile) element).getLoopingExpression());
		}

		if (element instanceof CtFor) {
			return collectVariableAccess(((CtFor) element).getExpression());
		}

		return collectVariableAccess(element);

	}
 
Example #7
Source File: SpecialStatementFixSpaceProcessor.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void process(CtStatement element) {

	if (element instanceof CtIf) {
		add(((CtIf) element).getCondition());
	} else if (element instanceof CtFor) {
		add(((CtFor) element).getExpression());
	} else if (element instanceof CtWhile) {
		add(((CtWhile) element).getLoopingExpression());
	} else if (element instanceof CtDo) {
		add(((CtDo) element).getLoopingExpression());
	} else if (element instanceof CtThrow) {
		add(((CtThrow) element).getThrownExpression());
	} else if (element instanceof CtInvocation && (element.getParent() instanceof CtBlock)) {
		add(element);
	} else if (element instanceof CtAssignment || element instanceof CtConstructorCall
			|| element instanceof CtCFlowBreak || element instanceof CtLocalVariable) {
		add(element);
	}

}
 
Example #8
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 #9
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 #10
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 #11
Source File: TransformStrategy.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public void visitCtFor(CtFor forLoop) {
	super.visitCtFor(forLoop);

	@SuppressWarnings("rawtypes")
	CtExpression exper=forLoop.getExpression();
	if (candidates.containsKey(exper)) {
		forLoop.setExpression(candidates.get(exper));
		saveSketchAndSynthesize();
		forLoop.setExpression(exper);
		resoreDiskFile();
	}
}
 
Example #12
Source File: VariableResolver.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private static List<CtLocalVariable> getVarsInFor(CtElement element) {
	List<CtLocalVariable> variables = new ArrayList<CtLocalVariable>();
	CtElement currentElement = element;
	CtFor ff = currentElement.getParent(CtFor.class);
	while (ff != null) {
		variables.addAll(ff.getForInit().stream().filter(e -> e instanceof CtLocalVariable)
				.map(CtLocalVariable.class::cast).collect(Collectors.toList()));

		ff = ff.getParent(CtFor.class);

	}
	return variables;
}
 
Example #13
Source File: LoopExpressionFixSpaceProcessor.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@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 #14
Source File: LogicalExpressionAnalyzer.java    From coming with MIT License 4 votes vote down vote up
@Override
public void visitCtFor(CtFor forLoop) {
	toScan = forLoop.getExpression();
}
 
Example #15
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 #16
Source File: VariableResolverTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void testExample288VariablesLocalAccess() throws Exception {
	// then to remove induction variables from varAccessCollected,
	// we would look for the induction variables' names in
	// varAccessCollected
	// and remove the local accesses but leave static references to those
	// names in the collection

	AstorMain main1 = new AstorMain();

	String[] args = new String[] { "-dependencies", dep, "-mode", "jgenprog", "-failing",
			"org.apache.commons.math.optimization.linear.SimplexSolverTest", "-location",
			new File("./examples/Math-issue-288").getAbsolutePath(), "-package", "org.apache.commons",
			"-srcjavafolder", "/src/main/java/", "-srctestfolder", "/src/main/test/", "-binjavafolder",
			"/target/classes", "-bintestfolder", "/target/test-classes", "-javacompliancelevel", "7", //
			"-flthreshold", "0.2", "-out", //
			out.getAbsolutePath(), "-scope", "package", "-seed", "10",
			// Force not evolution
			"-maxgen", "0", "-population", "1", //
			"-stopfirst", "true", "-maxtime", "100"

	};

	main1.execute(args);

	List<ProgramVariant> variants = main1.getEngine().getVariants();
	JGenProg jgp = (JGenProg) main1.getEngine();
	ModificationPoint mp = findModPoint(variants.get(0).getModificationPoints(), "return minPos");// variants.get(0).getModificationPoints().get(0);
	log.debug("Mpoint \n" + mp);
	log.debug(mp.getCtClass());

	log.debug("Mpoint Context \n" + mp.getContextOfModificationPoint());

	IngredientPool ispace = jgp.getIngredientSearchStrategy().getIngredientSpace();
	List<Ingredient> ingredients = ispace.getIngredients(mp.getCodeElement());

	// For with a induction variable
	CtElement ifor = findElement(ingredients, "for (int i = tableau.getNumObjectiveFunctions").getCode();// ingredients.get(46);
	// //for
	// (int
	// i
	// =
	// tableau.getNumObjectiveFunctions()
	assertTrue(ifor.toString().startsWith("for (int i = tableau.getNumObjectiveFunctions"));
	assertTrue(ifor instanceof CtFor);
	log.debug("fit? " + ifor + " in context: " + mp.getContextOfModificationPoint());
	boolean matchFor = VariableResolver.fitInContext(mp.getContextOfModificationPoint(), ifor, true);

	// the variable 'i' is declared inside the ingredient, and event it does
	// not exist
	assertTrue(matchFor);

	CtElement iif = findElement(ingredients,
			"if ((org.apache.commons.math.util.MathUtils.compareTo(tableau.getEntry(0, i)").getCode();// ingredients.get(45);
	// //if
	// ((org.apache.commons.math.util.MathUtils.compareTo(tableau.getEntry(0,
	// i),
	assertTrue(TestHelper.getWithoutParentheses(iif.toString()).startsWith(TestHelper.getWithoutParentheses(
			"if ((org.apache.commons.math.util.MathUtils.compareTo(tableau.getEntry(0, i)")));
	assertTrue(iif instanceof CtIf);
	boolean matchIf = VariableResolver.fitInContext(mp.getContextOfModificationPoint(), iif, true);

	// the variable 'i' does not exist in the context
	assertFalse(matchIf);

	CtElement iStaticSame = findElement(ingredients, "setMaxIterations(").getCode();// ingredients.get(0);//static
	// setMaxIterations(org.apache.commons.math.optimization.linear.AbstractLinearOptimizer.DEFAULT_MAX_ITERATIONS)
	assertTrue(iStaticSame instanceof CtInvocation);
	assertTrue(iStaticSame.toString().startsWith("setMaxIterations("));
	boolean matchStSame = VariableResolver.fitInContext(mp.getContextOfModificationPoint(), iStaticSame, true);
	assertTrue(matchStSame);

	CtElement iStaticDouble = findElement(ingredients, "double minRatio = java.lang.Double.MAX_VALUE").getCode();// ingredients.get(55);//static
	assertTrue(iStaticDouble instanceof CtLocalVariable);
	assertTrue(iStaticDouble.toString().startsWith("double minRatio = java.lang.Double.MAX_VALUE"));
	boolean matchSt = VariableResolver.fitInContext(mp.getContextOfModificationPoint(), iStaticDouble, true);
	assertTrue(matchSt);

}