@Override
 public void computeChildren(final @NotNull XCompositeNode node) {
   Promise<Void> promise =
       Variables.processScopeVariables(scope, node, context, callFrame == null);
   if (callFrame != null) {
     promise.done(
         new ObsolescentConsumer<Void>(node) {
           @Override
           public void consume(Void ignored) {
             callFrame
                 .getReceiverVariable()
                 .done(
                     new ObsolescentConsumer<Variable>(node) {
                       @Override
                       public void consume(Variable variable) {
                         node.addChildren(
                             variable == null
                                 ? XValueChildrenList.EMPTY
                                 : XValueChildrenList.singleton(
                                     new VariableView(variable, context)),
                             true);
                       }
                     })
                 .rejected(
                     new ObsolescentConsumer<Throwable>(node) {
                       @Override
                       public void consume(@Nullable Throwable error) {
                         node.addChildren(XValueChildrenList.EMPTY, true);
                       }
                     });
           }
         });
   }
 }
Пример #2
0
  @NotNull
  public static <T> Promise<T> all(
      @NotNull Collection<Promise<?>> promises, @Nullable T totalResult) {
    if (promises.isEmpty()) {
      //noinspection unchecked
      return (Promise<T>) DONE;
    }

    final AsyncPromise<T> totalPromise = new AsyncPromise<T>();
    Consumer done = new CountDownConsumer<T>(promises.size(), totalPromise, totalResult);
    Consumer<Throwable> rejected =
        new Consumer<Throwable>() {
          @Override
          public void consume(Throwable error) {
            totalPromise.setError(error);
          }
        };

    for (Promise<?> promise : promises) {
      //noinspection unchecked
      promise.done(done);
      promise.rejected(rejected);
    }
    return totalPromise;
  }
  @NotNull
  public static <T> Promise<T> chooseDebuggee(
      @NotNull final Collection<T> targets,
      final int selectedIndex,
      @NotNull final Function<T, String> itemToString) {
    if (targets.size() == 1) {
      return Promise.resolve(ContainerUtil.getFirstItem(targets));
    } else if (targets.isEmpty()) {
      return Promise.reject("No tabs to inspect");
    }

    final AsyncPromise<T> result = new AsyncPromise<T>();
    ApplicationManager.getApplication()
        .invokeLater(
            new Runnable() {
              @Override
              public void run() {
                final JBList list = new JBList(targets);
                list.setCellRenderer(
                    new ColoredListCellRenderer() {
                      @Override
                      protected void customizeCellRenderer(
                          JList list, Object value, int index, boolean selected, boolean hasFocus) {
                        //noinspection unchecked
                        append(itemToString.fun((T) value));
                      }
                    });
                if (selectedIndex != -1) {
                  list.setSelectedIndex(selectedIndex);
                }

                JBPopupFactory.getInstance()
                    .createListPopupBuilder(list)
                    .setTitle("Choose Page to Debug")
                    .setItemChoosenCallback(
                        new Runnable() {
                          @Override
                          public void run() {
                            @SuppressWarnings("unchecked")
                            T value = (T) list.getSelectedValue();
                            if (value == null) {
                              result.setError(Promise.createError("No target to inspect"));
                            } else {
                              result.setResult(value);
                            }
                          }
                        })
                    .createPopup()
                    .showInFocusCenter();
              }
            });
    return result;
  }
Пример #4
0
 @SuppressWarnings("ThrowableResultOfMethodCallIgnored")
 @Override
 public void operationComplete(ChannelFuture future) throws Exception {
   if (!future.isSuccess()) {
     Throwable cause = future.cause();
     setError(cause == null ? Promise.createError("No success") : cause);
   }
 }
Пример #5
0
  public static Promise<Void> all(@NotNull Collection<Promise<?>> promises) {
    if (promises.isEmpty()) {
      return DONE;
    }

    final AsyncPromise<Void> totalPromise = new AsyncPromise<Void>();
    Consumer done = new CountDownConsumer(promises.size(), totalPromise);
    Consumer<String> rejected =
        new Consumer<String>() {
          @Override
          public void consume(String error) {
            if (totalPromise.state == AsyncPromise.State.PENDING) {
              totalPromise.setError(error);
            }
          }
        };

    for (Promise<?> promise : promises) {
      //noinspection unchecked
      promise.done(done);
      promise.rejected(rejected);
    }
    return totalPromise;
  }
