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;
}
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());
}
}
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);
}
}
}
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);
}
}
}
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);
}
Aggregations