[
https://issues.apache.org/jira/browse/GROOVY-12791?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18147971#comment-18147971
]
ASF GitHub Bot commented on GROOVY-12791:
-----------------------------------------
paulk-asert opened a new pull request, #3314:
URL: https://github.com/apache/groovy/pull/3314
https://issues.apache.org/jira/browse/GROOVY-12791
A script file containing only method definitions is compiled to a
`groovy.lang.Script` subclass, with a `Binding` constructor, an empty `run()`
and a generated `main`. Writing a `main` method gives a plain class instead,
but it is package-private and adds a `main` method, which does not suit
frameworks that expect a class with exactly one method.
The new incubating `@NoScript` compiles such a file as a plain class:
```groovy
import groovy.transform.CompileStatic
import groovy.transform.stc.POJO
@NoScript @CompileStatic @POJO
import groovy.transform.NoScript
def foo() { println new Date() }
```
gives `public class Foo { public Foo(); public Object foo(); }`, with no
`GroovyObject`.
- The class is public and extends `Object`. It has no `run()`, no generated
`main` and no `Binding` constructor. Top-level declarations become fields.
- A class with only static methods and no declarations gets a private no-arg
constructor.
- `@NoScript` has the same targets as `@BaseScript`: package, import,
top-level declaration and type. Annotations next to it on the package
declaration or an import are moved to the class, so `@CompileStatic` and
`@POJO` apply to the whole class.
- A `@NoScript` file with statements other than declarations is a compile
error. So is a `@NoScript` that has no effect, for example on a class, or in a
file with no script code.
`ModuleNode.createStatementsClass()` builds the script class before any
transform runs, so it recognizes `@NoScript` by name, as it already does for
`@Override` on `main`: through its fully-qualified name, a single-type import
(aliased or not), or `import groovy.transform.*`. `NoScriptASTTransformation`
only reports a `@NoScript` that `ModuleNode` did not act on.
Open questions:
- Whether the type target should stay. It only matches `@BaseScript`, and is
reported as an error on a class.
- Annotations on a `@NoScript` top-level declaration are not copied to the
field, the same as on the instance-`main` path.
- A `@NoScript` file without `main` cannot be run with `groovy Foo.groovy`
or `GroovyShell.evaluate`.
The declaration loop shared with the instance-`main` path drops a
multiple-assignment declaration whose right-hand side is not a list literal,
together with the declarations after it. That is a separate, existing bug, to
be fixed in its own PR.
`NoScriptTransformTest` (14 tests), the inline Javadoc example,
`BaseScriptTransformTest`, `Groovy12720`, `Groovy3749`,
`ScriptRunAnnotationsStubTest`, and `:checkstyleGate :codenarcGate :rat` pass
locally on JDK 17.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
> @NoScript: compile a script file containing only methods and declarations as
> a plain class
> ------------------------------------------------------------------------------------------
>
> Key: GROOVY-12791
> URL: https://issues.apache.org/jira/browse/GROOVY-12791
> Project: Groovy
> Issue Type: New Feature
> Components: Compiler
> Reporter: Paul King
> Priority: Major
>
> A script file containing only method definitions is compiled to a
> {{groovy.lang.Script}} subclass, with a {{Binding}} constructor, an empty
> {{run()}} and a {{main}}:
> {code:groovy}
> // Foo.groovy
> def foo() { println new Date() }
> {code}
> {noformat}
> public class Foo extends groovy.lang.Script {
> public Foo();
> public Foo(groovy.lang.Binding);
> public static void main(java.lang.String...);
> public java.lang.Object run();
> public java.lang.Object foo();
> }
> {noformat}
> Writing a {{main}} method instead produces a plain class (GROOVY-11118), but
> it is package-private and adds a {{main}} method. That does not suit
> frameworks that expect a class with exactly one method, such as some
> dependency injection frameworks.
> Proposal: a {{@groovy.transform.NoScript}} annotation (incubating, source
> retention) with the same targets as {{@BaseScript}}: package, import,
> top-level declaration and type. It compiles the file as if it were:
> {code:groovy}
> class Foo {
> def foo() { println new Date() }
> }
> {code}
> * The class is public, extends {{Object}}, and has no {{run()}}, no generated
> {{main}} and no {{Binding}} constructor.
> * Top-level declarations become fields.
> * When every method is static and there are no declarations, the class gets a
> private no-arg constructor.
> * Other annotations next to {{@NoScript}} on the package declaration or an
> import are moved to the class, so {{@CompileStatic}} and {{@POJO}} can apply
> to the whole class:
> {code:groovy}
> import groovy.transform.CompileStatic
> import groovy.transform.stc.POJO
> @NoScript @CompileStatic @POJO
> import groovy.transform.NoScript
> def foo() { println new Date() }
> {code}
> * A {{@NoScript}} file with statements other than declarations is a compile
> error. So is a {{@NoScript}} that has no effect, for example in a file
> containing only classes.
> Since {{ModuleNode.createStatementsClass()}} runs before any transform, the
> annotation is recognised by name there, as {{@Override}} already is for
> {{main}}.
> Open questions:
> * Whether the type target should stay. It only matches {{@BaseScript}} and is
> reported as an error on a class.
> * Annotations on a {{@NoScript}} top-level declaration are not copied to the
> field, the same as on the instance-main path.
> * A {{@NoScript}} file without {{main}} cannot be run with {{groovy
> Foo.groovy}} or {{GroovyShell.evaluate}}.
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)