Repository: SpoonLabs/gumtree-spoon-ast-diff
Pull request #62 (2018-11-12 18:51:09)

link spoon objects and gumtree nodes


Patch:
diff --git a/src/main/java/gumtree/spoon/builder/NodeCreator.java b/src/main/java/gumtree/spoon/builder/NodeCreator.java
index bc9038b..7850c82 100644
--- a/src/main/java/gumtree/spoon/builder/NodeCreator.java
+++ b/src/main/java/gumtree/spoon/builder/NodeCreator.java
@@ -1,8 +1,11 @@
 package gumtree.spoon.builder;
 
+import java.util.Comparator;
+import java.util.Set;
+import java.util.TreeSet;
+
 import com.github.gumtreediff.tree.ITree;
-import spoon.reflect.code.CtBlock;
-import spoon.reflect.code.CtIf;
+
 import spoon.reflect.declaration.CtMethod;
 import spoon.reflect.declaration.CtModifiable;
 import spoon.reflect.declaration.CtVariable;
@@ -10,12 +13,9 @@ import spoon.reflect.declaration.ModifierKind;
 import spoon.reflect.reference.CtTypeReference;
 import spoon.reflect.visitor.CtInheritanceScanner;
 
-import java.util.Comparator;
-import java.util.Set;
-import java.util.TreeSet;
-
-/** responsible to add additional nodes
- * only overrides scan* to add new nodes */
+/**
+ * responsible to add additional nodes only overrides scan* to add new nodes
+ */
 class NodeCreator extends CtInheritanceScanner {
 	private final TreeScanner builder;
 
@@ -37,7 +37,7 @@ class NodeCreator extends CtInheritanceScanner {
 			}
 		});
 		modifiers1.addAll(m.getModifiers());
