Search in sources :

Example 1 with CompilationUnit

use of spoon.reflect.cu.CompilationUnit in project spoon by INRIA.

the class CompilationUnitFactory method getOrCreate.

/**
 * Creates or gets a compilation unit for a given file path.
 */
public CompilationUnit getOrCreate(String filePath) {
    CompilationUnit cu = cachedCompilationUnits.get(filePath);
    if (cu == null) {
        if (filePath.startsWith(JDTSnippetCompiler.SNIPPET_FILENAME_PREFIX)) {
            cu = factory.Core().createCompilationUnit();
            // put the virtual compilation unit of code snippet into cache too, so the JDTCommentBuilder can found it
            cachedCompilationUnits.put(filePath, cu);
            return cu;
        }
        cu = factory.Core().createCompilationUnit();
        cu.setFile(new File(filePath));
        cachedCompilationUnits.put(filePath, cu);
    }
    return cu;
}
Also used : CompilationUnit(spoon.reflect.cu.CompilationUnit) File(java.io.File)

Example 2 with CompilationUnit

use of spoon.reflect.cu.CompilationUnit 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 CompilationUnit

use of spoon.reflect.cu.CompilationUnit in project spoon by INRIA.

the class CompilationUnitFactory method getOrCreate.

public CompilationUnit getOrCreate(CtModule module) {
    if (module.getPosition() != null && module.getPosition().getCompilationUnit() != null) {
        return module.getPosition().getCompilationUnit();
    } else {
        File file = this.factory.getEnvironment().getOutputDestinationHandler().getOutputPath(module, null, null).toFile();
        try {
            String path = file.getCanonicalPath();
            CompilationUnit result = this.getOrCreate(path);
            result.setDeclaredModule(module);
            module.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)

Example 4 with CompilationUnit

use of spoon.reflect.cu.CompilationUnit 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 5 with CompilationUnit

use of spoon.reflect.cu.CompilationUnit in project spoon by INRIA.

the class ParentExiter method visitCtPackage.

@Override
public void visitCtPackage(CtPackage ctPackage) {
    if (child instanceof CtType) {
        if (ctPackage.getTypes().contains(child)) {
            ctPackage.removeType((CtType<?>) child);
        }
        ctPackage.addType((CtType<?>) child);
        if (child.getPosition() != null && child.getPosition().getCompilationUnit() != null) {
            CompilationUnit cu = child.getPosition().getCompilationUnit();
            List<CtType<?>> declaredTypes = new ArrayList<>(cu.getDeclaredTypes());
            declaredTypes.add((CtType<?>) child);
            cu.setDeclaredTypes(declaredTypes);
        }
        return;
    }
    super.visitCtPackage(ctPackage);
}
Also used : CompilationUnit(spoon.reflect.cu.CompilationUnit) CtType(spoon.reflect.declaration.CtType) ArrayList(java.util.ArrayList)

Aggregations

CompilationUnit (spoon.reflect.cu.CompilationUnit)32 Test (org.junit.Test)22 Launcher (spoon.Launcher)22 File (java.io.File)16 CtClass (spoon.reflect.declaration.CtClass)9 CtType (spoon.reflect.declaration.CtType)6 CtImport (spoon.reflect.declaration.CtImport)5 IOException (java.io.IOException)4 SpoonException (spoon.SpoonException)4 CtModule (spoon.reflect.declaration.CtModule)3 CtPackage (spoon.reflect.declaration.CtPackage)3 ArrayList (java.util.ArrayList)2 SourcePosition (spoon.reflect.cu.SourcePosition)2 PrintStream (java.io.PrintStream)1 Path (java.nio.file.Path)1 CompilationResult (org.eclipse.jdt.internal.compiler.CompilationResult)1 AbstractMethodDeclaration (org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration)1 AbstractVariableDeclaration (org.eclipse.jdt.internal.compiler.ast.AbstractVariableDeclaration)1 Annotation (org.eclipse.jdt.internal.compiler.ast.Annotation)1 AnnotationMethodDeclaration (org.eclipse.jdt.internal.compiler.ast.AnnotationMethodDeclaration)1