Skip to content

Commit

Permalink
ref(DefaultJavaPrettyPrinter): calculate is the central point for pri…
Browse files Browse the repository at this point in the history
…nting compilation units (#3233)
  • Loading branch information
monperrus committed Feb 10, 2020
1 parent 45d4cd4 commit c0caaaf
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 33 deletions.
54 changes: 25 additions & 29 deletions src/main/java/spoon/reflect/visitor/DefaultJavaPrettyPrinter.java
Expand Up @@ -1945,9 +1945,7 @@ public <T> void visitCtUnboundVariableReference(CtUnboundVariableReference<T> re

@Override
public String printCompilationUnit(CtCompilationUnit compilationUnit) {
reset();
applyPreProcessors(compilationUnit);
scanCompilationUnit(compilationUnit);
calculate(compilationUnit, compilationUnit.getDeclaredTypes());
return getResult();
}

Expand All @@ -1958,10 +1956,6 @@ public void applyPreProcessors(CtElement el) {
}
}

protected void scanCompilationUnit(CtCompilationUnit compilationUnit) {
scan(compilationUnit);
}

@Override
public String printPackageInfo(CtPackage pack) {
CtCompilationUnit cu = pack.getFactory().CompilationUnit().getOrCreate(pack);
Expand Down Expand Up @@ -1992,30 +1986,32 @@ public void reset() {

@Override
public void calculate(CtCompilationUnit sourceCompilationUnit, List<CtType<?>> types) {
reset();
if (types.isEmpty()) {
return;
}
CtType<?> type = types.get(0);
// reset the importsContext to avoid errors with multiple CU
if (sourceCompilationUnit == null) {
sourceCompilationUnit = type.getFactory().CompilationUnit().getOrCreate(type);
}
if (type.getPackage() == null) {
type.setParent(type.getFactory().Package().getRootPackage());
}
if (!hasSameTypes(sourceCompilationUnit, types)) {
//the provided CU has different types, then these which has to be printed
//clone CU and assign it expected types
sourceCompilationUnit = sourceCompilationUnit.clone();
sourceCompilationUnit.setDeclaredTypes(types);
}
CtPackageReference packRef = type.getPackage().getReference();
if (!packRef.equals(sourceCompilationUnit.getPackageDeclaration().getReference())) {
//the type was cloned and moved to different package. Adapt package reference of compilation unit too
sourceCompilationUnit.getPackageDeclaration().setReference(packRef);
}
printCompilationUnit(sourceCompilationUnit);
// is package-info.java, we cannot call types.get(0) in the then branch
} else {
CtType<?> type = types.get(0);
if (sourceCompilationUnit == null) {
sourceCompilationUnit = type.getFactory().CompilationUnit().getOrCreate(type);
}
if (type.getPackage() == null) {
type.setParent(type.getFactory().Package().getRootPackage());
}
CtPackageReference packRef = type.getPackage().getReference();
if (!packRef.equals(sourceCompilationUnit.getPackageDeclaration().getReference())) {
//the type was cloned and moved to different package. Adapt package reference of compilation unit too
sourceCompilationUnit.getPackageDeclaration().setReference(packRef);
}
if (!hasSameTypes(sourceCompilationUnit, types)) {
//the provided CU has different types, then these which has to be printed
//clone CU and assign it expected types
sourceCompilationUnit = sourceCompilationUnit.clone();
sourceCompilationUnit.setDeclaredTypes(types);
}
}
applyPreProcessors(sourceCompilationUnit);
scan(sourceCompilationUnit);
}

private boolean hasSameTypes(CtCompilationUnit compilationUnit, List<CtType<?>> types) {
List<CtTypeReference<?>> cuTypes = compilationUnit.getDeclaredTypeReferences();
Expand Down
10 changes: 6 additions & 4 deletions src/main/java/spoon/support/sniper/SniperJavaPrettyPrinter.java
Expand Up @@ -8,6 +8,7 @@
import java.util.ArrayDeque;
import java.util.Collections;
import java.util.Deque;
import java.util.List;

import spoon.OutputType;
import spoon.SpoonException;
Expand All @@ -17,6 +18,7 @@
import spoon.reflect.cu.position.NoSourcePosition;
import spoon.reflect.declaration.CtCompilationUnit;
import spoon.reflect.declaration.CtElement;
import spoon.reflect.declaration.CtType;
import spoon.reflect.path.CtRole;
import spoon.reflect.visitor.DefaultJavaPrettyPrinter;
import spoon.reflect.visitor.PrettyPrinter;
Expand Down Expand Up @@ -102,15 +104,15 @@ private TokenWriter createTokenWriterListener(TokenWriter tokenWriter) {
}

@Override
protected void scanCompilationUnit(CtCompilationUnit compilationUnit) {
public void calculate(CtCompilationUnit compilationUnit, List<CtType<?>> types) {
//use line separator of origin source file
setLineSeparator(detectLineSeparator(compilationUnit.getOriginalSourceCode()));
runInContext(new SourceFragmentContextList(mutableTokenWriter,
compilationUnit,
Collections.singletonList(compilationUnit.getOriginalSourceFragment()),
new ChangeResolver(getChangeCollector(), compilationUnit)),
() -> {
super.scanCompilationUnit(compilationUnit);
super.calculate(sourceCompilationUnit, types);;
});
}

Expand Down Expand Up @@ -311,7 +313,7 @@ private SourceFragmentPrinter detectCurrentContext(PrinterEvent event) {
}

/**
* scans the `element` which exist on `role` in it's parent
* scans the `element` which exist on `role` in its parent
* @param role {@link CtRole} of `element` in scope of it's parent
* @param element a scanned element
* @param fragment origin source fragment of element
Expand Down Expand Up @@ -444,7 +446,7 @@ private void runInContext(SourceFragmentPrinter context, Runnable code) {
try {
code.run();
} finally {
// we make sure to remve all contexts that have been pushed so far
// we make sure to remove all contexts that have been pushed so far
// and we also remove parameter `context`
// so that we can leave the sourceFragmentContextStack clean
while (true) {
Expand Down

0 comments on commit c0caaaf

Please sign in to comment.