Search in sources :

Example 21 with Launcher

use of spoon.Launcher in project spoon by INRIA.

the class FactoryTest method testFactoryOverriding.

@Test
public void testFactoryOverriding() throws Exception {
    @SuppressWarnings("serial")
    class MyCtMethod<T> extends CtMethodImpl<T> {
    }
    ;
    @SuppressWarnings("serial") final CoreFactory specialCoreFactory = new DefaultCoreFactory() {

        @Override
        public <T> CtMethod<T> createMethod() {
            MyCtMethod<T> m = new MyCtMethod<T>();
            m.setFactory(getMainFactory());
            return m;
        }
    };
    Launcher launcher = new Launcher() {

        @Override
        public Factory createFactory() {
            return new FactoryImpl(specialCoreFactory, new StandardEnvironment());
        }
    };
    CtClass<?> type = build("spoon.test.testclasses", "SampleClass", launcher.getFactory());
    CtMethod<?> m = type.getMethodsByName("method3").get(0);
    assertTrue(m instanceof MyCtMethod);
}
Also used : DefaultCoreFactory(spoon.support.DefaultCoreFactory) CtMethodImpl(spoon.support.reflect.declaration.CtMethodImpl) Launcher(spoon.Launcher) DefaultCoreFactory(spoon.support.DefaultCoreFactory) CoreFactory(spoon.reflect.factory.CoreFactory) FactoryImpl(spoon.reflect.factory.FactoryImpl) StandardEnvironment(spoon.support.StandardEnvironment) Test(org.junit.Test)

Example 22 with Launcher

use of spoon.Launcher in project spoon by INRIA.

the class TypeFactoryTest method testCreateTypeRef.

@Test
public void testCreateTypeRef() {
    Launcher launcher = new Launcher();
    CtTypeReference<Object> ctTypeReference = launcher.getFactory().Code().createCtTypeReference(short.class);
    assertEquals("short", ctTypeReference.getSimpleName());
    assertEquals("short", ctTypeReference.getQualifiedName());
    ctTypeReference = launcher.getFactory().Code().createCtTypeReference(Object.class);
    assertEquals("Object", ctTypeReference.getSimpleName());
    assertEquals("java.lang.Object", ctTypeReference.getQualifiedName());
    ctTypeReference = launcher.getFactory().Code().createCtTypeReference(null);
    assertEquals(null, ctTypeReference);
    ctTypeReference = launcher.getFactory().Code().createCtTypeReference(CtJavaDoc.CommentType.class);
    assertEquals("CommentType", ctTypeReference.getSimpleName());
    assertEquals("spoon.reflect.code.CtComment$CommentType", ctTypeReference.getQualifiedName());
}
Also used : Launcher(spoon.Launcher) Test(org.junit.Test)

Example 23 with Launcher

use of spoon.Launcher 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 24 with Launcher

use of spoon.Launcher in project spoon by INRIA.

the class ExceptionTest method testExceptionInvalidAPI.

@Test
public void testExceptionInvalidAPI() throws Exception {
    try {
        Launcher spoon = new Launcher();
        spoon.getFactory().getEnvironment().setLevel("OFF");
        SpoonModelBuilder comp = spoon.createCompiler();
        comp.setSourceClasspath("does_not_exist.jar");
        fail();
    } catch (InvalidClassPathException e) {
    }
    try {
        Launcher spoon = new Launcher();
        spoon.getFactory().getEnvironment().setLevel("OFF");
        SpoonModelBuilder comp = spoon.createCompiler();
        comp.setSourceClasspath("src");
    } catch (InvalidClassPathException e) {
        fail();
    // you're trying to give source code in the classpath, this should be accepted but causes a warn log entry
    }
}
Also used : SpoonModelBuilder(spoon.SpoonModelBuilder) InvalidClassPathException(spoon.compiler.InvalidClassPathException) Launcher(spoon.Launcher) Test(org.junit.Test)

Example 25 with Launcher

use of spoon.Launcher in project spoon by INRIA.

the class ExceptionTest method testUnionCatchExceptionInsideLambdaInNoClasspath.

@Test
public void testUnionCatchExceptionInsideLambdaInNoClasspath() {
    // contract: the model should be built when defining a union catch inside a lambda which is not known (noclasspath)
    // and the catch variable types should be the same than outside a lambda
    Launcher launcher = new Launcher();
    launcher.addInputResource("./src/test/resources/noclasspath/UnionCatch.java");
    launcher.getEnvironment().setNoClasspath(true);
    launcher.buildModel();
    List<CtCatch> catches = launcher.getFactory().getModel().getElements(new TypeFilter<>(CtCatch.class));
    assertEquals(2, catches.size());
    // inside a lambda
    CtCatchVariable variable1 = catches.get(0).getParameter();
    // outside the lambda
    CtCatchVariable variable2 = catches.get(1).getParameter();
    assertEquals(variable1.getMultiTypes(), variable2.getMultiTypes());
// for now the type of CtCatchVariable is not the same
// this should be fix in the future (see: https://github.com/INRIA/spoon/issues/1420)
// assertEquals(variable2, variable1);
}
Also used : Launcher(spoon.Launcher) CtCatch(spoon.reflect.code.CtCatch) CtCatchVariable(spoon.reflect.code.CtCatchVariable) Test(org.junit.Test)

Aggregations

Launcher (spoon.Launcher)524 Test (org.junit.Test)486 Factory (spoon.reflect.factory.Factory)163 CtClass (spoon.reflect.declaration.CtClass)111 CtMethod (spoon.reflect.declaration.CtMethod)79 File (java.io.File)75 CtType (spoon.reflect.declaration.CtType)66 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)60 CtTypeReference (spoon.reflect.reference.CtTypeReference)48 SpoonModelBuilder (spoon.SpoonModelBuilder)44 ArrayList (java.util.ArrayList)43 CtAnnotation (spoon.reflect.declaration.CtAnnotation)38 CtInvocation (spoon.reflect.code.CtInvocation)32 CtElement (spoon.reflect.declaration.CtElement)27 CtPackage (spoon.reflect.declaration.CtPackage)27 FileSystemFile (spoon.support.compiler.FileSystemFile)26 CtStatement (spoon.reflect.code.CtStatement)25 CtField (spoon.reflect.declaration.CtField)24 NamedElementFilter (spoon.reflect.visitor.filter.NamedElementFilter)24 SpoonAPI (spoon.SpoonAPI)22