public void build() {
    declareMethod();

    ExceptionWrapper mainTryBlock = new ExceptionWrapper(codeModel, method.body(), context);
    for (Integer arity : primitive.getArity()) {
      JInvocation invocation = invoke("doApply").arg(context).arg(environment);

      for (int i = 0; i < arity; ++i) {
        invocation.arg(args.component(lit(i)));
      }
      mainTryBlock
          .body()
          ._if(JExpr.direct("args.length").eq(JExpr.lit(arity)))
          ._then()
          ._return(invocation);
    }

    mainTryBlock.catchEvalExceptions();
    mainTryBlock.catchRuntimeExceptions();
    mainTryBlock.catchExceptions();

    method
        .body()
        ._throw(
            JExpr._new(codeModel.ref(EvalException.class))
                .arg(lit(primitive.getName() + ": max arity is " + primitive.getMaxArity())));
  }
 public synchronized Map<Throwable, Integer> getExceptions() {
   if (_exceptions == null) {
     return Collections.emptyMap();
   }
   final Map<Throwable, Integer> result = new HashMap<Throwable, Integer>();
   for (final ExceptionWrapper exception : _exceptions.keySet()) {
     result.put(exception.getException(), exception.getCount());
   }
   return result;
 }
 /**
  * Merge information from the other context into this (a root context). The caller must be the
  * thread that was working with the other context.
  *
  * @param context the other context
  */
 public synchronized void mergeThreadContext(final GraphBuildingContext context) {
   if (_exceptions == null) {
     _exceptions = new HashMap<ExceptionWrapper, ExceptionWrapper>();
   }
   if (context._exceptions != null) {
     for (final ExceptionWrapper exception : context._exceptions.keySet()) {
       final ExceptionWrapper existing = _exceptions.get(exception);
       if (existing != null) {
         existing.incrementCount(exception.getCount());
       } else {
         _exceptions.put(exception, exception);
       }
     }
   }
 }
 /**
  * Stores an exception that should be reported to the user. Only store the first copy of an
  * exception; after that increment the count of times that it occurred.
  *
  * @param t exception to store, not null
  */
 public void exception(final Throwable t) {
   s_logger.debug("Caught exception", t);
   if (_exceptions == null) {
     _exceptions = new HashMap<ExceptionWrapper, ExceptionWrapper>();
   }
   ExceptionWrapper.createAndPut(t, _exceptions);
 }
  public void buildVarArgs() {
    declareMethod();

    ExceptionWrapper mainTryBlock = new ExceptionWrapper(codeModel, method.body(), context);

    JvmMethod overload = primitive.getOverloads().get(0);
    VarArgParser parser = new VarArgParser(this, mainTryBlock.body(), overload);

    // convert the positional arguments
    convertArguments(parser.getArgumentProcessingBlock(), parser);

    // finally invoke the underlying function
    JInvocation invocation =
        classRef(overload.getDeclaringClass()).staticInvoke(overload.getName());
    for (JExpression argument : parser.getArguments()) {
      invocation.arg(argument);
    }

    CodeModelUtils.returnSexp(codeModel, mainTryBlock.body(), overload, invocation);

    mainTryBlock.catchEvalExceptions();
    mainTryBlock.catchRuntimeExceptions();
    mainTryBlock.catchExceptions();
  }
 public void flush(Exception exception) {
   Exception e = this.exception;
   wrapper.wrap(upload, (e != null) ? e : exception, repository);
   upload.setState(Transfer.State.DONE);
 }
 public void flush() {
   wrapper.wrap(upload, exception, repository);
   upload.setState(Transfer.State.DONE);
 }