diff --git a/src/main/java/spoon/reflect/visitor/DefaultJavaPrettyPrinter.java b/src/main/java/spoon/reflect/visitor/DefaultJavaPrettyPrinter.java index b051427679e..76c9a9d3324 100644 --- a/src/main/java/spoon/reflect/visitor/DefaultJavaPrettyPrinter.java +++ b/src/main/java/spoon/reflect/visitor/DefaultJavaPrettyPrinter.java @@ -1945,9 +1945,7 @@ public void visitCtUnboundVariableReference(CtUnboundVariableReference re @Override public String printCompilationUnit(CtCompilationUnit compilationUnit) { - reset(); - applyPreProcessors(compilationUnit); - scanCompilationUnit(compilationUnit); + calculate(compilationUnit, compilationUnit.getDeclaredTypes()); return getResult(); } @@ -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); @@ -1992,30 +1986,32 @@ public void reset() { @Override public void calculate(CtCompilationUnit sourceCompilationUnit, List> 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> types) { List> cuTypes = compilationUnit.getDeclaredTypeReferences(); diff --git a/src/main/java/spoon/support/sniper/SniperJavaPrettyPrinter.java b/src/main/java/spoon/support/sniper/SniperJavaPrettyPrinter.java index 4a59051b983..f12e536df60 100644 --- a/src/main/java/spoon/support/sniper/SniperJavaPrettyPrinter.java +++ b/src/main/java/spoon/support/sniper/SniperJavaPrettyPrinter.java @@ -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; @@ -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; @@ -102,7 +104,7 @@ private TokenWriter createTokenWriterListener(TokenWriter tokenWriter) { } @Override - protected void scanCompilationUnit(CtCompilationUnit compilationUnit) { + public void calculate(CtCompilationUnit compilationUnit, List> types) { //use line separator of origin source file setLineSeparator(detectLineSeparator(compilationUnit.getOriginalSourceCode())); runInContext(new SourceFragmentContextList(mutableTokenWriter, @@ -110,7 +112,7 @@ protected void scanCompilationUnit(CtCompilationUnit compilationUnit) { Collections.singletonList(compilationUnit.getOriginalSourceFragment()), new ChangeResolver(getChangeCollector(), compilationUnit)), () -> { - super.scanCompilationUnit(compilationUnit); + super.calculate(sourceCompilationUnit, types);; }); } @@ -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 @@ -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) {