Annotation to make available generic type
Posted
by mdma
on Stack Overflow
See other posts from Stack Overflow
or by mdma
Published on 2010-06-12T00:18:35Z
Indexed on
2010/06/12
0:22 UTC
Read the original article
Hit count: 481
Given an generic interface like
interface DomainObjectDAO<T>
{
T newInstance();
add(T t);
remove(T t);
T findById(int id);
// etc...
}
I'd like to create a subinterface that specifies the type parameter:
interface CustomerDAO extends DomainObjectDAO<Customer>
{
// customer-specific queries - incidental.
}
The implementation needs to know the actual template parameter type, but of course type erasure means isn't available at runtime. Is there some annotation that I could include to declare the interface type? Something like
@GenericParameter(Customer.class)
interface CustomerDAO extends DomainObjectDAO<Customer>
{
}
The implementation could then fetch this annotation from the interface and use it as a substitute for runtime generic type access.
Some background:
This interface is implemented using JDK dynamic proxies as outlined here. The non-generic version of this interface has been working well, but it would be nicer to use generics and not have to create a subinterface for each domain object type. The actual type is needed at runtime to implement the newInstance
method, amongst others.
© Stack Overflow or respective owner