title: Java - How to overcome the maximum method size in automatically-generated code
body: <p>I have an unusual requirement: My application generates Java code automatically from a very long script (written in a dynamically-typed language). The script is so long that I hit <a href="https://stackoverflow.com/questions/17422480/maximum-size-of-a-method-in-java-7-and-8">the maximum method size of 65k of the JVM</a>.</p>
  
  <p>The script consists only of simple instructions on primitive types (no call to other functions besides mathematical ones). It may look like:</p>
  
  <pre><code>...
  a = b * c + sin(d)
  ...
  if a&gt;10 then
     e = a * 2
  else
     e = a * abs(b)
  end
  ...
  </code></pre>
  
  <p>... which is transformed as:</p>
  
  <pre><code>...
  double a = b * c + Math.sin(d);
  ...
  double e;
  if(a&gt;10){
     e = a * 2;
  }else{
     e = a * Math.abs(b);
  }
  ...
  </code></pre>
  
  <p><hr>
  My first idea to overcome the method size limitation was the following:</p>
  
  <ul>
  <li>Turn all local variables into fields</li>
  <li>Split the code every 100 lines (or longer if needed in case of a if/else block) in separate methods.</li>
  </ul>
  
  <p>Something like:</p>
  
  <pre><code>class AutoGenerated {
  
     double a,b,c,d,e,....;
  
     void run1(){
        ...
        a = b * c + sin(d);
        ...
        run2();
     }
  
     void run2(){
        ...
        if(a&gt;10){
            e = a * 2;
        }else{
            e = a * Math.abs(b);
        }
        ...
        run3();
     }
  
     ...
  }
  </code></pre>
  
  <p>Do you know of any other way that would be more efficient? Note that I need the code to run as fast as possible as it will be executed in long loops. I cannot resort to compiling to C, as interoperability is also an issue... </p>
  
  <p>I would also appreciate pointers to libraries that might help me.</p>