This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin for Android App Development and have Peter Sommerhoff on-line!
See this thread for details.
Win a copy of Kotlin for Android App Development this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

MissingMethodException when trying to call a method dynamically with a String for the methodName  RSS feed

 
ranger
Posts: 17344
11
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you can use

"$methodName"(params)
or

"$methodName" params

And methodName is a string with the real method name.

This works in Groovy, but not everywhere. I seem to have the most problem with this in a Groovy script as opposed to a Groovy class.


(Command pattern)
So in my script I receive some JSON data (the command) and use one of its properties for the String with the method name. If I could call the method dynamically means I don't need to write a switch statement to check the value of that property to determine which method to call. So instead I name the method the same as a value in the property.

In the following code I pass json of {"action": "listOfCountries"}


In the script I have




As you can see the closure/method has the same name as in the json. When I call it I get the MissingMethodException thrown. But if I replace the call to "$action" to listOfCountries it calls that method fine.

I can't find a solution.

I wonder can I do "$action".asClosure().call(data, socket) or something like that, that was off the top of my head, so the method might not be called call.

Thanks

Mark
 
Mark Spritzler
ranger
Posts: 17344
11
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry looks like asClosure is just a method in the vertx project I am using and not something that is a part of the Groovy language.

Also, why is the Groovy codehaus website so slow to scroll down a page?

Mark
 
Mark Spritzler
ranger
Posts: 17344
11
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I resolved it by changing how I create my closure methods.

I was using

def functionName = {param ->

}

Which caused the error I think because

"$functionName" would be the pointer to the closure, not call the actual closure.

So I changed it to

def function(param) {
}

Mark
 
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!