Question

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.

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top