Repository: SpoonLabs/gumtree-spoon-ast-diff (java)

Issue:
Super type node does not need to be nested
Consider the following piece of code and its tree string.

```java
class Main extends SuperClass1 { }
```
```
-1@@
	65190232@@Main
		-1421180655@@
			-1203492450@@SuperClass1
```

I am not sure what is the need for nesting `SuperClass1` below a node without a label as Java allows a class to extend from _exactly 1 super class_. I propose that we remove the nesting and make `SuperClass1` a direct child of `Main`. The proposed tree would look something like this:

```
-1@@
	65190232@@Main
		-1203492450@@SuperClass1
```

> Test case is taken from [here](https://github.com/SpoonLabs/gumtree-spoon-ast-diff/blob/master/src/test/java/gumtree/spoon/AstComparatorTest.java#L1763).

Pull request:
fix: make super type node a direct descendant of class node
Fixes #180 




Patch:
diff --git a/src/main/java/gumtree/spoon/builder/LabelFinder.java b/src/main/java/gumtree/spoon/builder/LabelFinder.java
index 19899ef..ab76708 100644
--- a/src/main/java/gumtree/spoon/builder/LabelFinder.java
+++ b/src/main/java/gumtree/spoon/builder/LabelFinder.java
@@ -152,4 +152,11 @@ class LabelFinder extends CtInheritanceScanner {
 	public <T extends Annotation> void visitCtAnnotation(CtAnnotation<T> annotation) {
 		label = annotation.toString();
 	}
+
+	@Override
+	public <T> void visitCtTypeReference(CtTypeReference<T> e) {
+		if (e.getRoleInParent() == CtRole.SUPER_TYPE) {
+			label = e.getQualifiedName();
+		}
+	}
 }
diff --git a/src/main/java/gumtree/spoon/builder/NodeCreator.java b/src/main/java/gumtree/spoon/builder/NodeCreator.java
index 430ce8e..94d22c6 100644
--- a/src/main/java/gumtree/spoon/builder/NodeCreator.java
+++ b/src/main/java/gumtree/spoon/builder/NodeCreator.java
@@ -6,7 +6,6 @@ import spoon.reflect.declaration.CtModifiable;
 import spoon.reflect.declaration.CtVariable;
 import spoon.reflect.declaration.ModifierKind;
 import spoon.reflect.path.CtRole;
-import spoon.reflect.reference.CtReference;
 import spoon.reflect.reference.CtTypeReference;
 import spoon.reflect.visitor.CtInheritanceScanner;
 
@@ -101,19 +100,4 @@ public class NodeCreator extends CtInheritanceScanner {
 
 		super.visitCtMethod(e);
 	}
-
-    @Override
-    public void scanCtReference(CtReference reference) {
-
-
-        if (reference instanceof CtTypeReference && reference.getRoleInParent() == CtRole.SUPER_TYPE) {
-            ITree superType = builder.createNode("SUPER_TYPE", reference.toString());
-            superType.setMetadata(SpoonGumTreeBuilder.SPOON_OBJECT, reference);
-            reference.putMetadata(SpoonGumTreeBuilder.GUMTREE_NODE, superType);
-            builder.addSiblingNode(superType);
-        } else {
-            super.scanCtReference(reference);
-        }
-    }
-
 }
diff --git a/src/main/java/gumtree/spoon/builder/TreeScanner.java b/src/main/java/gumtree/spoon/builder/TreeScanner.java
index 7ba4074..c8f1886 100644
--- a/src/main/java/gumtree/spoon/builder/TreeScanner.java
+++ b/src/main/java/gumtree/spoon/builder/TreeScanner.java
@@ -114,6 +114,9 @@ public class TreeScanner extends CtScanner {
 		if (element instanceof CtBlock) {
 			nodeTypeName = element.getRoleInParent().toString();
 		}
+		if (element.getRoleInParent() == CtRole.SUPER_TYPE) {
+			nodeTypeName = "SUPER_TYPE";
+		}
 		return nodeTypeName;
 	}
 


Test patch:
diff --git a/src/test/java/gumtree/spoon/AstComparatorTest.java b/src/test/java/gumtree/spoon/AstComparatorTest.java
index 47e723d..905e1f2 100644
--- a/src/test/java/gumtree/spoon/AstComparatorTest.java
+++ b/src/test/java/gumtree/spoon/AstComparatorTest.java
@@ -4,6 +4,7 @@ import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
+import static org.junit.Assume.assumeTrue;
 
 import java.io.File;
 import java.io.FileReader;
@@ -1776,6 +1777,9 @@ public class AstComparatorTest {
 
 	@Test
 	public void testExtendsGenerics1() throws Exception {
+		// ToDo: Generic type is not being parsed for which #173 is already opened.
+		assumeTrue(false);
+
 		CtClass c1a = Launcher.parseClass("class Main extends SuperClass<One> { }");
 		CtClass c2a = Launcher.parseClass("class Main extends SuperClass<Two> { }");
 
@@ -1791,6 +1795,9 @@ public class AstComparatorTest {
 
 	@Test
 	public void testExtendsGenerics2() throws Exception {
+		// ToDo: Generic type is not being parsed for which #173 is already opened.
+		assumeTrue(false);
+
 		CtClass c1a = Launcher.parseClass("class Main extends SuperClass { }");
 		CtClass c2a = Launcher.parseClass("class Main extends SuperClass<One> { }");
 
diff --git a/src/test/java/gumtree/spoon/TreeTest.java b/src/test/java/gumtree/spoon/TreeTest.java
index 0589c0f..66b5a31 100644
--- a/src/test/java/gumtree/spoon/TreeTest.java
+++ b/src/test/java/gumtree/spoon/TreeTest.java
@@ -31,6 +31,7 @@ import spoon.compiler.SpoonResourceHelper;
 import spoon.reflect.code.CtStatement;
 import spoon.reflect.cu.SourcePosition;
 import spoon.reflect.cu.position.NoSourcePosition;
+import spoon.reflect.declaration.CtClass;
 import spoon.reflect.declaration.CtElement;
 import spoon.reflect.declaration.CtMethod;
 import spoon.reflect.declaration.CtType;
@@ -525,4 +526,16 @@ public class TreeTest {
 		ITree annotationNode = root.getChild(0).getChild(0).getChild(2);
 		assertEquals("@java.lang.Override", annotationNode.getLabel());
 	}
+
+	@Test
+	public void test_nestingLevelOfSuperType() {
+		CtClass<?> spoonClass = Launcher.parseClass("class Main extends SuperClass");
+		ITree root = new SpoonGumTreeBuilder().getTree(spoonClass);
+		ITree klass = root.getChild(0);
+		ITree superClass = klass.getChild(0);
+
+		assertEquals("SuperClass", superClass.getLabel());
+		assertEquals(superClass.getDepth(), klass.getDepth()+1);
+		assertEquals(0, superClass.getDescendants().size());
+	}
 }
diff --git a/src/test/resources/examples/spoon.json b/src/test/resources/examples/spoon.json
index 34e83bb..b55c359 100644
--- a/src/test/resources/examples/spoon.json
+++ b/src/test/resources/examples/spoon.json
@@ -18,15 +18,9 @@
           ]
         },
         {
-          "label": "",
-          "type": "TypeReference",
-          "children": [
-            {
-              "label": "javax.swing.JPanel",
-              "type": "SUPER_TYPE",
-              "children": []
-            }
-          ]
+          "label": "javax.swing.JPanel",
+          "type": "SUPER_TYPE",
+          "children": []
         },
         {
           "label": "serialVersionUID",