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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T00:29:12+00:00 2026-05-30T00:29:12+00:00

I’m trying to clean up some legacy code and have some unchecked cast warnings

  • 0

I’m trying to clean up some legacy code and have some unchecked cast warnings I’m struggeling to get rid of.

I’ve extracted the code giving the warnings into a compilable program below. Note that I removed much of the code to make it smaller so all of it might not make complete sense. It compiles, but running it wont do anything.

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipInputStream;

public class GenericsTest {

    public static void main(String[] args) throws IOException {
        Reader reader = new Reader();
        List<String> stringEntries = reader.readAll(StringEntry.class);
    }

    public static class Reader {

        public <T> ZipInputStream getInputStream(String fileName) throws ZipException, FileNotFoundException {
            return new ZipInputStream(new FileInputStream(fileName));//_file.getInputStream(_paths.get(fileName));
        }

        public <T, TEntry extends Entry<T>> List<T> readAll(Class<TEntry> type) throws IOException {
            List<T> list = new ArrayList<T>();
            List<TEntry> entries = createEntries(type);
            for (TEntry entry : entries) {
                list.add(read(entry));
            }
            return list;
        }

        public <T> T read(Entry<T> entry) throws IOException {
            ZipInputStream is = null;
            try {
                //is = _archive.getInputStream(entry.getName());
                return entry.read(is);
            } finally {
                if (is != null) {
                    is.close();
                }
            }
        }

        public <TEntry extends Entry> List<TEntry> createEntries(Class<TEntry> type) throws ZipException {
            List<TEntry> entries = new ArrayList<TEntry>();
            List<String> paths = new ArrayList<String>();//getPaths(type);
            for (String path : paths) {
                entries.add(createEntry(type, path));
            }
            return entries;
        }

        public <TEntry extends Entry> TEntry createEntry(Class<TEntry> type, String folder) {
            if (StringEntry.class.equals(type)) {
                return (TEntry) new StringEntry(folder);
            } else if (IntegerEntry.class.equals(type)) {
                return (TEntry) new IntegerEntry(folder);
            }
            throw new IllegalArgumentException("Unknown type: " + type);
        }
    }

    public static abstract class Entry<T> extends ZipEntry {

        private T _data;

        public Entry(T data, String folder, String name) {
            super(folder + "/" + name);
            _data = data;
        }

        protected abstract T read(InputStream is) throws IOException;
    };

    public static class StringEntry extends Entry<String> {

        public StringEntry(String folder) {
            super("Hallo world!", folder, "StringEntry");
        }

        @Override
        protected String read(InputStream is) throws IOException {
            throw new UnsupportedOperationException("Not supported yet.");
        }
    };

    public static class IntegerEntry extends Entry<Integer> {

        public IntegerEntry(String folder) {
            super(42, folder, "IntegerEntry");
        }

        @Override
        protected Integer read(InputStream is) throws IOException {
            throw new UnsupportedOperationException("Not supported yet.");
        }
    };
}

Compiling the above code gives the following warnings

GenericsTest.java:57: warning: [unchecked] unchecked cast 
found: GenericsTest.StringEntry
required: TEntry
                return (TEntry) new StringEntry(folder);
GenericsTest.java:59: warning: [unchecked] unchecked cast 
found: GenericsTest.IntegerEntry
required: TEntry
                return (TEntry) new IntegerEntry(folder);
2 warnings

Changing

public <TEntry extends Entry> TEntry createEntry...

to

public <TEntry extends Entry<T>> TEntry createEntry...

gives a compiler error (cannot find symbol: class T).

I don’t want to change the code too much since it is working fine, so how can I fix (not hide) the warnings with the least code changes?

  • 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-30T00:29:13+00:00Added an answer on May 30, 2026 at 12:29 am

    You’re already passing the Class object into the method, just use Class.cast():

    public <TEntry extends Entry<?>> TEntry createEntry(Class<TEntry> type, String folder) {
        if (StringEntry.class.equals(type)) {
            return type.cast(new StringEntry(folder));
        } else if (IntegerEntry.class.equals(type)) {
            return type.cast(new IntegerEntry(folder));
        }
        throw new IllegalArgumentException("Unknown type: " + type);
    }
    

    The above change makes your code compile without warnings for me.

    Also, the <T> in the signature of getInputStream() is probably not necessary.

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

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I am trying to loop through a bunch of documents I have to put
I have some data like this: 1 2 3 4 5 9 2 6
Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,

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.