I have a class
public abstract class AbstractE<T, E extends Enum<E> & Flags>
{
public interface Flags{} /*marker interface*/
//...
//other code
}
and an interface
public interface IXYZAdapter
{
public <E extends Enum<E> & Flags> Set<E> getFlags();
}
Where Flags is an interface defined in AbstractE itself.
M extends AbstractE thus:
public class M extends AbstractE<Long, M.EF> implements IXYZAdapter
{
public enum EF implements AbstractE.Flags{flag1, flag2}
@Override /*from IXYZAdapter*/
public Set<M.EF> getFlags()
{return EnumSet.allOf(EF.class);}
}
Now, from the main code, I try to get a handle on the interface IXYZAdapter and invoke the getFlags method
IXYZAdapter adapter = (IXYZAdapter)m; //where m is an instance of AbstractE
Set s = adapter.getFlags();
I get the following compile time error in the main program last line (Set s = adapter.getFlags();)
invalid inferred types for E; inferred type does not conform to declared bound(s)
inferred: AbstractE.Flags bound(s): java.lang.Enum<AbstractE.Flags>,AbstractE.Flags
What am I doing wrong? I am using Java 6
Edited to specify the error location
Try this:
public interface IXYZAdapter <E extends Enum<E> & AbstractE.Flags>
{
public Set<E> getFlags();
}
And
public class M extends AbstractE<Long, M.EF> implements IXYZAdapter<M.EF>
{
}
Or
Set<M.EF> s = adapter.getFlags();
The problem is that with Set s = adapter.getFlags(); The system doesn't know which type to infer for E in IXYZAdapter and thus the E in AbstractE doesn't match.
Edit:
Another option might be:
interface IXYZAdapter <E extends Enum<E> & AbstractE.Flags>
{
public Set<? extends E> getFlags();
}
class M extends AbstractE<Long, M.EF> implements IXYZAdapter<M.EF>
{
public enum EF implements AbstractE.Flags{flag1, flag2}
public Set<? extends M.EF> getFlags()
{return EnumSet.allOf(EF.class);}
}
And the call: Set<? extends AbstractE.Flags> s = adapter.getFlags();
This would allow you to get a set of flags without casting and force the flags to be declared as enum.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With