-, . .
private void saveConcreteDOs(AbstractDO[] theEntities) {
entityMap.put(theEntities.getClass().getComponentType(), theEntities);
}
, [0] , . , , , .
, getConcreteDOs() . . , .
, Java. , , , .
:
private Map<Class<? extends AbstractDO>, AbstractDO> map;
protected <E extends AbstractDO> E getConcreteDOs(Class<E> theType)
{
AbstractDO obj = map.get(theType);
return theType.cast(obj);
}
, . Class.cast() , .
, Class<T> T[] castArray(Object[]). , .
Or you can do it, but it's really overly sticky. Do not be afraid of an unverified throw warning if you know what you are doing and have carefully studied your type safety program.
protected <E extends AbstractDO> E[] getConcreteDOs(Class<E[]> arrayType)
{
AbstractDO[] array = map.get(arrayType.getComponentType());
return arrayType.cast(array);
}
...
X[] array = getConcreteDOs(X[].class);
source
share