[
https://issues.apache.org/jira/browse/GROOVY-9510?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17751171#comment-17751171
]
Leonard Brünings edited comment on GROOVY-9510 at 8/4/23 4:48 PM:
------------------------------------------------------------------
After giving it yet another try, much trial, and error and reading through
IntelliJ's source I managed to get this:
{code:groovy}
def conditionalExtensions = ['spock.lang.Requires', 'spock.lang.IgnoreIf',
'spock.lang.PendingFeatureIf'].collect { annot -> context(ctype:
"spock.lang.Specification", scope: closureScope(annotationName: annot))}
contributor(conditionalExtensions, {
delegatesTo(findClass('org.spockframework.runtime.extension.builtin.PreconditionContext'))
})
{code}
I guess, this works fine, as long as the annotation only has a single closure.
I assume your example would be the DSLD variant?
Yes, I think type use would be more elegant than the alternative of allowing it
for methods.
{code:java}
public @interface Requires {
@DelegatesTo(PreconditionContext.class) Class<? extends Closure> value();
}
{code}
bq. {{DelegatesTo}} does support a method target. I'm not sure if the intent is
for methods that return a closure. But you should be able to mark up your
value() method. Do you get a compiler error if you do this?
I don't have Eclipse set up, so I haven't tried it yet.
Do you know if it is possible to give the
{{PreconditionContext.getInstance()}}/ {{PreconditionContext.getShared()}} the
correct type information from the declaring Specification class? Currently,
they only return {{Object}}
was (Author: leonard84):
After giving it yet another try, much trial, and error and reading through
IntelliJ's source I managed to get this:
{code:groovy}
def conditionalExtensions = ['spock.lang.Requires', 'spock.lang.IgnoreIf',
'spock.lang.PendingFeatureIf'].collect { annot -> context(ctype:
"spock.lang.Specification", scope: closureScope(annotationName: annot))}
contributor(conditionalExtensions, {
delegatesTo(findClass('org.spockframework.runtime.extension.builtin.PreconditionContext'))
})
{code}
I guess, this works fine, as long as the annotation only has a single closure.
I assume your example would be the DSLD variant?
Yes, I think type use would be more elegant than the alternative of allowing it
for methods.
{code:java}
public @interface Requires {
@DelegatesTo(PreconditionContext.class) Class<? extends Closure> value();
}
{code}
> DelegatesTo and similar for annotation member methods
> -----------------------------------------------------
>
> Key: GROOVY-9510
> URL: https://issues.apache.org/jira/browse/GROOVY-9510
> Project: Groovy
> Issue Type: Bug
> Reporter: Björn Kautler
> Priority: Major
> Labels: annotations
> Attachments: image-2023-08-04-11-04-29-834.png
>
>
> If you have an annotation with a closure field like for example this in Spock:
> {code:java}
> @Retention(RetentionPolicy.RUNTIME)
> @Target({ElementType.TYPE, ElementType.METHOD})
> @ExtensionAnnotation(RequiresExtension.class)
> public @interface Requires {
> Class<? extends Closure> value();
> }{code}
> you can use it like
> {code:java}
> @Requires({ os.windows }){code}
> But I see no way to add IDE-hints for that closure like
> {{@DelegatesTo(PreconditionContext.class)}} as it is restriced to parameters.
> It would be nice if you could also annotate annotation member methods (and if
> IDEs would then support that too).
--
This message was sent by Atlassian Jira
(v8.20.10#820010)