2

I am trying to create a closure matcher for Fest with Groovy (2.1.6) like this:

def matcherLabel = [ isMatching: { JLabel label -> /* do something */ } ] as GenericTypeMatcher<JLabel>

GenericTypeMatcher is an abstract class with one method only to implement (isMatching (T t))

but I get this error:

org.codehaus.groovy.runtime.typehandling.GroovyCastException: Error casting map to org.fest.swing.core.GenericTypeMatcher, Reason: null
at org.codehaus.groovy.runtime.DefaultGroovyMethods.asType(DefaultGroovyMethods.java:7562)

Is it possible what I am trying to do?

Randomize
  • 8,651
  • 18
  • 78
  • 133
  • I'd say its possible as I've just tried and it worked (creating my own GenericTypeMatcher class though). Are you sure the error is exactly on that line? – Alfergon Aug 26 '13 at 20:43

1 Answers1

9

Your problem is that the GenericTypeMatcher class doesn't have a default, zero-parameter constructor. Cast the map to an interface like ComponentMatcher instead. If you can't use an interface, another alternative is to subclass GenericTypeMatcher and provide a zero-parameter constructor.

ataylor
  • 64,891
  • 24
  • 161
  • 189
  • Thanks for reply. Yes, it has a constructor parameter. Googling around looks is not possible having coercion with constructor parameters in Groovy. So I will use normal classes. – Randomize Aug 27 '13 at 08:53