5

Typically in Spring Source Tool Suite IDE or Eclipse Maven project, if I want to see what a framework/library method is doing behind the scenes, I can ctrl + click on the method name and it will take me to the source code. I understand that a lot of Grails methods are added dynamically at runtime so its not always possible for the IDE to know how to get to them. Otherwise I can search for the class by package on Google, Github or the API documentation. What is the best way to do this with Grails core source to better understand the framework?

For instance I want to see what the respond method in a controller looks like and how its returning a parameter called "clubInstanceList" to my club/index gsp when the index method looks like:

def index(Integer max) {
    params.max = Math.min(max ?: 10, 100)
    respond clubService.list(params), model:[clubInstanceCount: clubService.count()]
}

ctrl + click doesn't work in the IDE since this method is added at runtime. I have searched through the Grails core source on github, but don't know the package structure for this respond method on a controller.

bholl
  • 181
  • 1
  • 8
  • I answered below, but I wanted to add that I just started using IntelliJ and it has no problem opening the declaration of Grails controller methods like respond and render. It demonstrates a much stronger understanding of the code, including all references. – Philip Jul 15 '14 at 05:53

2 Answers2

5

That's may be not a trivial search, because some code is generated during compilation time, or features can be added to your classes at runtime. This is the price that you pay when using dynamic languages.

Dynamic Runtime Methods

When I need to find some functionality that's not so obvious I start by trying to find the internal plugin that's responsible for that. In STS I do ctrl shift T and search for *GrailsPlugin*. You can check for runtime changes in the doWithDynamicMethods closure of the plugin.

For example, Grails have the ControllersGrailsPlugin class that add databinding features for your controller classes.

Search For Methods

Since I didn't find anything related to the respond method in this plugin descriptor, this is probably made by compilation. You could use ctrl H, and do a Java search, marking method in the "search for" box. In the scope mark workspace and STS will look for Grails classes too.

The Answer

So the answer your your question is the ControllersRestApi class, and I found it using the "search method" approach.

  • 1
    Welcome :-) I wish that more folks become enthusiast like you and drive into the source! –  Jan 10 '14 at 17:27
  • 3
    Me too! I always like to know how things work. I am finding the book "Programming Grails" by Burt Beckwith, very helpful. – bholl Jan 10 '14 at 21:38
0

I had the identical question and found the answer from Sergio to be quite helpful, but here's another method: load up the debugger and step through using F5. Now, this can be exhausting unless you enable step filters (see GGTS/STS/Eclipse help). In particular, I recommend filtering out org.springsource.loaded.*, in addition to the preselected packages. That gets rid of a lot of the reflection and dynamic parts that - at least in cases like this - you don't care about.

I will also add that my quest found this issue to improve the respond method documentation:

https://jira.grails.org/browse/GRAILS-10721

So we're not the only ones to scratch our heads at this.

Philip
  • 697
  • 7
  • 20