Skip to content

Commit 24ab6a3

Browse files
committed
Fix generics handling error
Under the hood, the root cause seemed to be this: Caused by: java.lang.UnsupportedOperationException at java.base/java.util.AbstractMap.put(AbstractMap.java:209) at org.codehaus.groovy.control.ResolveVisitor.resolveGenericsHeader(ResolveVisitor.java:1452) at org.codehaus.groovy.control.ResolveVisitor.resolveGenericsHeader(ResolveVisitor.java:1409) Based on a look in the debugger, without this change, the genericParameterNames field will be a Collections$EmptyMap, which is immutable and therefore doesn't support PUT operations.
1 parent b56ddca commit 24ab6a3

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

src/main/java/org/codehaus/groovy/control/ResolveVisitor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1279,7 +1279,7 @@ public void visitClass(final ClassNode node) {
12791279
if (!Modifier.isStatic(node.getModifiers()))
12801280
genericParameterNames.putAll(outerNames); // outer names visible
12811281
} else {
1282-
genericParameterNames.clear(); // outer class: new generic namespace
1282+
genericParameterNames = new HashMap<>(); // outer class: new generic namespace
12831283
}
12841284
resolveGenericsHeader(node.getGenericsTypes());
12851285
switch (phase) { // GROOVY-9866, GROOVY-10466

src/test/groovy/groovy/util/GroovyScriptEngineReloadingTest.groovy

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,39 @@ final class GroovyScriptEngineReloadingTest {
182182

183183
}
184184

185+
@Test
186+
void testRecompilingWithGenericsAndConstants() {
187+
MapFileSystem.instance.modFile('BaseClass.groovy', 'class BaseClass<T> {}', gse.@time)
188+
189+
def tertiaryClassText = '''
190+
class NotGeneric {
191+
/**
192+
* Not typed on purpose - if typed as String then NotGeneric is no longer a dependency of
193+
* ParameterisedClass for some reason...
194+
*/
195+
public static final Object CONSTANT = "not generic"
196+
}
197+
'''
198+
MapFileSystem.instance.modFile('NotGeneric.groovy', tertiaryClassText, gse.@time)
199+
200+
def subClassText = '''
201+
class SubClass extends BaseClass<String> {
202+
public static final String CONSTANT = NotGeneric.CONSTANT
203+
}
204+
'''
205+
MapFileSystem.instance.modFile('SubClass.groovy', subClassText, gse.@time)
206+
207+
MapFileSystem.instance.modFile('scriptUsingGeneric.groovy', 'SubClass.CONSTANT', gse.@time)
208+
209+
210+
gse.loadScriptByName('scriptUsingGeneric.groovy')
211+
sleep 1000
212+
213+
// make a change to the sub-class so that it gets recompiled
214+
MapFileSystem.instance.modFile('SubClass.groovy', subClassText + '\n', gse.@time)
215+
gse.loadScriptByName('scriptUsingGeneric.groovy')
216+
}
217+
185218
@Test
186219
void testDeleteDependent() {
187220
sleep 10000

0 commit comments

Comments
 (0)