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

Eric Milles edited comment on GROOVY-9510 at 8/15/23 8:23 PM:
--------------------------------------------------------------

I found that the delegate needs to be set only once.  So the contributions can 
be simplified a bit:
{code:groovy}
import org.codehaus.groovy.ast.*
import org.codehaus.groovy.ast.expr.*

def hasPrecondition = { annotatedBy(name('spock.lang.Requires') | 
name('spock.lang.IgnoreIf') | name('spock.lang.PendingFeatureIf')) }

contribute(inClosure() & isThisType() & 
enclosingClass(subType('spock.lang.Specification'))
 & (enclosingClass(conditions: hasPrecondition()) | enclosingMethod(conditions: 
hasPrecondition()))) {
  for (AnnotationNode pre : conditions) {
    def condition = pre.getMember('value')
    if (condition.getCode().is(currentNode)) {
      delegateType = 
resolver.resolve('org.spockframework.runtime.extension.builtin.PreconditionContext')
      return
    }
  }
}

contribute(inClosure() & isThisType() & 
enclosingClass(subType('spock.lang.Specification'))
 & (enclosingClass(annotations: annotatedBy('spock.lang.Retry')) | 
enclosingMethod(annotations: annotatedBy('spock.lang.Retry')))) {
  for (AnnotationNode retry : annotations) {
    def condition = retry.getMember('condition')
    if (condition.getCode().is(currentNode)) {
      delegateType = 
resolver.resolve('org.spockframework.runtime.extension.builtin.RetryConditionContext')
      return
    }
  }
}
{code}

In this case, {{currentType}} is {{Class<TheSpecClass>}} in recent tooling 
snapshots.  At the moment, {{currentType}} is not used.  But if it were (as we 
discussed above) we'd need to check for {{Class<TheSpecClass>}} and 
{{TheSpecClass}}.  Otherwise there is a cross-compatible way to capture from 
the {{enclosingClass(...)}} pointcut.

Could you add this to your "spk.dsld"?  And possibly rename the file to 
"spock_tests.dsld"?


was (Author: emilles):
I found that the delegate needs to be set only once.  So the contributions can 
be simplified a bit:
{code:groovy}
def hasPrecondition = { annotatedBy(name('spock.lang.Requires') | 
name('spock.lang.IgnoreIf') | name('spock.lang.PendingFeatureIf')) }

contribute(inClosure() & isThisType() & 
enclosingClass(subType('spock.lang.Specification'))
 & (enclosingClass(conditions: hasPrecondition()) | enclosingMethod(conditions: 
hasPrecondition()))) {
  for (AnnotationNode pre : conditions) {
    def condition = pre.getMember('value')
    if (condition.getCode().is(currentNode)) {
      delegateType = 
resolver.resolve('org.spockframework.runtime.extension.builtin.PreconditionContext')
      provider = 'Spock framework'
      return
    }
  }
}

contribute(inClosure() & isThisType() & 
enclosingClass(subType('spock.lang.Specification'))
 & (enclosingClass(annotations: annotatedBy('spock.lang.Retry')) | 
enclosingMethod(annotations: annotatedBy('spock.lang.Retry')))) {
  for (AnnotationNode retry : annotations) {
    def condition = retry.getMember('condition')
    if (condition.getCode().is(currentNode)) {
      delegateType = 
resolver.resolve('org.spockframework.runtime.extension.builtin.RetryConditionContext')
      provider = 'Spock framework'
      return
    }
  }
}
{code}

In this case, {{currentType}} is {{Class<TheSpecClass>}} in recent tooling 
snapshots.  At the moment, {{currentType}} is not used.  But if it were (as we 
discussed above) we'd need to check for {{Class<TheSpecClass>}} and 
{{TheSpecClass}}.  Otherwise there is a cross-compatible way to capture from 
the {{enclosingClass(...)}} pointcut.

Could you add this to your "spk.dsld"?  And possibly rename the file to 
"spock_tests.dsld"?

> 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