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

sunlan pushed a commit to branch refine-groovydoc
in repository https://gitbox.apache.org/repos/asf/groovy.git

commit 254c7c8ea20122c4dadb78ab335f4c09f2109833
Author: Jesse Glick <jgl...@apache.org>
AuthorDate: Thu Dec 20 11:43:17 2018 -0500

    GROOVY-8936: NPE occurred when `null` message passed into 
`MissingPropertyException`(closes #844)
---
 .../groovy/groovy/lang/GroovyRuntimeException.java |  7 ++++-
 .../groovy/lang/MissingPropertyException.java      |  3 ++
 .../groovy/lang/MissingPropertyExceptionTest.java  | 34 ++++++++++++++++++++++
 3 files changed, 43 insertions(+), 1 deletion(-)

diff --git a/src/main/groovy/groovy/lang/GroovyRuntimeException.java 
b/src/main/groovy/groovy/lang/GroovyRuntimeException.java
index de489c3..af62bb6 100644
--- a/src/main/groovy/groovy/lang/GroovyRuntimeException.java
+++ b/src/main/groovy/groovy/lang/GroovyRuntimeException.java
@@ -60,7 +60,12 @@ public class GroovyRuntimeException extends RuntimeException 
{
     }
 
     public String getMessage() {
-        return getMessageWithoutLocationText() + getLocationText();
+        String messageWithoutLocationText = getMessageWithoutLocationText();
+        String locationText = getLocationText();
+        if (messageWithoutLocationText == null && locationText.isEmpty()) {
+            return null; // not "null"
+        }
+        return messageWithoutLocationText + locationText;
     }
 
     public ASTNode getNode() {
diff --git a/src/main/groovy/groovy/lang/MissingPropertyException.java 
b/src/main/groovy/groovy/lang/MissingPropertyException.java
index 273d963..4d27393 100644
--- a/src/main/groovy/groovy/lang/MissingPropertyException.java
+++ b/src/main/groovy/groovy/lang/MissingPropertyException.java
@@ -58,6 +58,9 @@ public class MissingPropertyException extends 
GroovyRuntimeException {
     }
 
     public String getMessageWithoutLocationText() {
+        if (property == null || type == null) {
+            return super.getMessageWithoutLocationText();
+        }
         final Throwable cause = getCause();
         if (cause == null) {
             if (super.getMessageWithoutLocationText() != null) {
diff --git a/src/test/groovy/lang/MissingPropertyExceptionTest.java 
b/src/test/groovy/lang/MissingPropertyExceptionTest.java
new file mode 100644
index 0000000..3b6c746
--- /dev/null
+++ b/src/test/groovy/lang/MissingPropertyExceptionTest.java
@@ -0,0 +1,34 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+package groovy.lang;
+
+import junit.framework.TestCase;
+
+/**
+ * Provides unit tests for the <code>MissingPropertyException</code> class.
+ */
+public class MissingPropertyExceptionTest extends TestCase {
+
+    public void testNullMessage() {
+        Throwable mpe = new MissingPropertyException(null);
+        assertNull(mpe.getMessage());
+        assertEquals("groovy.lang.MissingPropertyException", mpe.toString());
+    }
+
+}

Reply via email to