Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert java legacy code to generic - how to replace Object with type?

// 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?

like image 925
joejax Avatar asked Mar 04 '26 01:03

joejax


1 Answers

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);
like image 97
BalusC Avatar answered Mar 05 '26 15:03

BalusC



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!