This is an automated email from the ASF dual-hosted git repository. asf-gitbox-commits pushed a commit to branch GROOVY-12024 in repository https://gitbox.apache.org/repos/asf/groovy.git
commit 3e0cd8352a4c93c5bc464e54919f15723a61168d Author: Eric Milles <[email protected]> AuthorDate: Sun May 31 10:38:25 2026 -0500 GROOVY-12024: `getAt(Map,String)` and `putAt(Map,String,V)` --- .../groovy/runtime/DefaultGroovyMethods.java | 39 +++++++++++++++++++++- .../stc/FieldsAndPropertiesSTCTest.groovy | 12 ++++--- 2 files changed, 46 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java b/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java index 40b633fe2a..0b6e9d9920 100644 --- a/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java +++ b/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java @@ -7351,6 +7351,24 @@ public class DefaultGroovyMethods extends DefaultGroovyMethodsSupport { return self.get(key); } + /** + * Support the subscript operator for a Map. + * <pre class="language-groovy groovyTestCase">def map = [foo:'bar', class:'baz', empty:'nope'] + * assert map['foo'] == 'bar' + * assert map['class'] == 'baz' + * assert map['empty'] == 'nope' + * assert map['properties'] == null // GROOVY-12024 + * </pre> + * + * @param self a Map + * @param key a String as a key for the map + * @return the value corresponding to the given key + * @since 6.0.0 + */ + public static <V> V getAt(Map<? super String,V> self, String key) { + return self.get(key); + } + /** * Support the subscript operator for a Bitset * @@ -12950,7 +12968,7 @@ public class DefaultGroovyMethods extends DefaultGroovyMethodsSupport { * @param self a Map * @param key an Object as a key for the map * @param value the value to put into the map - * @return the value corresponding to the given key + * @return the value * @since 1.0 */ public static <K,V> V putAt(Map<K,V> self, K key, V value) { @@ -12958,6 +12976,25 @@ public class DefaultGroovyMethods extends DefaultGroovyMethodsSupport { return value; } + /** + * A helper method to allow maps to work with subscript operators. + * <pre class="language-groovy groovyTestCase">def map = [:] + * assert map['properties'] == null + * map['properties'] = new Object() // GROOVY-12024 + * assert map['properties'] != null + * </pre> + * + * @param self a Map + * @param key an Object as a key for the map + * @param value the value to put into the map + * @return the value + * @since 6.0.0 + */ + public static <V> V putAt(Map<? super String,V> self, String key, V value) { + self.put(key, value); + return value; + } + /** * Support assigning a range of values with a single assignment statement. * diff --git a/src/test/groovy/groovy/transform/stc/FieldsAndPropertiesSTCTest.groovy b/src/test/groovy/groovy/transform/stc/FieldsAndPropertiesSTCTest.groovy index a5b2028430..e71ed42fe1 100644 --- a/src/test/groovy/groovy/transform/stc/FieldsAndPropertiesSTCTest.groovy +++ b/src/test/groovy/groovy/transform/stc/FieldsAndPropertiesSTCTest.groovy @@ -806,8 +806,10 @@ class FieldsAndPropertiesSTCTest extends StaticTypeCheckingTestCase { map.put('a', new A()) assert map.get('a') != null assert map.get('b') == null - A a = map.a - B b = map.b + def a = map.a + def b = map.b + assert a instanceof A + assert b instanceof B a = map['a'] b = map['b'] assert a instanceof A @@ -815,8 +817,10 @@ class FieldsAndPropertiesSTCTest extends StaticTypeCheckingTestCase { ''' assertScript types + ''' def test(C map) { - A a = map.a - B b = map.b + def a = map.a + def b = map.b + assert a instanceof A + assert b instanceof B a = map['a'] b = map['b'] assert a instanceof A
