3 Replies Latest reply: Mar 13, 2010 2:22 PM by jschellSomeoneStoleMyAlias RSS

    any way to search for casts in java source code?

    843810
      Anyone know of a decent way to search your java source files to find all the casts?

      I ended up doing a simple text search for
           " = ("
      (quote marks not included) which works for my code because I am strict about writing my casts with spaces like
           String s = (String) iterator.next();
      Unfortunately, the above search has all kinds of problems with both false positives and negatives. It picks up lots of irrelevant lines like
           int index = (number > 0) ? 0 : 1;
      as well as misses casts that appear nested inside expressions like
           ((String) iter.next()).charAt(...)
      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.

      I think that the only way to properly do this is to have a tool which understands java syntax.

      Anyone know of an IDE which will do this? Does IntelliJ or Netbeans support this kind of search?

      In case you are wondering why I am interested in this, it is because I am refactoring some code to fully use generics, and searching for casts is one source of identifying candidates for genericity.
        • 1. Re: any way to search for casts in java source code?
          843810
          Better late than never?

          I came across this question because I am running in to a similar problem and was searching for a solution. The difference is I am not searching for candidates to refactor but rather have refactored and wanted to find all of my unnecessary casts.

          The answer I found to ID unnecessary casts was, using Eclipse IDE, In compiler error and warning preferences to warn on unnecessary casts.

          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?
          • 2. Re: any way to search for casts in java source code?
            843810
            cliffblob wrote:
            Better late than never?
            Yes!
            cliffblob wrote:
            ...The answer I found to ID unnecessary casts was, using Eclipse IDE, In compiler error and warning preferences to warn on unnecessary casts.
            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".

            cliffblob wrote:
            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?
            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.
            • 3. Re: any way to search for casts in java source code?
              jschellSomeoneStoleMyAlias
              cliffblob wrote:
              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?
              You would need a java parser. At the point where it identified a cast it would print the location.