Skip to content

Commit 56eb3e8

Browse files
committed
GROOVY-11874: add null check
5_0_X backport
1 parent c136326 commit 56eb3e8

2 files changed

Lines changed: 14 additions & 3 deletions

File tree

src/main/java/groovy/lang/MetaClassImpl.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,6 @@
113113
import java.util.function.BiConsumer;
114114

115115
import static groovy.lang.Tuple.tuple;
116-
import static java.lang.Character.isUpperCase;
117116
import static org.apache.groovy.ast.tools.ClassNodeUtils.isValidAccessorName;
118117
import static org.apache.groovy.util.Arrays.concat;
119118
import static org.codehaus.groovy.ast.tools.GeneralUtils.inSamePackage;
@@ -2132,8 +2131,11 @@ private boolean isVisibleProperty(final MetaProperty field, final MetaMethod met
21322131
private Tuple2<MetaMethod, MetaProperty> createMetaMethodAndMetaProperty(final Class<?> sender, final String name, final boolean useSuper, final boolean isStatic) {
21332132
MetaMethod method = null;
21342133
MetaProperty mp = getMetaProperty(sender, name, useSuper, isStatic);
2135-
if ((mp == null || mp instanceof CachedField) && !name.isEmpty() && isUpperCase(name.charAt(0)) && (name.length() < 2 || !isUpperCase(name.charAt(1))) && !"Class".equals(name) && !"MetaClass".equals(name)) {
2136-
// GROOVY-9618 adjust because capitalised properties aren't stored as meta bean props
2134+
2135+
if ((mp == null || mp instanceof CachedField)
2136+
&& name != null && !name.isEmpty() && Character.isUpperCase(name.charAt(0)) // GROOVY-11874
2137+
&& (name.length() == 1 || (!Character.isUpperCase(name.charAt(1)) && !name.equals("Class") && !name.equals("MetaClass"))) ) {
2138+
// GROOVY-9618: adjust because capitalised properties aren't stored as meta bean props
21372139
MetaProperty saved = mp;
21382140
mp = getMetaProperty(sender, BeanUtils.decapitalize(name), useSuper, isStatic);
21392141
if (mp == null || (saved != null && mp instanceof CachedField)) {

src/test/groovy/groovy/transform/stc/ArraysAndCollectionsSTCTest.groovy

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1008,6 +1008,15 @@ class ArraysAndCollectionsSTCTest extends StaticTypeCheckingTestCase {
10081008
'''
10091009
}
10101010

1011+
// GROOVY-11874
1012+
void testMapGetAt() {
1013+
assertScript '''
1014+
String name = null
1015+
def map = [:]
1016+
map[name]
1017+
'''
1018+
}
1019+
10111020
// GROOVY-6266
10121021
void testMapGenerics() {
10131022
assertScript '''

0 commit comments

Comments
 (0)