当前位置: 首页>>代码示例>>Java>>正文


Java CtBinaryOperator.setKind方法代码示例

本文整理汇总了Java中spoon.reflect.code.CtBinaryOperator.setKind方法的典型用法代码示例。如果您正苦于以下问题:Java CtBinaryOperator.setKind方法的具体用法?Java CtBinaryOperator.setKind怎么用?Java CtBinaryOperator.setKind使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在spoon.reflect.code.CtBinaryOperator的用法示例。


在下文中一共展示了CtBinaryOperator.setKind方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: createEqExpressionFromSwitchCase

import spoon.reflect.code.CtBinaryOperator; //导入方法依赖的package包/类
public CtExpression<Boolean> createEqExpressionFromSwitchCase(final @NotNull CtSwitch<?> switchStat, final @NotNull CtCase<?> caze) {
	if(caze.getCaseExpression() == null) {// i.e. default case
		return switchStat.getCases().stream().filter(c -> c.getCaseExpression() != null).
			map(c -> negBoolExpression(createEqExpressionFromSwitchCase(switchStat, c))).reduce((a, b) -> andBoolExpression(a, b, false)).
			orElseGet(() -> switchStat.getFactory().Code().createLiteral(Boolean.TRUE));
	}

	CtBinaryOperator<Boolean> exp = switchStat.getFactory().Core().createBinaryOperator();
	// A switch is an equality test against values
	exp.setKind(BinaryOperatorKind.EQ);
	// The tested object
	exp.setLeftHandOperand(switchStat.getSelector().clone());
	// The tested constant
	exp.setRightHandOperand(caze.getCaseExpression().clone());

	return exp;
}
 
开发者ID:diverse-project,项目名称:InspectorGuidget,代码行数:18,代码来源:SpoonHelper.java

示例2: applyMono

import spoon.reflect.code.CtBinaryOperator; //导入方法依赖的package包/类
public void applyMono(CodeFragment tp) throws Exception {
    transplantationPoint = tp;
    Factory factory = getInputProgram().getFactory();
    CtTypeReference tInt = factory.Type().INTEGER_PRIMITIVE;
    CtLiteral one = factory.Core().createLiteral();
    one.setValue(1);
    CtReturn retStmt = (CtReturn) tp.getCtCodeFragment();

    CtBinaryOperator retExpression = factory.Core().createBinaryOperator();
    retExpression.setKind(BinaryOperatorKind.MUL);
    retExpression.setRightHandOperand(retStmt.getReturnedExpression());
    retExpression.setLeftHandOperand(one);

    multiply = retExpression;
    CtReturn retStmt2 = (CtReturn) factory.Core().clone(tp.getCtCodeFragment());
    retStmt2.setReturnedExpression(retExpression);
    tp.getCtCodeFragment().replace(retStmt2);
}
 
开发者ID:DIVERSIFY-project,项目名称:sosiefier,代码行数:19,代码来源:MultiplyByOne.java

示例3: process

import spoon.reflect.code.CtBinaryOperator; //导入方法依赖的package包/类
@Override
public void process(CtElement candidate) {
	if (!(candidate instanceof CtBinaryOperator)) {
		return;
	}
	CtBinaryOperator op = (CtBinaryOperator)candidate;
	op.setKind(BinaryOperatorKind.MINUS);
}
 
开发者ID:SpoonLabs,项目名称:spoon-examples,代码行数:9,代码来源:BinaryOperatorMutator.java

示例4: andBoolExpression

import spoon.reflect.code.CtBinaryOperator; //导入方法依赖的package包/类
public CtExpression<Boolean> andBoolExpression(final @NotNull CtExpression<Boolean> exp1, final @NotNull CtExpression<Boolean> exp2, final boolean clone) {
	final CtBinaryOperator<Boolean> and = exp1.getFactory().Core().createBinaryOperator();
	and.setKind(BinaryOperatorKind.AND);

	if(clone) {
		and.setLeftHandOperand(exp1.clone());
		and.setRightHandOperand(exp2.clone());
	}else {
		and.setLeftHandOperand(exp1);
		and.setRightHandOperand(exp2);
	}
	return and;
}
 
开发者ID:diverse-project,项目名称:InspectorGuidget,代码行数:14,代码来源:SpoonHelper.java

示例5: example

