You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@struts.apache.org by ya...@apache.org on 2019/06/03 07:48:16 UTC

[struts] 02/05: not log user exceptions as missing properties (WW-4999)

This is an automated email from the ASF dual-hosted git repository.

yasserzamani pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/struts.git

commit 382124cfd2c3eb2f757e6faaf9a7a58f5e002765
Author: Yasser Zamani <ya...@apache.org>
AuthorDate: Fri May 31 17:57:25 2019 +0430

    not log user exceptions as missing properties (WW-4999)
    
    Also reaks loop on user method exceptions - but continue to next objects in stack on NoSuchMethodException.
    
    (cherry picked from commit 0999fba)
---
 .../opensymphony/xwork2/ognl/OgnlValueStack.java   |  3 +-
 .../xwork2/ognl/accessor/CompoundRootAccessor.java | 11 +++++--
 .../xwork2/ognl/OgnlValueStackTest.java            | 37 ++++++++++++++++++++++
 3 files changed, 47 insertions(+), 4 deletions(-)

diff --git a/core/src/main/java/com/opensymphony/xwork2/ognl/OgnlValueStack.java b/core/src/main/java/com/opensymphony/xwork2/ognl/OgnlValueStack.java
index 94ec450..3415b39 100644
--- a/core/src/main/java/com/opensymphony/xwork2/ognl/OgnlValueStack.java
+++ b/core/src/main/java/com/opensymphony/xwork2/ognl/OgnlValueStack.java
@@ -339,7 +339,8 @@ public class OgnlValueStack implements Serializable, ValueStack, ClearableValueS
     }
 
     protected boolean shouldLogMissingPropertyWarning(OgnlException e) {
-        return (e instanceof NoSuchPropertyException || e instanceof MethodFailedException)
+        return (e instanceof NoSuchPropertyException ||
+                (e instanceof MethodFailedException && e.getReason() instanceof NoSuchMethodException))
         		&& devMode && logMissingProperties;
     }
 
diff --git a/core/src/main/java/com/opensymphony/xwork2/ognl/accessor/CompoundRootAccessor.java b/core/src/main/java/com/opensymphony/xwork2/ognl/accessor/CompoundRootAccessor.java
index 2a4f5d2..811b008 100644
--- a/core/src/main/java/com/opensymphony/xwork2/ognl/accessor/CompoundRootAccessor.java
+++ b/core/src/main/java/com/opensymphony/xwork2/ognl/accessor/CompoundRootAccessor.java
@@ -218,13 +218,13 @@ public class CompoundRootAccessor implements PropertyAccessor, MethodAccessor, C
         }
 
         Throwable reason = null;
+        Class[] argTypes = getArgTypes(objects);
         for (Object o : root) {
             if (o == null) {
                 continue;
             }
 
             Class clazz = o.getClass();
-            Class[] argTypes = getArgTypes(objects);
 
             MethodCall mc = null;
 
@@ -236,12 +236,17 @@ public class CompoundRootAccessor implements PropertyAccessor, MethodAccessor, C
                 try {
                     return OgnlRuntime.callMethod((OgnlContext) context, o, name, objects);
                 } catch (OgnlException e) {
-                    // try the next one
                     reason = e.getReason();
 
-                    if ((mc != null) && (reason != null) && (reason.getClass() == NoSuchMethodException.class)) {
+                    if (reason != null && !(reason instanceof NoSuchMethodException)) {
+                        // method has found but thrown an exception
+                        break;
+                    }
+
+                    if ((mc != null) && (reason != null)) {
                         invalidMethods.put(mc, Boolean.TRUE);
                     }
+                    // continue and try the next one
                 }
             }
         }
diff --git a/core/src/test/java/com/opensymphony/xwork2/ognl/OgnlValueStackTest.java b/core/src/test/java/com/opensymphony/xwork2/ognl/OgnlValueStackTest.java
index 617f876..52b850f 100644
--- a/core/src/test/java/com/opensymphony/xwork2/ognl/OgnlValueStackTest.java
+++ b/core/src/test/java/com/opensymphony/xwork2/ognl/OgnlValueStackTest.java
@@ -277,6 +277,43 @@ public class OgnlValueStackTest extends XWorkTestCase {
         }
     }
 
+    public void testNotLogUserExceptionsAsMissingProperties() {
+        OgnlValueStack vs = createValueStack();
+        vs.setDevMode("true");
+        vs.setLogMissingProperties("true");
+
+        Dog dog = new Dog();
+        vs.push(dog);
+
+        TestAppender testAppender = new TestAppender();
+        Logger logger = (Logger) LogManager.getLogger(OgnlValueStack.class);
+        logger.addAppender(testAppender);
+        testAppender.start();
+
+        try {
+            vs.setValue("exception", "exceptionValue", false);
+            vs.findValue("exception", false);
+            vs.findValue("exception", String.class, false);
+            vs.findValue("getException()", false);
+            vs.findValue("getException()", String.class, false);
+            vs.findValue("bite", false);
+            vs.findValue("bite", void.class, false);
+            vs.findValue("getBite()", false);
+            vs.findValue("getBite()", void.class, false);
+
+            assertEquals(8, testAppender.logEvents.size());
+            for (int i = 0; i < testAppender.logEvents.size(); i += 2) {
+                assertTrue(testAppender.logEvents.get(i).getMessage().getFormattedMessage()
+                        .startsWith("Caught an exception while evaluating expression '"));
+                assertEquals("NOTE: Previous warning message was issued due to devMode set to true.",
+                        testAppender.logEvents.get(i + 1).getMessage().getFormattedMessage());
+            }
+        } finally {
+            testAppender.stop();
+            logger.removeAppender(testAppender);
+        }
+    }
+
     public void testFailOnMissingMethod() {
         OgnlValueStack vs = createValueStack();