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.

20
  • 1
    can you clarify? put some examples, also, where you have to use this code? Commented Aug 12, 2015 at 8:53
  • 1
    @FastSnail I think this is the point... remove just java commented lines, not the human comments that clarify the code Commented Aug 12, 2015 at 8:59
  • 1
    @Hash .... but why? this is not javascript you wont get any improvements... Commented Aug 12, 2015 at 9:00
  • 1
    Better off manually reviewing the code... Commented Aug 12, 2015 at 9:01
  • 1
    @dotwav my project like 100k line of code so manually reviewing is impossible in gived amount of time. Commented Aug 12, 2015 at 9:04

6 Answers 6

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Also, it would be difficult to detect if contains multiline comments like line1 - /*..... line2 abcd.....line3.....*/ In this case, line2 will remain undetected.
Yup, i have been working for 6 hour and have no solution yet
or imagine that comments are mixed with code... like: //Set<String> test = new HashSet<>(); could be used here, for blah blah..., or //instead of a TreeSet, use Set<String> test = new HashSet<>();
as Sum up we can start with an idea that check if String contains "//" in first place and ";" in the ends of line
also, you said you work with japanese people.... by chance ¿¿they comment in japanese?? this will make your work much easier....
|
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.

1 Comment

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".
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;
}

1 Comment

Thank you but i want to check all line which is commented out java code not just String code
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

Comments

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.

3 Comments

What about multiline comments /**/ which is present in multiple lines.
thank you, check for ";" in comment line will be my start point
@Harshit Yes, /* traditional comments */ will not be detected by this, you're right.
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); }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.