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:
- Create the application
- Configure the datasource
- Install the Database Reverse Engineering Plugin
- Configure the reverse engineering plugin
- Run db-reverse-engineer
- Check the generated domains
- Create a controller for each domain and set scaffolding to true
- Test your database
- 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.
Listing records will be no problem but showing, editing, and creating will fail.
As with most things in Grails, this is a simple fix. Simply divide your views into templates.
- Generate your views so you have code to work with.
- 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
- Once your view has been divided, render each template within the original method.
Please let me know if you have any questions!