Skip to content

feat: record positions of each node in Tree #325

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

Merged
merged 7 commits into from
Sep 22, 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
444 changes: 244 additions & 200 deletions src/main/java/gumtree/spoon/builder/NodeCreator.java

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions src/main/java/gumtree/spoon/builder/SpoonGumTreeBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.github.gumtreediff.tree.TreeContext;
import com.github.gumtreediff.tree.Type;

import spoon.reflect.cu.position.NoSourcePosition;
import spoon.reflect.declaration.CtElement;

/**
Expand All @@ -40,11 +41,22 @@ public class SpoonGumTreeBuilder {
public Tree getTree(CtElement element) {
Type type = type("root");
final Tree root = treeContext.createTree(type, "");
setPosition(root, element);
new TreeScanner(treeContext, root).scan(element);
return root;
}

public TreeContext getTreeContext() {
return treeContext;
}

public static void setPosition(Tree node, CtElement element) {
if (element.getPosition() instanceof NoSourcePosition) {
return;
}
int startPosition = element.getPosition().getSourceStart();
int endPosition = element.getPosition().getSourceEnd();
node.setPos(startPosition);
node.setLength(endPosition - startPosition);
}
}
1 change: 1 addition & 0 deletions src/main/java/gumtree/spoon/builder/TreeScanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ private Tree createNode(String nodeTypeName, CtElement element, String label) {
Tree newNode = createNode(nodeTypeName, label);
newNode.setMetadata(SpoonGumTreeBuilder.SPOON_OBJECT, element);
element.putMetadata(SpoonGumTreeBuilder.GUMTREE_NODE, newNode);
SpoonGumTreeBuilder.setPosition(newNode, element);
return newNode;
}

Expand Down
134 changes: 134 additions & 0 deletions src/test/java/gumtree/spoon/SourcePositionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
package gumtree.spoon;

import com.github.gumtreediff.tree.Tree;
import gumtree.spoon.builder.SpoonGumTreeBuilder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import spoon.Launcher;
import spoon.reflect.code.CtTypeAccess;
import spoon.reflect.declaration.CtElement;
import spoon.reflect.declaration.CtPackage;

import java.io.File;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.Queue;
import java.util.stream.Collectors;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertTrue;

@RunWith(Parameterized.class)
public class SourcePositionTest {

private File javaSourceFile;
private String fileName;

public SourcePositionTest(File javaSourceFile, String fileName) {
this.javaSourceFile = javaSourceFile;
this.fileName = fileName;
}

@Parameterized.Parameters(name = "{1}")
public static Collection<Object[]> data() {
File directory = new File("src/test/resources/source-positions");
return Arrays.stream(Objects.requireNonNull(directory.listFiles())).map(file -> new Object[]{file, file.getName()}).collect(Collectors.toUnmodifiableList());
}

private static Tree getRootNode(File javaSourceFile) {
Launcher launcher = new Launcher();
launcher.addInputResource(javaSourceFile.getAbsolutePath());
launcher.buildModel();
CtPackage rootPackage = launcher.getModel().getRootPackage();
return new SpoonGumTreeBuilder().getTree(rootPackage);
}

private static boolean ignoreNodes(Tree node) {
// they don't have a position in the Spoon model
CtElement element = (CtElement) node.getMetadata(SpoonGumTreeBuilder.SPOON_OBJECT);
if (element instanceof CtPackage) {
return true;
}
if (element instanceof CtTypeAccess) {
return true;
}
return false;
}

private static boolean siblingsDoNotContainEachOther(List<Tree> siblings) {
for (int i = 0; i < siblings.size(); i++) {
Tree sibling1 = siblings.get(i);
for (int j = i + 1; j < siblings.size(); j++) {
Tree sibling2 = siblings.get(j);
if (ignoreNodes(sibling1) || ignoreNodes(sibling2)) {
continue;
}
int startPosition1 = sibling1.getPos();
int endPosition1 = startPosition1 + sibling1.getLength();
int startPosition2 = sibling2.getPos();
int endPosition2 = startPosition2 + sibling2.getLength();
if (startPosition2 <= startPosition1 && endPosition2 >= endPosition1) {
throw new AssertionError(sibling1 + " and " + sibling2 + " are overlapping");
}
if (startPosition1 <= startPosition2 && endPosition2 <= endPosition1) {
throw new AssertionError(sibling1 + " and " + sibling2 + " are overlapping");
}
if (startPosition1 < startPosition2 && endPosition1 > startPosition2) {
throw new AssertionError(sibling1 + " and " + sibling2 + " are overlapping");
}
if (startPosition2 < startPosition1 && endPosition2 > startPosition1) {
throw new AssertionError(sibling1 + " and " + sibling2 + " are overlapping");
}
}
}
return true;
}

@Test
public void childNodeMustBeEnclosedInParentNode() {
// Arrange
Tree rootNode = getRootNode(javaSourceFile);
// Assert
for (Tree child : rootNode.getDescendants()) {
Tree parent = child.getParent();
if (ignoreNodes(child) || ignoreNodes(parent)) {
continue;
}
int startPosition = child.getPos();
int endPosition = startPosition + child.getLength();
assertThat(child + " must be enclosed in " + rootNode,
startPosition >= parent.getPos() && endPosition <= parent.getPos() + parent.getLength());
}
}

@Test
public void allNodesShouldHaveAtLeastZeroLength() {
// Arrange
Tree rootNode = getRootNode(javaSourceFile);
// Assert
for (Tree node : rootNode.getDescendants()) {
if (ignoreNodes(node)) {
continue;
}
assertTrue(node + " has negative length", node.getLength() >= 0);
}
}

@Test
public void siblingNodeShouldNotContainEachOther() {
// Arrange
Tree rootNode = getRootNode(javaSourceFile);
// Assert
Queue<Tree> roots = new ArrayDeque<>();
roots.add(rootNode);
while (!roots.isEmpty()) {
List<Tree> siblings = roots.poll().getChildren();
assertTrue(siblingsDoNotContainEachOther(siblings));
roots.addAll(siblings);
}
}
}
1 change: 0 additions & 1 deletion src/test/java/gumtree/spoon/TreeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.io.File;
import java.util.List;
Expand Down
8 changes: 8 additions & 0 deletions src/test/resources/source-positions/Annotations.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import org.junit.Ignore;

import java.beans.JavaBean;

@Ignore("This is a test class")
@JavaBean(description = "This is a test class", defaultProperty = "name")
public class Annotations {
}
3 changes: 3 additions & 0 deletions src/test/resources/source-positions/Field.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Field {
private int x = 0;
}
5 changes: 5 additions & 0 deletions src/test/resources/source-positions/Return.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Return {
int foo() {
return 42;
}
}
7 changes: 7 additions & 0 deletions src/test/resources/source-positions/SuperInterface.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package x;

public class SuperInterface implements Runnable {
public void run() {
System.out.println("Hello, World!");
}
}
8 changes: 8 additions & 0 deletions src/test/resources/source-positions/ThrownType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import java.io.IOException;

public class ThrownType {
public void iThrow(String[] args) throws IndexOutOfBoundsException, IOException {
String s = args[42];
throw new IOException();
}
}
9 changes: 9 additions & 0 deletions src/test/resources/source-positions/TypeAccess.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
public class ActualTypeContainer<A, B> {
private A a;
private B b;

public ActualTypeContainer(A a, B b) {
this.a = a;
this.b = b;
}
}
5 changes: 5 additions & 0 deletions src/test/resources/source-positions/TypeArgument.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import java.util.List;

public class TypeArgument {
private List<List<String>> list;
}
7 changes: 7 additions & 0 deletions src/test/resources/source-positions/TypeCast.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
public class TypeCast {
public static void main(String[] args) {
double d = 10.0;
int i = (int) d;
System.out.println(i);
}
}
7 changes: 7 additions & 0 deletions src/test/resources/source-positions/VariableType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class VariableType {
public static void main(String[] args) {
int a = 142343;
int b = 2;
int c = a + b;
}
}