Skip to content

Commit

Permalink
[feat] Compare with gumtree-spoon, ignoring comments, fix #45
Browse files Browse the repository at this point in the history
  • Loading branch information
slarse committed Mar 4, 2020
1 parent 23dfa51 commit 07e1c5a
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 8 deletions.
28 changes: 20 additions & 8 deletions src/main/java/se/kth/spork/cli/Cli.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package se.kth.spork.cli;

import gumtree.spoon.AstComparator;
import gumtree.spoon.diff.Diff;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import picocli.CommandLine;
Expand Down Expand Up @@ -69,7 +71,7 @@ public Integer call() {
}

@CommandLine.Command(name = "compare", mixinStandardHelpOptions = true,
description = "Compare the ASTs of two Java files, disregarding the order of unordered elements")
description = "Compare the ASTs of two Java files, disregarding comments and the order of unordered elements")
private static class CompareCommand implements Callable<Integer> {
@CommandLine.Parameters(index = "0", paramLabel = "LEFT", description = "Path to a Java file")
File left;
Expand All @@ -79,16 +81,26 @@ private static class CompareCommand implements Callable<Integer> {

@Override
public Integer call() {
CtModule leftModule = Parser.parse(left.toPath());
CtModule rightModule = Parser.parse(right.toPath());
CtModule leftModule = Parser.parseWithoutComments(left.toPath());
CtModule rightModule = Parser.parseWithoutComments(right.toPath());

Compare.sortUnorderedElements(leftModule);
Compare.sortUnorderedElements(rightModule);

if (Compare.compare(leftModule, rightModule)) {
LOGGER.info("The ASTs are equal");
return 0;
Object leftImports = leftModule.getMetadata(Parser.IMPORT_STATEMENTS);
Object rightImports = rightModule.getMetadata(Parser.IMPORT_STATEMENTS);

Diff diff = new AstComparator().compare(leftModule, rightModule);
System.out.println(diff);

boolean importsEqual = leftImports.equals(rightImports);
if (!importsEqual) {
LOGGER.warn("Import statements differ");
LOGGER.info("Left: " + leftImports);
LOGGER.info("Right: " + rightImports);
}

LOGGER.info("The ASTs differ");
return 1;
return diff.getRootOperations().isEmpty() && importsEqual ? 0 : 1;
}
}

Expand Down
14 changes: 14 additions & 0 deletions src/main/java/se/kth/spork/spoon/Compare.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,20 @@ public static boolean compare(CtModule left, CtModule right) {
compare(left.getRootPackage().clone(), right.getRootPackage().clone());
}

/**
* Sort the unordered elements of the provided module.
*
* @param mod A Spoon unnamed module.
*/
public static void sortUnorderedElements(CtModule mod) {
sortUnorderedElements(mod.getRootPackage());
}

private static void sortUnorderedElements(CtPackage pkg) {
pkg.getTypes().forEach(Compare::sortTypeMembers);
pkg.getPackages().forEach(Compare::sortUnorderedElements);
}

/**
* Compare the left package with the right package, without regard to the order of unordered type
* members.
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/se/kth/spork/spoon/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,23 @@ public static CtModule parse(String javaFileContents) {
return parse(launcher -> launcher.addInputResource(new VirtualFile(javaFileContents)));
}

/**
* Parse a Java file to a Spoon tree. Any import statements in the file are attached to the returned module's
* metadata with the {@link Parser#IMPORT_STATEMENTS} key. The imports are sorted in ascending lexicographical
* order.
*
* Comments are ignored
*
* @param javaFile Path to a Java file.
* @return The root module of the Spoon tree.
*/
public static CtModule parseWithoutComments(Path javaFile) {
return parse(launcher -> {
launcher.getEnvironment().setCommentEnabled(false);
launcher.addInputResource(new VirtualFile(read(javaFile)));
});
}

private static CtModule parse(Consumer<Launcher> addResource) {
Launcher launcher = new Launcher();
launcher.getEnvironment().setPrettyPrinterCreator(
Expand Down

0 comments on commit 07e1c5a

Please sign in to comment.