Anonymous avatar Anonymous committed 8196f64

XW-133: DefaultActionInvocation gives a better error if the class doesn't implement the Action Interface

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

Comments (0)

Files changed (1)

src/java/com/opensymphony/xwork/DefaultActionInvocation.java

         return returnResult;
     }
 
-    public Result createResult() throws Exception {
-        Map results = proxy.getConfig().getResults();
-        ResultConfig resultConfig = (ResultConfig) results.get(resultCode);
-        Result newResult = null;
-
-        if (resultConfig != null) {
-            Class resultClass = resultConfig.getClazz();
-
-            if (resultClass != null) {
-                try {
-                    newResult = (Result) resultClass.newInstance();
-                } catch (Exception e) {
-                    LOG.error("There was an exception while instantiating the result of type " + resultClass, e);
-                    throw e;
-                }
-
-                OgnlUtil.setProperties(resultConfig.getParams(), newResult, ActionContext.getContext().getContextMap());
-            }
-        }
-        return newResult;
-    }
-
     public String getResultCode() {
         return resultCode;
     }
         preResultListeners.add(listener);
     }
 
+    public Result createResult() throws Exception {
+        Map results = proxy.getConfig().getResults();
+        ResultConfig resultConfig = (ResultConfig) results.get(resultCode);
+        Result newResult = null;
+
+        if (resultConfig != null) {
+            Class resultClass = resultConfig.getClazz();
+
+            if (resultClass != null) {
+                try {
+                    newResult = (Result) resultClass.newInstance();
+                } catch (Exception e) {
+                    LOG.error("There was an exception while instantiating the result of type " + resultClass, e);
+                    throw e;
+                }
+
+                OgnlUtil.setProperties(resultConfig.getParams(), newResult, ActionContext.getContext().getContextMap());
+            }
+        }
+
+        return newResult;
+    }
+
     public String invoke() throws Exception {
         if (executed) {
             throw new IllegalStateException("Action has already executed");
         if (!executed) {
             if (preResultListeners != null) {
                 for (Iterator iterator = preResultListeners.iterator();
-                     iterator.hasNext();) {
+                        iterator.hasNext();) {
                     PreResultListener listener = (PreResultListener) iterator.next();
                     listener.beforeResult(this, resultCode);
                 }
 
     protected void createAction() {
         // load action
+        Class actionClass = proxy.getConfig().getClazz();
+
         try {
-            action = (Action) proxy.getConfig().getClazz().newInstance();
+            action = (Action) actionClass.newInstance();
         } catch (InstantiationException e) {
             throw new XworkException("Unable to intantiate Action!", e);
         } catch (IllegalAccessException e) {
             throw new XworkException("Illegal access to constructor, is it public?", e);
+        } catch (ClassCastException e) {
+            throw new XworkException("Action class " + actionClass.getClass().getName() + "does not implement " + Action.class.getName(), e);
         } catch (Exception e) {
             String gripe = "";
 
                 gripe = "Unable to instantiate Action, " + proxy.getConfig().getClazz().getName() + ",  defined for '" + proxy.getActionName() + "' in namespace '" + proxy.getNamespace() + "'";
             }
 
-            gripe += (" -- " + e.getMessage());
-            throw new XworkException(gripe);
+            gripe += (((" -- " + e.getMessage()) != null) ? e.getMessage() : " [no message in exception]");
+            throw new XworkException(gripe, e);
         }
     }
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.