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)
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to