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

refactor: add support for malformed files through inheritance #3668

Merged
merged 4 commits into from
Nov 4, 2020
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
13 changes: 12 additions & 1 deletion src/main/java/spoon/Launcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,7 @@ protected JSAPResult parseArgs() {
* the factory this compiler works on
*/
public SpoonModelBuilder createCompiler(Factory factory) {
SpoonModelBuilder comp = new JDTBasedSpoonCompiler(factory);
SpoonModelBuilder comp = getCompilerInstance(factory);
Environment env = getEnvironment();
// building
comp.setBinaryOutputDirectory(jsapActualArgs.getFile("destination"));
Expand All @@ -614,6 +614,17 @@ public SpoonModelBuilder createCompiler(Factory factory) {
return comp;
}

/**
* Instantiates the compiler. This method is invoked by {@link #createCompiler(Factory)} to retrieve
* an empty compiler instance. Clients can override this method to use their custom compiler implementation.
* @param factory the factory to pass on to the compiler.
* @return a new compiler.
* @see #createCompiler(Factory)
*/
protected SpoonModelBuilder getCompilerInstance(Factory factory) {
return new JDTBasedSpoonCompiler(factory);
}

public SpoonModelBuilder createCompiler(Factory factory, List<SpoonResource> inputSources) {
SpoonModelBuilder c = createCompiler(factory);
c.addInputSources(inputSources);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
import org.apache.logging.log4j.Level;
import org.eclipse.jdt.core.compiler.CategorizedProblem;
import org.eclipse.jdt.core.compiler.IProblem;
import org.eclipse.jdt.internal.compiler.ASTVisitor;
import org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration;
import org.eclipse.jdt.internal.compiler.env.INameEnvironment;
import org.eclipse.jdt.internal.compiler.lookup.CompilationUnitScope;
import spoon.OutputType;
import spoon.SpoonException;
import spoon.compiler.Environment;
Expand Down Expand Up @@ -432,7 +434,7 @@ protected void buildModel(CompilationUnitDeclaration[] units, Factory aFactory)

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);
traverseUnitDeclaration(builder, unit);
});
//we need first imports before we can place comments. Mainly comments on imports need that
forEachCompilationUnit(unitList, SpoonProgress.Process.IMPORT, unit -> {
Expand Down Expand Up @@ -461,12 +463,24 @@ private void forEachCompilationUnit(List<CompilationUnitDeclaration> unitList, S
if (canProcessCompilationUnit(unitPath)) {
consumer.accept(unit);
}
getEnvironment().getSpoonProgress().step(process, unitPath, ++i, unitList.size());
getEnvironment().getSpoonProgress().step(process, unitPath, ++i, unitList.size());
}
}
getEnvironment().getSpoonProgress().end(process);
getEnvironment().getSpoonProgress().end(process);
}

/**
* Invokes the traversal of the given compilation unit declaration using the given builder as a visitor.
* Overriders of this method must either invoke {@link CompilationUnitDeclaration#traverse(ASTVisitor, CompilationUnitScope)})} )}
* or this method before returning.
* @param builder the builder to use to traverse the unit.
* @param unitDeclaration the unit declaration.
*/
protected void traverseUnitDeclaration(JDTTreeBuilder builder, CompilationUnitDeclaration unitDeclaration) {
unitDeclaration.traverse(builder, unitDeclaration.scope);
}


private boolean canProcessCompilationUnit(String unitPath) {
for (final CompilationUnitFilter cuf : compilationUnitFilters) {
if (cuf.exclude(unitPath)) {
Expand Down