Anonymous avatar Anonymous committed dcedc51

XW-271 backport this issue from XWork_1-2 branch to the head. This issue is to fix localization text can't be loaded when the property returns null

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

Comments (0)

Files changed (2)

src/java/com/opensymphony/xwork2/util/LocalizedTextUtil.java

 import com.opensymphony.xwork2.ActionContext;
 import com.opensymphony.xwork2.ActionInvocation;
 import com.opensymphony.xwork2.ModelDriven;
+
+import ognl.OgnlRuntime;
+
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
+import java.beans.PropertyDescriptor;
 import java.lang.reflect.Field;
 import java.lang.reflect.Method;
 import java.lang.reflect.InvocationTargetException;
  * @author Jason Carreira
  * @author Mark Woon
  * @author Rainer Hermanns
+ * @author tm_jee
+ * 
+ * @version $Date$ $Id$
  */
 public class LocalizedTextUtil {
+	
+	private static final Log _log = LogFactory.getLog(LocalizedTextUtil.class);
 
     private static List DEFAULT_RESOURCE_BUNDLES = null;
     private static final Log LOG = LogFactory.getLog(LocalizedTextUtil.class);
 
             if (prop != null) {
                 Object obj = valueStack.findValue(prop);
-
-                if (obj != null) {
-                    Class clazz = obj.getClass();
-
-                    if (clazz != null) {
-                        valueStack.push(obj);
-                        msg = findText(clazz, newKey, locale, null, args);
-                        valueStack.pop();
-
-                        if (msg != null) {
-                            return msg;
-                        }
-                    }
+                try {
+                	Object actionObj = OgnlUtil.getRealTarget(prop, valueStack.getContext(), valueStack.getRoot());
+                	if (actionObj != null) {
+                		PropertyDescriptor propertyDescriptor = OgnlRuntime.getPropertyDescriptor(actionObj.getClass(), prop);
+
+                		if (propertyDescriptor != null) {
+                			Class clazz=propertyDescriptor.getPropertyType();
+
+                			if (clazz != null) {
+                				if (obj != null)
+                					valueStack.push(obj);
+                				msg = findText(clazz, newKey, locale, null, args);
+                				if (obj != null)
+                					valueStack.pop();
+
+                				if (msg != null) {
+                					return msg;
+                				}
+                			}
+                		}
+                	}
+                }
+                catch(Exception e) {
+                	_log.warn("unable to find property "+prop, e);
                 }
             }
         }

src/test/com/opensymphony/xwork2/util/LocalizedTextUtilTest.java

 import com.opensymphony.xwork2.*;
 import com.opensymphony.xwork2.test.ModelDrivenAction2;
 import com.opensymphony.xwork2.test.TestBean2;
-import junit.framework.TestCase;
 
 import java.util.Collections;
 import java.util.Date;
  * Unit test for {@link LocalizedTextUtil}.
  *
  * @author jcarreira
+ * @author tm_jee
+ * 
+ * @version $Date$ $Id$
  */
 public class LocalizedTextUtilTest extends XWorkTestCase {
 
 		}
 	}
 	
+	public void testActionGetTextWithNullObject() throws Exception {
+		MyAction action = new MyAction();
+		
+		Mock mockActionInvocation = new Mock(ActionInvocation.class);
+        mockActionInvocation.expectAndReturn("getAction", action);
+        ActionContext.getContext().setActionInvocation((ActionInvocation) mockActionInvocation.proxy());
+		ActionContext.getContext().getValueStack().push(action);
+		
+		String message = action.getText("barObj.title");
+		assertEquals("Title:", message);
+	}
+	
+	
+	public static class MyAction extends ActionSupport {
+		private Bar testBean2;
+		
+		public Bar getBarObj() {
+			return testBean2;
+		}
+		public void setBarObj(Bar testBean2) {
+			this.testBean2 = testBean2;
+		}
+	}
+	
     public void testActionGetText() throws Exception {
         ModelDrivenAction2 action = new ModelDrivenAction2();
         TestBean2 bean = (TestBean2) action.getModel();
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.