Git Product home page Git Product logo

Comments (6)

konradgro avatar konradgro commented on July 3, 2024

or maybe just good description in documentation.

from modelmapper.

jhalterman avatar jhalterman commented on July 3, 2024

Enums are mappable so long as the source and destination enum names are the same. Is there an example you can share that is giving you trouble?

from modelmapper.

konradgro avatar konradgro commented on July 3, 2024

First of all, thanks for fast replay ;)

Code:

public class TreeNodeMap extends PropertyMap<AttributeStructureNode, TreeNode> {

@Override
protected void configure() {
    final ValueType type;

    switch (source.getAttributeKind()) {
    case TEXT_TYPE:
        type = ValueType.STRING;
        break;
    case FLOAT_TYPE:
        type = ValueType.DECIMAL;
        break;
    case INTEGER_TYPE:
        type = ValueType.INTEGER;
        break;
    case LOGICAL_TYPE:
        type = ValueType.BOOLEAN;
        break;
    case DATE_TYPE:
        type = ValueType.DATE;
        break;
    default:
        type = null;
        break;
    }

    map().setAttributeKind(type);
}

}

When I'm calling source.getAttributeKind() it throws me exception:

ModelMapper configuration errors:

  1. An error occured while building mappings

1 error

RequestURI=/sdlconsole/MainInterface

Caused by:

org.modelmapper.ConfigurationException: ModelMapper configuration errors:
  1. An error occured while building mappings

1 error
at org.modelmapper.internal.Errors.throwConfigurationExceptionIfErrorsExist(Errors.java:224)
at org.modelmapper.internal.MappingBuilderImpl.build(MappingBuilderImpl.java:138)
at org.modelmapper.internal.TypeMapImpl.addMappings(TypeMapImpl.java:66)
at org.modelmapper.internal.TypeMapStore.getOrCreate(TypeMapStore.java:93)
at org.modelmapper.ModelMapper.addMappings(ModelMapper.java:90)
at pl.dandelite.empik.sdl.manager.server.service.MainInterfaceServiceImpl.(MainInterfaceServiceImpl.java:77)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at java.lang.Class.newInstance0(Class.java:355)
at java.lang.Class.newInstance(Class.java:308)
at org.mortbay.jetty.servlet.Holder.newInstance(Holder.java:153)
at org.mortbay.jetty.servlet.ServletHolder.getServlet(ServletHolder.java:339)
at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:463)
at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:362)
at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:181)
at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:729)
at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:405)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at org.mortbay.jetty.handler.RequestLogHandler.handle(RequestLogHandler.java:49)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at org.mortbay.jetty.Server.handle(Server.java:324)
at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:505)
at org.mortbay.jetty.HttpConnection$RequestHandler.content(HttpConnection.java:843)
at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:647)
at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:211)
at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:380)
at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:395)
at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:488)
Caused by: org.modelmapper.internal.ErrorsException
at org.modelmapper.internal.Errors.toException(Errors.java:237)
at org.modelmapper.internal.ProxyFactory.proxyFor(ProxyFactory.java:85)
at org.modelmapper.internal.MappingInterceptor.intercept(MappingInterceptor.java:41)
at pl.dandelite.empik.sdl.core.domain.dataStructure.AttributeStructureNode$$EnhancerByModelMapper$$a6c9de64.getAttributeKind()
at pl.dandelite.empik.sdl.manager.server.modelmapper.TreeNodeMap.configure(TreeNodeMap.java:15)
at org.modelmapper.PropertyMap.configure(PropertyMap.java:270)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.modelmapper.internal.MappingBuilderImpl.build(MappingBuilderImpl.java:123)
... 29 more

from modelmapper.

jhalterman avatar jhalterman commented on July 3, 2024

PropertyMap.source isn't meant to be used for anything other than defining mappings. This is why you're getting an exception when getAttributeKind is called. Mapping actual enum values isn't supported yet by the mapping API, but you can accomplish the same thing by creating a Converter:

    modelMapper.createTypeMap(Source.class, Dest.class).setConverter(new Converter<Source, Dest>() {
      public Dest convert(MappingContext<Source, Dest> context) {
        switch (context.getSource()) {
          case A: return Dest.One;
          case B: return Dest.Two;
          case C: return Dest.Three;
          default: return null;
        }
      }
    });

I actually just pushed a small change to make this possible. I'll be pushing a release today that will include that change. Let me know if this doesn't resolve your use case.

from modelmapper.

konradgro avatar konradgro commented on July 3, 2024

ok, thanks for example. This time I used converter, but know I have another exception:

Failed to instantiate instance of destination pl.dandelite.empik.sdl.manager.shared.model.ValueType. Ensure that pl.dandelite.empik.sdl.manager.shared.model.ValueType has a non-private no-argument constructor.

Of course its not possible to create public constructor for enum type ;)

from modelmapper.

jhalterman avatar jhalterman commented on July 3, 2024

Can you post some example mapping code that led to that exception? You may be hitting on a different scenario than I've encountered. This test works fine:

https://github.com/jhalterman/modelmapper/blob/master/core/src/test/java/org/modelmapper/functional/enums/EnumConverter.java

from modelmapper.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.