Sun CodeModel - How to create enum with multiple parameters

1.3k Views Asked by At

I want to create an enum class similar to the following code snippet using Sun's codemodel

public enum REPORT_COLUMNS {

    MONTH("month", true, false),
    DAY("day", false, true);

    private final String column;
    private final boolean filterable;
    private final boolean includeInHavingClause;

    private REPORT_COLUMNS(String column, boolean filterable, boolean includeInHavingClause) {
        this.column = column;
        this.filterable = filterable;
        this.includeInHavingClause = includeInHavingClause;
    }

    public String getColumn() {
        return column;
    }

    public boolean isFilterable() {
        return filterable;
    }

    public boolean includeInHavingClause() {
        return includeInHavingClause;
    }
}

I was able to generate the code for the enum's constructor, fields and getter methods. But, I am unable to initialize the enum constants with three values. The JDefinedClass has a method enumConstant which takes in only the name of the enum constant as a parameter. I have read through the documentation of the JEnumConstant class too, but couldn't find anything that will add three values to the enum constant.

1

There are 1 best solutions below

1
On BEST ANSWER

You can use "JEnumConstant.arg()" together with "Jexpr.lit()".

    JEnumConstant enumMonth = definedClass.enumConstant("MONTH");
    enumMonth.arg(lit("month"));
    enumMonth.arg(lit(true));
    enumMonth.arg(lit(false));

I wrote some sample code for this, check out the complete example here: https://github.com/jangalinski/stackoverflow-jangalinski/blob/master/src/test/java/de/github/jangalinski/codemodel/GenerateEnumTest.java