Search code examples
javacode-generationjavacstringtemplate-4

Generating really big Java code


I need to populate ~20k built-in function and constants from an CHM file into a List. First I tried to serialize a list of them, but deserialization takes 1500ms (too long for a quick code-completion, even at the first hit).

Tried code generation using StringTemplate, which generates valid code. But it won't compile because "initEnums() method exceeded size limit of 65536". (And my Eclipse dies soon after)

The generated method looks like this:

public XModelField[] initEnums() {
    return new XModelField[] {
        new XModelField("aName", "aType", ...),
        ...
        // About 4'000 more entries
    };
}

If this limit is constrained only to source code, I could use another library to generate the class file directly (maybe CodeModel). I'd like to keep my code, if possible without rewriting the output generator from scratch again. Any suggestions?


Solution

  • I think you should reconsider your strategy. Model your data structures and methods accordingly and you don't have to generate code. Instead to generate the Java code for 4000 constants, store your data in a file or database and write a short method to read that data in at initialization time to fill a list with your data objects.

    Generating code is a powerfull tool but you should utilize it carefully. I don't see it is even necessary for your use case, but if you think it is, then you can try to minimize the portion of code to generate to an absolute minimum to keep things simple. Often you can extract a lot of generic code to an abstract super class so that you only has to generate the code for some methods in subclasses. And generating code for 4000 constants is really unnecessary. Keep data as data and operate on your data dynamically.