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());
}
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());
}
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());
}
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());
}
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));
}
Aggregations