S
S
synapse_people2018-04-19 17:03:01
Java
synapse_people, 2018-04-19 17:03:01

How to make the right generics?

There are 2 interfaces:
First:

public interface ArgsConverter<D> {

  public void prepare(...);

  public D conv(...., Object[] args );
}

the task is to get an array of arguments and turn it into an object.
Second:
public interface Callback<D> {

  MethType getMethodType();

  Class<D> getDataObjectClass();

  ArgsConverter<D> conv();

  Object execute(D data);
}

There is a collection that consists of, <Callback<? extends Object>>
that is, all kinds of callbacks in the heap ... We find the desired callback - there are no problems, we take it out
and we get the type-safety problem. Although the callback determines the type of data it accepts as input, and also determines the converter to be used, the type-safe warning still crashes!
Callback<? extends Object> cb = CALLBACKS.stream....;

    // convert ARRAY of arguments to DATA OBJECT

    return cb.execute(cb.conv().conv( args));

Mistake:
The method execute(capture#4-of ? extends Object) in the type Callback<capture#4-of ? extends Object> is not applicable for the arguments (capture#5-of ? extends Object)

Who can tell the thread - why is this happening and how to solve the problem WITHOUT a crutch with @SuppressWarnings("rawtypes")
I understand the whole point is how the collection is stored, and also that the wildcard of the collection does not match the one specified on the variable (?)
Or what the matter in general
Tell me)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
Codebaker, 2018-04-24
@synapse_people

Why are you making client code do all the work for you?
Why is ArgsConverter conv() sticking out? at the callback?
Try rewriting your execute to this:

public Object execute(Object[] args) {
    ArgsConverter<?> convImpl = conv();
    return convImpl.conv(args);
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question