Пример #6
0
  @NotNull
  @Override
  public Promise<XExpression> calculateEvaluationExpression() {
    if (evaluationExpression != null) {
      return Promise.resolve(evaluationExpression);
    } else {
      final AsyncPromise<XExpression> res = new AsyncPromise<XExpression>();
      myEvaluationContext
          .getManagerThread()
          .schedule(
              new SuspendContextCommandImpl(myEvaluationContext.getSuspendContext()) {
                @Override
                public Priority getPriority() {
                  return Priority.HIGH;
                }

                @Override
                public void contextAction() throws Exception {
                  evaluationExpression =
                      ApplicationManager.getApplication()
                          .runReadAction(
                              new Computable<XExpression>() {
                                @Override
                                public XExpression compute() {
                                  try {
                                    PsiElement psiExpression =
                                        getDescriptor()
                                            .getTreeEvaluation(
                                                JavaValue.this, getDebuggerContext());
                                    if (psiExpression != null) {
                                      XExpression res =
                                          TextWithImportsImpl.toXExpression(
                                              new TextWithImportsImpl(psiExpression));
                                      // add runtime imports if any
                                      Set<String> imports =
                                          psiExpression.getUserData(
                                              DebuggerTreeNodeExpression.ADDITIONAL_IMPORTS_KEY);
                                      if (imports != null && res != null) {
                                        if (res.getCustomInfo() != null) {
                                          imports.add(res.getCustomInfo());
                                        }
                                        res =
                                            new XExpressionImpl(
                                                res.getExpression(),
                                                res.getLanguage(),
                                                StringUtil.join(imports, ","),
                                                res.getMode());
                                      }
                                      return res;
                                    }
                                  } catch (EvaluateException e) {
                                    LOG.info(e);
                                  }
                                  return null;
                                }
                              });
                  res.setResult(evaluationExpression);
                }
              });
      return res;
    }
  }
Пример #7
0
public abstract class Client extends UserDataHolderBase {
  static final RuntimeException REJECTED = Promise.createError("rejected");

  protected final Channel channel;

  final ConcurrentIntObjectMap<AsyncPromise<Object>> messageCallbackMap =
      ContainerUtil.createConcurrentIntObjectMap();

  protected Client(@NotNull Channel channel) {
    this.channel = channel;
  }

  @NotNull
  public final EventLoop getEventLoop() {
    return channel.eventLoop();
  }

  @NotNull
  public final ByteBufAllocator getByteBufAllocator() {
    return channel.alloc();
  }

  protected abstract ChannelFuture send(@NotNull ByteBuf message);

  public abstract void sendHeartbeat();

  @Nullable
  final <T> AsyncPromise<T> send(int messageId, @NotNull ByteBuf message) {
    ChannelFuture channelFuture = send(message);
    if (messageId == -1) {
      return null;
    }

    ChannelFutureAwarePromise<T> promise =
        new ChannelFutureAwarePromise<T>(messageId, messageCallbackMap);
    channelFuture.addListener(promise);
    //noinspection unchecked
    messageCallbackMap.put(messageId, (AsyncPromise<Object>) promise);
    return promise;
  }

  final void rejectAsyncResults(@NotNull ExceptionHandler exceptionHandler) {
    if (!messageCallbackMap.isEmpty()) {
      Enumeration<AsyncPromise<Object>> elements = messageCallbackMap.elements();
      while (elements.hasMoreElements()) {
        try {
          elements.nextElement().setError(REJECTED);
        } catch (Throwable e) {
          exceptionHandler.exceptionCaught(e);
        }
      }
    }
  }

  private static final class ChannelFutureAwarePromise<T> extends AsyncPromise<T>
      implements ChannelFutureListener {
    private final int messageId;
    private final ConcurrentIntObjectMap<?> messageCallbackMap;

    public ChannelFutureAwarePromise(int messageId, ConcurrentIntObjectMap<?> messageCallbackMap) {
      this.messageId = messageId;
      this.messageCallbackMap = messageCallbackMap;
    }

    @Override
    public boolean setError(@NotNull Throwable error) {
      boolean result = super.setError(error);
      messageCallbackMap.remove(messageId);
      return result;
    }

    @SuppressWarnings("ThrowableResultOfMethodCallIgnored")
    @Override
    public void operationComplete(ChannelFuture future) throws Exception {
      if (!future.isSuccess()) {
        Throwable cause = future.cause();
        setError(cause == null ? Promise.createError("No success") : cause);
      }
    }
  }
}