1

How to resolve the following warning from the quarkus maven plugin?

Found unrecommended usage of private members (use package-private instead) in application beans

My code looks like this:

@Mapper(componentModel = "cdi", uses = SampleFactory.class)
public interface SampleMapper {
// mapping functions
}

The generated Impl class looks like this:

@ApplicationScoped
public class SampleMapperImpl implements SampleMapper {

    @Inject
    private SampleFactory sampleFactory;
    // mapping functions
}

The warning suggests to make the sampleFactory member package-private. Is there a possiblity to configures this, e.g. with an annotation? The current mapstruct (1.4.2) documentation states you can only configure an injection strategy. However, configuring the visibility of the injected member is not possible - is it?

Settings:

  • quarkus-maven-plugin:1.12.1
  • mapstruct 1.4.2.Final
  • Java 14.0.1

1 Answers1

2

Switching to constructor injection strategy will resolve the warning. It’s also recommended by MapStruct:

It is recommended to use constructor injection to simplify testing.

Field injection on private members requires CDI implementations to use reflection. Quarkus tries to avoid reflection in order to be able to compile with GraalVM‘s native-image compiler. Also, using reflection is simply not necessary here, so we should avoid it.

Oliver Marienfeld
  • 1,154
  • 9
  • 17
  • Thanks @rowing-ghoul Using constructor injection strategy resolves the issue. :-) Do you know why map-struct does not use this constructor injection strategy as default? – Jaqueline Schweigert Mar 16 '21 at 14:08
  • @JaquelineSchweigert I think field injection is more intuitive to programmers (at least it is to me). But thinking about it and using DI techniques for a while, you'll realize that constructor injection is much easier to deal with. There is a feature request in the MapStruct repo about this: https://github.com/mapstruct/mapstruct/issues/1973 – Oliver Marienfeld Mar 16 '21 at 16:20