public Method get(MethodInvocation invocation) {
   Class<?> owner = invocation.getDelegate().getClass();
   String name = invocation.getName();
   Class<?>[] parameterTypes = invocation.getParameterTypes();
   MethodInvocationKey key = new MethodInvocationKey(owner, name, parameterTypes);
   lock.readLock().lock();
   Optional<Method> cached = store.get(key);
   if (cached == null) {
     cacheMiss++;
     lock.readLock().unlock();
     lock.writeLock().lock();
     try {
       cached = store.get(key);
       if (cached == null) {
         cached = lookup(owner, name, parameterTypes);
         if (cacheMiss % 10 == 0) {
           removeDirtyEntries();
         }
         store.put(key, cached);
       }
       lock.readLock().lock();
     } finally {
       lock.writeLock().unlock();
     }
   } else {
     cacheHit++;
   }
   try {
     return cached.orNull();
   } finally {
     lock.readLock().unlock();
   }
 }
    public Object invoke(Object target, Method method, Object[] params) throws Throwable {
      if (EQUALS_METHOD.equals(method)) {
        Object param = params[0];
        if (param == null || !Proxy.isProxyClass(param.getClass())) {
          return false;
        }
        InvocationHandler other = Proxy.getInvocationHandler(param);
        return equals(other);
      } else if (HASHCODE_METHOD.equals(method)) {
        return hashCode();
      }

      MethodInvocation invocation =
          new MethodInvocation(
              method.getName(),
              method.getReturnType(),
              method.getGenericReturnType(),
              method.getParameterTypes(),
              delegate,
              params);
      invoker.invoke(invocation);
      if (!invocation.found()) {
        String methodName =
            method.getDeclaringClass().getSimpleName() + "." + method.getName() + "()";
        throw Exceptions.unsupportedMethod(methodName);
      }
      return invocation.getResult();
    }
 public void invoke(MethodInvocation invocation) throws Throwable {
   next.invoke(invocation);
   if (invocation.found() && invocation.getResult() != null) {
     invocation.setResult(
         convert(invocation.getGenericReturnType(), invocation.getResult(), mapping));
   }
 }
 /**
  * Checks whether the given method was called with the arguments given in expectedParamValues.
  *
  * @param name method name
  * @param expectedParamValues parameter values
  * @return true if the method was called with these parameters, false otherwise
  */
 public boolean wasMethodCalled(String name, Object... expectedParamValues) {
   MethodInvocation expected = new MethodInvocation(name, expectedParamValues);
   for (MethodInvocation invocation : invocations) {
     if (invocation.equals(expected)) {
       return true;
     }
   }
   return false;
 }
    public void invoke(MethodInvocation invocation) throws Throwable {
      if (current.get() != null) {
        // Already invoking a method on the mix-in
        return;
      }

      if (instance == null) {
        instance = DirectInstantiator.INSTANCE.newInstance(mixInClass, proxy);
      }
      MethodInvocation beanInvocation =
          new MethodInvocation(
              invocation.getName(),
              invocation.getReturnType(),
              invocation.getGenericReturnType(),
              invocation.getParameterTypes(),
              instance,
              invocation.getParameters());
      current.set(beanInvocation);
      try {
        next.invoke(beanInvocation);
      } finally {
        current.set(null);
      }
      if (beanInvocation.found()) {
        invocation.setResult(beanInvocation.getResult());
      }
    }
    /**
     * Checks whether the given methods were called in the given order.
     *
     * @param methodInvocations methods that should have been called
     * @return true if the method with the given parameters was called, false otherwise
     */
    public boolean wereMethodsCalledInOrder(MethodInvocation... methodInvocations) {
      if (methodInvocations == null || methodInvocations.length == 0) return true;

      int i = 0;
      for (MethodInvocation invocation : invocations) {
        if (invocation.equals(methodInvocations[i])) {
          i++;
          if (i >= methodInvocations.length) return true;
        }
      }
      return false;
    }
    public void invoke(MethodInvocation invocation) throws Throwable {
      Method targetMethod = locateMethod(invocation);
      if (targetMethod == null) {
        return;
      }

      Object returnValue;
      try {
        returnValue = targetMethod.invoke(invocation.getDelegate(), invocation.getParameters());
      } catch (InvocationTargetException e) {
        throw e.getCause();
      }

      invocation.setResult(returnValue);
    }
    public void invoke(MethodInvocation method) throws Throwable {
      if (method.isGetter()) {
        if (properties.containsKey(method.getName())) {
          method.setResult(properties.get(method.getName()));
          return;
        }
        if (unknown.contains(method.getName())) {
          return;
        }

        Object value;
        next.invoke(method);
        if (!method.found()) {
          unknown.add(method.getName());
          return;
        }
        value = method.getResult();
        properties.put(method.getName(), value);
        return;
      }

      next.invoke(method);
    }
    public void invoke(MethodInvocation invocation) throws Throwable {
      Matcher matcher = IS_SUPPORT_METHOD.matcher(invocation.getName());
      if (!matcher.matches()) {
        next.invoke(invocation);
        return;
      }

      String getterName = String.format("get%s", matcher.group(1));
      MethodInvocation getterInvocation =
          new MethodInvocation(
              getterName,
              invocation.getReturnType(),
              invocation.getGenericReturnType(),
              new Class[0],
              invocation.getDelegate(),
              EMPTY);
      next.invoke(getterInvocation);
      invocation.setResult(getterInvocation.found());
    }
    public void invoke(MethodInvocation invocation) throws Throwable {
      next.invoke(invocation);
      if (invocation.found() || invocation.getParameterTypes().length != 1) {
        return;
      }

      if (!invocation.isIsOrGet()) {
        return;
      }

      MethodInvocation getterInvocation =
          new MethodInvocation(
              invocation.getName(),
              invocation.getReturnType(),
              invocation.getGenericReturnType(),
              EMPTY_CLASS_ARRAY,
              invocation.getDelegate(),
              EMPTY);
      next.invoke(getterInvocation);
      if (getterInvocation.found() && getterInvocation.getResult() != null) {
        invocation.setResult(getterInvocation.getResult());
      } else {
        invocation.setResult(invocation.getParameters()[0]);
      }
    }
 public void invoke(MethodInvocation method) throws Throwable {
   for (int i = 0; !method.found() && i < invokers.length; i++) {
     MethodInvoker invoker = invokers[i];
     invoker.invoke(method);
   }
 }
 /**
  * Checks whether the given method was called - allows distinguishing between overloads by
  * specifying the parameter types.
  *
  * @param name method name
  * @param paramTypes method parameters
  * @return true if the method with the given parameters was called, false otherwise
  */
 public boolean wasMethodCalled(String name, Class<?>... paramTypes) {
   for (MethodInvocation invocation : invocations) {
     if (invocation.method.equals(name) && invocation.paramTypesMatch(paramTypes)) return true;
   }
   return false;
 }
