(quote marks not included) which works for my code because I am strict about writing my casts with spaces like
" = ("
Unfortunately, the above search has all kinds of problems with both false positives and negatives. It picks up lots of irrelevant lines like
String s = (String) iterator.next();
as well as misses casts that appear nested inside expressions like
int index = (number > 0) ? 0 : 1;
I suppose that one could do a regular expression search for any pair of open and close parens which bound non-blank text, but that would pick up even more non-cast expressions in typical java code.
Better late than never?
cliffblob wrote:Thanks for pointing IDEs out. I just opened IntelliJ, and going back to at least version 7.04 (maybe earlier) they have an inspection for "Redundant type cast".
...The answer I found to ID unnecessary casts was, using Eclipse IDE, In compiler error and warning preferences to warn on unnecessary casts.
cliffblob wrote:The only solutions that I can think of are either a complicated regex search, or you must use some tool like an IDE that understand Java syntax and can determine if a cast is happening.
I would still be interested to know if there is a way to identify casts in general in your source, perhaps now four years later there is a way?