javamidi

Error: Code too Large in Generated Java File


I'm coding a Java application that reads a .MID file and generates a .java(which when compiled and executed generates a .wav file), however, the generated .java file contains a very large constructor method, so the generated code won't compile. I'm aware of the maximum size of 64kb for each method, but is there a way to change it? Thanks!


Solution

  • Simply make your app break down the constructor into several methods - that is, the constructor calls multiple small methods to do its job. This also makes debugging easier as fixing bugs scattered across methods one method at a time is more of a piece of cake rather than dealing with a huge, broken method.

    Here's an example of a method (not necessarily a constructor, in fact you can break down all kinds of methods) broken down into smaller methods:

    public int doLotsOfStuff(String arg0, int arg1, boolean arg2, BiFunction<Boolean, String, Integer> arg3){
        arg0 = reverse(arg0);
        foo(arg1, arg2);
        return bar(arg0, Integer.valueOf(arg1), arg2, arg3);
    }
    
    String reverse(String arg0){
        StringBuilder foobar = new StringBuilder(arg0);
        foobar.reverse();
        return foobar.toString();
    }
    
    void foo(int arg0, boolean arg1){
        System.out.println(arg1 ? ~arg0 : arg0);
    }
    
    <A, B> int bar(String arg0, A arg1, B arg2, BiFunction<B, String, A> arg3){
        return String.valueOf(arg3.apply(arg2, arg1)).concat(arg0).hashCode();
    }