[
https://issues.apache.org/jira/browse/GROOVY-12720?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18140064#comment-18140064
]
ASF GitHub Bot commented on GROOVY-12720:
-----------------------------------------
codeconsole opened a new pull request, #3254:
URL: https://github.com/apache/groovy/pull/3254
https://issues.apache.org/jira/browse/GROOVY-12720
A script that declares its own no-argument `run()` keeps it, and
`ModuleNode.createStatementsClass` copies the method's annotations to the
script class so that, for example, `@CompileStatic` on `run()` applies to the
whole class. `@Override` was copied too, and the class then failed to compile
with "Annotation @java.lang.Override is not allowed on element TYPE", although
the method does override `Script#run()`:
```groovy
@Override
Object run() {
'done'
}
```
The copy now leaves `@Override` out; it stays on the method. The other
annotations move to the class as before.
Tests: `bugs.Groovy12720` covers the script above compiling and running,
`@CompileStatic` beside `@Override` still reaching the class, and `@Override`
on another script method still being rejected. Also run locally:
`groovy.lang.GroovyShellTest`, `groovy.util.MiscScriptTest`,
`org.codehaus.groovy.ast.*` and `gls.annotations.*`, 427 tests in all, passing.
> Script: @Override on a script's own run() method is copied to the script
> class and rejected ("not allowed on element TYPE")
> ---------------------------------------------------------------------------------------------------------------------------
>
> Key: GROOVY-12720
> URL: https://issues.apache.org/jira/browse/GROOVY-12720
> Project: Groovy
> Issue Type: Bug
> Affects Versions: 6.0.0
> Reporter: Scott Murphy Heiberg
> Priority: Major
>
> A script may declare its own no-argument {{run()}}:
> {{ModuleNode.createStatementsClass}} keeps it instead of generating one, and
> copies its annotations to the script class so that, for example,
> {{@CompileStatic}} on {{run()}} applies to the whole class. Every annotation
> is copied, including {{@Override}}, whose only target is METHOD:
> {code:groovy}
> class PriceRules { int quotes = 0 }
> @Override
> Object run() {
> println new PriceRules().quotes
> null
> }
> {code}
> Expected: compiles and runs; the method does override
> {{groovy.lang.Script#run()}}. Actual, on 6.0.0 and master:
> {noformat}
> script.groovy: 5: Annotation @java.lang.Override is not allowed on element
> TYPE
> {noformat}
> The same script with {{@Deprecated}} compiles, and {{@Override}} on any other
> script method is checked as usual ("does not override method from its
> superclass").
> Proposed fix: when the method's annotations are copied to the class, leave
> {{@Override}} out; it stays on the method, where it is correct. The other
> annotations keep moving to the class as before.
> Found while scoring Groovy 6.0.0 code written by a language model: told that
> the file is compiled with static type checking, it wrote {{@Override
> @TypeChecked Object run() { ... }}} and was given an error about an element
> it had not annotated.
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)