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

Eric Milles edited comment on GROOVY-9510 at 8/5/23 4:55 PM:
-------------------------------------------------------------

Here is a more complete example that handles the 3 annotation types and checks 
for closure containment.  The hover is not so good, but that's a greclipse 
problem that I can remedy.  And I do have some duplication in proposals because 
"method name:'getInstance', ..." and "delegatesTo type:..." both provide 
"getInstance".

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

boolean isInClosure(ClosureExpression closure, ASTNode node) {
  // leverage closure.visit(...) to determine if node is inside
}

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)) {
      property name: 'instance', type: currentType, readOnly: true
      method name: 'getInstance', type: currentType, params: [:]
      property name: 'shared', type: currentType, readOnly: true
      method name: 'getShared', type: currentType, params: [:]
      // must follow method/property contributions that PreconditionContext 
also provides
      delegatesTo type: 
resolver.resolve('org.spockframework.runtime.extension.builtin.PreconditionContext')
      provider = 'Spock framework'
      return
    }
  }
}
{code}

 !screenshot-1.png! 


was (Author: emilles):
Here is a more complete example that handles the 3 annotation types and checks 
for closure containment.  The hover is not so good, but that's a greclipse 
problem that I can remedy.  And I do have some duplication in proposals because 
"method name:'getInstance', ..." and "delegatesTo type:..." both provide 
"getInstance".

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

boolean isInClosure(ClosureExpression closure, ASTNode node) {
  // leverage closure.visit(...) to determine if node is within
}

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)) {
      property name: 'instance', type: currentType, readOnly: true
      method name: 'getInstance', type: currentType, params: [:]
      property name: 'shared', type: currentType, readOnly: true
      method name: 'getShared', type: currentType, params: [:]
      // must follow method/property contributions that PreconditionContext 
also provides
      delegatesTo type: 
resolver.resolve('org.spockframework.runtime.extension.builtin.PreconditionContext')
      provider = 'Spock framework'
      return
    }
  }
}
{code}

 !screenshot-1.png! 

> 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