문제

I am trying to setup Dozer to perform a complex mapping between my two entities. Essentially, I want it to convert my percentCompleted double to a boolean, based on if the value is 1 (100%) or not.

To do this I created the following method:

private void initEntityMappings()
{
    BeanMappingBuilder builder = new BeanMappingBuilder() {
        @Override
        protected void configure() {
            class isCompletedConverter implements CustomConverter {
                @Override
                public Object convert(Object destination, Object source, Class destClass, Class sourceClass) {
                    if (source == null) { return null; }

                    // Make sure the source is a double and the destination is a boolean
                    if (!(source instanceof Double) || !(destination instanceof Boolean))
                        throw new MappingException("Source was not a double or destination was not a boolean");

                    Boolean castedDest = (Boolean)destination;
                    Double castedSrc = (Double)source;
                    castedDest = castedSrc == 1;

                    return castedDest;
                }
            };

            mapping(TaskDetailsViewModel.class, TaskSummaryViewModel.class)
                .fields("percentCompleted", "isCompleted", customConverter(isCompletedConverter));
        }
    };  
}

The problem is that it the .fields() call complains because it says it can't find the symbol for isCompletedConverter. As this is my first time doing a local class, I am sure I am doing something wrong but I can't figure out exactly what.

도움이 되었습니까?

해결책

You are using the token isCompletedConverter (as oppose to an instance of isCompletedConverter, or it's .class object) which isn't valid at the particular point you use it. The way you're including it is kind of like in a cast, or an instanceof check, but that's a different syntax from a method invocation as customConverter appears to be.

Either try isCompletedConverter.class, or new isCompletedConverter() depending on what customConverter() is doing (I can't tell from the given code). It also might become clearer if you rename the local class from isCompletedConverter to IsCompletedConverter to match regular Java conventions.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top