Search in sources :

Example 36 with Factory

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

the class ExecutableFactoryTest method testCreateReference.

@Test
public void testCreateReference() {
    Factory f = createFactory();
    ExecutableFactory ef = f.Executable();
    String signature = "boolean Object#equals(Object)";
    CtExecutableReference<Object> eref = ef.createReference(signature);
    String type = eref.getType().getQualifiedName();
    String decltype = eref.getDeclaringType().getQualifiedName();
    String name = eref.getSimpleName();
    List<CtTypeReference<?>> params = eref.getParameters();
    List<CtTypeReference<?>> atas = eref.getActualTypeArguments();
    Assert.assertEquals("boolean", type);
    Assert.assertEquals("Object", decltype);
    Assert.assertEquals("equals", name);
    Assert.assertEquals(1, params.size());
    Assert.assertEquals(0, atas.size());
}
Also used : CtTypeReference(spoon.reflect.reference.CtTypeReference) ExecutableFactory(spoon.reflect.factory.ExecutableFactory) Factory(spoon.reflect.factory.Factory) ModelUtils.createFactory(spoon.testing.utils.ModelUtils.createFactory) ExecutableFactory(spoon.reflect.factory.ExecutableFactory) Test(org.junit.Test)

Example 37 with Factory

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

the class FieldFactoryTest method testCreateFromSource.

@Test
public void testCreateFromSource() throws Exception {
    CtClass<?> target = build("spoon.test.testclasses", "SampleClass");
    Factory factory = build(Foo.class, Bar.class, SuperClass.class);
    final CtClass<Object> type = factory.Class().get(Foo.class);
    CtField<?> source = type.getField("i");
    FieldFactory ff = type.getFactory().Field();
    TypeFactory tf = type.getFactory().Type();
    ff.create(target, source);
    CtField<?> field = target.getField("i");
    Assert.assertEquals("i", field.getSimpleName());
    CtTypeReference<?> tref = tf.createReference("int");
    Assert.assertEquals(tref, field.getType());
    CtElement parent = field.getParent();
    Assert.assertTrue(parent instanceof CtClass<?>);
    Assert.assertEquals("SampleClass", ((CtClass<?>) parent).getSimpleName());
}
Also used : FieldFactory(spoon.reflect.factory.FieldFactory) CtElement(spoon.reflect.declaration.CtElement) FieldFactory(spoon.reflect.factory.FieldFactory) Factory(spoon.reflect.factory.Factory) TypeFactory(spoon.reflect.factory.TypeFactory) TypeFactory(spoon.reflect.factory.TypeFactory) Test(org.junit.Test)

Example 38 with Factory

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

the class ExceptionTest method testExceptionIfNotCompilable.

@Test
public void testExceptionIfNotCompilable() throws Exception {
    try {
        Launcher spoon = new Launcher();
        Factory factory = spoon.createFactory();
        spoon.createCompiler(factory, SpoonResourceHelper.resources("./src/test/resources/spoon/test/exceptions/ClassWithError.java")).build();
        fail();
    } catch (ModelBuildingException e) {
    // perfect
    }
}
Also used : Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) ModelUtils.createFactory(spoon.testing.utils.ModelUtils.createFactory) ModelBuildingException(spoon.compiler.ModelBuildingException) Test(org.junit.Test)

Example 39 with Factory

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

the class ExceptionTest method testExceptionInSnippet.

@Test
public void testExceptionInSnippet() {
    try {
        Factory factory = createFactory();
        factory.Code().createCodeSnippetStatement("" + "class X {" + "public void foo() {" + // does not compile here
        " int x=Foo;" + "}};").compile();
        fail();
    } catch (ModelBuildingException e) {
    // perfect
    }
}
Also used : Factory(spoon.reflect.factory.Factory) ModelUtils.createFactory(spoon.testing.utils.ModelUtils.createFactory) ModelBuildingException(spoon.compiler.ModelBuildingException) Test(org.junit.Test)

Example 40 with Factory

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

the class EnumsTest method testGetAllMethods.

@Test
public void testGetAllMethods() throws Exception {
    // contract: getAllMethods also returns the methods of Enum
    final Factory factory = build(Burritos.class);
    final CtType<Burritos> burritos = factory.Type().get(Burritos.class);
    CtMethod name = factory.Core().createMethod();
    // from Enum
    name.setSimpleName("name");
    name.setType(factory.Type().createReference(String.class));
    assertTrue(burritos.hasMethod(name));
    assertTrue(burritos.getAllMethods().contains(name));
}
Also used : Burritos(spoon.test.enums.testclasses.Burritos) Factory(spoon.reflect.factory.Factory) CtMethod(spoon.reflect.declaration.CtMethod) AnnotationTest(spoon.test.annotation.AnnotationTest) 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