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/08 17:31:28 UTC
[groovy] 01/01: 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 GROOVY-5245
in repository https://gitbox.apache.org/repos/asf/groovy.git
commit bbc96591ac3f375bf263147e103b0577806452b7
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Tue Jun 8 12:31:10 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..d6accce 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 parameterLength = method.getParameterTypes().length;
- if (name.startsWith("get") && name.length() > 3 && parameterLength == 0) {
+ if (name.startsWith("get") && nameLength > 3 && parameterLength == 0) {
propertyGetterMap = putPropertyAccessor(3, name, propertyGetterMap);
- }
- else if (name.startsWith("set") && name.length() > 3 && parameterLength == 1) {
+ } else if (name.startsWith("is") && nameLength > 2 && parameterLength == 0
+ && method.getReturnType().equals(boolean.class)) { // GROOVY-5245
+ propertyGetterMap = putPropertyAccessor(2, name, propertyGetterMap);
+ } else if (name.startsWith("set") && nameLength > 3 && parameterLength == 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) {