You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by em...@apache.org on 2021/06/10 20:28:49 UTC

[groovy] branch master updated: GROOVY-5245: bean-style property access for boolean "is" category method

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

emilles 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 c0b5b23  GROOVY-5245: bean-style property access for boolean "is" category method
c0b5b23 is described below

commit c0b5b234e4f2d35f5d5aca416f6edee0261b13e0
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Thu Jun 10 15:26:34 2021 -0500

    GROOVY-5245: bean-style property access for boolean "is" category method
---
 .../groovy/runtime/GroovyCategorySupport.java        | 20 +++++++++++---------
 src/test/groovy/CategoryTest.groovy                  | 18 ++++++++++++++++++
 .../groovy/groovy/xml/GpathSyntaxTestSupport.groovy  | 12 ++++++------
 3 files changed, 35 insertions(+), 15 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/runtime/GroovyCategorySupport.java b/src/main/java/org/codehaus/groovy/runtime/GroovyCategorySupport.java
index adbfa33..bc1493a 100644
--- a/src/main/java/org/codehaus/groovy/runtime/GroovyCategorySupport.java
+++ b/src/main/java/org/codehaus/groovy/runtime/GroovyCategorySupport.java
@@ -161,22 +161,25 @@ public class GroovyCategorySupport {
             }
         }
 
-        private void cachePropertyAccessor(CategoryMethod method) {
-             String name = method.getName();
-             int parameterLength = method.getParameterTypes().length;
+        private void cachePropertyAccessor(final CategoryMethod method) {
+             final String name = method.getName();
+             final int nameLength = name.length();
+             final int parameterCount = method.getParameterTypes().length;
 
-             if (name.startsWith("get") && name.length() > 3 && parameterLength == 0) {
+             if (name.startsWith("get") && nameLength > 3 && parameterCount == 0) {
                  propertyGetterMap = putPropertyAccessor(3, name, propertyGetterMap);
-             }
-             else if (name.startsWith("set") && name.length() > 3 && parameterLength == 1) {
+             } else if (name.startsWith("is") && nameLength > 2 && parameterCount == 0
+                     && method.getReturnType().equals(boolean.class)) { // GROOVY-5245
+                 propertyGetterMap = putPropertyAccessor(2, name, propertyGetterMap);
+             } else if (name.startsWith("set") && nameLength > 3 && parameterCount == 1) {
                  propertySetterMap = putPropertyAccessor(3, name, propertySetterMap);
              }
         }
 
         // Precondition: accessorName.length() > prefixLength
-        private Map<String, String> putPropertyAccessor(int prefixLength, String accessorName, Map<String, String> map) {
+        private Map<String, String> putPropertyAccessor(final int prefixLength, final String accessorName, Map<String, String> map) {
             if (map == null) {
-                map = new HashMap<String, String>();
+                map = new HashMap<>();
             }
             String property = BeanUtils.decapitalize(accessorName.substring(prefixLength));
             map.put(property, accessorName);
@@ -200,7 +203,6 @@ public class GroovyCategorySupport {
             return level == 0 ? null : get(name);
         }
 
-
         String getPropertyCategoryGetterName(String propertyName) {
             if (propertyGetterMap == null) return null;
             String getter = propertyGetterMap.get(propertyName);
diff --git a/src/test/groovy/CategoryTest.groovy b/src/test/groovy/CategoryTest.groovy
index 2a7f73f..743a5e9 100644
--- a/src/test/groovy/CategoryTest.groovy
+++ b/src/test/groovy/CategoryTest.groovy
@@ -68,6 +68,24 @@ final class CategoryTest extends GroovyTestCase {
         }
     }
 
+    // GROOVY-5245
+    void testCategoryDefinedProperties2() {
+        assertScript '''
+            class Isser {
+                boolean isWorking() { true }
+            }
+            class IsserCat {
+                static boolean getWorking2(Isser b) { true }
+                static boolean isNotWorking(Isser b) { true }
+            }
+            use (IsserCat) {
+                assert new Isser().working
+                assert new Isser().working2
+                assert new Isser().notWorking // MissingPropertyException
+            }
+        '''
+    }
+
     void testCategoryReplacedPropertyAccessMethod() {
         def cth = new CategoryTestHelper()
         cth.aProperty = "aValue"
diff --git a/subprojects/groovy-xml/src/test/groovy/groovy/xml/GpathSyntaxTestSupport.groovy b/subprojects/groovy-xml/src/test/groovy/groovy/xml/GpathSyntaxTestSupport.groovy
index 614a292..f26d20f 100644
--- a/subprojects/groovy-xml/src/test/groovy/groovy/xml/GpathSyntaxTestSupport.groovy
+++ b/subprojects/groovy-xml/src/test/groovy/groovy/xml/GpathSyntaxTestSupport.groovy
@@ -18,12 +18,12 @@
  */
 package groovy.xml
 
-import org.custommonkey.xmlunit.XMLUnit
 import org.custommonkey.xmlunit.Diff
-import groovy.xml.XmlUtil
+import org.custommonkey.xmlunit.XMLUnit
 
 class GpathSyntaxTestSupport {
-    private static final sampleXml = '''
+
+    private static final sampleXml = '''\
 <characters>
     <character id="1" name="Wallace">
         <likes>cheese</likes>
@@ -35,11 +35,11 @@ class GpathSyntaxTestSupport {
     <booleanValue>y</booleanValue>
     <uriValue>http://example.org/</uriValue>
     <urlValue>http://example.org/</urlValue>
-    <empty/>
+    <noValue/>
 </characters>
 '''
 
-    private static final nestedXml = '''
+    private static final nestedXml = '''\
 <root>
     <a><z/><z/><y/></a>
     <b><z/></b>
@@ -100,7 +100,7 @@ class GpathSyntaxTestSupport {
         def unknownAttr = root.'@xxx'
         assert isSlurper(root) || !unknownAttr
         assert !isSlurper(root) || unknownAttr.isEmpty()
-        assert root.'empty'.text() == ''
+        assert root.'noValue'.text() == ''
     }
 
     static void checkCDataText(Closure getRoot) {