/**
   * タスク開始前の処理を埋め込む。
   *
   * @param ctMethod 埋め込み対象のメソッド
   * @throws CannotCompileException
   */
  private void insertPreProcessTask(final CtBehavior ctMethod) throws CannotCompileException {
    ctMethod.instrument(
        new ExprEditor() {
          public void edit(MethodCall m) throws CannotCompileException {
            String methodName = m.getMethodName();
            String className = m.getClassName();
            if ((methodName.equals("invokeRemotely") || methodName.equals("invokeRemotelyInFuture"))
                && className.equals("org.infinispan.remoting.rpc.RpcManager")) {

              m.replace(
                  "{String address = $0.getAddress().toString();"
                      + MapReduceTaskMonitor.class.getName()
                      + ".preProcessTaskForInvokeRemotely(this, address);"
                      + "$_ = $proceed($$);}");
            } else if (methodName.equals("invokeRemotelyInFuture")
                && className.equals("org.infinispan.remoting.rpc.RpcManager")) {
              m.replace(
                  "{String addressStr = address.toString();"
                      + MapReduceTaskMonitor.class.getName()
                      + ".preProcessTaskForInvokeRemotelyInFuture(this, addressStr);"
                      + "$_ = $proceed($$);}");
            } else if (methodName.equals("perform")
                && className.equals("org.infinispan.commands.read.MapReduceCommand")) {
              m.replace(
                  "{String address = rpc.getAddress().toString();"
                      + MapReduceTaskMonitor.class.getName()
                      + ".preProcessTask(this, address);"
                      + "$_ = $proceed($$);}");
            }
          }
        });
  }
 /**
  * ジョブ終了後の処理を埋め込む
  *
  * @param ctMethod 埋め込む対象のメソッド
  * @throws CannotCompileException
  */
 private void insertPostProcess(final CtBehavior ctMethod) throws CannotCompileException {
   ctMethod.instrument(
       new ExprEditor() {
         public void edit(MethodCall m) throws CannotCompileException {
           String className = m.getClassName();
           String methodName = m.getMethodName();
           if (className.equals("org.infinispan.remoting.rpc.RpcManager")
                   && methodName.equals("invokeRemotely")
               || className.equals(
                       "org.infinispan.distexec.mapreduce." + "MapReduceTask.MapReduceFuture")
                   && methodName.equals("get")) {
             m.replace(
                 "{"
                     + "$_ = $proceed($$);"
                     + "java.util.Map map2 = (java.util.Map)$_;"
                     + "String[] keys = new String[0];"
                     + "java.util.Set keySet = map2.keySet();"
                     + "java.util.Map newMap = new java.util.HashMap();"
                     + "java.util.Iterator it = keySet.iterator();"
                     + "while (it.hasNext()) {"
                     + "org.infinispan.remoting.transport.Address key = "
                     + "(org.infinispan.remoting.transport.Address)it.next();"
                     + "newMap.put(key.toString(), "
                     + "Boolean.valueOf"
                     + "(((org.infinispan.remoting.responses.Response)map2.get(key))"
                     + ".isSuccessful()));}"
                     + MapReduceTaskMonitor.class.getName()
                     + ".postProcessTaskForInvokeRemotely(this, newMap);"
                     + "$_ = map2;"
                     + "}");
           }
           if (className.equals("org.infinispan.commands.read.MapReduceCommand")
               && methodName.equals("perform")) {
             m.replace(
                 "{"
                     + "$_ = $proceed($$);"
                     + MapReduceTaskMonitor.class.getName()
                     + ".postProcessTask(this);}");
           }
         }
       });
 }
 /**
  * タスク終了時の処理を埋め込む。
  *
  * @param ctMethod 埋め込み対象のメソッド
  * @throws CannotCompileException
  */
 private void insertPostProcessTaskNG(final CtBehavior ctMethod) throws CannotCompileException {
   ctMethod.instrument(
       new ExprEditor() {
         public void edit(MethodCall m) throws CannotCompileException {
           if (m.getClassName().equals("org.infinispan.remoting.rpc.RpcManager")
                   && m.getMethodName().equals("invokeRemotelyInFuture")
               || m.getClassName().equals("org.infinispan.remoting.rpc.RpcManager")
                   && m.getMethodName().equals("invokeRemotely")
               || m.getClassName()
                       .equals("org.infinispan.distexec.mapreduce.MapReduceTask.MapReduceFuture")
                   && m.getMethodName().equals("get")
               || m.getClassName().equals("org.infinispan.commands.read.MapReduceCommand")
                   && m.getMethodName().equals("perform")) {
             m.replace(
                 "try{$_ = $proceed($$);}catch(Exception e){"
                     + MapReduceTaskMonitor.class.getName()
                     + ".postProcessNGTask(this);}");
           }
         }
       });
 }