Search in sources :

Example 6 with CtPackageExport

use of spoon.reflect.declaration.CtPackageExport in project spoon by INRIA.

the class CtModuleImpl method setExportedPackages.

@Override
public <T extends CtModule> T setExportedPackages(List<CtPackageExport> exportedPackages) {
    if (exportedPackages == null || exportedPackages.isEmpty()) {
        return (T) this;
    }
    List<CtPackageExport> oldExportedPackages = getExportedPackages();
    getFactory().getEnvironment().getModelChangeListener().onListDelete(this, CtRole.EXPORTED_PACKAGE, this.moduleDirectives, new ArrayList<>(oldExportedPackages));
    this.moduleDirectives.removeAll(oldExportedPackages);
    for (CtPackageExport exportedPackage : exportedPackages) {
        exportedPackage.setOpenedPackage(false);
        this.addModuleDirective(exportedPackage);
    }
    return (T) this;
}
Also used : CtPackageExport(spoon.reflect.declaration.CtPackageExport)

Example 7 with CtPackageExport

use of spoon.reflect.declaration.CtPackageExport in project spoon by INRIA.

the class TestModule method testCompleteModuleInfoContentNoClasspath.

@Test
public void testCompleteModuleInfoContentNoClasspath() {
    // contract: all information of the module-info should be available through the model
    final Launcher launcher = new Launcher();
    launcher.addInputResource("./src/test/resources/spoon/test/module/simple_module/module-info.java");
    launcher.getEnvironment().setNoClasspath(true);
    launcher.getEnvironment().setComplianceLevel(9);
    launcher.buildModel();
    assertEquals(2, launcher.getModel().getAllModules().size());
    CtModule unnamedModule = launcher.getFactory().Module().getOrCreate(CtModule.TOP_LEVEL_MODULE_NAME);
    assertSame(unnamedModule, launcher.getModel().getUnnamedModule());
    CtModule moduleGreetings = launcher.getFactory().Module().getOrCreate("simple_module");
    assertEquals("simple_module", moduleGreetings.getSimpleName());
    assertEquals(7, moduleGreetings.getModuleDirectives().size());
    List<CtModuleRequirement> requiredModules = moduleGreetings.getRequiredModules();
    assertEquals(1, requiredModules.size());
    CtModuleRequirement moduleRequirement = requiredModules.get(0);
    assertEquals("java.logging", moduleRequirement.getModuleReference().getSimpleName());
    assertTrue(moduleRequirement.getRequiresModifiers().contains(CtModuleRequirement.RequiresModifier.TRANSITIVE));
    List<CtPackageExport> moduleExports = moduleGreetings.getExportedPackages();
    assertEquals(1, moduleExports.size());
    assertEquals("com.greetings.pkg", moduleExports.get(0).getPackageReference().getQualifiedName());
    assertEquals(2, moduleExports.get(0).getTargetExport().size());
    for (CtModuleReference target : moduleExports.get(0).getTargetExport()) {
        if (!target.getSimpleName().equals("com.other.module") && !target.getSimpleName().equals("com.second.module")) {
            fail();
        }
    }
    List<CtPackageExport> moduleOpened = moduleGreetings.getOpenedPackages();
    assertEquals(2, moduleOpened.size());
    CtPackageExport openedFirst = moduleOpened.get(0);
    CtPackageExport openedSecond = moduleOpened.get(1);
    assertEquals("com.greetings.otherpkg", openedFirst.getPackageReference().getSimpleName());
    assertTrue(openedFirst.getTargetExport().isEmpty());
    assertEquals("com.greetings.openpkg", openedSecond.getPackageReference().getSimpleName());
    assertEquals(1, openedSecond.getTargetExport().size());
    assertEquals("com.third.module", openedSecond.getTargetExport().iterator().next().getSimpleName());
    List<CtUsedService> consumedService = moduleGreetings.getUsedServices();
    assertEquals(1, consumedService.size());
    assertEquals("com.greetings.pkg.ConsumedService", consumedService.get(0).getServiceType().getQualifiedName());
    List<CtProvidedService> providedServices = moduleGreetings.getProvidedServices();
    assertEquals(2, providedServices.size());
    CtProvidedService providedService1 = providedServices.get(0);
    CtProvidedService providedService2 = providedServices.get(1);
    assertEquals("com.greetings.pkg.ConsumedService", providedService1.getServiceType().getQualifiedName());
    assertEquals(2, providedService1.getImplementationTypes().size());
    assertEquals("com.greetings.pkg.ProvidedClass1", providedService1.getImplementationTypes().get(0).getQualifiedName());
    assertEquals("com.greetings.otherpkg.ProvidedClass2", providedService1.getImplementationTypes().get(1).getQualifiedName());
    assertEquals("java.logging.Service", providedService2.getServiceType().getQualifiedName());
    assertEquals(1, providedService2.getImplementationTypes().size());
    assertEquals("com.greetings.logging.Logger", providedService2.getImplementationTypes().get(0).getQualifiedName());
}
Also used : CtPackageExport(spoon.reflect.declaration.CtPackageExport) CtProvidedService(spoon.reflect.declaration.CtProvidedService) CtModuleReference(spoon.reflect.reference.CtModuleReference) Launcher(spoon.Launcher) CtModuleRequirement(spoon.reflect.declaration.CtModuleRequirement) CtModule(spoon.reflect.declaration.CtModule) CtUsedService(spoon.reflect.declaration.CtUsedService) Test(org.junit.Test)

Aggregations

CtPackageExport (spoon.reflect.declaration.CtPackageExport)7 ArrayList (java.util.ArrayList)3 CtModuleReference (spoon.reflect.reference.CtModuleReference)3 ModuleReference (org.eclipse.jdt.internal.compiler.ast.ModuleReference)2 CtModule (spoon.reflect.declaration.CtModule)2 CtModuleRequirement (spoon.reflect.declaration.CtModuleRequirement)2 CtProvidedService (spoon.reflect.declaration.CtProvidedService)2 CtUsedService (spoon.reflect.declaration.CtUsedService)2 CtPackageReference (spoon.reflect.reference.CtPackageReference)2 ExportsStatement (org.eclipse.jdt.internal.compiler.ast.ExportsStatement)1 OpensStatement (org.eclipse.jdt.internal.compiler.ast.OpensStatement)1 ProvidesStatement (org.eclipse.jdt.internal.compiler.ast.ProvidesStatement)1 RequiresStatement (org.eclipse.jdt.internal.compiler.ast.RequiresStatement)1 UsesStatement (org.eclipse.jdt.internal.compiler.ast.UsesStatement)1 Test (org.junit.Test)1 Launcher (spoon.Launcher)1 CtPackageExportImpl (spoon.support.reflect.declaration.CtPackageExportImpl)1