/**
   * Discovers annotated methods on the action and calls them according to the workflow
   *
   * @see
   *     com.opensymphony.xwork2.interceptor.Interceptor#intercept(com.opensymphony.xwork2.ActionInvocation)
   */
  public String intercept(ActionInvocation invocation) throws Exception {
    final Object action = invocation.getAction();
    invocation.addPreResultListener(this);
    List<Method> methods =
        new ArrayList<Method>(AnnotationUtils.getAnnotatedMethods(action.getClass(), Before.class));
    if (methods.size() > 0) {
      // methods are only sorted by priority
      Collections.sort(
          methods,
          new Comparator<Method>() {
            public int compare(Method method1, Method method2) {
              return comparePriorities(
                  method1.getAnnotation(Before.class).priority(),
                  method2.getAnnotation(Before.class).priority());
            }
          });
      for (Method m : methods) {
        final String resultCode = (String) m.invoke(action, (Object[]) null);
        if (resultCode != null) {
          // shortcircuit execution
          return resultCode;
        }
      }
    }

    String invocationResult = invocation.invoke();

    // invoke any @After methods
    methods =
        new ArrayList<Method>(AnnotationUtils.getAnnotatedMethods(action.getClass(), After.class));

    if (methods.size() > 0) {
      // methods are only sorted by priority
      Collections.sort(
          methods,
          new Comparator<Method>() {
            public int compare(Method method1, Method method2) {
              return comparePriorities(
                  method1.getAnnotation(After.class).priority(),
                  method2.getAnnotation(After.class).priority());
            }
          });
      for (Method m : methods) {
        m.invoke(action, (Object[]) null);
      }
    }

    return invocationResult;
  }
  /**
   * Invokes any &#64;BeforeResult annotated methods
   *
   * @see
   *     com.opensymphony.xwork2.interceptor.PreResultListener#beforeResult(com.opensymphony.xwork2.ActionInvocation,String)
   */
  public void beforeResult(ActionInvocation invocation, String resultCode) {
    Object action = invocation.getAction();
    List<Method> methods =
        new ArrayList<Method>(
            AnnotationUtils.getAnnotatedMethods(action.getClass(), BeforeResult.class));

    if (methods.size() > 0) {
      // methods are only sorted by priority
      Collections.sort(
          methods,
          new Comparator<Method>() {
            public int compare(Method method1, Method method2) {
              return comparePriorities(
                  method1.getAnnotation(BeforeResult.class).priority(),
                  method2.getAnnotation(BeforeResult.class).priority());
            }
          });
      for (Method m : methods) {
        try {
          m.invoke(action, (Object[]) null);
        } catch (Exception e) {
          throw new XWorkException(e);
        }
      }
    }
  }