-
Notifications
You must be signed in to change notification settings - Fork 463
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Field access on Groovy 4 erroneously converted to getter call #1544
Comments
@kriegaex I think this is not a Spock issue it is more a Groovy 4 issue. If I strip down to sample to only use Groovy and not Spock, it also fails with the same exception. Sample code: package de.scrum_master.stackoverflow.q71631373
abstract class BaseEvent{
private static final Random random = new Random()
private static final String alphabet = (('A'..'Z') + ('0'..'9')).join()
protected int id
protected String name
void init() {
id = 1 + random.nextInt(100)
name = (1..10).collect { alphabet[random.nextInt(alphabet.length())] }.join()
}
}
class FirstEvent extends BaseEvent {
}
def e = new FirstEvent()
e.init() //<-- Fails with exception in Groovy 4 but not in Groovy 3 |
@AndreasTu, that is an interesting find. You seem to be an experienced Groovy developer. Would you mind creating a Groovy issue? I have not tried in any version higher than 4.0.9 (GWC version, latest Groovy release is 4.0.13), but assuming that the problem still occurs in the latest release, an issue should be created, linking to this one, so we can track it. |
I have created GROOVY-11144 for that. |
The issue is fixed in Groovy 5.0.0-alpha-1 and it seams that there will not be a fix in Groovy 4. I guess we can close this ticket here. |
I still have hope that they might fix it for Groovy 4, because the latter is the current release and the current behaviour clearly a bug. For the record, a few workarounds: Workaround 1: Use class name when referring to static fields from within a closure.abstract class BaseEvent implements Event {
private static final Random random = new Random()
private static final String alphabet = (('A'..'Z') + ('0'..'9')).join()
protected int id
protected String name
@Override
void init() {
id = 1 + random.nextInt(100)
name = (1..10).collect { BaseEvent.alphabet[BaseEvent.random.nextInt(BaseEvent.alphabet.length())] }.join()
}
} Workaround 2: Use
|
@leonard84, DYT it would make sense to leave the issue open until the problem was fixed in Groovy 4 and then there is a Spock release using the fixed version? |
@kriegaex the DefaultGroovyMethods should not be required in case 2. You have to use join("") though since there is no join() method |
@blackdrag, thank you. You are absolutely right. I played around with the variants and did not prune it back to the minimal change necessary. I updated my sample code and the corresponding comments. |
Describe the bug
I upgraded an old playground project from Groovy 3 to 4. Now, a field access like
alphabet[random.nextInt(alphabet.length())]
is converted to something like a bogusgetAlphabet()
call, yielding:To Reproduce
Try it in the Groovy Web Console. Sorry for not minimising the spec, I simply copied an old spec from a Stack Overflow answer I wrote a while ago.
Expected behavior
The spec passes on Groovy 4.
Actual behavior
The spec fails on Groovy 4, while on 2.5 and 3 it passes. Therefore, I have deactivated the spec for now like this:
Java version
17
Buildtool version
Maven 3.8.1
What operating system are you using
Windows
Dependencies
Spock 2.3, Groovy 4. See GWC configuration.
Additional context
No response
The text was updated successfully, but these errors were encountered: