Today I've noticed a very strange conversion behavior in my JSF application. Please have a look at this:
I have this CRUD base class that is intended to be extended开发者_开发知识库 by some concrete managed bean classes:
public abstract class Cadastro<T, C extends Number> implements Serializable
{
    // _chave is the primary key
    private C _chave;
    // more fields...
    public C getChave()
    {
        return _chave;
    }
    public void setChave(C chave)
    {
        _chave = chave;
        if (_chave != null)
        {
            // More about this below...
            System.out.println(_chave.getClass().getName());
        }
    }
    // more methods...
}
This is a concrete managed bean class that extends the previous class:
@ManagedBean
@ViewScoped
public class CadastroArquivos extends Cadastro<Arquivo, Short>
{
}
Note the Short type for the primary key.
In the page I have an input text component for the key with a Short converter attached to it:
<h:inputText value="#{cadastroArquivos.chave}">
    <f:converter converterId="javax.faces.Short" />
</h:inputText>
So, as you can see, in the java side the *_chave* field has type Short in the CadastroArquivos class and in the page side there's a Short converter that matches this type. But when the page is posted back to the server, the System.out.println above prints for the *_chave* class:
java.lang.Long
and not java.lang.Short. How can it be?
Also, if I change the CadastroArquivos class to this to force even more the right conversion:
@ManagedBean
@ViewScoped
public class CadastroArquivos extends Cadastro<Arquivo, Short>
{
    @Override
    public Short getChave()
    {
        return super.getChave();
    }
    @Override
    public void setChave(Short chave)
    {
        super.setChave(chave);
    }
}
I get a ClassCastException:
value="#{cadastroArquivos.chave}": java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Short
But if I change the declaration of the Cadastro class to this (note the C doesn't extend Number anymore):
public abstract class Cadastro<T, C> implements Serializable
everything works fine, at the cost of making my application less type safe in the java side. Can someone explain to me what's going on and how I can solve this?
Thank you.
Marcos
 
         
                                         
                                         
                                         
                                        ![Interactive visualization of a graph in python [closed]](https://www.devze.com/res/2023/04-10/09/92d32fe8c0d22fb96bd6f6e8b7d1f457.gif) 
                                         
                                         
                                         
                                         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论