JOOQ Cast String to Enum with Converter inline

recklessGreed :

As asked here the follow up Question to this #58538732

As suggested by Lukas Eder I wrote an EnumConverter to convert the Integer to a DayOfWeek

public class DOWConverter extends EnumConverter<Integer, DayOfWeek>  {

    public DOWConverter()
    {
        super(Integer.class, DayOfWeek.class);           
    }               
}

The select now looks the following

DataType<DayOfWeek> typeDOW = SQLDataType.INTEGER.asConvertedDataType(new DOWConverter() /*ERROR*/);
Field<DayOfWeek> fieldDOW = DSL.field("{0}", typeDOW, lecture.DAY_OF_WEEK);

create.select( ..., fieldDOW, ...)...

With the Error Message:

Exception in thread "main" java.lang.Error: Unresolved compilation problem:
No enclosing instance of type QueryFeaturesTask is accessible. Must qualify the allocation with an enclosing instance of type QueryFeaturesTask (e.g. x.new A() where x is an instance of QueryFeaturesTask).

As stated, using the Converter at CodeGen Time is currently not an option.

Lukas Eder :

It seems that you placed your DOWConverter inside of another class, thus creating an inner class. I recommend you place the converter at the top level, in its own file, making it a top level class. If you must create a nested class, make sure it is not an inner class by making it static:

public class Enclosing {
    // Make this class here static:
    public static class DOWConverter extends EnumConverter<Integer, DayOfWeek> {
        public DOWConverter() {
            super(Integer.class, DayOfWeek.class);           
        }               
    }
}

Oracle's tutorial on nested classes explains this really well.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=327983&siteId=1