Question

I have used a Strategy pattern in my code, here is a short fragment:

public interface FindingStrategy<T> {
    Callable<T> setAction();
}

class FindSomething implements BindingActionStrategy {

...

    @Override
    public Callable<SomeObject> setAction() {
        return new Callable<SomeObject>() {
            @Override
            public SomeObject call() throws ... {
                return (SomeObject)binding.findSomething(somethingsId);
            }
        };
    }

...

}

Somewhere else i do:

 Callable<SomeObject> ts = (Callable<SomeObject>) strategy.setAction();

And compiler signals:

unchecked conversion warning 
required: Callable<SomeObject>
found:    Callable

What is weird, if I do this, there is no warning:

Callable<SomeObject> ts = new Callable<SomeObject>() {
            @Override
            public SomeObject call() throws ... {
                return (SomeObject)binding.findSomething(somethingsId);
            }
        };

How do I fix this? I tried to change a lot and I still get a warning. Please help!

Was it helpful?

Solution

Make sure that the strategy variable has the generics type information, too.

The following will cause this warning:

FindingStrategy strategy;

while this should be fine:

FindingStrategy<SomeObject> strategy;

you shouldn't even need the cast then.

OTHER TIPS

I suspect your class definition should read

class BindingFindingSomething implements FindingStrategy<SomeObject>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top