// legacy code
void setCacheValue(String name, Object value){
getServletContext().setAttribute(name, value);
}
Object getCacheValue(String name){
return getServletContext().getAttribute(name);
}
// so I want to use generic for "type safety"
// first, set method seems working perfectly
<T> void setCacheObject(String name, T value){
getServletContext().setAttribute(name, value);
}
// then, here comes the trouble
<T> T getCacheValue(String name){
// of course, I cannot change servlet class - it returns Object type
Object value = getServletContext().getAttribute(name);
// would this work:
return (T) value;
// this cast is meaningless... but how should I do it?
}
// This is what I what to achieve in my clean calling code:
{
double x = 1.2;
setCacheValue("x", x);
//...
// later
Double value = getCacheValue("x");
// too bad cannot use primitive type - it cannot handle null
}
So, what is the correct way of doing this?
That's indeed not possible. You'll need to pass the "concrete" T
somehow as method argument so that the actual type is known during runtime. Commonly used approach is passing it as Class<T>
, so that you can make use of Class#cast()
:
<T> T getCacheValue(String name, Class<T> type) {
return type.cast(getServletContext().getAttribute(name));
}
You can use it as follows:
Double value = getCacheValue("x", Double.class);