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: throw informative exception when sniper printing cannot be done #3764

Merged
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
27 changes: 27 additions & 0 deletions src/main/java/spoon/support/sniper/SniperJavaPrettyPrinter.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@

import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Deque;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import org.apache.commons.lang3.tuple.Pair;
import spoon.OutputType;
Expand Down Expand Up @@ -130,6 +134,8 @@ private static CtCompilationUnit getUnambiguousCompilationUnit(CtType<?>[] type)

@Override
public void calculate(CtCompilationUnit compilationUnit, List<CtType<?>> types) {
checkGivenTypesMatchDeclaredTypes(compilationUnit, types);

sourceCompilationUnit = compilationUnit;

//use line separator of origin source file
Expand All @@ -149,6 +155,27 @@ public void calculate(CtCompilationUnit compilationUnit, List<CtType<?>> types)
});
}

/** Throws an {@link IllegalArgumentException} if the given types do not exactly match the types of the CU. */
private static void checkGivenTypesMatchDeclaredTypes(CtCompilationUnit cu, List<CtType<?>> types) {
Set<CtType<?>> givenTypes = toIdentityHashSet(types);
Set<CtType<?>> declaredTypes = toIdentityHashSet(cu.getDeclaredTypes());
if (!givenTypes.equals(declaredTypes)) {
throw new IllegalArgumentException(
"Can only sniper print exactly all declared types of the compilation unit. Given types: "
+ toNameList(givenTypes) + ". Declared types: " + toNameList(declaredTypes));
}
}

private static List<String> toNameList(Collection<CtType<?>> types) {
return types.stream().map(CtType::getQualifiedName).collect(Collectors.toList());
}

private static <T> Set<T> toIdentityHashSet(Collection<T> items) {
Set<T> idHashSet = Collections.newSetFromMap(new IdentityHashMap<>());
idHashSet.addAll(items);
return idHashSet;
}

private static final String CR = "\r";
private static final String CRLF = "\r\n";
private static final String LF = "\n";
Expand Down
19 changes: 19 additions & 0 deletions src/test/java/spoon/test/prettyprinter/TestSniperPrinter.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import spoon.reflect.declaration.CtField;
import spoon.reflect.declaration.CtImport;
import spoon.reflect.declaration.CtMethod;
import spoon.reflect.declaration.CtModifiable;
import spoon.reflect.declaration.CtPackage;
import spoon.reflect.declaration.CtType;
import spoon.reflect.declaration.ModifierKind;
Expand Down Expand Up @@ -67,6 +68,7 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static spoon.test.SpoonTestHelpers.assumeNotWindows;
Expand Down Expand Up @@ -630,6 +632,23 @@ public void testPrintTypeWithMethodImportBelowMethodDefinition() {
assertThat(output, containsString("import static methodimport.ClassWithStaticMethod.staticMethod;"));
}

@Test
public void testThrowsWhenTryingToPrintSubsetOfCompilationUnitTypes() {
// contract: Printing a subset of a compilation unit's types is a hassle to implement at the time of writing
// this, as a) DJPP will replace the compilation unit with a clone, and b) it makes it more difficult to
// match source code fragments. For now, we're lazy and simply don't allow it.

Launcher launcher = createLauncherWithSniperPrinter();
launcher.addInputResource(getResourcePath("MultipleTopLevelTypes"));

CtModel model = launcher.buildModel();
CtType<?> primaryType = model.getAllTypes().stream().filter(CtModifiable::isPublic).findFirst().get();
CtCompilationUnit cu = primaryType.getFactory().CompilationUnit().getOrCreate(primaryType);
SniperJavaPrettyPrinter sniper = (SniperJavaPrettyPrinter) launcher.getEnvironment().createPrettyPrinter();

assertThrows(IllegalArgumentException.class, () -> sniper.calculate(cu, Collections.singletonList(primaryType)));
}

/**
* 1) Runs spoon using sniper mode,
* 2) runs `typeChanger` to modify the code,
Expand Down
12 changes: 12 additions & 0 deletions src/test/resources/MultipleTopLevelTypes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
public class MultipleTopLevelTypes {
private int x;
}

class OtherTopLevelType {
private long z;
}

interface TopLevelInterface {
int getValue();
}