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 7176031
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T16:25:30+00:00 2026-05-28T16:25:30+00:00

I have a Cache object for persisting objects to disk, and the way I’ve

  • 0

I have a Cache object for persisting objects to disk, and the way I’ve implemented this causes me to have to use @SupressWarnings . I’m not a Java expert, but this seems like a code smell, and I’m wondering if there’s a ‘better’ way to do what I’m doing here (for instance, somehow storing a Type when I write the cache, and reading this value to be able to instantiate a specific type?).

My Cache object looks like this (not-relevant code removed for brevity):

/**
 * Write object o to cache file
 * 
 * @param cacheName
 * @param o
 *            Serializable object to write to cache
 */
public static void put(String cacheName, Serializable o) throws IOException {
    FileOutputStream fos = new FileOutputStream(getHashedFileName(cacheName));
    ObjectOutputStream oos = new ObjectOutputStream(fos);
    oos.writeObject(o);
    fos.close();
}

/**
 * Get an object from the cache. Caller should use exists() before calling
 * here to make sure the cache item exists otherwise Exception will be
 * thrown
 * 
 * @param cacheName
 * @return Object from cache
 */
public static Object get(String cacheName) throws CacheNotFoundException,
        IOException, ClassNotFoundException {
    FileInputStream fis = new FileInputStream(getHashedFileName(cacheName));
    ObjectInputStream ois = new ObjectInputStream(fis);
    Object o = ois.readObject();
    return o;
}

Because get() can return any type of object, I now have to cast it back to the original type when I read from the cache. I’m wondering if I can avoid generating the warning in the first place:

class somethingDoingSomeWork {

    /**
     * Save all work done to the cache.
     * @throws IOException
     */
    public void saveToCache() throws IOException {
        Cache.put(getCacheKey(), (MyObject<String>) summary);
    }

    /**
     * Repopulate from cache
     */
    @SuppressWarnings("unchecked")
    public boolean loadFromCache() throws IOException, CacheNotFoundException,
            ClassNotFoundException {
        // Code removed, checking for existence of cache, etc.

        // Cast required here, and this row generates unchecked warning
        summary = (MyObject<String>) Cache.get(getCacheKey());
        return true;
    }
}

Edit: For people suggesting moving @SuppressWarnings closer to the code, annotations can only be used on lines that are declarations, so this won’t help me

Also using generics helps, but it appears to move the problem into the Cache.get() method. The best solution appears to be this: http://www.velocityreviews.com/forums/t499693-how-do-you-prevent-unchecked-cast-warning-when-reading-from-objectinputstream.html

  • 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-28T16:25:30+00:00Added an answer on May 28, 2026 at 4:25 pm

    and I’m wondering if there’s a ‘better’ way to do what I’m doing here (for instance, somehow storing a Type when I write the cache, and reading this value to be able to instantiate a specific type?)

    Use generics here. If you have

    public <String, P extends Serializable> R get( String key);
    public <String, P extends Serializable> void put( String key, R value);
    

    I am not pointing to already existing Cache implementations. Like Guava, those support cache anyways, but I blieve you want to improve this code.

    At the last resort, One big thing is to always keep @SupressWarnings as close as possible to the code causing it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a cache object that caches a number of different types of objects,
I have a object cache class like this: #include boost/thread/mutex.hpp #include boost/unordered_map.hpp template <typename
I'm looking for a way to have a generic local cache for any object.
I have a cache which has soft references to the cached objects. I am
I have requirement to get application cache object => session object, modify it and
I am still confused about passing by ref. If I have a Cache object
I have an object cache which implements the Singleton design pattern. My approach to
I got this síngleton cache object and it exposes an IEnumerable property which just
I have a named cache and simply want to return (programmatically) its object count.
I have a very simple Country entity which I want to cache. This works

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.