[
https://issues.apache.org/jira/browse/GROOVY-12040?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
]
Paul King updated GROOVY-12040:
-------------------------------
Description:
This broke Grails.
h3. Summary
{{@groovy.transform.builder.Builder}} was {{@Retention(RUNTIME)}} through
Groovy 4.0.x. In Groovy 5.0.0 it was changed to {{@Retention(SOURCE)}} in
[GROOVY-10855|https://issues.apache.org/jira/browse/GROOVY-10855] ("normalized
annotation metadata"). As a result, {{Class.getAnnotation(Builder.class)}} now
returns {{null}} at runtime, breaking frameworks that reflect on the annotation
to detect builder-shaped classes (e.g. Grails
{{ConfigurationBuilder.buildRecurse}} -- see
[apache/grails-core#15557|https://github.com/apache/grails-core/pull/15557] row
4).
GROOVY-10855 was initially about the annotation collectors but we bundled in
some more AST transform annotations that we weren't aware anyone was using at
runtime. Builder ended up being a case that was being used.
h3. Minimal Standalone Reproducer
{code:groovy}
import groovy.transform.builder.Builder
import groovy.transform.builder.SimpleStrategy
@Builder(builderStrategy = SimpleStrategy, prefix = '')
class ConnectionSettings { String host = 'localhost' }
// Copied from ConfigurationBuilder.buildRecurse, line 244-245:
Builder ann = ConnectionSettings.getAnnotation(Builder)
assert ann != null && ann.builderStrategy() == SimpleStrategy
{code}
|| Version || {{getAnnotation(Builder)}} ||
| 3.0.x | non-null (RUNTIME) |
| 4.0.x | non-null (RUNTIME) |
| 5.0.x / 6.0.0-alpha-1 | *null* (SOURCE) |
h3. Reproducer (drives the real {{ConfigurationBuilder}}; no spring-context /
spring-boot)
{{ConfigurationBuilder}}'s constructor takes
{{org.springframework.core.env.PropertyResolver}}, so {{spring-core}} is
unavoidable on the classpath -- but no Spring container, binding chain, or
converter machinery is needed.
{code:groovy}
@Grab('org.grails:grails-datastore-core:9.0.0-M3')
@Grab('org.springframework:spring-core:6.1.14')
import groovy.transform.builder.Builder
import groovy.transform.builder.SimpleStrategy
import org.grails.datastore.mapping.config.ConfigurationBuilder
import org.springframework.core.env.PropertyResolver
import org.springframework.core.env.AbstractPropertyResolver
@Builder(builderStrategy = SimpleStrategy, prefix = '')
class ConnectionSettings {
String host = 'localhost'
}
class RootSettings {
ConnectionSettings connection = new ConnectionSettings()
void setConnection(ConnectionSettings cs) { this.connection = cs }
ConnectionSettings getConnection() { connection }
}
class MapResolver extends AbstractPropertyResolver {
private final Map<String, String> m
MapResolver(Map<String, String> m) { this.m = m }
boolean containsProperty(String key) { m.containsKey(key) }
String getProperty(String key) { m.get(key) }
<T> T getProperty(String key, Class<T> t) { (T) m.get(key) }
String getPropertyOrDefault(String key, String d) { m.getOrDefault(key, d)
}
protected String getPropertyAsRawString(String key) { m.get(key) }
}
class RootBuilder extends ConfigurationBuilder<RootSettings, RootSettings> {
RootBuilder(PropertyResolver pr) { super(pr, 'app') }
protected RootSettings createBuilder() { new RootSettings()
}
protected RootSettings toConfiguration(RootSettings b) { b }
}
def resolver = new MapResolver(['app.connection.host': 'remotehost'])
def cfg = new RootBuilder(resolver).build()
println "Groovy ${GroovySystem.version}: host = ${cfg.connection.host}
(expected 'remotehost')"
assert cfg.connection.host == 'remotehost' :
"ConfigurationBuilder did not recurse into the @Builder(SimpleStrategy)
nested type — " +
"getAnnotation(Builder) returned null"
{code}
h3. Result
|| Version || Output ||
| 4.0.27 | {{host = remotehost}} (passes) |
| 5.0.6 | {{host = localhost}} (AssertionError) |
Same JVM, same Spring jar, same Grails jar. Only the Groovy runtime differs.
h3. Root cause
Commit {{3311ee95f5}} flipped {{Builder.java}} from {{RUNTIME}} to {{SOURCE}}.
GROOVY-10855's stated scope was only the collector/meta-annotations
({{@Canonical}}, {{@CompileDynamic}}, {{@Immutable}}, {{@RecordType}}) -- the
change to {{@Builder}} (and a number of other user-facing class annotations)
was scope-creep, undocumented in release notes, and not covered by tests.
h3. Proposed fix
Revert just {{@Builder}}'s retention to {{RUNTIME}}.
was:
This broke Grails.
h3. Summary
{{@groovy.transform.builder.Builder}} was {{@Retention(RUNTIME)}} through
Groovy 4.0.x. In Groovy 5.0.0 it was changed to {{@Retention(SOURCE)}} in
[GROOVY-10855|https://issues.apache.org/jira/browse/GROOVY-10855] ("normalized
annotation metadata"). As a result, {{Class.getAnnotation(Builder.class)}} now
returns {{null}} at runtime, breaking frameworks that reflect on the annotation
to detect builder-shaped classes (e.g. Grails
{{ConfigurationBuilder.buildRecurse}} -- see
[apache/grails-core#15557|https://github.com/apache/grails-core/pull/15557] row
4).
GROOVY-10855 was initially about the annotation collectors but we bundled in
some more AST transform annotations that we weren't aware anyone was using at
runtime. Builder ended up being a case that was being used.
h3. Minimal Standalone Reproducer
{code:groovy}
import groovy.transform.builder.Builder
import groovy.transform.builder.SimpleStrategy
@Builder(builderStrategy = SimpleStrategy, prefix = '')
class ConnectionSettings { String host = 'localhost' }
Builder ann = ConnectionSettings.getAnnotation(Builder)
assert ann != null && ann.builderStrategy() == SimpleStrategy
{code}
|| Version || {{getAnnotation(Builder)}} ||
| 3.0.x | non-null (RUNTIME) |
| 4.0.x | non-null (RUNTIME) |
| 5.0.x / 6.0.0-alpha-1 | *null* (SOURCE) |
h3. Reproducer (drives the real {{ConfigurationBuilder}}; no spring-context /
spring-boot)
{{ConfigurationBuilder}}'s constructor takes
{{org.springframework.core.env.PropertyResolver}}, so {{spring-core}} is
unavoidable on the classpath -- but no Spring container, binding chain, or
converter machinery is needed.
{code:groovy}
@Grab('org.grails:grails-datastore-core:9.0.0-M3')
@Grab('org.springframework:spring-core:6.1.14')
import groovy.transform.builder.Builder
import groovy.transform.builder.SimpleStrategy
import org.grails.datastore.mapping.config.ConfigurationBuilder
import org.springframework.core.env.PropertyResolver
import org.springframework.core.env.AbstractPropertyResolver
@Builder(builderStrategy = SimpleStrategy, prefix = '')
class ConnectionSettings {
String host = 'localhost'
}
class RootSettings {
ConnectionSettings connection = new ConnectionSettings()
void setConnection(ConnectionSettings cs) { this.connection = cs }
ConnectionSettings getConnection() { connection }
}
class MapResolver extends AbstractPropertyResolver {
private final Map<String, String> m
MapResolver(Map<String, String> m) { this.m = m }
boolean containsProperty(String key) { m.containsKey(key) }
String getProperty(String key) { m.get(key) }
<T> T getProperty(String key, Class<T> t) { (T) m.get(key) }
String getPropertyOrDefault(String key, String d) { m.getOrDefault(key, d)
}
protected String getPropertyAsRawString(String key) { m.get(key) }
}
class RootBuilder extends ConfigurationBuilder<RootSettings, RootSettings> {
RootBuilder(PropertyResolver pr) { super(pr, 'app') }
protected RootSettings createBuilder() { new RootSettings()
}
protected RootSettings toConfiguration(RootSettings b) { b }
}
def resolver = new MapResolver(['app.connection.host': 'remotehost'])
def cfg = new RootBuilder(resolver).build()
println "Groovy ${GroovySystem.version}: host = ${cfg.connection.host}
(expected 'remotehost')"
assert cfg.connection.host == 'remotehost' :
"ConfigurationBuilder did not recurse into the @Builder(SimpleStrategy)
nested type — " +
"getAnnotation(Builder) returned null"
{code}
h3. Result
|| Version || Output ||
| 4.0.27 | {{host = remotehost}} (passes) |
| 5.0.6 | {{host = localhost}} (AssertionError) |
Same JVM, same Spring jar, same Grails jar. Only the Groovy runtime differs.
h3. Root cause
Commit {{3311ee95f5}} flipped {{Builder.java}} from {{RUNTIME}} to {{SOURCE}}.
GROOVY-10855's stated scope was only the collector/meta-annotations
({{@Canonical}}, {{@CompileDynamic}}, {{@Immutable}}, {{@RecordType}}) -- the
change to {{@Builder}} (and a number of other user-facing class annotations)
was scope-creep, undocumented in release notes, and not covered by tests.
h3. Proposed fix
Revert just {{@Builder}}'s retention to {{RUNTIME}}.
> restore @Builder retention to RUNTIME in 5.0.x
> ----------------------------------------------
>
> Key: GROOVY-12040
> URL: https://issues.apache.org/jira/browse/GROOVY-12040
> Project: Groovy
> Issue Type: Bug
> Reporter: Paul King
> Assignee: Paul King
> Priority: Major
>
> This broke Grails.
> h3. Summary
> {{@groovy.transform.builder.Builder}} was {{@Retention(RUNTIME)}} through
> Groovy 4.0.x. In Groovy 5.0.0 it was changed to {{@Retention(SOURCE)}} in
> [GROOVY-10855|https://issues.apache.org/jira/browse/GROOVY-10855]
> ("normalized annotation metadata"). As a result,
> {{Class.getAnnotation(Builder.class)}} now returns {{null}} at runtime,
> breaking frameworks that reflect on the annotation to detect builder-shaped
> classes (e.g. Grails {{ConfigurationBuilder.buildRecurse}} -- see
> [apache/grails-core#15557|https://github.com/apache/grails-core/pull/15557]
> row 4).
> GROOVY-10855 was initially about the annotation collectors but we bundled in
> some more AST transform annotations that we weren't aware anyone was using at
> runtime. Builder ended up being a case that was being used.
> h3. Minimal Standalone Reproducer
> {code:groovy}
> import groovy.transform.builder.Builder
> import groovy.transform.builder.SimpleStrategy
> @Builder(builderStrategy = SimpleStrategy, prefix = '')
> class ConnectionSettings { String host = 'localhost' }
> // Copied from ConfigurationBuilder.buildRecurse, line 244-245:
> Builder ann = ConnectionSettings.getAnnotation(Builder)
> assert ann != null && ann.builderStrategy() == SimpleStrategy
> {code}
> || Version || {{getAnnotation(Builder)}} ||
> | 3.0.x | non-null (RUNTIME) |
> | 4.0.x | non-null (RUNTIME) |
> | 5.0.x / 6.0.0-alpha-1 | *null* (SOURCE) |
> h3. Reproducer (drives the real {{ConfigurationBuilder}}; no spring-context /
> spring-boot)
> {{ConfigurationBuilder}}'s constructor takes
> {{org.springframework.core.env.PropertyResolver}}, so {{spring-core}} is
> unavoidable on the classpath -- but no Spring container, binding chain, or
> converter machinery is needed.
> {code:groovy}
> @Grab('org.grails:grails-datastore-core:9.0.0-M3')
> @Grab('org.springframework:spring-core:6.1.14')
> import groovy.transform.builder.Builder
> import groovy.transform.builder.SimpleStrategy
> import org.grails.datastore.mapping.config.ConfigurationBuilder
> import org.springframework.core.env.PropertyResolver
> import org.springframework.core.env.AbstractPropertyResolver
> @Builder(builderStrategy = SimpleStrategy, prefix = '')
> class ConnectionSettings {
> String host = 'localhost'
> }
> class RootSettings {
> ConnectionSettings connection = new ConnectionSettings()
> void setConnection(ConnectionSettings cs) { this.connection = cs }
> ConnectionSettings getConnection() { connection }
> }
> class MapResolver extends AbstractPropertyResolver {
> private final Map<String, String> m
> MapResolver(Map<String, String> m) { this.m = m }
> boolean containsProperty(String key) { m.containsKey(key) }
> String getProperty(String key) { m.get(key) }
> <T> T getProperty(String key, Class<T> t) { (T) m.get(key) }
> String getPropertyOrDefault(String key, String d) { m.getOrDefault(key,
> d) }
> protected String getPropertyAsRawString(String key) { m.get(key) }
> }
> class RootBuilder extends ConfigurationBuilder<RootSettings, RootSettings> {
> RootBuilder(PropertyResolver pr) { super(pr, 'app') }
> protected RootSettings createBuilder() { new
> RootSettings() }
> protected RootSettings toConfiguration(RootSettings b) { b }
> }
> def resolver = new MapResolver(['app.connection.host': 'remotehost'])
> def cfg = new RootBuilder(resolver).build()
> println "Groovy ${GroovySystem.version}: host = ${cfg.connection.host}
> (expected 'remotehost')"
> assert cfg.connection.host == 'remotehost' :
> "ConfigurationBuilder did not recurse into the
> @Builder(SimpleStrategy) nested type — " +
> "getAnnotation(Builder) returned null"
> {code}
> h3. Result
> || Version || Output ||
> | 4.0.27 | {{host = remotehost}} (passes) |
> | 5.0.6 | {{host = localhost}} (AssertionError) |
> Same JVM, same Spring jar, same Grails jar. Only the Groovy runtime differs.
> h3. Root cause
> Commit {{3311ee95f5}} flipped {{Builder.java}} from {{RUNTIME}} to
> {{SOURCE}}. GROOVY-10855's stated scope was only the
> collector/meta-annotations ({{@Canonical}}, {{@CompileDynamic}},
> {{@Immutable}}, {{@RecordType}}) -- the change to {{@Builder}} (and a number
> of other user-facing class annotations) was scope-creep, undocumented in
> release notes, and not covered by tests.
> h3. Proposed fix
> Revert just {{@Builder}}'s retention to {{RUNTIME}}.
--
This message was sent by Atlassian Jira
(v8.20.10#820010)