Method code too large! How to fix this Grails error

6
March 13, 2014 at 9:51 pm  •  Posted in Grails by  •  6 Comments

Method Code too Large! Wait, what?

I started a new Grails application today which makes use of a legacy database. Typically, the steps go something like this:

  1. Create the application
  2. Configure the datasource
  3. Install the Database Reverse Engineering Plugin
  4. Configure the reverse engineering plugin
  5. Run db-reverse-engineer
  6. Check the generated domains
  7. Create a controller for each domain and set scaffolding to true
  8. Test your database
  9. Start developing!

I followed this work flow until step 8 and hit the following error when trying to show a database entity:

General error during class generation: Method code too large!
java.lang.RuntimeException: Method code too large!

 How is my method code too large? I haven’t even written anything!

Even though I haven’t written any code, Grails generated my controllers and views. through scaffolding. Remember when I mentioned the legacy database? It turns out that many of the tables in the database have 100+ columns.

The Java Virtual Machine has a limitation that methods cannot be larger than 65536 bytes. Meaning that any methods that are generated that use all 100+ columns could reach the 65536 byte limit. When it comes to the dynamically generated views, there are two that will use all 100+ columns.

  1. show.gsp
  2. _form.gsp

Listing records will be no problem but showing, editing, and creating will fail.

The Fix!

As with most things in Grails, this is a simple fix. Simply divide your views into templates.

  1. Generate your views so you have code to work with.
  2. Create template files which will be used to divide a method. I divided my views into four templates which each held 1/4 of the form data. For example, in addition to the generated view create four more views but with the template naming convention. _form1.gsp, _form2.gsp, _form3.gsp, form4.gsp
  3. Once your view has been divided, render each template within the original method.

Please let me know if you have any questions!

 

6 Comments

  1. Susanne / August 26, 2014 at 12:01 pm / Reply

    Thanks a lot, you saved my day! 🙂 Got this error message this morning, working with a realy nice(!) legacy db. I’m happy to find your solution!

    • Bl_nK / August 26, 2014 at 12:23 pm / Reply

      Glad I could help! This error drove me insane for a few hours.

  2. Manuel Cervantes / February 19, 2015 at 7:17 pm / Reply

    Thank you very much Bl_nK, it is an excellent documentation and is a nice fix.

  3. vikaschauhan / April 29, 2015 at 4:07 am / Reply

    How to generate views and create templates ?

  4. Shikha / September 4, 2015 at 11:02 pm / Reply

    But how to check which file has this problem?

    • Bl_nK / October 1, 2015 at 4:52 pm / Reply

      Hi Shikha,

      The issue will occur when going to a specific GSP page. The GSP page that this happens on is the one with the problem.

Leave a Reply