Java Code Examples for spoon.reflect.code.CtCodeSnippetExpression#setValue()

The following examples show how to use spoon.reflect.code.CtCodeSnippetExpression#setValue() . 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: NumericVariableMetaMutator.java    From metamutator with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Add AbsoluteValue, Plus, Minus, Increment or Decrement Unary Operator on Numeric Variable 
 */
@Override
public void process(CtVariableRead candidate) {
	thisIndex++;
	
	String expression = "(";
	for(UNARY unary : absSet){
		if(unary.equals(UNARY.INIT)) continue;
		/*expression += PREFIX+thisIndex + ".is(\"" + unary.toString() + "\")?( " + UnaryEquivalent(unary)  + candidate.toString() + ")):";*/
		expression += PREFIX+thisIndex + ".is(" + unary.getClass().getCanonicalName()+'.'+unary.toString()+ ")?( " + UnaryEquivalent(unary)  + candidate.getVariable().getSimpleName() + ")):";
	}
	expression += "(" + candidate.toString() + "))";
	CtCodeSnippetExpression<Boolean> codeSnippet = getFactory().Core()
			.createCodeSnippetExpression();
	codeSnippet.setValue(expression);
	candidate.replace(codeSnippet);
	
	Selector.generateSelector(candidate, UNARY.INIT, thisIndex, absSet, PREFIX);
}
 
Example 2
Source File: ConditionalAdder.java    From nopol with GNU General Public License v2.0 6 votes vote down vote up
@Override
public CtIf processCondition(CtStatement element, String newCondition) {
    //logger.debug("##### {} ##### Before:\n{}", element, element.getParent());
    // if the element is not a line
    if (!new LineFilter().matches(element)) {
        element = element.getParent(new LineFilter());
    }
    CtElement parent = element.getParent();
    CtIf newIf = element.getFactory().Core().createIf();
    CtCodeSnippetExpression<Boolean> condition = element.getFactory().Core().createCodeSnippetExpression();
    condition.setValue(newCondition);
    newIf.setCondition(condition);
    // Fix : warning: ignoring inconsistent parent for [CtElem1] ( [CtElem2] != [CtElem3] )
    newIf.setParent(parent);
    element.replace(newIf);
    // this should be after the replace to avoid an StackOverflowException caused by the circular reference.
    newIf.setThenStatement(element);
    //logger.debug("##### {} ##### After:\n{}", element, element.getParent().getParent());
    return newIf;
}
 
Example 3
Source File: DynamothIngredientSynthesizer.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Override
public List<CtElement> executeSynthesis(ModificationPoint modificationPoint, CtElement hole, CtType expectedtype,
		List<CtVariable> contextOfModificationPoint, ExecutionContext values) {
	List<CtElement> result = new ArrayList<>();
	DynamothSynthesizer dynamothSynthesizer = new DynamothSynthesizer((DynamothSynthesisContext) values);
	Candidates candidates = dynamothSynthesizer.combineValues();

	for (fr.inria.lille.repair.expression.Expression expression : candidates) {
		String candidateCode = expression.toString();
		log.info("Candidate: " + candidateCode);

		if (hole instanceof CtExpression) {

			CtCodeSnippetExpression<Boolean> snippet = MutationSupporter.getFactory().Core()
					.createCodeSnippetExpression();
			snippet.setValue(candidateCode);
			result.add(snippet);
		} else {
			log.debug("Error: Other type not analyzed " + hole.getClass().getName());
		}

	}

	return result;
}
 
Example 4
Source File: LogicalExpressionMetaMutator.java    From metamutator with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 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 5
Source File: VariabletoNullMetaMutator.java    From metamutator with GNU General Public License v3.0 5 votes vote down vote up
/**
 *
 * @param expression
 * @param operators
 */
private void mutateOperator(final CtExpression expression, EnumSet<Null> operators) {
			
	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;
	
	String actualExpression = expression.toString();
	
	String newExpression = String.format("(%s%s.is(%s))?"+actualExpression+":null",PREFIX,thisIndex,"metamutator.Null.NO");
	
	CtCodeSnippetExpression codeSnippet = getFactory().Core()
			.createCodeSnippetExpression();
	codeSnippet.setValue('(' + newExpression + ')');
	
	expression.replace(codeSnippet);
	expression.replace(expression);
	
	Selector.generateSelector(expression, Null.NO, thisIndex, operators, PREFIX);

	hostSpots.add(expression);

}
 
Example 6
Source File: ConditionalReplacer.java    From nopol with GNU General Public License v2.0 5 votes vote down vote up
@Override
public CtIf processCondition(CtStatement element, String newCondition) {
    CtCodeSnippetExpression<Boolean> snippet = element.getFactory().Core().createCodeSnippetExpression();
    snippet.setValue(newCondition);
    CtExpression<Boolean> condition = getCondition(element);
    condition.replace(snippet);
    return (CtIf) element;
}
 
