Search in sources :

Example 81 with Factory

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());
}
Also used : SpoonModelBuilder(spoon.SpoonModelBuilder) DefaultCoreFactory(spoon.support.DefaultCoreFactory) Factory(spoon.reflect.factory.Factory) CtClass(spoon.reflect.declaration.CtClass) CtInvocation(spoon.reflect.code.CtInvocation) JDTSnippetCompiler(spoon.support.compiler.jdt.JDTSnippetCompiler) Launcher(spoon.Launcher) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Example 82 with Factory

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());
}
Also used : SpoonModelBuilder(spoon.SpoonModelBuilder) CtAssignment(spoon.reflect.code.CtAssignment) DefaultCoreFactory(spoon.support.DefaultCoreFactory) DefaultCoreFactory(spoon.support.DefaultCoreFactory) Factory(spoon.reflect.factory.Factory) FactoryImpl(spoon.reflect.factory.FactoryImpl) CtClass(spoon.reflect.declaration.CtClass) JDTSnippetCompiler(spoon.support.compiler.jdt.JDTSnippetCompiler) CtMethod(spoon.reflect.declaration.CtMethod) StandardEnvironment(spoon.support.StandardEnvironment) Test(org.junit.Test)

Example 83 with Factory

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());
}
Also used : CtBinaryOperator(spoon.reflect.code.CtBinaryOperator) Factory(spoon.reflect.factory.Factory) ModelUtils.createFactory(spoon.testing.utils.ModelUtils.createFactory) Test(org.junit.Test)

Example 84 with Factory

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());
}
Also used : CtFieldAccess(spoon.reflect.code.CtFieldAccess) Factory(spoon.reflect.factory.Factory) CtConstructor(spoon.reflect.declaration.CtConstructor) Test(org.junit.Test)

Example 85 with Factory

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));
}
Also used : Foo(spoon.test.targeted.testclasses.Foo) Factory(spoon.reflect.factory.Factory) Bar(spoon.test.targeted.testclasses.Bar) CtInvocation(spoon.reflect.code.CtInvocation) 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