@Test public void testConstructorCallFactory() throws Exception { CtTypeReference<ArrayList> ctTypeReference = factory.Code().createCtTypeReference(ArrayList.class); CtConstructorCall<ArrayList> constructorCall = factory.Code().createConstructorCall(ctTypeReference); assertEquals("new java.util.ArrayList()", constructorCall.toString()); CtConstructorCall<ArrayList> constructorCallWithParameter = factory.Code().createConstructorCall(ctTypeReference, constructorCall); assertEquals( "new java.util.ArrayList(new java.util.ArrayList())", constructorCallWithParameter.toString()); }
protected Map<Statement, Double> buildCodeFragmentFor(CtType cl, Coverage coverage) { Factory factory = cl.getFactory(); Map<Statement, Double> codeFragments = new LinkedHashMap<>(); for (CtMethod<?> mth : (Set<CtMethod>) cl.getMethods()) { if (!mth.getModifiers().contains(ModifierKind.ABSTRACT) && !mth.getModifiers().contains(ModifierKind.PRIVATE)) { // && getCoverageForMethod(coverage, cl, mth) != 1.0) { CtExecutableReference executableRef = factory.Executable().createReference(mth); executableRef.setStatic(mth.getModifiers().contains(ModifierKind.STATIC)); CtInvocation invocation = factory.Code().createInvocation(buildVarRef(cl.getReference(), factory), executableRef); invocation.setArguments( mth.getParameters() .stream() .map(param -> buildVarRef(param.getType(), factory)) .collect(Collectors.toList())); invocation.setType(mth.getType()); Statement stmt = new Statement(invocation); codeFragments.put(stmt, getCoverageForMethod(coverage, cl, mth)); } } return codeFragments; }
protected CtVariableRead buildVarRef(CtTypeReference type, Factory factory) { CtTypeReference<Object> typeRef = factory.Core().clone(type); CtLocalVariable<Object> localVar = factory.Core().createLocalVariable(); localVar.setType(typeRef); localVar.setSimpleName("var_" + type.getSimpleName() + "_" + System.currentTimeMillis()); CtVariableReadImpl varRead = new CtVariableReadImpl(); varRead.setVariable(factory.Code().createLocalVariableReference(localVar)); return varRead; }
@Test public void testTransformationOnConstructorWithInsertBegin() throws Exception { final CtConstructor<?> ctConstructor = aClass.getElements(new TypeFilter<CtConstructor<?>>(CtConstructor.class)).get(0); ctConstructor.getBody().insertBegin(factory.Code().createCodeSnippetStatement("int i = 0")); assertEquals(2, ctConstructor.getBody().getStatements().size()); assertEquals("super()", ctConstructor.getBody().getStatement(0).toString()); TestUtils.canBeBuilt("./target/spooned/spoon/test/constructor/testclasses/", 8); }
@Test public void testTransformationOnConstructorWithInsertBefore() throws Exception { final CtConstructor<?> ctConstructor = aClass.getElements(new TypeFilter<CtConstructor<?>>(CtConstructor.class)).get(0); try { ctConstructor .getBody() .getStatement(0) .insertBefore(factory.Code().createCodeSnippetStatement("int i = 0")); fail(); } catch (RuntimeException ignore) { } assertEquals(1, ctConstructor.getBody().getStatements().size()); assertEquals("super()", ctConstructor.getBody().getStatement(0).toString()); }
@Test public void testAccessToStringOnPostIncrement() throws Exception { // contract: a target to a post increment on a variable access write brackets. Factory factory = createFactory(); CtClass<?> clazz = factory.Code() .createCodeSnippetStatement( // "class X {" // + "public void foo() {" // + " Integer i = 1;" // + " (i++).toString();" // + " int k = 0;" // + " k++;" // + "}};") .compile(); CtMethod<?> foo = (CtMethod<?>) clazz.getMethods().toArray()[0]; assertEquals("(i++).toString()", foo.getBody().getStatement(1).toString()); assertEquals("k++", foo.getBody().getStatement(3).toString()); }
@Override public CtElement[] getMutatedEntries(CtBinaryOperator<?> element, Factory factory) { ArrayList<CtElement> ret = new ArrayList<CtElement>(); if (element == null) return emptySet(); for (BinaryOperatorKind kind : operators) { if (element.getKind() != kind) { CtExpression<?> right_c = factory.Core().clone(element.getRightHandOperand()); CtExpression<?> left_c = factory.Core().clone(element.getLeftHandOperand()); CtBinaryOperator<?> binaryOp = factory.Code().createBinaryOperator(left_c, right_c, kind); // Set parent right_c.setParent(binaryOp); left_c.setParent(binaryOp); ret.add(binaryOp); } } return ret.toArray(new CtElement[1]); }