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

  • Home
  • SEARCH
  • 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 7776689
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T17:56:47+00:00 2026-06-01T17:56:47+00:00

my questions relates to introducing Generics into legacy Java classes. The legacy classes from

  • 0

my questions relates to introducing Generics into legacy Java classes.
The legacy classes from the code below that I want to generify are ClientObjectProxy and ClientObjectContainer. ClientObject remains unchanged.
For simplicity I put all the classes into one outer class.

Now in the legacy code there are many calls of the method ClientObjectProxyContainer.getProxies() on ClientObjectProxyContainer instances that are not parameterized.

By introducing Generics existing code like in Loop 4 won’t compile anymore, the call needs to be extracted to a local variable like in Loop 1 or a declaration with a question mark needs to be used similar to Loop 2.
New code should use parameterized variables like in Loop 3.

My question is, is this is the way to go for me, change the existing code to either Loop 1 or Loop 2, or is there’s a way of not modifying legacy code?

Thanks

Jan

import java.util.ArrayList;
import java.util.List;

public class GenericForumsExample {
    /**
     * This class is unchanged
     */
    private class ClientObject {
    }

    /**
     * @param <T> Type of the ClientObject behind this proxy
     */
    private class ClientObjectProxy<T extends ClientObject> {
    }

    /**
     * @param <T> Type of the ClientObject contained in this collection
     */
    private class ClientObjectProxyContainer<T extends ClientObject> {
        //Previous signature was:
        //  public List<ClientObjectProxy> getProxies(){
        // New signature is the following
        public List<ClientObjectProxy<T>> getProxies(){
            return new ArrayList<ClientObjectProxy<T>>();
        }
    }

    public void testScenario() {
        ClientObjectProxyContainer proxyContainer = new ClientObjectProxyContainer();
        List<ClientObjectProxy> proxies = proxyContainer.getProxies(); // Just a compiler warning

        // Loop 1
        for (ClientObjectProxy proxy : proxies) {                     // Compiler OK
            //Do something...
        }

        // Loop 2
        ClientObjectProxyContainer<?> clientObjectProxyContainer = new ClientObjectProxyContainer();
        for (ClientObjectProxy<?> proxy : clientObjectProxyContainer.getProxies()) {
            //Do something...
        }

        // Loop 3
        for (ClientObjectProxy<ClientObject> proxy : new ClientObjectProxyContainer<ClientObject>().getProxies()) {
            //Do something...
        }

        // Loop 4
        // Compiler fails on next code line
        // incompatible types
        // found   : java.lang.Object
        // required: GenericForumsExample.ClientObjectProxy
        for (ClientObjectProxy proxy : proxyContainer.getProxies()) {
            //Do something...
        }
    }

}
  • 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-06-01T17:56:48+00:00Added an answer on June 1, 2026 at 5:56 pm

    In code dealing with generics, try to avoid using raw types at all. When you use these types, you will often loose more type-information than you expect, as is happening in this case.

    The call proxyContainer.getProxies() in the last loop returns actually a List (because proxyContainer is of a raw type, all generics relating to its type are removed, including generics in method signatures). And if you iterate over a raw List, you will only get Objects out of it, so there is a compile error. You can resolve this by writing (List<ClientObjectProxy>)proxyContainer.getProxies() in the for loop (you will get a warning of course).

    Thus it is usually clearer to use wildcards instead of raw types. In code which is already “genericified”, never use the raw type ClientObjectProxyContainer, but always ClientObjectProxyContainer<?>, if you don’t have a concrete type. This type has basically the same meaning, but it will not lead to all generic types being ignored when it is used.

    When using the wildcard for the type of proxyContainer, the result type of getProxies() will be List<ClientProxyObject<?>> instead of just List, so you can take ClientProxyObjects out of it (but here, too, prefer to use ClientProxyObject<?>!).

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

Sidebar

Related Questions

This question relates to https://stackoverflow.com/questions/9603/what-is-some-great-source-code-to-read which compiles a list of great (in the sense
I have two questions related to the performance of Dozer using its Java API:
I've looked at other questions related to the GLPaint sample from Apple. Maybe I'm
This questions somehow relates to the question when I was looking to get the
I know that similar questions have been asked all over the place, but I'm
This question relates to Francois Deschenes's answer to one of my previous questions. I
This is actually two questions: The first relates to my program The second is
This post relates to a previous that i posted but the question is different
I have this code: <?php class Compare { private $questions; private $q_scores = array();
This question relates to my previous question which asks about loading a page into

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.