Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 766791
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T17:06:19+00:00 2026-05-14T17:06:19+00:00

// legacy code void setCacheValue(String name, Object value){ getServletContext().setAttribute(name, value); } Object getCacheValue(String name){

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T17:06:19+00:00Added an answer on May 14, 2026 at 5:06 pm

    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);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to use java-legacy code with the following method: public void doit(Map <String,
I am using some legacy code to return an Excel worksheet as a Dataset.
I'm having some legacy code that recieves a string[,] as one of the method
I've seen the following in legacy code: public void someFunction(List myList){ List myList2 =
I am working with some legacy code which uses something like this: void store_data(FILE
I have some legacy code that looks like this: void* getData(DataType dataType) { switch(dataType)
To compile some legacy code A.c , which has a function prototype void somefun(...)
I've got some legacy code here that makes use of boost::filesystem in version 1.34.1.
I want to use Spring inside a legacy application. The core piece is a
I'm struggling with legacy code. I'm creating unit tests so I've decided to use

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.