2

I have an interface "TestInterface" and grails service "TestService" that implements the "TestInterface". But when I test if I have a service that implements the interface like this:

application.serviceClasses?.each { serviceClazz ->

            if(serviceClazz instanceof TestInterface) {
                println "service name => "+ serviceClazz.name;
            }
}

The result is I am not getting anything neither error nor my expectation ( service name => TestService )

I have also tried changing the serviceClazz to serviceClazz.class,serviceClazz.metaClass in the if condition but still not working.

Thank you,

t31321
  • 763
  • 1
  • 7
  • 22

1 Answers1

2

What about:

if (TestInterface.class.isAssignableFrom(serviceClazz)) {
    ... 
}

UPDATE

So I managed to run the actual example using Grails 2.3.11.

class BootStrap {

    def grailsApplication

    def init = { servletContext ->
        grailsApplication.serviceClasses.each { serviceClazz ->
            if (TestInterface.isAssignableFrom(serviceClazz.clazz)) {
                println serviceClazz
            }
        }
    }
    def destroy = {
    }
}

As you can see, the important part is clazz in serviceClazz.clazz.

Hope this helps!

Marcin Świerczyński
  • 2,302
  • 22
  • 32
  • That throws an exception , so I changed it to isAssignableFrom(serviceClazz.class) but still it returns false. – t31321 Nov 15 '14 at 12:44
  • What's the exception? I'm away from computer now. Let me test in a few hours and I'll get back to you. – Marcin Świerczyński Nov 15 '14 at 12:45
  • Error groovy.lang.MissingMethodException: No signature of method: static com.apposit.mmp.grails.client.event.TestInterface.isAssignableFrom() is applicable for argument types: (org.codehaus.groovy.grails.commons.DefaultGrailsServiceClass) values: [Artefact > Topic] | – t31321 Nov 15 '14 at 12:54
  • But you can mark it as correct answer, right? I'd appreciate it :) – Marcin Świerczyński Nov 15 '14 at 13:39