Example 7
Source File: EvalTOSCoreApproach.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
protected IngredientFromDyna createIngredient(Expression expression) {
	String candidateCode = expression.asPatch();
	CtCodeSnippetExpression<Boolean> snippet = MutationSupporter.getFactory().Core().createCodeSnippetExpression();
	snippet.setValue(candidateCode);
	IngredientFromDyna ingredient = new IngredientFromDyna(snippet, expression);
	// take one form the cluster by type
	log.debug("Creating ingredient from Dynamoth expression: " + expression + " --result--> Spoon Ingredient: "
			+ ingredient + "| value: " + expression.getValue().getRealValue());
	return ingredient;
}
 
Example 8
Source File: Selector.java    From metamutator with GNU General Public License v3.0 4 votes vote down vote up
/** Generates a field containing a new selector for this element and adds it to the current class 
 * 
 */
public static <E> void generateSelector(CtElement element, E initialChoice, int selectorId, EnumSet<?> possibleChoices, String prefix ) {
	
	Class<?> choiceClass = possibleChoices.iterator().next().getClass();
	
	long hashCode = (element.getPosition().toString() + element.getParent()
	.toString()).hashCode();

	CtTypeReference<Object> fieldType = element.getFactory().Type().createTypeParameterReference(ISelector.class.getCanonicalName());
	
	//doesn't work with spoon for the moment
	//CtTypeReference<Object> genericRefs = element.getFactory().Type().createTypeParameterReference(choiceClass.getCanonicalName());
	//fieldType.addActualTypeArgument(genericRefs);
	
	String selectorFieldName = prefix + selectorId;
	
	CtCodeSnippetExpression<Object> codeSnippet = element.getFactory().Core()
			.createCodeSnippetExpression();
	StringBuilder sb = new StringBuilder(Selector.class.getCanonicalName() + ".of(");
	
	// we disable the ids
	// sb.append(procId+""+selectorId);
	// sb.append(',');

	// now the options
	sb.append("new "+choiceClass.getCanonicalName()+"[]{");
	
	// the original operator, always the first one
	sb.append(initialChoice.getClass().getCanonicalName()+"."+initialChoice.toString());
	
	// the other alternatives
	for (Object choose : possibleChoices) {
		if (choose.equals(initialChoice)) {
			continue;
		}
		sb.append(',').append(choose.getClass().getCanonicalName()+"."+choose.toString());
	}
	
	sb.append("})");
	
	// adding location
	if (element.getParent(CtType.class).isTopLevel()) {
		sb.append(".in("
				+ element.getParent(CtType.class).getQualifiedName()
				+ ".class)");
	}
	
	// adding identifier
	sb.append(".id(\"" + selectorFieldName + "\")");
	
	codeSnippet.setValue(sb.toString());
	
	CtClass<?> type = getTopLevelClass(element);
	
	CtField<Object> field = element.getFactory().Field().create(
			type,
			EnumSet.of(ModifierKind.FINAL, ModifierKind.PRIVATE,
					ModifierKind.STATIC), fieldType, selectorFieldName,
			codeSnippet);
	
	type.addField(field);
}
 
Example 9
Source File: ArithmeticOperatorMetaMutator.java    From metamutator with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Converts "a op b" bean op one of "-", "+", "*", "/"
 *    (  (op(1, 0, "-") && (a - b))
 *    || (op(1, 1, "+") && (a + b))
 *    || (op(1, 2, "*") && (a * b))
 *    || (op(1, 3, "/") && (a / b))
 *    )
 *
 * @param expression
 * @param operators
 */
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 originalExpression = expression.toString();
	
	String newExpression = "";
	
	int cpt = 0;
	BinaryOperatorKind tmp = null;
	for(BinaryOperatorKind op : ARITHMETIC_OPERATORS){
			expression.setKind(op);
			newExpression += "(" + PREFIX + thisIndex + ".is(" + op.getClass().getCanonicalName()+"."+op.toString() + ")) ? (" + expression + ")";
			newExpression += " : ";
	}

	newExpression += "(" + originalExpression + ")"; 
	CtCodeSnippetExpression<Boolean> codeSnippet = getFactory().Core()
			.createCodeSnippetExpression();
	codeSnippet.setValue('(' + newExpression + ')');
	expression.replace(codeSnippet);
	expression.replace(expression);
	Selector.generateSelector(expression, originalKind, thisIndex, operators, PREFIX);
	System.out.println("nb mutants " +index);

	hostSpots.add(expression);
}