[ 
https://issues.apache.org/jira/browse/GROOVY-11884?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Paul King updated GROOVY-11884:
-------------------------------
    Description: 
Groovy lets you have annotations like @Grab on various nodes and various 
statements including imports. Targeting imports is not possible in Java, but 
this is part of Groovy's stance of providing more flexibility.

Groovy also does validation that annotations don't appear in incorrect places. 
Currently there is a gap in our validation - we ignore things like checking 
what things can be on imports, e.g. the following is currently valid but 
totally ignored:
{code}
@Deprecated
import java.lang.String
{code}

As well as @Grab, @Newify and @BaseScript can also appear on imports.
With the new incubating AST transforms on loops, @Invariant can now appear on 
loops.

I propose we create some additional classes to capture these extended 
annotation targets and close the gaps in validation. Something like:

{code}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface ExtendedTarget {
    ExtendedElementType[] value();
}
{code}

And:

{code}
public enum ExtendedElementType {
    /** Import statement. */
    IMPORT,
    /** Loop statement ({@code for}, {@code while}, {@code do-while}). */
    LOOP
}
{code}

And then things like @BaseScript would change from this:

{code}
@Retention(RetentionPolicy.SOURCE)
@Target({ElementType.LOCAL_VARIABLE, ElementType.PACKAGE, ElementType.TYPE /*, 
ElementType.IMPORT*/})
@GroovyASTTransformationClass("org.codehaus.groovy.transform.BaseScriptASTTransformation")
public @interface BaseScript {
    Class value() default Script.class;
}
{code}

to this:

{code}
@Retention(RetentionPolicy.SOURCE)
@Target({ElementType.LOCAL_VARIABLE, ElementType.PACKAGE, ElementType.TYPE})
@ExtendedTarget(ExtendedElementType.IMPORT)
@GroovyASTTransformationClass("org.codehaus.groovy.transform.BaseScriptASTTransformation")
public @interface BaseScript {
    Class value() default Script.class;
}
{code}

This would be a breaking change for folks relying on Groovy's leniency, e.g. 
maybe they had a convention of putting @Deprecated on an import they were 
hoping to get rid of. In such cases folks would need to now make and use an 
annotation with @ExtendedTarget.

  was:
Groovy lets you have annotations like @Grab on various nodes and various 
statements including imports. Targeting imports is not possible in Java, but 
this is part of Groovy's stance of providing more flexibility.

Groovy also does validation that annotations don't appear in incorrect places. 
Currently there is a gap in our validation - we ignore things like checking 
what things can be on imports, e.g. the following is currently valid but 
totally ignored:
{code}
@Deprecated
import java.lang.String
{code}

As well as @Grab, @Newify and @BaseScript can also appear on imports.
With the new incubating AST transforms on loops, @Invariant can now appear on 
loops.

I propose we create some additional classes to capture these extended 
annotation targets and close the gaps in validation. Something like:

{code}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface ExtendedTarget {
    ExtendedElementType[] value();
}
{code}

And:

{code}
public enum ExtendedElementType {
    /** Import statement. */
    IMPORT,
    /** Loop statement ({@code for}, {@code while}, {@code do-while}). */
    LOOP
}
{code}

And then things like @BaseScript would change from this:

{code}
@Retention(RetentionPolicy.SOURCE)
@Target({ElementType.LOCAL_VARIABLE, ElementType.PACKAGE, ElementType.TYPE /*, 
ElementType.IMPORT*/})
@GroovyASTTransformationClass("org.codehaus.groovy.transform.BaseScriptASTTransformation")
public @interface BaseScript {
    Class value() default Script.class;
}
{code}

to this:

{code}
@Retention(RetentionPolicy.SOURCE)
@Target({ElementType.LOCAL_VARIABLE, ElementType.PACKAGE, ElementType.TYPE})
@ExtendedTarget(ExtendedElementType.IMPORT)
@GroovyASTTransformationClass("org.codehaus.groovy.transform.BaseScriptASTTransformation")
public @interface BaseScript {
    Class value() default Script.class;
}
{code}


> Close gaps in Groovy annotation validation
> ------------------------------------------
>
>                 Key: GROOVY-11884
>                 URL: https://issues.apache.org/jira/browse/GROOVY-11884
>             Project: Groovy
>          Issue Type: Improvement
>            Reporter: Paul King
>            Priority: Major
>              Labels: breaking
>
> Groovy lets you have annotations like @Grab on various nodes and various 
> statements including imports. Targeting imports is not possible in Java, but 
> this is part of Groovy's stance of providing more flexibility.
> Groovy also does validation that annotations don't appear in incorrect 
> places. Currently there is a gap in our validation - we ignore things like 
> checking what things can be on imports, e.g. the following is currently valid 
> but totally ignored:
> {code}
> @Deprecated
> import java.lang.String
> {code}
> As well as @Grab, @Newify and @BaseScript can also appear on imports.
> With the new incubating AST transforms on loops, @Invariant can now appear on 
> loops.
> I propose we create some additional classes to capture these extended 
> annotation targets and close the gaps in validation. Something like:
> {code}
> @Retention(RetentionPolicy.RUNTIME)
> @Target(ElementType.ANNOTATION_TYPE)
> public @interface ExtendedTarget {
>     ExtendedElementType[] value();
> }
> {code}
> And:
> {code}
> public enum ExtendedElementType {
>     /** Import statement. */
>     IMPORT,
>     /** Loop statement ({@code for}, {@code while}, {@code do-while}). */
>     LOOP
> }
> {code}
> And then things like @BaseScript would change from this:
> {code}
> @Retention(RetentionPolicy.SOURCE)
> @Target({ElementType.LOCAL_VARIABLE, ElementType.PACKAGE, ElementType.TYPE 
> /*, ElementType.IMPORT*/})
> @GroovyASTTransformationClass("org.codehaus.groovy.transform.BaseScriptASTTransformation")
> public @interface BaseScript {
>     Class value() default Script.class;
> }
> {code}
> to this:
> {code}
> @Retention(RetentionPolicy.SOURCE)
> @Target({ElementType.LOCAL_VARIABLE, ElementType.PACKAGE, ElementType.TYPE})
> @ExtendedTarget(ExtendedElementType.IMPORT)
> @GroovyASTTransformationClass("org.codehaus.groovy.transform.BaseScriptASTTransformation")
> public @interface BaseScript {
>     Class value() default Script.class;
> }
> {code}
> This would be a breaking change for folks relying on Groovy's leniency, e.g. 
> maybe they had a convention of putting @Deprecated on an import they were 
> hoping to get rid of. In such cases folks would need to now make and use an 
> annotation with @ExtendedTarget.



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to