package fr.univ.nantes.processor;

import spoon.processing.AbstractProcessor;
import spoon.reflect.code.CtStatement;
import spoon.reflect.declaration.CtMethod;

/**
 * @author Alexis Giraudet
 * @author Thomas Minier
 */
public class MethodCallTreeProcessor extends AbstractProcessor<CtMethod> {
    @Override
    public void process(CtMethod method) {
        System.out.println(method.getParent().getSignature() + ": " + method.getSignature());
        String enterCode = "fr.univ.nantes.logger.LogWriter.enterMethod(\"" + method.getParent().getSignature() + ": " + method.getSignature() + "\"); try {//";
        String leaveCode = "} finally { fr.univ.nantes.logger.LogWriter.leaveMethod(); }//";
        CtStatement enterStatement = getFactory().Code().createCodeSnippetStatement(enterCode);
        CtStatement leaveStatement = getFactory().Code().createCodeSnippetStatement(leaveCode);
        method.getBody().insertBegin(enterStatement);
        method.getBody().insertEnd(leaveStatement);
    }
}

@@ -99,25 +99,93 @@
 .code.Ct
-Statement
+Block;%0Aimport spoon.reflect.code.CtStatement;%0Aimport spoon.reflect.code.CtTry
 ;%0Aimport
@@ -508,16 +508,17 @@
 ure());%0A
+%0A
         
@@ -662,17 +662,8 @@
 %22%5C%22)
-; try %7B//
 %22;%0A 
@@ -693,20 +693,8 @@
  = %22
-%7D finally %7B 
 fr.u
@@ -738,13 +738,8 @@
 od()
-; %7D//
 %22;%0A 
@@ -758,24 +758,26 @@
 tement enter
+Ct
 Statement = 
@@ -856,24 +856,26 @@
 tement leave
+Ct
 Statement = 
@@ -945,103 +945,456 @@
     
-method.getBody().insertBegin(enterStatement);%0A        method.getBody().insertEnd(leaveStatement
+CtTry ctTry = getFactory().Core().createTry();%0A        CtBlock finallyCtBlock = getFactory().Core().createBlock();%0A        CtBlock bodyCtBlock = getFactory().Core().createBlock();%0A%0A        finallyCtBlock.addStatement(leaveCtStatement);%0A        ctTry.setFinalizer(finallyCtBlock);%0A        ctTry.setBody(method.getBody());%0A        bodyCtBlock.addStatement(enterCtStatement);%0A        bodyCtBlock.addStatement(ctTry);%0A        method.setBody(bodyCtBlock
 );%0A 

use properly spoon library