You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by pa...@apache.org on 2022/01/19 23:53:40 UTC

[groovy] branch master updated: GROOVY-10456: Inconsistent exception upon accessing empty property

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

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


The following commit(s) were added to refs/heads/master by this push:
     new b7a0fd6  GROOVY-10456: Inconsistent exception upon accessing empty property
b7a0fd6 is described below

commit b7a0fd6d3dfa2b511d9ac18e65e76c625fd5d1bb
Author: Paul King <pa...@asert.com.au>
AuthorDate: Thu Jan 20 09:53:33 2022 +1000

    GROOVY-10456: Inconsistent exception upon accessing empty property
---
 src/main/java/groovy/lang/MetaClassImpl.java |  2 +-
 src/test/groovy/PropertyTest.groovy          | 12 ++++++++++++
 2 files changed, 13 insertions(+), 1 deletion(-)

diff --git a/src/main/java/groovy/lang/MetaClassImpl.java b/src/main/java/groovy/lang/MetaClassImpl.java
index 94af0a3..aa9fdf8 100644
--- a/src/main/java/groovy/lang/MetaClassImpl.java
+++ b/src/main/java/groovy/lang/MetaClassImpl.java
@@ -2241,7 +2241,7 @@ public class MetaClassImpl implements MetaClass, MutableMetaClass {
     private Tuple2<MetaMethod, MetaProperty> createMetaMethodAndMetaProperty(final Class senderForMP, final Class senderForCMG, final String name, final boolean useSuper, final boolean isStatic) {
         MetaMethod method = null;
         MetaProperty mp = getMetaProperty(senderForMP, name, useSuper, isStatic);
-        if ((mp == null || mp instanceof CachedField) && isUpperCase(name.charAt(0)) && (name.length() < 2 || !isUpperCase(name.charAt(1))) && !"Class".equals(name)) {
+        if ((mp == null || mp instanceof CachedField) && name.length() > 0 && isUpperCase(name.charAt(0)) && (name.length() < 2 || !isUpperCase(name.charAt(1))) && !"Class".equals(name)) {
             // GROOVY-9618 adjust because capitalised properties aren't stored as meta bean props
             MetaProperty saved = mp;
             mp = getMetaProperty(senderForMP, BeanUtils.decapitalize(name), useSuper, isStatic);
diff --git a/src/test/groovy/PropertyTest.groovy b/src/test/groovy/PropertyTest.groovy
index b4957a2..fdbd437 100644
--- a/src/test/groovy/PropertyTest.groovy
+++ b/src/test/groovy/PropertyTest.groovy
@@ -243,6 +243,18 @@ class PropertyTest extends GroovyTestCase {
         }
     }
 
+    // GROOVY-10456
+    void testEmptyPropertyAccessForObject() {
+        assertScript '''
+            import static groovy.test.GroovyAssert.shouldFail
+
+            shouldFail(MissingPropertyException) {
+                o = new Object()
+                o[""]
+            }
+        '''
+    }
+
     void testPrivatePropertyThroughSubclass() {
         assertScript '''
             class A {