Search code examples
javaabstraction

Elegant way of holding large static typesafe dictionary in java - or avoiding code too large


Basically I would like to have some dictionary that is an abstaction over legacy #define directives.

I have an old header file that contains 6000+ defines, that are used as flag parametersome function and these defines denote one type of entity parameter.

In C i have

  GetParameter(... , T_CTITLE, ...); 

In Java i would like to call

  Connector.getParameter(Parameter.CTITLE, ...); 

And Parameter would encapsulate all logic that is associated with getting the parameter from library.

Parameter instances are automatically extracted from header and converted to java code, but the problem is that Parameter class gets too big - i.e. i get code too large compile error (let me underline: there are more that 6000 parameters).

And I would be thrilled to do this abstraction in a way that enables IDE o use autocompletion, and would wery much dislike idea of storing Parameter objects in say HashMap.

EDIT: Parameter Class is defined in following way:

public Parameter{
    /** logic **/
    public static final Parameter<T> parameter1 = new Parameter<T>("NAME", "T", 0xAAB);
    ...
    public static final Parameter<T> parameter6000 = new Parameter<T>("FOO", "T", 0xZZZ);
}

Solution

  • An obvious hack would be to either partition into a big inheritance chain, or better partition into interfaces (no need for the public static final noise) and one interface to inherit them all.

    You could save space by making the creation code smaller. Instead of:

    new Parameter<T>("NAME", "T", 0xAAB)
    

    A minimalist approach would be:

    parameter("NAME T AAB")
    

    For details of limitations, see section 4.10 of the JVM Spec (2nd Ed). To see what your compiled code is like, use javap -c.