-		
+
 		for (ModifierKind kind : modifiers1) {
 			ITree modifier = builder.createNode("Modifier", kind.toString());
 			modifier.setMetadata(SpoonGumTreeBuilder.SPOON_OBJECT, m);
@@ -53,6 +53,7 @@ class NodeCreator extends CtInheritanceScanner {
 		if (type != null) {
 			ITree variableType = builder.createNode("VARIABLE_TYPE", type.getQualifiedName());
 			variableType.setMetadata(SpoonGumTreeBuilder.SPOON_OBJECT, type);
+			type.putMetadata(SpoonGumTreeBuilder.GUMTREE_NODE, variableType);
 			builder.addSiblingNode(variableType);
 		}
 	}
@@ -64,11 +65,13 @@ class NodeCreator extends CtInheritanceScanner {
 		if (type != null) {
 			ITree returnType = builder.createNode("RETURN_TYPE", type.getQualifiedName());
 			returnType.setMetadata(SpoonGumTreeBuilder.SPOON_OBJECT, type);
+			type.putMetadata(SpoonGumTreeBuilder.GUMTREE_NODE, returnType);
 			builder.addSiblingNode(returnType);
 		}
 
 		for (CtTypeReference thrown : e.getThrownTypes()) {
 			ITree thrownType = builder.createNode("THROWS", thrown.getQualifiedName());
+			type.putMetadata(SpoonGumTreeBuilder.GUMTREE_NODE, thrownType);
 			builder.addSiblingNode(thrownType);
 		}
 
diff --git a/src/main/java/gumtree/spoon/builder/SpoonGumTreeBuilder.java b/src/main/java/gumtree/spoon/builder/SpoonGumTreeBuilder.java
index c62d702..0bd6d27 100644
--- a/src/main/java/gumtree/spoon/builder/SpoonGumTreeBuilder.java
+++ b/src/main/java/gumtree/spoon/builder/SpoonGumTreeBuilder.java
@@ -20,6 +20,7 @@ package gumtree.spoon.builder;
 import com.github.gumtreediff.tree.ITree;
 import com.github.gumtreediff.tree.TreeContext;
 import com.github.gumtreediff.tree.TreeUtils;
+
 import spoon.reflect.declaration.CtElement;
 
 /**
@@ -30,6 +31,7 @@ import spoon.reflect.declaration.CtElement;
 public class SpoonGumTreeBuilder {
 	public static final String SPOON_OBJECT = "spoon_object";
 	public static final String SPOON_OBJECT_DEST = "spoon_object_dest";
+	public static final String GUMTREE_NODE = "gtnode";
 
 	private final TreeContext treeContext = new TreeContext();
 
diff --git a/src/main/java/gumtree/spoon/builder/TreeScanner.java b/src/main/java/gumtree/spoon/builder/TreeScanner.java
index 9a1967f..a0fac88 100644
--- a/src/main/java/gumtree/spoon/builder/TreeScanner.java
+++ b/src/main/java/gumtree/spoon/builder/TreeScanner.java
@@ -1,7 +1,10 @@
 package gumtree.spoon.builder;
 
+import java.util.Stack;
+
 import com.github.gumtreediff.tree.ITree;
 import com.github.gumtreediff.tree.TreeContext;
+
 import spoon.reflect.code.CtBlock;
 import spoon.reflect.code.CtCase;
 import spoon.reflect.code.CtStatementList;
@@ -10,8 +13,6 @@ import spoon.reflect.path.CtRole;
 import spoon.reflect.reference.CtReference;
 import spoon.reflect.visitor.CtScanner;
 
-import java.util.Stack;
-
 public class TreeScanner extends CtScanner {
 	public static final String NOTYPE = "<notype>";
 	private final TreeContext treeContext;
@@ -37,7 +38,7 @@ public class TreeScanner extends CtScanner {
 
 		new NodeCreator(this).scan(element);
 
-		if (nodes.size() != depthBefore ) {
+		if (nodes.size() != depthBefore) {
 			// contract: this should never happen
 			throw new RuntimeException("too many nodes pushed");
 		}
@@ -45,6 +46,7 @@ public class TreeScanner extends CtScanner {
 
 	/**
 	 * Ignore some element from the AST
+	 * 
 	 * @param element
 	 * @return
 	 */
@@ -92,6 +94,7 @@ public class TreeScanner extends CtScanner {
 
 		ITree newNode = createNode(nodeTypeName, label);
 		newNode.setMetadata(SpoonGumTreeBuilder.SPOON_OBJECT, element);
+		element.putMetadata(SpoonGumTreeBuilder.GUMTREE_NODE, newNode);
 		return newNode;
 	}
 


Test patch:
diff --git a/src/test/java/gumtree/spoon/diff/support/SpoonSupportTest.java b/src/test/java/gumtree/spoon/diff/support/SpoonSupportTest.java
index 741fb22..436ec72 100644
--- a/src/test/java/gumtree/spoon/diff/support/SpoonSupportTest.java
+++ b/src/test/java/gumtree/spoon/diff/support/SpoonSupportTest.java
@@ -7,7 +7,10 @@ import static org.junit.Assert.assertTrue;
 
 import org.junit.Test;
 
+import com.github.gumtreediff.tree.ITree;
+
 import gumtree.spoon.AstComparator;
+import gumtree.spoon.builder.SpoonGumTreeBuilder;
 import gumtree.spoon.diff.Diff;
 import gumtree.spoon.diff.operations.DeleteOperation;
 import gumtree.spoon.diff.operations.InsertOperation;
@@ -134,4 +137,29 @@ public class SpoonSupportTest {
 		assertNull(methodLeft);
 	}
 
+	@Test
+	public void testGUMTREE_NODE_Link_1() {
+		String c1 = "" + "class X {" + "public int foo0() {" + " int x = 0;return 1;" + "}" + "};";
+
+		String c2 = "" + "class X {" + "public int foo0() {" + " int x = 0;x = 10;return 1;" + "}" + "};";
+
+		AstComparator diff = new AstComparator();
+		Diff editScript = diff.compare(c1, c2);
+		assertEquals(1, editScript.getRootOperations().size());
+
+		Operation op = editScript.getRootOperations().get(0);
+		assertTrue(op instanceof InsertOperation);
+
+		assertNotNull(op.getSrcNode());
+		assertEquals("x = 10", op.getSrcNode().toString());
+
+		ITree tree = (ITree) op.getSrcNode().getMetadata(SpoonGumTreeBuilder.GUMTREE_NODE);
+		assertNotNull(tree);
+
+		assertEquals("=", tree.getLabel());
+
+		assertEquals(2, tree.getChildren().size());
+
+	}
+
 }