Commits

Anonymous committed 2c2b23c

Fix Java 6 build by fixing how annotation priorities are evaluated

git-svn-id: http://svn.opensymphony.com/svn/xwork/trunk@1709e221344d-f017-0410-9bd5-d282ab1896d7

Comments (0)

Files changed (3)

src/java/com/opensymphony/xwork2/interceptor/annotations/AnnotationWorkflowInterceptor.java

         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 method2.getAnnotation(Before.class).priority()
-                            - method1.getAnnotation(Before.class).priority();
+                    return comparePriorities(method1.getAnnotation(Before.class).priority(),
+                                method2.getAnnotation(Before.class).priority());
                 }
             });
             for (Method m : methods) {
-                // action superclass methods first then action methods
                 final String resultCode = (String) m
                         .invoke(action, (Object[]) null);
                 if (resultCode != null) {
         methods = new ArrayList<Method>(AnnotationUtils.getAnnotatedMethods(action.getClass(), After.class));
 
         if (methods.size() > 0) {
-            // action methods first then action superclass methods
+            // methods are only sorted by priority
             Collections.sort(methods, new Comparator<Method>() {
                 public int compare(Method method1, Method method2) {
-                    return method2.getAnnotation(After.class).priority()
-                            - method1.getAnnotation(After.class).priority();
+                    return comparePriorities(method1.getAnnotation(After.class).priority(),
+                                method2.getAnnotation(After.class).priority());
                 }
             });
             for (Method m : methods) {
     public void init() {
     }
 
+    protected static int comparePriorities(int val1, int val2) {
+        if (val2 < val1) {
+            return -1;
+        } else if (val2 > val1) {
+            return 1;
+        } else {
+            return 0;
+        }
+    }
+
     /**
      * Invokes any &#64;BeforeResult annotated methods
      *
         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 method2.getAnnotation(BeforeResult.class).priority()
-                            - method1.getAnnotation(BeforeResult.class).priority();
+                    return comparePriorities(method1.getAnnotation(BeforeResult.class).priority(),
+                                method2.getAnnotation(BeforeResult.class).priority());
                 }
             });
             for (Method m : methods) {

src/test/com/opensymphony/xwork2/interceptor/annotations/AnnotatedAction.java

  */
 public class AnnotatedAction extends BaseAnnotatedAction {
 
-    @Before
+    @Before(priority=5)
 	public String before() {
 		log = log + "before";
 		return null;
 		log = log +"-beforeResult";
 	}
 	
-	@After
+	@After(priority=5)
 	public void after() {
 		log = log + "-after";
 	}

src/test/com/opensymphony/xwork2/interceptor/annotations/ShortcircuitedAction.java

  * @author Rainer Hermanns
  */
 public class ShortcircuitedAction extends BaseAnnotatedAction {	
-	@Before
+	@Before(priority=5)
 	public String before() {
 		log = log + "before";
 		return "shortcircuit";