use of spoon.reflect.factory.Factory in project spoon by INRIA.
the class SignatureTest method testNullSignatureInUnboundVariable.
@Test
public void testNullSignatureInUnboundVariable() throws Exception {
// Unbound variable access bug fix:
// Bug description: The signature printer ignored the element Unbound variable reference
// (as well all Visitor that extend CtVisitor)
// Fix description: modify CtVisitor (including SignaturePrinter) for visiting unbound variable access.
Factory factory = new Launcher().createFactory();
// We want to compile a class with an reference to a class that is not
// in the classpath
// As consequence, we set the option NoClasspath as true.
factory.getEnvironment().setNoClasspath(true);
String unboundVarAccess = "Complex.I";
String content = "" + "class X {" + "public Object foo(java.util.List<String> l) {" + " Integer.toString(" + unboundVarAccess + ");" + " return null;" + "}};";
SpoonModelBuilder builder = new JDTSnippetCompiler(factory, content);
try {
builder.build();
Assert.fail();
} catch (Exception e) {
// Must fail due to the unbound element "Complex.I"
}
CtClass<?> clazz1 = (CtClass<?>) factory.Type().getAll().get(0);
Set<CtMethod<?>> methods = clazz1.getMethods();
CtMethod<?> method = (CtMethod<?>) methods.toArray()[0];
assertEquals("foo(java.util.List)", method.getSignature());
CtInvocation<?> invo = (CtInvocation<?>) method.getBody().getStatement(0);
CtExpression<?> argument1 = invo.getArguments().get(0);
assertEquals(unboundVarAccess, argument1.toString());
}
use of spoon.reflect.factory.Factory in project spoon by INRIA.
the class SignatureTest method testUnboundFieldSignature.
@Test
public void testUnboundFieldSignature() {
Factory factory = new FactoryImpl(new DefaultCoreFactory(), new StandardEnvironment());
factory.getEnvironment().setNoClasspath(true);
String content = "" + "class PR {" + "public java.io.File foo(String p) {" + " this.mfield = p; " + " return null;" + "}" + "};";
SpoonModelBuilder builder = new JDTSnippetCompiler(factory, content);
try {
builder.build();
fail();
} catch (Exception e) {
// must fail
}
CtClass<?> clazz1 = (CtClass<?>) factory.Type().getAll().get(0);
assertNotNull(clazz1);
// **FIRST PART: passing local variable access.
// /--------From the first method we take the method invocations
CtMethod<?> methodString = (CtMethod<?>) clazz1.getMethods().toArray()[0];
assertEquals("foo(java.lang.String)", methodString.getSignature());
CtAssignment<?, ?> invoToInt1 = (CtAssignment<?, ?>) methodString.getBody().getStatement(0);
CtExpression<?> left = invoToInt1.getAssigned();
assertEquals("this.mfield", left.toString());
// null because noclasspath
assertEquals(null, left.getType());
assertEquals("this.mfield = p", invoToInt1.toString());
}
use of spoon.reflect.factory.Factory in project spoon by INRIA.
the class SnippetTest method testCompileSnippetSeveralTimes.
@Test
public void testCompileSnippetSeveralTimes() throws Exception {
// contract: a snippet object can be reused several times
final Factory factory = createFactory();
final CtCodeSnippetExpression<Object> snippet = factory.Code().createCodeSnippetExpression("1 > 2");
// Compile a first time the snippet.
final CtExpression<Object> compile = snippet.compile();
// Compile a second time the same snippet.
final CtExpression<Object> secondCompile = snippet.compile();
assertTrue(compile instanceof CtBinaryOperator);
assertEquals("1 > 2", compile.toString());
assertTrue(secondCompile instanceof CtBinaryOperator);
assertEquals("1 > 2", secondCompile.toString());
// Compile a third time a snippet but with an expression set.
snippet.setValue("1 > 3");
final CtExpression<Object> thirdCompile = snippet.compile();
assertTrue(thirdCompile instanceof CtBinaryOperator);
assertEquals("1 > 3", thirdCompile.toString());
}
use of spoon.reflect.factory.Factory in project spoon by INRIA.
the class TargetedExpressionTest method testTargetOfFieldAccess.
@Test
public void testTargetOfFieldAccess() throws Exception {
Factory factory = build(Foo.class, Bar.class, SuperClass.class);
final CtClass<Object> type = factory.Class().get(Foo.class);
CtConstructor<?> constructor = type.getConstructors().toArray(new CtConstructor<?>[0])[0];
final List<CtFieldAccess<?>> elements = constructor.getElements(new TypeFilter<CtFieldAccess<?>>(CtFieldAccess.class));
assertEquals(2, elements.size());
assertEquals("Target is CtThisAccessImpl if there is a 'this' explicit.", CtThisAccessImpl.class, elements.get(0).getTarget().getClass());
assertNotNull("Target isn't null if there is a 'this' explicit.", elements.get(1).getTarget());
assertTrue(elements.get(1).getTarget().isImplicit());
}
use of spoon.reflect.factory.Factory in project spoon by INRIA.
the class TargetedExpressionTest method testStaticTargetsOfInv.
@Test
public void testStaticTargetsOfInv() throws Exception {
// contract: Specify declaring type of the executable of an static invocation, the target of the static 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<Foo.Fii.Fuu> expectedFuuType = fuu.getReference();
final CtThisAccess<Foo> expectedThisAccess = type.getFactory().Code().createThisAccess(expectedType);
final CtTypeAccess<Foo> expectedTypeAccess = type.getFactory().Code().createTypeAccess(expectedType);
final CtTypeAccess<Bar> expectedBarTypeAccess = type.getFactory().Code().createTypeAccess(expectedBarType);
final CtMethod<?> invMethod = type.getMethodsByName("invStatic").get(0);
final List<CtInvocation<?>> elements = invMethod.getElements(new TypeFilter<CtInvocation<?>>(CtInvocation.class));
assertEquals(8, elements.size());
assertEqualsInvocation(new ExpectedTargetedExpression().declaringType(expectedType).target(CtConstructorCallImpl.class).result("new spoon.test.targeted.testclasses.Foo(0, 0).staticMethod()"), elements.get(0));
assertEqualsInvocation(new ExpectedTargetedExpression().declaringType(expectedType).target(CtFieldReadImpl.class).result("foo.staticMethod()"), elements.get(1));
assertEqualsInvocation(new ExpectedTargetedExpression().declaringType(expectedType).target(expectedThisAccess).result("this.staticMethod()"), elements.get(2));
assertEqualsInvocation(new ExpectedTargetedExpression().declaringType(expectedType).target(expectedTypeAccess).result("spoon.test.targeted.testclasses.Foo.staticMethod()"), elements.get(3));
assertEqualsInvocation(new ExpectedTargetedExpression().declaringType(expectedType).target(expectedTypeAccess).result("spoon.test.targeted.testclasses.Foo.staticMethod()"), elements.get(4));
assertEqualsInvocation(new ExpectedTargetedExpression().declaringType(expectedBarType).target(expectedBarTypeAccess).result("spoon.test.targeted.testclasses.Bar.staticMethodBar()"), elements.get(5));
assertEqualsInvocation(new ExpectedTargetedExpression().declaringType(expectedBarType).target(expectedBarTypeAccess).result("spoon.test.targeted.testclasses.Bar.staticMethodBar()"), elements.get(6));
assertEqualsInvocation(new ExpectedTargetedExpression().declaringType(expectedFuuType).target(factory.Code().createTypeAccess(expectedFuuType)).result("spoon.test.targeted.testclasses.Foo.Fii.Fuu.m()"), elements.get(7));
}
Aggregations