You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@groovy.apache.org by "Winnebeck, Jason" <Ja...@windstream.com> on 2016/11/22 16:09:57 UTC

ClosureSignatureHint for class generic type?

Suppose I have:

class Blah<T> { void doit(Closure<R> c) }

Used so:


Blah<String> b = new Blah<>("abc")
b.doit {it.trim()}

Is there a ClosureSignatureHint that specifies this?

This method from DGM is what I'd want, except doit is an instance method and not a static one, so I can't use "FirstParam"
public static <T> Collection<T> each(Collection<T> self, @ClosureParams(FirstParam.FirstGenericType.class) Closure closure)

I even tried FromString, but type checker still thinks the closure takes object.

void doit(@ClosureParams(value=FromString.class, options=["T"]) Closure<?> c)

[Static type checking] - Cannot find matching method java.lang.Object#trim(). Please check if the declared type is right and if the method exists.

I even tried my own hint class:

public class GenericClassType extends SingleSignatureClosureHint {
 public ClassNode[] getParameterTypes(MethodNode node, String[] options, SourceUnit sourceUnit, CompilationUnit unit, ASTNode usage) {
  return new ClassNode[] { pickGenericType(node.getDeclaringClass(), 0) };
 }
}

In GenericClassType hint it finds "T -> java.lang.Object". It doesn't seem to know about the Blah instance.

What's weird is even if I change doit to take a java.util.function.Consumer<T>, it still doesn't work.

Jason

This email message and any attachments are for the sole use of the intended recipient(s). Any unauthorized review, use, disclosure or distribution is prohibited. If you are not the intended recipient, please contact the sender by reply email and destroy all copies of the original message and any attachments.

RE: ClosureSignatureHint for class generic type?

Posted by "Winnebeck, Jason" <Ja...@windstream.com>.
I never received a response from this, but my DSL changed slightly such that the closure is called with the receiving object rather than the generic parameter of the receiving object, and that template parameter is picked up properly, and in that case I can have a getter methods that return type T that the static compiler can pick up.

However, I noticed that there is no closure params hint that specifies this! So I had to make my own, if this really doesn't exist in Groovy today, I can make a PR someday with it (but on what branch?):

public class ThisParam extends SingleSignatureClosureHint {
@Override
public ClassNode[] getParameterTypes(MethodNode node, String[] options, SourceUnit sourceUnit, CompilationUnit unit, ASTNode usage) {
  return new ClassNode[] { node.getDeclaringClass() };
}
}

Jason

From: Winnebeck, Jason [mailto:Jason.Winnebeck@windstream.com]
Sent: Tuesday, November 22, 2016 11:10 AM
To: users@groovy.apache.org
Subject: [External Source] ClosureSignatureHint for class generic type?

Suppose I have:

class Blah<T> { void doit(Closure<R> c) }

Used so:


Blah<String> b = new Blah<>("abc")
b.doit {it.trim()}

Is there a ClosureSignatureHint that specifies this?

This method from DGM is what I'd want, except doit is an instance method and not a static one, so I can't use "FirstParam"
public static <T> Collection<T> each(Collection<T> self, @ClosureParams(FirstParam.FirstGenericType.class) Closure closure)

I even tried FromString, but type checker still thinks the closure takes object.

void doit(@ClosureParams(value=FromString.class, options=["T"]) Closure<?> c)

[Static type checking] - Cannot find matching method java.lang.Object#trim(). Please check if the declared type is right and if the method exists.

I even tried my own hint class:

public class GenericClassType extends SingleSignatureClosureHint {
 public ClassNode[] getParameterTypes(MethodNode node, String[] options, SourceUnit sourceUnit, CompilationUnit unit, ASTNode usage) {
  return new ClassNode[] { pickGenericType(node.getDeclaringClass(), 0) };
 }
}

In GenericClassType hint it finds "T -> java.lang.Object". It doesn't seem to know about the Blah instance.

What's weird is even if I change doit to take a java.util.function.Consumer<T>, it still doesn't work.

Jason
This email message and any attachments are for the sole use of the intended recipient(s). Any unauthorized review, use, disclosure or distribution is prohibited. If you are not the intended recipient, please contact the sender by reply email and destroy all copies of the original message and any attachments.