1

I'm working on a large project so i need to write some code to make sure that people clear all comment code like:

//String a = "something";

I was googled for check if string is java code but i found nothing. Any suggestion? Thank for your support.

Madhawa Priyashantha
  • 9,633
  • 7
  • 33
  • 60
Hash
  • 79
  • 1
  • 7

6 Answers6

2

What you want to do is really hard to get:

You can detect java code parsing lines starting with // and detect comments.

But imagine:

// String = "my cat is blue" + 
//          "seems a smurffle" +
//          "and I love it";

Hoy will you determine 2nd line is human or java comment? by " that does not seem very handy...

I would recommend you to use some IDE plugin like UCDetector for Eclipse or try oracle plugin for NetBeans and remove unused comments and code with more safety.

Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
  • 2
    Also, it would be difficult to detect if contains multiline comments like `line1 - /*..... line2 abcd.....line3.....*/` In this case, line2 will remain undetected. – Harshit Aug 12 '15 at 09:10
  • Yup, i have been working for 6 hour and have no solution yet – Hash Aug 12 '15 at 09:14
  • 2
    or imagine that comments are mixed with code... like: `//Set test = new HashSet<>(); could be used here, for blah blah...`, or `//instead of a TreeSet, use Set test = new HashSet<>();` – vefthym Aug 12 '15 at 09:16
  • as Sum up we can start with an idea that check if String contains "//" in first place and ";" in the ends of line – Hash Aug 12 '15 at 09:23
  • @Hash yes, `string.contains("//");` with `string.endsWith(";");` will clean most part of Java lines, but you must be carefull because `//a human comment;` will be erased also, and you will have orphan lines after this process as pointed in my answer... I think if you start here and you combine it with [Sonar Qube](https://en.wikipedia.org/wiki/SonarQube) as [@hinneLinks](http://stackoverflow.com/users/5072526/hinnelinks) will be more precise and a good start for a nice Java tool :) – Jordi Castilla Aug 12 '15 at 10:07
  • 1
    also, you said you work with japanese people.... by chance ¿¿they comment in japanese?? this will make your work much easier.... – Jordi Castilla Aug 12 '15 at 10:11
2

You can use a static code analyses Tool like SonarQube - they Check for problems by just looking at the code without running it, e.g. not closed Connections etc. There are plenty of so called rules, that check something.

There is also a rule (namend CommentedOutCodeLine) that detects uncommented code.

If you connect that to your continious integration system, you might be able to automate the process - eg. building the code, checking it and send out an email, if some uncommented code was found.

hinneLinks
  • 3,673
  • 26
  • 40
  • Totally agree on this! It's a code quality issue, so code quality measurement and detection beyond uncommented lines of code is a very good idea, especially for a "large project". – Mathias Begert Aug 12 '15 at 09:55
1
... // ... ;

No 100% solution, but for java a final semi-colon is very decisive.

String withoutCommentedOutCode(String line) {
    String clean = line.replaceFirst("\\s*//.*;\\s*$", "");
    if (clean != line) { // Or clean.length() != line.length()
        Logger.getLogger(getClass().getName(), Level.INFO, "Commented code: {0}", line); 
    }
    return clean;
}
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • Thank you but i want to check all line which is commented out java code not just String code – Hash Aug 12 '15 at 09:25
1

You can easily find commented lines on the code, but to find if it's java code is more complicated. Once you find a commented line, you can use a syntax checker and depending on the type of error it returns, find out if it's pure java code (no error returned), or "maybe" java code (depending on the error returned).

Check this code, it uses the compiler to check if the syntax is correct and returns the error: Syntax Checking in Java

Community
  • 1
  • 1
Asoub
  • 2,273
  • 1
  • 20
  • 33
0

The simplest way is to search for comments ending in a ;. Just use grep:

cd /to/your/project/root
grep "//.*;" -r *

If you don't have grep, install cygwin or babun.

Mathias Begert
  • 2,354
  • 1
  • 16
  • 26
0

I just do a little trick and it work with 99% accurate: -First i read line to line of code - Then i check: if((line.contains("//") && line.contains(";") || (line.contains("*/") && line.contains(";")) { Systems.out.print(line); }

Hash
  • 79
  • 1
  • 7