Search in sources :

Example 6 with CtThisAccess

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

the class QualifiedThisRefTest method testCloneThisAccess.

@Test
public void testCloneThisAccess() throws Exception {
    // contract: the target of "this" is correct and can be cloned
    final Factory factory = build(Adobada.class);
    final CtClass<Adobada> adobada = factory.Class().get(Adobada.class);
    final CtMethod<?> m2 = adobada.getMethod("methodUsingjlObjectMethods");
    CtThisAccess th = (CtThisAccess) m2.getElements(new TypeFilter(CtThisAccess.class)).get(0);
    assertEquals(true, th.isImplicit());
    assertEquals("notify()", th.getParent().toString());
    CtInvocation<?> clone = m2.clone().getBody().getStatement(0);
    // clone preserves implicitness
    assertEquals(true, clone.getTarget().isImplicit());
    // the original bug
    assertEquals("notify()", clone.toString());
// note that this behavior means that you can only keep cloned "this" in the same class,
// and you cannot "transplant" a cloned "this" to another class
// it makes perfectly sense about the meaning of this.
// to "transplant" a this, you have to first set the target to null
}
Also used : Factory(spoon.reflect.factory.Factory) CtThisAccess(spoon.reflect.code.CtThisAccess) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) Adobada(spoon.test.delete.testclasses.Adobada) Test(org.junit.Test)

Example 7 with CtThisAccess

use of spoon.reflect.code.CtThisAccess 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)

Aggregations

CtThisAccess (spoon.reflect.code.CtThisAccess)7 Factory (spoon.reflect.factory.Factory)6 Test (org.junit.Test)5 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)4 Foo (spoon.test.targeted.testclasses.Foo)4 CtFieldAccess (spoon.reflect.code.CtFieldAccess)2 CtInvocation (spoon.reflect.code.CtInvocation)2 CtClass (spoon.reflect.declaration.CtClass)2 SuperClass (spoon.test.targeted.testclasses.SuperClass)2 CtAssignment (spoon.reflect.code.CtAssignment)1 CtLocalVariable (spoon.reflect.code.CtLocalVariable)1 CtTargetedExpression (spoon.reflect.code.CtTargetedExpression)1 CtTry (spoon.reflect.code.CtTry)1 CtConstructor (spoon.reflect.declaration.CtConstructor)1 CtParameter (spoon.reflect.declaration.CtParameter)1 Adobada (spoon.test.delete.testclasses.Adobada)1