Search in sources :

Example 86 with Factory

use of spoon.reflect.factory.Factory in project spoon by INRIA.

the class ReplaceTest method testReplaceAParameterReferenceToFieldReference.

@Test
public void testReplaceAParameterReferenceToFieldReference() throws Exception {
    // contract: replace a parameter reference to a field reference.
    final Factory factory = build(Tacos.class);
    final CtType<Tacos> aTacos = factory.Type().get(Tacos.class);
    final CtInvocation inv = aTacos.getMethodsByName("m3").get(0).getElements(new TypeFilter<>(CtInvocation.class)).get(0);
    final CtVariableRead<?> variableRead = (CtVariableRead<?>) inv.getArguments().get(0);
    final CtParameterReference<?> aParameterReference = (CtParameterReference<?>) variableRead.getVariable();
    final CtFieldReference<?> aFieldReference = aTacos.getField("field").getReference();
    assertEquals(aParameterReference, variableRead.getVariable());
    assertEquals("java.lang.System.err.println(param)", inv.toString());
    aParameterReference.replace(aFieldReference);
    assertEquals(aFieldReference, variableRead.getVariable());
    assertEquals("java.lang.System.err.println(field)", inv.toString());
}
Also used : CtInvocation(spoon.reflect.code.CtInvocation) CtParameterReference(spoon.reflect.reference.CtParameterReference) Tacos(spoon.test.replace.testclasses.Tacos) Factory(spoon.reflect.factory.Factory) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) ReferenceTypeFilter(spoon.reflect.visitor.filter.ReferenceTypeFilter) CtVariableRead(spoon.reflect.code.CtVariableRead) Test(org.junit.Test)

Example 87 with Factory

use of spoon.reflect.factory.Factory in project spoon by INRIA.

the class ReplaceTest method testReplaceIntegerReference.

@Test
public void testReplaceIntegerReference() throws Exception {
    // contract: replace a primitive type reference by another one.
    final Factory factory = build(Tacos.class);
    final CtType<Tacos> aTacos = factory.Type().get(Tacos.class);
    final CtMethod<?> aMethod = aTacos.getMethodsByName("m").get(0);
    assertEquals(factory.Type().INTEGER_PRIMITIVE, aMethod.getType());
    aMethod.getType().replace(factory.Type().DOUBLE_PRIMITIVE);
    assertEquals(factory.Type().DOUBLE_PRIMITIVE, aMethod.getType());
}
Also used : Tacos(spoon.test.replace.testclasses.Tacos) Factory(spoon.reflect.factory.Factory) Test(org.junit.Test)

Example 88 with Factory

use of spoon.reflect.factory.Factory in project spoon by INRIA.

the class ClassesTest method testInnerClassContruction.

@Test
public void testInnerClassContruction() throws Exception {
    Factory f = build(PrivateInnerClasses.class);
    CtClass<?> c = f.Class().get(PrivateInnerClasses.class);
    assertNotNull(c);
    assertEquals(0, f.getEnvironment().getErrorCount());
}
Also used : Factory(spoon.reflect.factory.Factory) Test(org.junit.Test)

Example 89 with Factory

use of spoon.reflect.factory.Factory in project spoon by INRIA.

the class TargetedExpressionTest method testTargetsOfInv.

@Test
public void testTargetsOfInv() throws Exception {
    // contract: Specify declaring type of the executable of an invocation, the target of the invocation and its result.
    final Factory factory = build(Foo.class, Bar.class, SuperClass.class);
    final CtClass<Foo> type = factory.Class().get(Foo.class);
    final CtClass<Foo.Fii.Fuu> fuu = factory.Class().<Foo.Fii.Fuu>get(Foo.Fii.Fuu.class);
    final CtTypeReference<Foo> expectedType = type.getReference();
    final CtTypeReference<Bar> expectedBarType = factory.Class().<Bar>get(Bar.class).getReference();
    final CtTypeReference<SuperClass> expectedSuperClassType = factory.Class().<SuperClass>get(SuperClass.class).getReference();
    final CtTypeReference<Foo.Fii.Fuu> expectedFuuType = fuu.getReference();
    final CtThisAccess<Foo> expectedThisAccess = factory.Code().createThisAccess(expectedType);
    final CtTypeAccess<Foo> fooTypeAccess = factory.Code().createTypeAccess(expectedType);
    final CtTypeAccess<SuperClass> superClassTypeAccess = factory.Code().createTypeAccess(expectedSuperClassType);
    final CtThisAccess<Foo> expectedSuperThisAccess = factory.Code().createThisAccess(expectedType);
    expectedSuperThisAccess.setTarget(superClassTypeAccess);
    final List<CtInvocation<?>> elements = type.getMethodsByName("inv").get(0).getElements(new TypeFilter<CtInvocation<?>>(CtInvocation.class));
    assertEquals(7, elements.size());
    assertEqualsInvocation(new ExpectedTargetedExpression().declaringType(expectedType).target(CtConstructorCallImpl.class).result("new spoon.test.targeted.testclasses.Foo(0, 0).method()"), elements.get(0));
    assertEqualsInvocation(new ExpectedTargetedExpression().declaringType(expectedType).target(CtFieldReadImpl.class).result("foo.method()"), elements.get(1));
    assertEqualsInvocation(new ExpectedTargetedExpression().declaringType(expectedType).target(expectedThisAccess).result("this.method()"), elements.get(2));
    assertEqualsInvocation(new ExpectedTargetedExpression().declaringType(expectedType).target(expectedThisAccess).result("method()"), elements.get(3));
    assertEqualsInvocation(new ExpectedTargetedExpression().declaringType(expectedBarType).target(CtFieldReadImpl.class).result("bar.methodBar()"), elements.get(4));
    assertEqualsInvocation(new ExpectedTargetedExpression().declaringType(expectedFuuType).target(CtFieldReadImpl.class).result("fuu.method()"), elements.get(5));
    assertEqualsInvocation(new ExpectedTargetedExpression().declaringType(expectedSuperClassType).target(expectedSuperThisAccess).result("superMethod()"), elements.get(6));
    assertEquals(fooTypeAccess.getType().getQualifiedName(), ((CtThisAccess) elements.get(2).getTarget()).getTarget().getType().getQualifiedName());
    assertEquals(fooTypeAccess.getType().getQualifiedName(), ((CtThisAccess) elements.get(3).getTarget()).getTarget().getType().getQualifiedName());
}
Also used : Foo(spoon.test.targeted.testclasses.Foo) SuperClass(spoon.test.targeted.testclasses.SuperClass) Factory(spoon.reflect.factory.Factory) Bar(spoon.test.targeted.testclasses.Bar) CtInvocation(spoon.reflect.code.CtInvocation) Test(org.junit.Test)

