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

Eric Milles edited comment on GROOVY-9510 at 8/7/23 12:09 PM:
--------------------------------------------------------------

One possibility for handling "getInstance()" and "getShared()" is to declare a 
type parameter.
{code:java}
class PreconditionContext<S extends Specification> {
  S getInstance() { ... }
  S getShared() { ... }
}
{code}

Then when indicating the type in DSLD, it would be given with a type argument:
{code:groovy}
contribute(...) {
  for (AnnotationNode pre : conditions) {
    Expression value= pre.getMember('value')
    if (value instanceof ClosureExpression && isInClosure(value, currentNode)) {
      delegateType = 
resolver.resolve("org.spockframework.runtime.extension.builtin.PreconditionContext<$currentType.name>")
      ...
    }
  }
}
{code}

Here is another example that is in use:
{code:groovy}
contribute(currentType(annotatedBy(Sortable))) {
    provider = '{@link groovy.transform.Sortable Sortable} AST transform'
    delegatesTo type: "java.lang.Comparable<${currentType.name}>"
{code}


Note: I also tried "Inferencing Suggestions" which lets a user specify the type 
of a variable like "instance".  The drawback with that feature is you get one 
type per project.  So not a good option.

I also tried just checking for "PreconditionContext" or "RetryConditionContext" 
and changing the return type.  This does work, but requires each IDE to make a 
change and cannot extend to a new situation.


was (Author: emilles):
One possibility for handling "getInstance()" and "getShared()" is to declare a 
type parameter.
{code:java}
class PreconditionContext<S extends Specification> {
  S getInstance() { ... }
  S getShared() { ... }
}
{code}

Then when indicating the type in DSLD, it would be given with a type argument:
{code:groovy}
contribute(...) {
  for (AnnotationNode pre : conditions) {
    Expression value= pre.getMember('value')
    if (value instanceof ClosureExpression && isInClosure(value, currentNode)) {
      delegateType = 
resolver.resolve("org.spockframework.runtime.extension.builtin.PreconditionContext<$currentType.name>")
      ...
    }
  }
}
{code}

Note: I also tried "Inferencing Suggestions" which lets a user specify the type 
of a variable like "instance".  The drawback with that feature is you get one 
type per project.  So not a good option.

I also tried just checking for "PreconditionContext" or "RetryConditionContext" 
and changing the return type.  This does work, but requires each IDE to make a 
change and cannot extend to a new situation.

> 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