Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Import not generated for generic argument #844

Closed
HereThereBeMonsters opened this issue Jul 27, 2016 · 4 comments
Closed

Import not generated for generic argument #844

HereThereBeMonsters opened this issue Jul 27, 2016 · 4 comments

Comments

@HereThereBeMonsters
Copy link

I already posted a question on StackOverflow : http://stackoverflow.com/questions/38610306/why-does-this-mapstruct-generated-class-does-not-include-import-statement

I am using MapStruct to map between JPA entities and POJO DTOs.

All my entities extend a common base class that has an ID field (a java.lang.Long).

I have the following abstract mapper, that allows me to map from relationship in JPA to a simple Long field (or List) in the DTOs.

An entity or List<entity> field can be mapped to a Long/List<Long> field, e.g. User.groups could be mapped to UserDTO.groupIds :

 @Mapper
    public abstract class EntityMapper {

        public Long entityToLongId(AbstractBaseEntity entity){
            return entity.getId();
        }

        public abstract List<Long> entityCollectionToLongIdList(Collection<? extends AbstractBaseEntity> entities);
    }

However the generated implementation class does not include any import statement for the AbstractBaseEntity class, although it is present in the abstract class, so the code does not compile :

 package ....;

    import java.util.ArrayList;
    import java.util.Collection;
    import java.util.List;
    import javax.annotation.Generated;
    import org.springframework.stereotype.Component;

    @Generated(
        value = "org.mapstruct.ap.MappingProcessor",
        date = "2016-07-27T12:11:25+0200",
        comments = "version: 1.0.0.Final, compiler: javac, environment: Java 1.8.0_66 (Oracle Corporation)"
    )
    @Component
    public class EntityMapperImpl extends EntityMapper {

        @Override
        public List<Long> entityCollectionToLongIdList(Collection<? extends ch.unine.tango.model.AbstractBaseEntity> entities) {
            if ( entities == null ) {
                return null;
            }

            List<Long> list = new ArrayList<Long>();
            for ( AbstractBaseEntity abstractBaseEntity : entities ) { // compilation error here !
                list.add( entityToLongId( abstractBaseEntity ) );
            }

            return list;
        }
    }

I am using MapStruct 1.0.0.Final with Java 8.

If I add an abstract method that uses the AbstractBaseEntity class directly, then the import is added :

public abstract AbstractBaseEntityDTO entityToDTO(AbstractBaseEntity abstractBaseEntity);

@agudian
Copy link
Member

agudian commented Jul 28, 2016

That's not good. I'm almost sure we didn't fix anything in that area after 1.0.0.Final, but just in case: could you please try it with the latest 1.1.0.Beta2 as well? Thanks!

@HereThereBeMonsters
Copy link
Author

Thanks for the quick reply.

This is actually fixed in 1.1.0.Beta2, the import is created.

So, I'll leave the explicit method for now as a workaround although I don't otherwise need it, and I will switch to 1.1.0 as soon as it hits production status.

Thanks again.

@agudian
Copy link
Member

agudian commented Jul 29, 2016

Great, thanks for the feedback! I'll close this and mark it as a duplicate then.

@HereThereBeMonsters
Copy link
Author

I answered my own question on SO linking to this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants