/** {@inheritDoc} */
  @Override
  public Object invoke(final Object proxy, final Method method, final Object[] args)
      throws Throwable {
    unwrapArgs(method.getParameterTypes(), args);
    if (!mixinHandlers.isEmpty()) {
      MethodSignature methodSignature = MethodSignature.forMethod(method);
      if (mixinHandlers.containsKey(methodSignature)) {
        return mixinHandlers.get(methodSignature).invoke(proxy, method, args);
      }
    }

    final InvocationHandler invocationHandler = handlers.get(MethodSignature.forMethod(method));
    if (invocationHandler != null) {
      try {
        return invocationHandler.invoke(proxy, method, args);
      } catch (XPathExpressionException e) {
        throw new XBPathException(e, method, "??");
      }
    }

    throw new IllegalArgumentException(
        "I don't known how to invoke method "
            + method
            + ". Did you forget to add a XB*-annotation or to register a mixin?");
  }
  @SuppressWarnings({"ThrowableInstanceNeverThrown"})
  @Test(expected = IllegalStateException.class)
  public void shouldRethrowAllOtherExceptions() throws Throwable {
    EasyMock.expect(mockFilterChain.filter(mockMethodParam));
    EasyMock.expectLastCall().andThrow(new IllegalStateException());
    mockControl.replay();

    handler.invoke(null, method, NO_PARAM);
  }
示例#3
0
 protected boolean getBooleanParam(
     final InvocationHandler invocationHandler, final Method method, final boolean defaultValue)
     throws Throwable {
   Boolean value = (Boolean) invocationHandler.invoke(configuration, method, null);
   if (value == null) {
     value = defaultValue;
   }
   logger.debug("Boolean param: {}: {}", method.getName(), value);
   return value;
 }
示例#4
0
 protected String getStringParam(
     final InvocationHandler invocationHandler, final Method method, final String defaultValue)
     throws Throwable {
   String value = (String) invocationHandler.invoke(configuration, method, null);
   if (value == null) {
     value = defaultValue;
   }
   logger.debug("String param: {}: {}", method.getName(), value);
   return value;
 }
  public Object intercept(
      Object object, java.lang.reflect.Method method, Object[] arguments, MethodProxy proxy)
      throws Throwable {
    Object res = null;

    {
      res = invocationHandler.invoke(object, method, arguments);
    }

    return res;
  }
示例#6
0
 /** {@inheritDoc} */
 @Override
 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
   try {
     return delegate.invoke(proxy, method, args);
   } catch (final InvocationTargetException e) {
     if (e.getTargetException() != null) {
       throw e.getTargetException();
     }
     throw e;
   }
 }
示例#7
0
    public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy)
        throws Throwable {

      // We conveniently mock abstract methods be default
      if (Modifier.isAbstract(method.getModifiers())) {
        return handler.invoke(obj, method, args);
      }

      // Here I need to check if the fillInStackTrace was called by EasyMock inner code
      // If it's the case, just ignore the call. We ignore it for two reasons
      // 1- In Java 7, the fillInStackTrace won't work because, since no constructor was called, the
      // stackTrace attribute is null
      // 2- There might be some unexpected side effect in the original fillInStackTrace. So it seems
      // more logical to ignore the call
      if (obj instanceof Throwable && method.getName().equals("fillInStackTrace")) {
        if (isCallerMockInvocationHandlerInvoke(new Throwable())) {
          return obj;
        }
      }

      // Bridges should delegate to their bridged method. It should be done before
      // checking for mocked methods because only unbridged method are mocked
      // It also make sure the method passed to the handler is not the bridge. Normally it
      // shouldn't be necessary because bridges are never mocked so are never in the mockedMethods
      // map. So the normal case is that is will call invokeSuper which will call the interceptor
      // for
      // the bridged method. The problem is that it doesn't happen. It looks like a cglib bug. For
      // package scoped bridges (see GenericTest), the interceptor is not called for the bridged
      // method. Not normal from my point of view.
      if (method.isBridge()) {
        method = BridgeMethodResolver.findBridgedMethod(method);
      }

      if (mockedMethods != null && !mockedMethods.contains(method)) {
        return proxy.invokeSuper(obj, args);
      }

      return handler.invoke(obj, method, args);
    }
  @SuppressWarnings({"ThrowableInstanceNeverThrown"})
  @Test
  public void shouldThrowAnImplementationNotFoundExceptionIfAClassCantBeLocated() throws Throwable {
    EasyMock.expect(mockFilterChain.filter(mockMethodParam));
    EasyMock.expectLastCall().andThrow(new MatchNotFoundException());

    mockControl.replay();

    try {
      handler.invoke(null, method, NO_PARAM);
      fail();
    } catch (ImplementationNotFoundException e) {
      mockControl.verify();
      assertThat(e.getMessage(), equalTo("Could not find implementation for class [B"));
    }
  }
示例#9
0
  /**
   * Build the action context from the Play {@link Context} and the {@link RequiresAuthentication}
   * annotation.
   *
   * @param ctx the Play context.
   * @param configuration the configuration.
   * @return
   */
  public static ActionContext build(Context ctx, Object configuration) {
    JavaWebContext context = new JavaWebContext(ctx.request(), ctx.response(), ctx.session());
    String clientName = null;
    String targetUrl = "";
    Boolean isAjax = false;
    Boolean stateless = false;
    String requireAnyRole = "";
    String requireAllRoles = "";

    if (configuration != null) {
      try {
        final InvocationHandler invocationHandler = Proxy.getInvocationHandler(configuration);
        clientName = (String) invocationHandler.invoke(configuration, clientNameMethod, null);
        targetUrl = (String) invocationHandler.invoke(configuration, targetUrlMethod, null);
        logger.debug("targetUrl : {}", targetUrl);
        isAjax = (Boolean) invocationHandler.invoke(configuration, isAjaxMethod, null);
        logger.debug("isAjax : {}", isAjax);
        stateless = (Boolean) invocationHandler.invoke(configuration, statelessMethod, null);
        logger.debug("stateless : {}", stateless);
        requireAnyRole =
            (String) invocationHandler.invoke(configuration, requireAnyRoleMethod, null);
        logger.debug("requireAnyRole : {}", requireAnyRole);
        requireAllRoles =
            (String) invocationHandler.invoke(configuration, requireAllRolesMethod, null);
        logger.debug("requireAllRoles : {}", requireAllRoles);
      } catch (Throwable e) {
        logger.error("Error during configuration retrieval", e);
        throw new TechnicalException(e);
      }
    }
    clientName =
        (clientName != null)
            ? clientName
            : context.getRequestParameter(Config.getClients().getClientNameParameter());
    logger.debug("clientName : {}", clientName);
    String sessionId = (stateless) ? null : StorageHelper.getOrCreationSessionId(ctx.session());

    return new ActionContext(
        ctx,
        ctx.request(),
        sessionId,
        context,
        clientName,
        targetUrl,
        isAjax,
        stateless,
        requireAnyRole,
        requireAllRoles);
  }
 @Override
 public Object invoke(final Object proxy, final Method method, final Object[] args)
     throws Throwable {
   return DEFAULT_METHOD_INVOCATION_HANDLER.invoke(proxy, defaultMethod, args);
 }