Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove redundant casts in Java

I've been generifying some Java code that used lots of casts, and now most of them are redundant and unnecessary.

It could be very tedious to inspect all the usages of the code to remove them, so: are there any tools to help to identify (and remove) superfluous casts?

like image 914
fortran Avatar asked Aug 17 '10 13:08

fortran


3 Answers

If you're using Android Studio it's an easy fix: Analyze > Code Cleanup

like image 100
Bobby Avatar answered Nov 14 '22 08:11

Bobby


This can be automatically done using Eclipse Helios. In your project, go to Properties -> Java code Style -> Clean Up. There, edit a profile, go to the "Unnecessary Code" Tab and check "Remove unnecessary casts". Then right-click your source root and klick Source-> Clean up. Job done.

alt text

like image 14
Sylar Avatar answered Nov 14 '22 08:11

Sylar


In Eclipse, under "warnings and errors" you can set "Unnecessary cast or instanceof operations" to be "Warn" or "Error" rather than "Ignore". That should help you find them very quickly, although it won't clean them up for you.

But you can also set a "Save" action configured with an "Additional action" of "Remove unnecessary casts".

And you can right-click on an area of Package Explorer, select "Clean up..." and configure that with a "Remove unnecessary casts" action.

So basically this should be simple :)

like image 4
Jon Skeet Avatar answered Nov 14 '22 08:11

Jon Skeet