Search in sources :

Example 1 with CtModule

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

the class ModuleFactory method getOrCreate.

public CtModule getOrCreate(String moduleName) {
    if (moduleName == null || moduleName.isEmpty()) {
        return getUnnamedModule();
    }
    CtModule ctModule = getUnnamedModule().getModule(moduleName);
    if (ctModule == null) {
        ctModule = factory.Core().createModule().setSimpleName(moduleName);
        ctModule.setRootPackage(new CtModelImpl.CtRootPackage());
        ctModule.setParent(getUnnamedModule());
    }
    return ctModule;
}
Also used : CtModule(spoon.reflect.declaration.CtModule) CtModelImpl(spoon.reflect.CtModelImpl)

Example 2 with CtModule

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

the class CompilationUnitFactory method getOrCreate.

public CompilationUnit getOrCreate(CtType type) {
    if (type == null) {
        return null;
    }
    if (type.getPosition() != null && type.getPosition().getCompilationUnit() != null) {
        return type.getPosition().getCompilationUnit();
    }
    if (type.isTopLevel()) {
        CtModule module;
        if (type.getPackage() != null && factory.getEnvironment().getComplianceLevel() > 8) {
            module = type.getPackage().getParent(CtModule.class);
        } else {
            module = null;
        }
        File file = this.factory.getEnvironment().getOutputDestinationHandler().getOutputPath(module, type.getPackage(), type).toFile();
        try {
            String path = file.getCanonicalPath();
            CompilationUnit result = this.getOrCreate(path);
            result.setDeclaredPackage(type.getPackage());
            result.addDeclaredType(type);
            type.setPosition(this.factory.createPartialSourcePosition(result));
            return result;
        } catch (IOException e) {
            throw new SpoonException("Cannot get path for file: " + file.getAbsolutePath(), e);
        }
    } else {
        return getOrCreate(type.getTopLevelType());
    }
}
Also used : CompilationUnit(spoon.reflect.cu.CompilationUnit) SpoonException(spoon.SpoonException) IOException(java.io.IOException) File(java.io.File) CtModule(spoon.reflect.declaration.CtModule)

Example 3 with CtModule

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

the class CompilationUnitFactory method getOrCreate.

public CompilationUnit getOrCreate(CtPackage ctPackage) {
    if (ctPackage.getPosition() != null && ctPackage.getPosition().getCompilationUnit() != null) {
        return ctPackage.getPosition().getCompilationUnit();
    } else {
        CtModule module;
        if (factory.getEnvironment().getComplianceLevel() > 8) {
            module = ctPackage.getParent(CtModule.class);
        } else {
            module = null;
        }
        File file = this.factory.getEnvironment().getOutputDestinationHandler().getOutputPath(module, ctPackage, null).toFile();
        try {
            String path = file.getCanonicalPath();
            CompilationUnit result = this.getOrCreate(path);
            result.setDeclaredPackage(ctPackage);
            ctPackage.setPosition(this.factory.createPartialSourcePosition(result));
            return result;
        } catch (IOException e) {
            throw new SpoonException("Cannot get path for file: " + file.getAbsolutePath(), e);
        }
    }
}
Also used : CompilationUnit(spoon.reflect.cu.CompilationUnit) SpoonException(spoon.SpoonException) IOException(java.io.IOException) File(java.io.File) CtModule(spoon.reflect.declaration.CtModule)

Example 4 with CtModule

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

the class JDTTreeBuilder method visit.

@Override
public boolean visit(ModuleDeclaration moduleDeclaration, CompilationUnitScope scope) {
    CtModule module = getHelper().createModule(moduleDeclaration);
    context.compilationUnitSpoon.setDeclaredModule(module);
    return true;
}
Also used : CtModule(spoon.reflect.declaration.CtModule)

Example 5 with CtModule

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

the class JDTTreeBuilderHelper method createModule.

/**
 * Creates an entire object CtModule from a module declaration.
 * @return a CtModule
 */
CtModule createModule(ModuleDeclaration moduleDeclaration) {
    CtModule module = jdtTreeBuilder.getFactory().Module().getOrCreate(new String(moduleDeclaration.moduleName));
    module.setIsOpenModule(moduleDeclaration.isOpen());
    jdtTreeBuilder.getContextBuilder().enter(module, moduleDeclaration);
    if (moduleDeclaration.requires != null && moduleDeclaration.requires.length > 0) {
        List<CtModuleRequirement> moduleRequirements = new ArrayList<>();
        for (RequiresStatement requiresStatement : moduleDeclaration.requires) {
            moduleRequirements.add(this.createModuleRequirement(requiresStatement));
        }
        module.setRequiredModules(moduleRequirements);
    }
    if (moduleDeclaration.exports != null && moduleDeclaration.exports.length > 0) {
        List<CtPackageExport> moduleExports = new ArrayList<>();
        for (ExportsStatement exportsStatement : moduleDeclaration.exports) {
            moduleExports.add(this.createModuleExport(exportsStatement));
        }
        module.setExportedPackages(moduleExports);
    }
    if (moduleDeclaration.opens != null && moduleDeclaration.opens.length > 0) {
        List<CtPackageExport> moduleOpens = new ArrayList<>();
        for (OpensStatement opensStatement : moduleDeclaration.opens) {
            moduleOpens.add(this.createModuleExport(opensStatement));
        }
        module.setOpenedPackages(moduleOpens);
    }
    if (moduleDeclaration.uses != null && moduleDeclaration.uses.length > 0) {
        List<CtUsedService> consumedServices = new ArrayList<>();
        for (UsesStatement consumedService : moduleDeclaration.uses) {
            consumedServices.add(this.createUsedService(consumedService));
        }
        module.setUsedServices(consumedServices);
    }
    if (moduleDeclaration.services != null && moduleDeclaration.services.length > 0) {
        List<CtProvidedService> moduleProvidedServices = new ArrayList<>();
        for (ProvidesStatement providesStatement : moduleDeclaration.services) {
            moduleProvidedServices.add(this.createModuleProvidedService(providesStatement));
        }
        module.setProvidedServices(moduleProvidedServices);
    }
    module.setPosition(this.jdtTreeBuilder.getPositionBuilder().buildPosition(moduleDeclaration.declarationSourceStart, moduleDeclaration.declarationSourceEnd));
    return module;
}
Also used : OpensStatement(org.eclipse.jdt.internal.compiler.ast.OpensStatement) CtProvidedService(spoon.reflect.declaration.CtProvidedService) RequiresStatement(org.eclipse.jdt.internal.compiler.ast.RequiresStatement) ArrayList(java.util.ArrayList) CtModuleRequirement(spoon.reflect.declaration.CtModuleRequirement) CtModule(spoon.reflect.declaration.CtModule) CtUsedService(spoon.reflect.declaration.CtUsedService) ProvidesStatement(org.eclipse.jdt.internal.compiler.ast.ProvidesStatement) CtPackageExport(spoon.reflect.declaration.CtPackageExport) UsesStatement(org.eclipse.jdt.internal.compiler.ast.UsesStatement) ExportsStatement(org.eclipse.jdt.internal.compiler.ast.ExportsStatement)

Aggregations

CtModule (spoon.reflect.declaration.CtModule)18 Test (org.junit.Test)9 Launcher (spoon.Launcher)9 File (java.io.File)5 CtModuleRequirement (spoon.reflect.declaration.CtModuleRequirement)4 CtPackage (spoon.reflect.declaration.CtPackage)4 CtProvidedService (spoon.reflect.declaration.CtProvidedService)4 CtType (spoon.reflect.declaration.CtType)4 CtUsedService (spoon.reflect.declaration.CtUsedService)4 CompilationUnit (spoon.reflect.cu.CompilationUnit)3 CtElement (spoon.reflect.declaration.CtElement)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 SpoonException (spoon.SpoonException)2 CtPackageExport (spoon.reflect.declaration.CtPackageExport)2 CtModuleReference (spoon.reflect.reference.CtModuleReference)2 OutputDestinationHandler (spoon.support.OutputDestinationHandler)2 Iterator (java.util.Iterator)1 ExportsStatement (org.eclipse.jdt.internal.compiler.ast.ExportsStatement)1 OpensStatement (org.eclipse.jdt.internal.compiler.ast.OpensStatement)1