0

I'm getting the warning "Raw use of parameterized class 'MapperService' and "Unchecked call to 'mapSourceToBehandlerKrav(T)' as a member of raw type .. from IntelliJ. I'm not really grasping the concepts of generics in general, trying to figure it out. But some explanation and solution to these warnings might be helpful

Interface:

public interface MapperService<T> {
    Behandlerkrav mapSourceToBehandlerkrav(T sourceInput) throws DataSourceException, MapperException;
}

Implementing class:

public class XMLToDomainMapper implements MapperService {

    public Behandlerkrav mapSourceToBehandlerkrav(Object input) throws DataSourceException, MapperException {

        if (!(input instanceof File)) {
            throw new DataSourceException(
                "Input er av ugyldig type: " + input.getClass() + " | tillat type = " + "File");
        }

        // More stuff
    }

Calling of implemting class (where the warnings occur):

public class InnrapporteringServiceImpl implements InnrapporteringService {
    MapperService kildesystemInputMapper; <-- IntelliJ WARNING HERE

    public InnrapporteringServiceImpl(XMLToDomainMapper mapper) {
        this.kildesystemInputMapper = mapper;
    }

    @ServiceActivator(inputChannel = "sftChannel")
    public void init(Message<File> message) {
        // call mapper service
        // call persistence service
        // call reporting service
        var timestamp = message.getHeaders().getTimestamp();

        Behandlerkrav behandlerkrav = kildesystemInputMapper.mapSourceToBehandlerkrav(message.getPayload()); <-- IntelliJ WARNING here
    }
}
robinmanz
  • 361
  • 1
  • 5
  • 17
  • Should you not declare the kildesystemInputMapper field type as MapperService[XMLToDomainMapper] or mention if your mapper extends any parents mapper class. – Rajesh Somasundaram Mar 03 '21 at 10:41

1 Answers1

1

The MapperService interface is meant to be parameterised with a type.

This allows it to check that the parameter to mapSourceToBehandlerkrav is of the type it expects.

If you wrote XMLToDomainMapper as:

class XMLToDomainMapper implements MapperService<File> {
 ...
}

Then you would have a compile time check that parameter being passed is a File, and you wouldn't need:

if (!(input instanceof File)) {
            throw new DataSourceException(
                    "Input er av ugyldig type: " + input.getClass() + " | tillat type = " + "File");
        }

as you have in your current implementation -- it would be impossible to call it with a parameter of any type that wasn't a File.

tgdavies
  • 10,307
  • 4
  • 35
  • 40