Search in sources :

Example 26 with CtLocalVariable

use of spoon.reflect.code.CtLocalVariable in project spoon by INRIA.

the class ParentTest method testGetParentWithFilter.

@Test
public void testGetParentWithFilter() throws Exception {
    // addType should set Parent
    CtClass<Foo> clazz = (CtClass<Foo>) factory.Class().getAll().get(0);
    CtMethod<Object> m = clazz.getMethod("m");
    // get three = "" in one = two = three = "";
    CtExpression statement = ((CtAssignment) ((CtAssignment) m.getBody().getStatement(3)).getAssignment()).getAssignment();
    CtPackage ctPackage = statement.getParent(new TypeFilter<CtPackage>(CtPackage.class));
    assertEquals(Foo.class.getPackage().getName(), ctPackage.getQualifiedName());
    CtStatement ctStatement = statement.getParent(new AbstractFilter<CtStatement>(CtStatement.class) {

        @Override
        public boolean matches(CtStatement element) {
            return element.getParent() instanceof CtStatementList && super.matches(element);
        }
    });
    // the filter has to return one = two = three = ""
    assertEquals(m.getBody().getStatement(3), ctStatement);
    m = clazz.getMethod("internalClass");
    CtStatement ctStatement1 = m.getElements(new AbstractFilter<CtStatement>(CtStatement.class) {

        @Override
        public boolean matches(CtStatement element) {
            return element instanceof CtLocalVariable && super.matches(element);
        }
    }).get(0);
    // get the top class
    ctStatement1.getParent(CtType.class);
    CtType parent = ctStatement1.getParent(new AbstractFilter<CtType>(CtType.class) {

        @Override
        public boolean matches(CtType element) {
            return !element.isAnonymous() && element.isTopLevel() && super.matches(element);
        }
    });
    assertEquals(clazz, parent);
    assertNotEquals(ctStatement1.getParent(CtType.class), parent);
    // not present element
    CtWhile ctWhile = ctStatement1.getParent(new TypeFilter<CtWhile>(CtWhile.class));
    assertEquals(null, ctWhile);
    CtStatement statementParent = statement.getParent(new AbstractFilter<CtStatement>(CtStatement.class) {

        @Override
        public boolean matches(CtStatement element) {
            return true;
        }
    });
    // getParent must not return the current element
    assertNotEquals(statement, statementParent);
}
Also used : CtAssignment(spoon.reflect.code.CtAssignment) AbstractFilter(spoon.reflect.visitor.filter.AbstractFilter) CtExpression(spoon.reflect.code.CtExpression) CtLocalVariable(spoon.reflect.code.CtLocalVariable) CtWhile(spoon.reflect.code.CtWhile) CtClass(spoon.reflect.declaration.CtClass) CtType(spoon.reflect.declaration.CtType) CtStatement(spoon.reflect.code.CtStatement) CtPackage(spoon.reflect.declaration.CtPackage) CtStatementList(spoon.reflect.code.CtStatementList) Test(org.junit.Test)

Example 27 with CtLocalVariable

use of spoon.reflect.code.CtLocalVariable in project Ex2Amplifier by STAMP-project.

the class MainGenerator method removeNonStaticElement.

private static void removeNonStaticElement(final CtMethod<?> mainMethod, final CtType testClass) {
    final CtBlock<?> body = mainMethod.getBody();
    final Factory factory = mainMethod.getFactory();
    // 1 create a local variable of the test class
    final CtLocalVariable localVariableOfTestClass = factory.createLocalVariable(testClass.getReference(), Character.toLowerCase(testClass.getSimpleName().charAt(0)) + testClass.getSimpleName().substring(1), factory.createConstructorCall(testClass.getReference()));
    // 2 invoke setUp(@Before) at the begin if present
    final CtTry wrappedBefore = wrapInTryCatchMethodWithSpecificAnnotation(testClass, factory, localVariableOfTestClass, "org.junit.Before");
    if (wrappedBefore != null) {
        body.insertBegin(wrappedBefore);
    }
    // 3 invoke tearDown(@After) at the end of the block
    final CtTry wrappedAfter = wrapInTryCatchMethodWithSpecificAnnotation(testClass, factory, localVariableOfTestClass, "org.junit.After");
    if (wrappedAfter != null) {
        body.insertEnd(wrappedAfter);
    }
    // 4 replaces all non-static accesses to accesses on the local variable created at the first step
    body.getElements(new TypeFilter<CtTargetedExpression>(CtTargetedExpression.class) {

        @Override
        public boolean matches(CtTargetedExpression element) {
            return element.getTarget() instanceof CtThisAccess;
        }
    }).stream().map(CtTargetedExpression::getTarget).forEach(target -> target.replace(factory.createVariableRead(localVariableOfTestClass.getReference(), false)));
    body.insertBegin(localVariableOfTestClass);
}
Also used : Factory(spoon.reflect.factory.Factory) CtTargetedExpression(spoon.reflect.code.CtTargetedExpression) CtThisAccess(spoon.reflect.code.CtThisAccess) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) CtTry(spoon.reflect.code.CtTry) CtLocalVariable(spoon.reflect.code.CtLocalVariable)

