Anonymous avatar Anonymous committed 5788443

XW-271
- added NPE check
- added a log statement (in debug level) to log exception when
exception is thrown due to OgnlException and IntrospectionException

git-svn-id: http://svn.opensymphony.com/svn/xwork/branches/xwork_1-2@1093 e221344d-f017-0410-9bd5-d282ab1896d7

Comments (0)

Files changed (2)

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

 import com.opensymphony.xwork.ActionInvocation;
 import com.opensymphony.xwork.ModelDriven;
 
-import ognl.OgnlException;
 import ognl.OgnlRuntime;
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
-import java.beans.IntrospectionException;
+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
+ * 
+ * @author $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);
                 try {
                     Object realTarget = OgnlUtil.getRealTarget(prop, valueStack.getContext(), valueStack.getRoot());
                     
                     if (realTarget != null) {
-                        Class clazz = OgnlRuntime.getPropertyDescriptor(realTarget.getClass(), prop).getPropertyType();
-                        
-                        if (clazz != null) {
-                            msg = findText(clazz, newKey, locale, null, args);
-                            if (msg != null) {
-                                return msg;
-                            }
+                        PropertyDescriptor propertyDescriptor = OgnlRuntime.getPropertyDescriptor(realTarget.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(OgnlException ognlex) {
-                    ; // ignore
-                }
-                catch(IntrospectionException ie) {
-                    ; // ignore
+                catch(Exception e) {
+                	_log.debug("unable to find property "+prop, e);
                 }
             }
         }

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

 import com.opensymphony.xwork.config.ConfigurationManager;
 import com.opensymphony.xwork.test.ModelDrivenAction2;
 import com.opensymphony.xwork.test.TestBean2;
+import com.opensymphony.xwork.ActionContext;
+import com.opensymphony.xwork.ActionInvocation;
+import com.opensymphony.xwork.ActionSupport;
+import com.opensymphony.xwork.util.Bar;
+
 import junit.framework.TestCase;
 
 import java.util.Collections;
  * Unit test for {@link LocalizedTextUtil}.
  * 
  * @author jcarreira
+ * @author tm_jee
+ * 
+ * @version $Date$ $Id$
  */
 public class LocalizedTextUtilTest extends TestCase {
 
 		}
 	}
 	
+	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();
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.