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

fix https://github.com/SpoonLabs/coming/issues/264 #313

Merged
merged 4 commits into from
Apr 30, 2024
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
42 changes: 34 additions & 8 deletions src/main/java/gumtree/spoon/AstComparator.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import spoon.compiler.SpoonResource;
import spoon.compiler.SpoonResourceHelper;
import spoon.reflect.declaration.CtElement;
import spoon.reflect.declaration.CtPackage;
import spoon.reflect.declaration.CtType;
import spoon.reflect.factory.Factory;
import spoon.reflect.factory.FactoryImpl;
Expand Down Expand Up @@ -57,12 +58,12 @@ protected Factory createFactory() {
* compares two java files
*/
public Diff compare(File f1, File f2) throws Exception {
CtType<?> ctType1 = getCtType(f1);
CtType<?> ctType2 = getCtType(f2);
if (ctType1 == null || ctType2 == null) {
CtPackage ctPackage1 = getCtPackage(f1);
CtPackage ctPackage2 = getCtPackage(f2);
if (ctPackage1 == null || ctPackage2 == null) {
return null;
} else {
return compare(ctType1, ctType2);
return compare(ctPackage1, ctPackage2);
}
}

Expand All @@ -72,21 +73,32 @@ public Diff compare(File f1, File f2) throws Exception {
public Diff compare(File f1, File f2, DiffConfiguration configuration) throws Exception {
final SpoonGumTreeBuilder scanner = new SpoonGumTreeBuilder();
return new DiffImpl(
scanner.getTreeContext(), scanner.getTree(getCtType(f1)), scanner.getTree(getCtType(f2)), configuration);
scanner.getTreeContext(), scanner.getTree(getCtPackage(f1)), scanner.getTree(getCtPackage(f2)), configuration);
}

/**
* compares two snippets
*/
public Diff compare(String left, String right) {
return compare(getCtType(left), getCtType(right));
return compare(getCtPackage(left, getFilename(left)), getCtPackage(right, getFilename(right)));
}

private static String getFilename(String leftcontent) {
return "test"+Math.abs(leftcontent.hashCode()) + ".java";
}


public CtPackage getCtPackage(String content, String filename) {
VirtualFile resource = new VirtualFile(content, filename);
return getCtPackage(resource);
}


/**
* compares two snippets
*/
public Diff compare(String left, String right, DiffConfiguration configuration) {
return compare(getCtType(left), getCtType(right),configuration);
return compare(getCtPackage(left, getFilename(left)), getCtPackage(right, getFilename(right)),configuration);
}


Expand All @@ -95,7 +107,7 @@ public Diff compare(String left, String right, DiffConfiguration configuration)
* compares two snippets that come from the files given as argument
*/
public Diff compare(String left, String right, String filenameLeft, String filenameRight) {
return compare(getCtType(left, filenameLeft), getCtType(right, filenameRight));
return compare(getCtPackage(left, filenameLeft), getCtPackage(right, filenameRight));
}

/**
Expand All @@ -122,6 +134,10 @@ public CtType getCtType(File file) throws Exception {
SpoonResource resource = SpoonResourceHelper.createResource(file);
return getCtType(resource);
}
public CtPackage getCtPackage(File file) throws Exception {
SpoonResource resource = SpoonResourceHelper.createResource(file);
return getCtPackage(resource);
}

public CtType getCtType(SpoonResource resource) {
Factory factory = createFactory();
Expand All @@ -143,6 +159,16 @@ public CtType getCtType(SpoonResource resource) {
return factory.Type().get(type.getQualifiedName());
}

public CtPackage getCtPackage(SpoonResource resource) {
Factory factory = createFactory();
factory.getModel().setBuildModelIsFinished(false);
SpoonModelBuilder compiler = new JDTBasedSpoonCompiler(factory);
compiler.getFactory().getEnvironment().setLevel("OFF");
compiler.addInputSource(resource);
compiler.build();
return factory.Package().getRootPackage();
}

public CtType<?> getCtType(String content) {
return getCtType(content, "/test");
}
Expand Down
19 changes: 17 additions & 2 deletions src/test/java/gumtree/spoon/AstComparatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1714,8 +1714,8 @@ public void testFileName() throws Exception {
Diff result = diff.compare(left, right);

assertEquals(1, result.getRootOperations().size());
// by default, name is "test"
assertEquals("test", result.getRootOperations().get(0).getSrcNode().getPosition().getFile().getName());
// by default, name is prefixed by "test"
assertEquals("test2092383990.java", result.getRootOperations().get(0).getSrcNode().getPosition().getFile().getName());

// let's put other names
result = diff.compare(left, right, "mleft.java", "mright.java");
Expand Down Expand Up @@ -2101,4 +2101,19 @@ public void thisAndSuperShouldResultInAnASTDiff() {
// assert
assertTrue(diff.containsOperations(OperationKind.Update, "Invocation", "this", "super"));
}

@Test
public void test264() throws Exception {
File s = new File("src/test/resources/issue264/Util_before.java");
File t = new File("src/test/resources/issue264/Util_after.java");
boolean includeComments = true;
AstComparator r = new AstComparator(includeComments);

Diff diffOut = r.compare(s, t);
Assert.assertEquals(3, diffOut.getRootOperations().size());
Assert.assertEquals("Delete Literal at org.wso2.transport.http.netty.contractimpl.common.Util:147\n" +
"\tfalse\n", diffOut.getRootOperations().get(0).toString());

}

}
Loading