Пример #13
0
  /**
   * Aspect implementation which executes grid-enabled methods on remote nodes.
   *
   * @param invoc Method invocation instance provided by JBoss AOP framework.
   * @return Method execution result.
   * @throws Throwable If method execution failed.
   */
  @SuppressWarnings({
    "ProhibitedExceptionDeclared",
    "ProhibitedExceptionThrown",
    "CatchGenericClass",
    "unchecked"
  })
  @Bind(
      pointcut = "execution(* *->@org.gridgain.grid.gridify.Gridify(..))",
      cflow = "org.gridgain.grid.gridify.aop.jboss.GridifyJbossAspect.CFLOW_STACK")
  public Object gridify(MethodInvocation invoc) throws Throwable {
    Method mtd = invoc.getMethod();

    Gridify ann = mtd.getAnnotation(Gridify.class);

    assert ann != null : "Intercepted method does not have gridify annotation.";

    // Since annotations in Java don't allow 'null' as default value
    // we have accept an empty string and convert it here.
    // NOTE: there's unintended behavior when user specifies an empty
    // string as intended grid name.
    // NOTE: the 'ann.gridName() == null' check is added to mitigate
    // annotation bugs in some scripting languages (e.g. Groovy).
    String gridName = F.isEmpty(ann.gridName()) ? null : ann.gridName();

    if (G.state(gridName) != STARTED) {
      throw new GridException("Grid is not locally started: " + gridName);
    }

    // Initialize defaults.
    GridifyArgument arg =
        new GridifyArgumentAdapter(
            mtd.getDeclaringClass(),
            mtd.getName(),
            mtd.getParameterTypes(),
            invoc.getArguments(),
            invoc.getTargetObject());

    if (!ann.interceptor().equals(GridifyInterceptor.class)) {
      // Check interceptor first.
      if (!ann.interceptor().newInstance().isGridify(ann, arg)) {
        return invoc.invokeNext();
      }
    }

    if (!ann.taskClass().equals(GridifyDefaultTask.class) && ann.taskName().length() > 0) {
      throw new GridException(
          "Gridify annotation must specify either Gridify.taskName() or "
              + "Gridify.taskClass(), but not both: "
              + ann);
    }

    try {
      Grid grid = G.grid(gridName);

      // If task class was specified.
      if (!ann.taskClass().equals(GridifyDefaultTask.class)) {
        return grid.execute(
                (Class<? extends GridTask<GridifyArgument, Object>>) ann.taskClass(),
                arg,
                ann.timeout())
            .get();
      }

      // If task name was not specified.
      if (ann.taskName().length() == 0) {
        return grid.execute(
                new GridifyDefaultTask(invoc.getActualMethod().getDeclaringClass()),
                arg,
                ann.timeout())
            .get();
      }

      // If task name was specified.
      return grid.execute(ann.taskName(), arg, ann.timeout()).get();
    } catch (Throwable e) {
      for (Class<?> ex : invoc.getMethod().getExceptionTypes()) {
        // Descend all levels down.
        Throwable cause = e.getCause();

        while (cause != null) {
          if (ex.isAssignableFrom(cause.getClass())) {
            throw cause;
          }

          cause = cause.getCause();
        }

        if (ex.isAssignableFrom(e.getClass())) {
          throw e;
        }
      }

      throw new GridifyRuntimeException("Undeclared exception thrown: " + e.getMessage(), e);
    }
  }