Tutorial :How to search lines which don't contain an empty line before the return statement



Question:

I would prefer to have an empty line prior to the last return statement of any Java method. How do I find and replace such lines using Eclipse?

public int foo() {      ...      ...        System.out.println("end of foo");      return 1;  }  

In this case , I would prefer an empty line before the return statement, do note that there could be any statement before return. How do I find and replace in such occurences.


Solution:1

You could use a regular expression to search for the pattern "; *\n[ \t]*return" and replace the \n with 2 \n characters

Check out this overview of regular expressions in java (only if you need it. You could be a regexp pro for all I know ;) ) http://www.regular-expressions.info/java.html


Solution:2

You can go to Window -> Preferences and expand the trees Java -> Code Style -> Formatter and change/enforce many rules, but I don't think you can check/enforce a new line before 'return' as a built in option in Eclipse.

Instead you'll have to follow Josiah's advice and write your own nifty check-style tool (which are really fun to have).


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »