Generics in legacy code

We've got a fairly large amount of code that just made the jump to Java 5. We've been using generics in those components targeted at being released in the Java 5 version, but the remaining code is, of course, full of raw types. I've set the compiler to generate an error for raw types and started manually clearing them, but at the present rate it'll take a very long time to go through with it (there are about 2500 errors). And that's with Eclipse's helpful Infer Generic Type quick fix, which always gets rid of the errors, but often generates code that needs further work.

Is there any better way to dealing with this? Are there any automated tools better than Eclipse? Any way to apply the refactoring to all occurences instead of doing them one-by-one? Or do you just ignore the warnings?


Asked by: Ryan473 | Posted: 28-01-2022






Answer 1

I would suggest ignoring the warnings. Otherwise, you'll be putting a lot of time into updating the legacy code without making any improvements to its functionality.

Update: Great comment from Luke that I thought should get more visibility: "Generics are a way to catch run time bugs at compile time. Unless this legacy code has bugs in it that you think are related to casting I would leave it alone (if it ain't broke, don't fix it)"

Answered by: Chelsea350 | Posted: 01-03-2022



Answer 2

As far as I know, you're going about it as efficiently as possible. It's obviously not perfect, but you'll finish eventually.

I recommend that you do it in stages, however; there are likely parts of the code that would benefit more from this than others, focus on those. Trying to do it all in one sweep runs the risk of introducing new bugs to your code. We have one such place where we have a collection that holds context-dependent data, and generics actually can't work for it.

Basically, do what you're doing, but do it in stages as part of other work, instead of trying to fix it all in one throw.

Answered by: Charlie357 | Posted: 01-03-2022



Answer 3

Faced with a similar challenge, we opted to upgrade to Java 5 style generics only in the code that was edited for another reason. So if you have a bug to fix in DoItFast.java, update DoItFast.java to use Java 5 style generics. The areas of the code that are being edited and changed regularly will get updated quickly. After a few weeks or months of this behavior, you can reevaluate the condition of your code base.

If that doesn't get the job done fast enough for you, sometimes I'll use the slow time after lunch to just mindlessly cleanup a few classes and speed the process along.

Answered by: Ada368 | Posted: 01-03-2022



Answer 4

I don't think it is necessary to update all the old code. Maybe if you could somehow identify which parts of the old code are used frequently, and only update those to use generic types? Or maybe you could only worry when the raw type is returned from a public function? A lot of these cases are probably just private/local variables, which are already written without generic types and presumably work just fine, so it's probably not worth the effort to rewrite them.

Answered by: Paul112 | Posted: 01-03-2022



Answer 5

I understand with IDEA you can select all your classes, popup the context menu and select Refactor | Generify. Job done. It's certainly a more enjoyable experience to work with generified code (IMO).

Answered by: Emma615 | Posted: 01-03-2022



Similar questions

java - Use generics with legacy code

In my project, the external API we used is not genericfied, so there's a class called ItemList which is an implementation of java.util.List, it holds a list of Item objects. however in our new code we express this as List<Item>, I want to write a method that can takes both ItemList and List<Item>, I tried this kind of signature: ...


Java Generics Syntax for arrays

What data structure does the following declaration specify? List<ArrayList>[] myArray; I think it should declare an array where each element is a List (e.g., a LinkedList or an ArrayList) and require that each List contain ArrayList objects. My reasoning: List<String> someList; ...


generics - How do I write a Java function that returns a typed instance of 'this' and works when extended?

This is a bit of a lazyweb question but you get the rep so :-) I have a Java class that returns instances of itself to allow chaining (e.g. ClassObject.doStuff().doStuff()) For instance: public class Chainer { public Chainer doStuff() { /* Do stuff ... */ return this; } } I would like to extend this class. Is there a way, perhaps using generics, t...


generics - Java Class Type

I have a block of code that works and I wanted to ask what exactly is happening here? Class<?> normalFormClass = null; ---added--- The wildcard "<?>" is the part that is confusing for me. Why would this be used rather than not using it (generics)?


How to use generics in a world of mixed Java versions?

I like generics a lot and use them whereever I can. Every now and then I need to use one of my classes in another project which has to run on an old JVM (before 5.0), needs to run on JavaME (where generics are not allowed neither) or in Microsoft J# (which has VERY poor Support for generics). At the moment, I remove all generics manually, which means inserting many casts as well. Since generics are said to ...


C# vs Java generics

This question already has answers here:


In Java, when should I use "Object o" instead of generics?

For example, I could write either of these: class example <T> { ... public void insert (T data) { ... } } or class example { ... public void insert (Object o) { ... } } Is there a signficant difference between the 2 in terms of performance? With generics I could restrict the type of the parameter and ...


generics - Why can't the Java compiler figure this out?

Why is the compiler unable to infer the correct type for the result from Collections.emptySet() in the following example? import java.util.*; import java.io.*; public class Test { public interface Option<A> { public <B> B option(B b, F<A,B> f); } public interface F<A,B> { public B f(A a); } public Collection<String> getColl() { Opt...


java - How do you access Class object for generics?

How can I access Class object for Generics? Currently, I am doing it this way: List<String> list= new ArrayList<String>(); list.getClass(); Is this OK? Or, what should be the way?


How do I fix this Java generics wildcard error?

In this question, TofuBeer was having problems creating a genericized IterableEnumeration. The answer came from jcrossley3 pointing to this link http://www.javaspecialists.eu/archive/Issue107.html which pretty much solved the p...


Java: Generics won't work for my method, what else can I do?

In the code below, I would like the second method to be generic, too, but since I create the Calendar object inside the method, because of type erasure, I don't see how. One possibility would be to pass in the Calendar object, but that would defeat the main purpose for having this method at all (not h...






Still can't find your answer? Check out these amazing Java communities for help...



Java Reddit Community | Java Help Reddit Community | Dev.to Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)



top