Example 28 with CtLocalVariable

use of spoon.reflect.code.CtLocalVariable in project dspot by STAMP-project.

the class TestValueCreator method testCreateRandomLocalVar.

@Test
public void testCreateRandomLocalVar() throws Exception {
    /*
			Test the value created randomly by the value creator.
                - one primitive
                - one array
                - one object
         */
    AmplificationHelper.setSeedRandom(23L);
    ValueCreator.count = 0;
    Factory factory = Utils.getFactory();
    int count = 0;
    CtLocalVariable randomLocalVar = ValueCreator.createRandomLocalVar(factory.Type().INTEGER_PRIMITIVE);
    assertEquals("__DSPOT_vc_" + count, randomLocalVar.getSimpleName());
    assertEquals(factory.Type().INTEGER_PRIMITIVE, randomLocalVar.getType());
    assertEquals(-1150482841, ((CtLiteral) randomLocalVar.getDefaultExpression()).getValue());
    randomLocalVar = ValueCreator.createRandomLocalVar(factory.Type().createArrayReference("int"));
    count++;
    assertEquals("__DSPOT_vc_" + count, randomLocalVar.getSimpleName());
    assertEquals(factory.Type().createArrayReference("int"), randomLocalVar.getType());
    randomLocalVar = ValueCreator.createRandomLocalVar(factory.Type().createReference("fr.inria.mutation.ClassUnderTest"));
    count++;
    assertEquals("__DSPOT_vc_" + count, randomLocalVar.getSimpleName());
    assertEquals(factory.Type().createReference("fr.inria.mutation.ClassUnderTest"), randomLocalVar.getType());
}
Also used : Factory(spoon.reflect.factory.Factory) CtLocalVariable(spoon.reflect.code.CtLocalVariable) Test(org.junit.Test) AbstractTest(fr.inria.AbstractTest)

Example 29 with CtLocalVariable

use of spoon.reflect.code.CtLocalVariable in project dspot by STAMP-project.

the class TestValueCreator method testCreateNull.

@Test
public void testCreateNull() throws Exception {
    /*
			Test the value created randomly by the value creator.
                - one primitive
                - one array
                - one object
         */
    AmplificationHelper.setSeedRandom(23L);
    ValueCreator.count = 0;
    Factory factory = Utils.getFactory();
    int count = 0;
    CtLocalVariable randomLocalVar = ValueCreator.generateNullValue(factory.Type().INTEGER_PRIMITIVE);
    assertEquals("vc_" + count, randomLocalVar.getSimpleName());
    assertEquals(factory.Type().INTEGER_PRIMITIVE, randomLocalVar.getType());
    assertEquals("((int) (null))", randomLocalVar.getDefaultExpression().toString());
    randomLocalVar = ValueCreator.generateNullValue(factory.Type().createArrayReference("int"));
    count++;
    assertEquals("vc_" + count, randomLocalVar.getSimpleName());
    assertEquals(factory.Type().createArrayReference("int"), randomLocalVar.getType());
    assertEquals("((int[]) (null))", randomLocalVar.getDefaultExpression().toString());
    randomLocalVar = ValueCreator.generateNullValue(factory.Type().createReference("mutation.ClassUnderTest"));
    count++;
    assertEquals("vc_" + count, randomLocalVar.getSimpleName());
    assertEquals(factory.Type().createReference("mutation.ClassUnderTest"), randomLocalVar.getType());
    assertEquals("((mutation.ClassUnderTest) (null))", randomLocalVar.getDefaultExpression().toString());
}
Also used : Factory(spoon.reflect.factory.Factory) CtLocalVariable(spoon.reflect.code.CtLocalVariable) Test(org.junit.Test) AbstractTest(fr.inria.AbstractTest)

Aggregations

CtLocalVariable (spoon.reflect.code.CtLocalVariable)29 Test (org.junit.Test)21 Factory (spoon.reflect.factory.Factory)15 Launcher (spoon.Launcher)10 CtParameter (spoon.reflect.declaration.CtParameter)7 CtTypeReference (spoon.reflect.reference.CtTypeReference)7 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)7 CtStatement (spoon.reflect.code.CtStatement)6 CtClass (spoon.reflect.declaration.CtClass)6 CtMethod (spoon.reflect.declaration.CtMethod)6 CtElement (spoon.reflect.declaration.CtElement)5 CtLocalVariableReference (spoon.reflect.reference.CtLocalVariableReference)5 List (java.util.List)4 CtComment (spoon.reflect.code.CtComment)4 CtTry (spoon.reflect.code.CtTry)4 CtField (spoon.reflect.declaration.CtField)4 CtType (spoon.reflect.declaration.CtType)4 CtAssignment (spoon.reflect.code.CtAssignment)3 CtCatchVariable (spoon.reflect.code.CtCatchVariable)3 CtExpression (spoon.reflect.code.CtExpression)3