0
votes

I'm trying to call a method on a grails service from a controller, but it looks like execution is just skipping the method call. I've tried debugging the application with a breakpoint inside the method but it is never hit.

My service (generated with grails create-service) is:

class FormatterService {
    static transactional = false

    def formatList (List<Host>, String fmt) {
        OutputObject somePOGO = new OutputObject();
        (snip)
        return somePOGO
    }
}

Then on my controller I have:

class HostController {
    def formatterService

    def getHostsByLabels = {
        (snip)
        OutputObject o = formatterService.formatList(someHosts,params.format)
        (snip)
    }
}

When the formatterService.formatList method should be called in the controller, execution simply skips to the next line, no output is printed to the console and breakpoints within the method are not hit. The OutputObject o reference is null afterward.

What is wrong here? It could be a really basic mistake from my part, but I just can't put my finger on it...

2
Perhaps there's some code in the (snip)ped sections causing this? What you've posted looks correct. - Rob Hruska

2 Answers

1
votes

To Me it seems a MetaProgramming Disaster..

Well there are 3 Tests to Debug:

_1) first try to do

    println formatterService
    println formatterService.getClass()

just to check if its injected bean is the desired one, some plugins sometimes inject beans which overrides the default.

_2) Make sure that the method with a name "formatList" is not injected in your services through metaprogramming by any plugin or core code.

How to test this is simple: Just change the name of the method to some Unrealistic One, ex: "formatListabcdewdw" and then call that one. If it works then its method overriden issue.

and if you are more enthusiastic you can see the metaMethods by

println formatterService.metaClass.methods

_3) just try to do "params.format as String" as the last argument in the method call.\

.

Hope any of these helps, please Do let me know of the findings, i am curious to know.. :)

0
votes

I found the issue. It has to do with the method signature.

Printing out the thrown exception's message, it says:

No signature of method: hms.FormatterService.formatList() is applicable for argument types: (java.util.TreeSet, java.lang.String) values: (...) Possible solutions: formatList(java.util.List, java.lang.String)

So, a rookie mistake (wanting to pass a TreeSet for a List) aided by weak typing in Groovy... :P

I've changed the method signature to

def formatList ( items, String fmt) {

and call it as

def activeHosts = ...
OutputObject o = formatterService.formatList(activeHosts, params.format as String)

and now it works.