开发者

Automatically update code to Java 5

I have a lot of Java 1.4 code, which I'm in the process of migrating to Java 5. I would like to fix all the compiler warnings about using raw types in 开发者_如何学编程parameterized classes, and am wondering if there is a tool that can automatically transform something like:

List myList = new ArrayList();
myList.add("foo");
String foo = (String) myList.get(0);

to:

List<String> myList = new ArrayList<String>();
myList.add("foo");
String foo = myList.get(0);

I understand that Generics are quite complex and therefore don't expect that such a tool would be able to choose the most correct parameterized type in all cases. However, as long as it can handle simple cases (such as the above), and won't break my code, I'll be satisfied.

Similarly, I would like to change all the old-skool loops, to the simplified for loop introduced in Java 5.

Does such a tool exist?


I would do this using Eclipse.

Select the package structure you need to be upgraded, then right click -> Refactor -> Infer Generic Type Arguments

Imho it's a starting point for what you need.


I think the safest thing for you to do is have a @SuppressWarnings("rawtypes") annotation for your methods. Since your code is already tested to work in 1.4 (i assume) and having generic like syntax inserted that way can cause some nasty bugs to come up. You can have other values for suppresswarnings.

You can also add this annotation to a class

@SuppressWarnings("rawtypes") 
public class MyClass {}

Although i can't think of a tool to do that.

As for the for loop, using the new "for" loop makes sense when you are writing new code. Because its easier. Why change the old style loop? Come on, its not that bad a syntax and certainly not that hard to understand either :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