package gumtree.spoon.builder;

import com.github.gumtreediff.tree.ITree;
import spoon.reflect.declaration.CtMethod;
import spoon.reflect.declaration.CtModifiable;
import spoon.reflect.declaration.CtVariable;
import spoon.reflect.declaration.ModifierKind;
import spoon.reflect.visitor.CtInheritanceScanner;

/** responsible to add additional nodes
 * only overrides scan* to add new nodes */
class NodeCreator extends CtInheritanceScanner {
	private final TreeScanner builder;

	NodeCreator(TreeScanner builder) {
		this.builder = builder;
	}

	@Override
	public void scanCtModifiable(CtModifiable m) {
		ITree modifiers = builder.createNode("Modifiers", "");
		modifiers.setMetadata(SpoonGumTreeBuilder.SPOON_OBJECT, m);
		for (ModifierKind kind : m.getModifiers()) {
			ITree modifier = builder.createNode("Modifier", kind.toString());
			modifier.setMetadata(SpoonGumTreeBuilder.SPOON_OBJECT, m);
			modifiers.addChild(modifier);
		}
		builder.addSiblingNode(modifiers);

	}

	@Override
	public <T> void scanCtVariable(CtVariable<T> e) {
		ITree variableType = builder.createNode("VARIABLE_TYPE", e.getType().getQualifiedName());
		variableType.setMetadata(SpoonGumTreeBuilder.SPOON_OBJECT, e.getType());
		builder.addSiblingNode(variableType);
	}

	@Override
	public <T> void visitCtMethod(CtMethod<T> e) {
		// add the return type of the method
		ITree returnType = builder.createNode("RETURN_TYPE", e.getType().getQualifiedName());
		returnType.setMetadata(SpoonGumTreeBuilder.SPOON_OBJECT, e.getType());
		builder.addSiblingNode(returnType);
		super.visitCtMethod(e);
	}
}

@@ -67,16 +67,83 @@
 .ITree;%0A
+import spoon.reflect.code.CtBlock;%0Aimport spoon.reflect.code.CtIf;%0A
 import s
@@ -316,16 +316,64 @@
 erKind;%0A
+import spoon.reflect.reference.CtTypeReference;%0A
 import s
@@ -1145,24 +1145,88 @@
 ble%3CT%3E e) %7B%0A
+%09%09CtTypeReference%3CT%3E type = e.getType();%0A%09%09if (type != null) %7B%0A%09
 %09%09ITree vari
@@ -1268,35 +1268,28 @@
 ABLE_TYPE%22, 
-e.getT
+t
 ype
-()
 .getQualifie
@@ -1298,16 +1298,17 @@
 ame());%0A
+%09
 %09%09variab
@@ -1356,38 +1356,32 @@
 OON_OBJECT, 
-e.getT
+t
 ype
-()
 );%0A
+%09
 %09%09builder.ad
@@ -1408,16 +1408,20 @@
 eType);%0A
+%09%09%7D%0A
 %09%7D%0A%0A%09@Ov
@@ -1514,16 +1514,80 @@
  method%0A
+%09%09CtTypeReference%3CT%3E type = e.getType();%0A%09%09if (type != null) %7B%0A%09
 %09%09ITree 
@@ -1633,27 +1633,20 @@
 _TYPE%22, 
-e.getT
+t
 ype
-()
 .getQual
@@ -1659,16 +1659,17 @@
 ame());%0A
+%09
 %09%09return
@@ -1723,22 +1723,16 @@
 CT, 
-e.getT
+t
 ype
-()
 );%0A
+%09
 %09%09bu
@@ -1765,16 +1765,20 @@
 nType);%0A
+%09%09%7D%0A
 %09%09super.

handle null type for method and variable (noclasspath)