javajsfjsf-2java-7el

Is it possible to get a list of Dog from a list of Animal without a cast?


I have a protected Map<String, List<Animal>> animals; in my class. In my constructor, I have

animals.put("dogs", new ArrayList<Animal>());
animals.put("cats", new ArrayList<Animal>());

and eveything works, as expected. If I want a Dog or a Cat, I have only to it get from map and cast.

But, what if I should want

animals.put("dogs", new ArrayList<Dog>());

Java does not allow me this. I read also it in this SO answers But I need it, since I'm using JSF and I need in the EL expression of the page a method of Dog that Animal doesn't have.

Since I created a factory for Animal I thought to use the factory to get the right object type, but it seems me a little weird.

Is there some way I can use animals.put("dogs", new ArrayList<Dog>()); or some other alternatives?

PS: I'm using Java 7


Solution

  • Ok, it seems absurd but the problem was in the JSF code:

    <ui:repeat var="animal" value="#{bean.animals.get('dogs')}" varStatus="status">
    

    I don't know why, but changing var="animal" to anything else works. I do not need any cast.