10

Is there a quick way to find all the commented-out code across Java files in Eclipse? Any option in Search, perhaps, or any add-on that can do this?

It should be able to find only code which is commented out, but not ordinary comments.

durron597
  • 31,968
  • 17
  • 99
  • 158
akjain
  • 1,787
  • 3
  • 20
  • 35
  • I doubt you will find something like that. As stated, there might be a plugin that does it, but none that I have come across. – Nico Huysamen Nov 10 '10 at 12:01
  • 2
    Your question isn't clear. Finding comments is easy: search for "/*" or "//". And since comments have no formal relation to code, when is a comment "about" the code nearby, and when is it just a comment ("a sonnet to ...")? What is it you really want to do? – Ira Baxter Nov 10 '10 at 12:01
  • 1
    I think he wants to find code that is commented out, not code that has a comment. – Jorn Nov 10 '10 at 12:18
  • @Ira, here is what I wanted to do & the reason I asked that question: I have a codebase checked out from SCM and have to find out all the files which has commented code. (just to make sure there is no code existing which is not usable/executable any more) – akjain Nov 11 '10 at 09:51

6 Answers6

20

In Eclipse, I just do a file search with the regular expression checkbox turned on:

(/\*.*;.*\*/)|(//.*;)

It will find semicolons in

// These;

and /* these; */

Works for me.

Boann
  • 48,794
  • 16
  • 117
  • 146
Scott Merritt
  • 1,284
  • 2
  • 13
  • 24
  • 1
    It is a good start, even if tt might give some false positives like String s = "http: // some.site"; – Telmo Pimentel Mota Jan 31 '14 at 23:31
  • 1
    To help avoid the false positives from urls, I use `/\*.*;.*\*/|[^:]//.*;|^//.*;`, which requires // to not come after a : – KevinL Mar 05 '14 at 20:45
  • 2
    @KevinL, I don't think that will work with urls of the form `file:///somefile` – Avinash R Mar 07 '14 at 12:54
  • 1
    Nice and quick. For those preferring `rgrep`, don't forget the `-E` needed for the `|` operator in the regular expression. – Harald Jun 30 '17 at 08:32
  • Using semicolon is a good idea, i must say. Hardly 100% precise, but finds most commented-out code blocks. – noamtm Jul 05 '18 at 07:45
4

Sonar can do it: http://www.sonarsource.org/commented-out-code-eradication-with-sonar/

Oliver
  • 56
  • 2
  • 1
    To integrate Sonar & Eclipse, see http://docs.codehaus.org/display/SONAR/Using+Sonar+in+Eclipse – thSoft Dec 17 '12 at 09:41
1

You can mark your own commented code with a task tag. You can create your own task tags in Eclipse.

From the menu, go to Window -> Preferences. In the Preferences dialog, go to General -> Editors -> Structured Text Editors -> Task Tags.

Add an appropriate task tag, like COMMENTED. Set the priority to Low.

Then, any code you comment out, you can mark with the COMMENTED task tag. A list of these task tags, along with their locations, appears in the Tasks view.

Gilbert Le Blanc
  • 50,182
  • 6
  • 67
  • 111
1

If the problem is to find commented-out code, what is needed is a way to find comments, and way to decide if a comment might contain code.

A simple way to do this is to search for comment that contain code-like things. I'd be tempted to hunt for comments containing a ";" character (or some other rare indicator such as "="); it will be pretty hard to have any interesting commented code that doesn't contain this and in my experience with comments, I don't see many that people write that contain this. A regexp search for this should be pretty straightforward, even if it picked up a few addtional false positives (e.g. // in a string literal).

A more sophisticated way to accomplish this is to use a Java lexer or parser. If you have a lexer that returns comments at tokens (not all of them do, Java compilers aren't interested in comments), then you can simply scan the lexemes for a comment and do the semicolon check I described above. You won't get any false positives hits for comment like things in string literals with this approach.

If you have a re-engineering parser that captures comments as part of the AST ( such as our SD Java Front End), you can mechanically scan the parse tree for comments, feed the comment context back to the parser to see if the content is code like, and report any that passes that test modulo some size-depedent error rate (10 errors in 15 characters implies "really is a comment"). Now the "code-like" test requires the reengineering parser be willing to recognize any substring of the (Java) language. Our DMS Software Reengineering Toolkit underlying the Java Front End can actually do that, using access to the grammar buried in the front end, as it is willing to start a parse for any language (non)terminal, and this question is "can you find a sequuence of (non)terminals that consumes the string?".

The lexer and parser approaches are small and big sledgehammers respectively. If OP is going to do this just once, he can stick to the manual regex search. If the problem is to vet the code base repeatedly (needed in big organizations), he'd want a tool that can be run on regular basis.

Ira Baxter
  • 93,541
  • 22
  • 172
  • 341
0

@Jorn said:

I think [the OP] wants to find code that is commented out, not code that has a comment.

If the intention is to find commented out code, then I don't think it is possible in general. The problem is that it is impossible to distinguish between comments that were written as code or pseudo-code, and code that is commented out. Making that distinction requires human intelligence.

Now IDE's typically have a "toggle comments" function that comments out code in a particular way. It would be feasible to write a tool / plugin that matches the style produced by a particular IDE. But that's probably not good enough, especially since reformatting the code typically gets rid of the characteristics that made the commented out code recognizable.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
-1

You can do a search in Eclipse.

All you need to search for is /* and //

However, you will only find the files which contain that expression, and not the actual content which I believe you are after.

However, if you are using Linux you can easily get all the comments with a one liner.

Shervin Asgari
  • 23,901
  • 30
  • 103
  • 143