Search in sources :

Example 56 with Factory

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());
}
Also used : SpoonModelBuilder(spoon.SpoonModelBuilder) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) Test(org.junit.Test)

Example 57 with Factory

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);
}
Also used : ImportScannerImpl(spoon.reflect.visitor.ImportScannerImpl) CtImport(spoon.reflect.declaration.CtImport) NotImportExecutableType(spoon.test.imports.testclasses.NotImportExecutableType) Factory(spoon.reflect.factory.Factory) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) ImportScanner(spoon.reflect.visitor.ImportScanner) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 58 with Factory

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

Example 59 with Factory

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());
}
Also used : CtStatement(spoon.reflect.code.CtStatement) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) FileSystemFile(spoon.support.compiler.FileSystemFile) InvocationSubstitutionByStatementTemplate(spoon.test.template.testclasses.InvocationSubstitutionByStatementTemplate) Test(org.junit.Test)

Example 60 with Factory

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());
}
Also used : InvocationSubstitutionByExpressionTemplate(spoon.test.template.testclasses.InvocationSubstitutionByExpressionTemplate) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) FileSystemFile(spoon.support.compiler.FileSystemFile) 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