import spoon.reflect.code.CtBinaryOperator; //导入方法依赖的package包/类
@Test
public void example() throws Exception {
 Launcher l = new Launcher();
 
 // required for having IFoo.class in the classpath in Maven
 l.setArgs(new String[] {"--source-classpath","target/test-classes"});
 
 l.addInputResource("src/test/resources/transformation/");
 l.buildModel();
 
 CtClass foo = l.getFactory().Package().getRootPackage().getElements(new NamedElementFilter<>(CtClass.class, "Foo1")).get(0);

 // compiling and testing the initial class
 Class<?> fooClass = InMemoryJavaCompiler.newInstance().compile(foo.getQualifiedName(), "package "+foo.getPackage().getQualifiedName()+";"+foo.toString());
 IFoo x = (IFoo) fooClass.newInstance();
 // testing its behavior
 assertEquals(5, x.m());

 // now we apply a transformation
 // we replace "+" by "-"
 for(Object e : foo.getElements(new TypeFilter(CtBinaryOperator.class))) {
  CtBinaryOperator op = (CtBinaryOperator)e;
  if (op.getKind()==BinaryOperatorKind.PLUS) {
	  op.setKind(BinaryOperatorKind.MINUS);
  }
 }
 
 // first assertion on the results of the transfo
 
 // there are no more additions in the code
 assertEquals(0, foo.getElements(new Filter<CtBinaryOperator<?>>() {
@Override
public boolean matches(CtBinaryOperator<?> arg0) {
	return arg0.getKind()==BinaryOperatorKind.PLUS;
}		  
 }).size());

 // second assertions on the behavior of the transformed code
 
 // compiling and testing the transformed class
 fooClass = InMemoryJavaCompiler.newInstance().compile(foo.getQualifiedName(), "package "+foo.getPackage().getQualifiedName()+";"+foo.toString());
 IFoo y = (IFoo) fooClass.newInstance();
 // testing its behavior with subtraction
 assertEquals(1, y.m());
 
 System.out.println("yes y.m()="+y.m());
}
 
开发者ID:SpoonLabs,项目名称:spoon-examples,代码行数:48,代码来源:OnTheFlyTransfoTest.java

示例6: testM70ExpressionAsModificationPoint

import spoon.reflect.code.CtBinaryOperator; //导入方法依赖的package包/类
/**
 * This test checks if astor is able to manipulate expression as element in
 * a modification point
 * 
 * @throws Exception
 */
@Test
public void testM70ExpressionAsModificationPoint() throws Exception {

	CommandSummary command = MathCommandsTests.getMath70Command();
	command.command.put("-parameters",
			ExtensionPoints.INGREDIENT_PROCESSOR.identifier + File.pathSeparator
					+ ExpressionIngredientSpaceProcessor.class.getCanonicalName() + File.pathSeparator
					+ "applytemplates:false");
	command.command.put("-maxgen", "0");// Avoid evolution

	AstorMain main1 = new AstorMain();
	main1.execute(command.flat());

	List<ProgramVariant> variantss = main1.getEngine().getVariants();
	assertTrue(variantss.size() > 0);

	JGenProg jgp = (JGenProg) main1.getEngine();
	AstorIngredientSpace ingSpace = (AstorIngredientSpace) jgp.getIngredientStrategy().getIngredientSpace();

	int i = 0;
	for (ModificationPoint modpoint : variantss.get(0).getModificationPoints()) {
		System.out.println("--> " + (i++) + " " + modpoint.getCodeElement());

	}
	ModificationPoint modificationPoint = variantss.get(0).getModificationPoints().get(14);
	CtExpression originalExp = (CtExpression) modificationPoint.getCodeElement();
	assertEquals("i < (maximalIterationCount)", originalExp.toString());
	CtClass parentClass = originalExp.getParent(CtClass.class);
	String parentClassString = parentClass.toString();

	assertNotNull(parentClass);
	// Let's mutate the expression
	CtExpression clonedExp = (CtExpression) MutationSupporter.clone(originalExp);
	CtBinaryOperator binOpExpr = (CtBinaryOperator) clonedExp;
	// Let's check that the operator that will be inserted is not equal to
	// the current.
	assertNotEquals(BinaryOperatorKind.GT, binOpExpr.getKind());
	// Update operator
	binOpExpr.setKind(BinaryOperatorKind.GT);

	ExpressionReplaceOperator expOperator = new ExpressionReplaceOperator();
	OperatorInstance expOperatorInstance = new OperatorInstance(modificationPoint, expOperator, originalExp,
			clonedExp);

	boolean applied = expOperatorInstance.applyModification();
	assertTrue(applied);

	assertNotEquals(clonedExp, originalExp);
	assertEquals("i > (maximalIterationCount)", clonedExp.toString());
	assertEquals("i > (maximalIterationCount)", modificationPoint.getCodeElement().toString());

	// Let's check that the mutated class is different to the original
	String mutatedClassString = parentClass.toString();
	assertNotEquals(parentClassString, mutatedClassString);

	// Undo operator, we should have the original class
	boolean undo = expOperatorInstance.undoModification();
	String revertedChangeClassString = parentClass.toString();
	assertTrue(undo);
	assertEquals("i < (maximalIterationCount)", modificationPoint.getCodeElement().toString());

	assertEquals(parentClassString, revertedChangeClassString);

}
 
开发者ID:SpoonLabs,项目名称:astor,代码行数:71,代码来源:ExpressionIngredientSpaceTest.java


注:本文中的spoon.reflect.code.CtBinaryOperator.setKind方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。