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