Example 90 with Factory

use of spoon.reflect.factory.Factory in project spoon by INRIA.

the class TargetedExpressionTest method testTargetsOfFieldInAnonymousClass.

@Test
public void testTargetsOfFieldInAnonymousClass() throws Exception {
    final Factory factory = build(Foo.class, Bar.class, SuperClass.class);
    final CtClass<Foo> type = factory.Class().get(Foo.class);
    final CtTypeReference<Foo> expectedType = type.getReference();
    final CtClass<?> anonymousClass = type.getElements(new TypeFilter<CtClass>(CtClass.class) {

        @Override
        public boolean matches(CtClass element) {
            return element.isAnonymous() && super.matches(element);
        }
    }).get(0);
    final CtTypeReference<?> expectedAnonymousType = anonymousClass.getReference();
    final CtThisAccess<Foo> expectedThisAccess = factory.Code().createThisAccess(expectedType);
    final CtThisAccess expectedAnonymousThisAccess = factory.Code().createThisAccess(expectedAnonymousType);
    final CtMethod<?> method = anonymousClass.getMethodsByName("invStatic").get(0);
    final List<CtFieldAccess> elements = method.getElements(new TypeFilter<>(CtFieldAccess.class));
    assertEquals(3, elements.size());
    assertEqualsFieldAccess(new ExpectedTargetedExpression().declaringType(expectedType).target(expectedThisAccess).result("this.i"), elements.get(0));
    assertEqualsFieldAccess(new ExpectedTargetedExpression().declaringType(expectedAnonymousType).target(expectedAnonymousThisAccess).result("this.i"), elements.get(1));
    assertEqualsFieldAccess(new ExpectedTargetedExpression().declaringType(expectedAnonymousType).target(expectedAnonymousThisAccess).result("i"), elements.get(2));
}
Also used : CtFieldAccess(spoon.reflect.code.CtFieldAccess) Foo(spoon.test.targeted.testclasses.Foo) Factory(spoon.reflect.factory.Factory) CtThisAccess(spoon.reflect.code.CtThisAccess) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) CtClass(spoon.reflect.declaration.CtClass) Test(org.junit.Test)

Aggregations

Factory (spoon.reflect.factory.Factory)364 Test (org.junit.Test)322 Launcher (spoon.Launcher)154 CtClass (spoon.reflect.declaration.CtClass)87 CtMethod (spoon.reflect.declaration.CtMethod)73 ModelUtils.createFactory (spoon.testing.utils.ModelUtils.createFactory)65 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)53 File (java.io.File)41 CtStatement (spoon.reflect.code.CtStatement)36 CtTypeReference (spoon.reflect.reference.CtTypeReference)32 CtAnnotation (spoon.reflect.declaration.CtAnnotation)31 CtInvocation (spoon.reflect.code.CtInvocation)30 SpoonModelBuilder (spoon.SpoonModelBuilder)29 DefaultCoreFactory (spoon.support.DefaultCoreFactory)29 List (java.util.List)25 FileSystemFile (spoon.support.compiler.FileSystemFile)25 ArrayList (java.util.ArrayList)24 CtExpression (spoon.reflect.code.CtExpression)20 Annotation (java.lang.annotation.Annotation)19 MainTest (spoon.test.main.MainTest)19