[ 
https://issues.apache.org/jira/browse/GROOVY-9510?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17752432#comment-17752432
 ] 

Eric Milles edited comment on GROOVY-9510 at 8/9/23 3:27 PM:
-------------------------------------------------------------

In addition to setting the delegate to {{PreconditionContext}}, spock also 
calls the closure with a {{PreconditionContext}} argument.  So I am able to do 
this:
{code:groovy}
import org.spockframework.runtime.extension.builtin.PreconditionContext
import spock.lang.*

@Requires({ PreconditionContext pc -> pc.os.windows })
class Groovy9510 extends Specification {
  String string
  def sample() {
   expect:
    println 'works'
  }
}
{code}

Update {{@Retry}} is not called with a parameter.

This is the calling code from {{ConditionalExtension}}.  I think owner should 
be passed as the 3rd argument to rehydrate so {{getOwner()}} and 
{{getThisObject()}} return the same thing.  And the closure could be invoked 
with "call()" instead of "call(context)".
{code:java}
  private static Object evaluateCondition(Closure condition,
                                          Object sharedInstance,
                                          Object instance,
                                          Map<String, Object> dataVariables, 
Object owner) {
    PreconditionContext context = new PreconditionContext(sharedInstance, 
instance, dataVariables);
    condition = condition.rehydrate(context, owner, null);
    condition.setResolveStrategy(Closure.DELEGATE_FIRST);

    try {
      return condition.call(context);
    } catch (Exception e) {
      throw new ExtensionException("Failed to evaluate condition", e);
    }
  }
{code}


was (Author: emilles):
In addition to setting the delegate to {{PreconditionContext}}, spock also 
calls the closure with a {{PreconditionContext}} argument.  So I am able to do 
this:
{code:groovy}
import org.spockframework.runtime.extension.builtin.PreconditionContext
import spock.lang.*

@Requires({ PreconditionContext pc -> pc.os.windows })
class Groovy9510 extends Specification {
  String string
  def sample() {
   expect:
    println 'works'
  }
}
{code}

Update {{@Retry}} is not called with a parameter.

> 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, screenshot-1.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)

Reply via email to