use of spoon.reflect.factory.Factory in project spoon by INRIA.
the class ImportTest method testMissingImport.
@Test
public void testMissingImport() throws Exception {
Launcher spoon = new Launcher();
spoon.setArgs(new String[] { "--output-type", "nooutput" });
Factory factory = spoon.createFactory();
factory.getEnvironment().setNoClasspath(true);
factory.getEnvironment().setLevel("OFF");
SpoonModelBuilder compiler = spoon.createCompiler(factory, SpoonResourceHelper.resources("./src/test/resources/import-resources/fr/inria/MissingImport.java"));
compiler.build();
CtTypeReference<?> type = factory.Class().getAll().get(0).getFields().get(0).getType();
assertEquals("Abcd", type.getSimpleName());
assertEquals("fr.inria.internal", type.getPackage().getSimpleName());
}
use of spoon.reflect.factory.Factory in project spoon by INRIA.
the class ImportTest method testNotImportExecutableType.
@Test
public void testNotImportExecutableType() throws Exception {
final Factory factory = getFactory("./src/test/java/spoon/test/imports/testclasses/internal3/Foo.java", "./src/test/java/spoon/test/imports/testclasses/internal3/Bar.java", "./src/test/java/spoon/test/imports/testclasses/NotImportExecutableType.java");
ImportScanner importContext = new ImportScannerImpl();
importContext.computeImports(factory.Class().get(NotImportExecutableType.class));
Collection<CtImport> imports = importContext.getAllImports();
// java.lang.Object is considered as imported but it will never be output
assertEquals(3, imports.size());
Set<String> expectedImports = new HashSet<>(Arrays.asList("spoon.test.imports.testclasses.internal3.Foo", "java.io.File", "java.lang.Object"));
Set<String> actualImports = imports.stream().map(CtImport::getReference).map(CtReference::toString).collect(Collectors.toSet());
assertEquals(expectedImports, actualImports);
}
use of spoon.reflect.factory.Factory in project spoon by INRIA.
the class FieldAccessTest method testFieldAccessInLambda.
@Test
public void testFieldAccessInLambda() throws Exception {
Factory build = null;
try {
build = build(MyClass.class);
} catch (NullPointerException ignore) {
fail();
}
final CtFieldAccess logFieldAccess = Query.getElements(build, new TypeFilter<>(CtFieldAccess.class)).get(0);
assertEquals(Logger.class, logFieldAccess.getType().getActualClass());
assertEquals("LOG", logFieldAccess.getVariable().getSimpleName());
assertEquals(MyClass.class, logFieldAccess.getVariable().getDeclaringType().getActualClass());
String expectedLambda = "() -> {" + System.lineSeparator() + " spoon.test.fieldaccesses.MyClass.LOG.info(\"bla\");" + System.lineSeparator() + "}";
assertEquals(expectedLambda, logFieldAccess.getParent(CtLambda.class).toString());
}
use of spoon.reflect.factory.Factory in project spoon by INRIA.
the class TemplateInvocationSubstitutionTest method testInvocationSubstitutionByStatement.
@Test
public void testInvocationSubstitutionByStatement() throws Exception {
// contract: the template engine supports substitution of any method invocation
Launcher spoon = new Launcher();
spoon.addTemplateResource(new FileSystemFile("./src/test/java/spoon/test/template/testclasses/InvocationSubstitutionByStatementTemplate.java"));
spoon.buildModel();
Factory factory = spoon.getFactory();
CtBlock<?> templateArg = factory.Class().get(InvocationSubstitutionByStatementTemplate.class).getMethod("sample").getBody();
CtClass<?> resultKlass = factory.Class().create("Result");
CtStatement result = new InvocationSubstitutionByStatementTemplate(templateArg).apply(resultKlass);
assertEquals("throw new java.lang.RuntimeException(\"Failed\")", result.toString());
}
use of spoon.reflect.factory.Factory in project spoon by INRIA.
the class TemplateInvocationSubstitutionTest method testInvocationSubstitutionByExpression.
@Test
public void testInvocationSubstitutionByExpression() throws Exception {
// contract: the template engine supports substitution of any method invocation
Launcher spoon = new Launcher();
spoon.addTemplateResource(new FileSystemFile("./src/test/java/spoon/test/template/testclasses/InvocationSubstitutionByExpressionTemplate.java"));
spoon.buildModel();
Factory factory = spoon.getFactory();
CtClass<?> resultKlass = factory.Class().create("Result");
CtBlock<?> result = new InvocationSubstitutionByExpressionTemplate(factory.createLiteral("abc")).apply(resultKlass);
assertEquals("java.lang.System.out.println(\"abc\".substring(1))", result.getStatement(0).toString());
assertEquals("java.lang.System.out.println(\"abc\".substring(1))", result.getStatement(1).toString());
}
Aggregations