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

Eric Milles edited comment on GROOVY-9510 at 8/7/23 11:49 AM:
--------------------------------------------------------------

Here is what I have at the moment.  I worked out the hover display.  And I am 
going to check on some other ways to handle "getInstance()" and "getShared()".

{code:groovy}
import org.codehaus.groovy.ast.*
import org.codehaus.groovy.ast.expr.*

boolean isInClosure(ClosureExpression closure, ASTNode node) {
  final boolean[] result = new boolean[1]

  closure.visit(new CodeVisitorSupport() {
    @Override
    void visitConstantExpression(ConstantExpression expression) {
      if (expression == node) result[0] = true
    }
    @Override
    void visitVariableExpression(VariableExpression expression) {
      if (expression == node) result[0] = true
    }
  })

  return result[0]
}

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) {
    Expression value= pre.getMember('value')
    if (value instanceof ClosureExpression && isInClosure(value, currentNode)) {
      setDelegateType 
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) {
    Expression cond = retry.getMember('condition')
    if (cond instanceof ClosureExpression && isInClosure(cond, currentNode)) {
      setDelegateType 
resolver.resolve('org.spockframework.runtime.extension.builtin.RetryConditionContext')
      provider = 'Spock framework'
      return
    }
  }
}
{code}


was (Author: emilles):
Here is what I have at the moment.  I worked out the hover display.  And I am 
going to check on some other ways to handle "getInstance()" and "getShared()".

{code:groovy}
import org.codehaus.groovy.ast.*
import org.codehaus.groovy.ast.expr.*

boolean isInClosure(ClosureExpression closure, ASTNode node) {
  final boolean[] result = new boolean[1]

  closure.visit(new CodeVisitorSupport() {
    @Override
    void visitConstantExpression(ConstantExpression expression) {
      if (expression == node) result[0] = true
    }
    @Override
    void visitVariableExpression(VariableExpression expression) {
      if (expression == node) result[0] = true
    }
  })

  return result[0]
}

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) {
    Expression value= pre.getMember('value')
    if (value instanceof ClosureExpression && isInClosure(value, currentNode)) {
      delegatesTo type: 
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) {
    Expression cond = retry.getMember('condition')
    if (cond instanceof ClosureExpression && isInClosure(cond, currentNode)) {
      delegatesTo type: 
resolver.resolve('org.spockframework.runtime.extension.builtin.RetryConditionContext')
      provider = 'Spock framework'
      return
    }
  }
}
{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, 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