0

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!

Swapnil
  • 8,201
  • 4
  • 38
  • 57
Raidmaster
  • 603
  • 2
  • 7
  • 15

2 Answers2

4

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.

Has QUIT--Anony-Mousse
  • 76,138
  • 12
  • 138
  • 194
  • Thank you, that was it! You both helped me but since your answer was first I guess I should give you the "checkmark". I love StackOverflow! – Raidmaster Dec 28 '12 at 13:23
4

I suspect your class definition should read

class BindingFindingSomething implements FindingStrategy<SomeObject>
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130