Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: JDT spoon compiler builds imports and then comments #2807

Merged
merged 3 commits into from
Dec 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/main/java/spoon/support/compiler/SpoonProgress.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ enum Process {
COMMENT,
MODEL,
IMPORT,
COMMENT_LINKING,
PROCESS,
PRINT
}
Expand Down
63 changes: 33 additions & 30 deletions src/main/java/spoon/support/compiler/jdt/JDTBasedSpoonCompiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.function.Consumer;

/**
* Main class of Spoon to build the model.
Expand Down Expand Up @@ -424,53 +425,55 @@ protected List<CompilationUnitDeclaration> sortCompilationUnits(CompilationUnitD
}

protected void buildModel(CompilationUnitDeclaration[] units) {
if (getEnvironment().getSpoonProgress() != null) {
getEnvironment().getSpoonProgress().start(SpoonProgress.Process.MODEL);
}
JDTTreeBuilder builder = new JDTTreeBuilder(factory);
List<CompilationUnitDeclaration> unitList = this.sortCompilationUnits(units);

forEachCompilationUnit(unitList, SpoonProgress.Process.MODEL, unit -> {
// we need first to go through the whole model before getting the right reference for imports
unit.traverse(builder, unit.scope);
});
if (getFactory().getEnvironment().isAutoImports()) {
//we need first imports before we can place comments. Mainly comments on imports need that
forEachCompilationUnit(unitList, SpoonProgress.Process.IMPORT, unit -> {
new JDTImportBuilder(unit, factory).build();
});
}
if (getFactory().getEnvironment().isCommentsEnabled()) {
forEachCompilationUnit(unitList, SpoonProgress.Process.COMMENT_LINKING, unit -> {
new JDTCommentBuilder(unit, factory).build();
});
}
}

private void forEachCompilationUnit(List<CompilationUnitDeclaration> unitList, SpoonProgress.Process process, Consumer<CompilationUnitDeclaration> consumer) {
if (getEnvironment().getSpoonProgress() != null) {
getEnvironment().getSpoonProgress().start(process);
}
int i = 0;
unitLoop:
for (CompilationUnitDeclaration unit : unitList) {
if (unit.isModuleInfo() || !unit.isEmpty()) {
final String unitPath = new String(unit.getFileName());
for (final CompilationUnitFilter cuf : compilationUnitFilters) {
if (cuf.exclude(unitPath)) {
// do not traverse this unit
continue unitLoop;
}
}
unit.traverse(builder, unit.scope);
if (getFactory().getEnvironment().isCommentsEnabled()) {
new JDTCommentBuilder(unit, factory).build();
if (canProcessCompilationUnit(unitPath)) {
consumer.accept(unit);
}
if (getEnvironment().getSpoonProgress() != null) {
getEnvironment().getSpoonProgress().step(SpoonProgress.Process.MODEL, new String(unit.getFileName()), ++i, unitList.size());
getEnvironment().getSpoonProgress().step(process, unitPath, ++i, unitList.size());
}
}
}
if (getEnvironment().getSpoonProgress() != null) {
getEnvironment().getSpoonProgress().end(SpoonProgress.Process.MODEL);
getEnvironment().getSpoonProgress().end(process);
}
}

// we need first to go through the whole model before getting the right reference for imports
if (getFactory().getEnvironment().isAutoImports()) {
if (getEnvironment().getSpoonProgress() != null) {
getEnvironment().getSpoonProgress().start(SpoonProgress.Process.IMPORT);
}

i = 0;
for (CompilationUnitDeclaration unit : units) {
new JDTImportBuilder(unit, factory).build();
if (getEnvironment().getSpoonProgress() != null) {
getEnvironment().getSpoonProgress().step(SpoonProgress.Process.IMPORT, new String(unit.getFileName()), ++i, units.length);
}
}
if (getEnvironment().getSpoonProgress() != null) {
getEnvironment().getSpoonProgress().end(SpoonProgress.Process.IMPORT);
private boolean canProcessCompilationUnit(String unitPath) {
for (final CompilationUnitFilter cuf : compilationUnitFilters) {
if (cuf.exclude(unitPath)) {
// do not traverse this unit
return false;
}
}
return true;
}

protected void generateProcessedSourceFilesUsingTypes(Filter<CtType<?>> typeFilter) {
Expand Down