开发者

Remove redundant casts in Java

I've been ge开发者_高级运维nerifying 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?


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.

Remove redundant casts in Java


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


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 :)


You should probably use a tool to analyze your Java source code for potential problems (such as unnecessary casting).

Perhaps you can try PMD: http://pmd.sourceforge.net


If you use Eclipse, this should be fairly easy. Right click the class/package/project and select Source - Clean Up....

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