Java Eclipse/STS Tips | Disable formatting for specific section

Sometimes which is a necessary and we get habituated off using that more often then not may cause an undesirable effect. Confused?

Let me give you an example of how a frequently used practise possess issues.
Consider “formatting” (CTRL + SHIFT + F), this has been one of the favourite combination for all eclipse users .

If you are a java or spring developer, then you might have figured it out… right ?

No? No issues .. In Java when we use streams and we intentionally break steps  of the chain logically. But if we intended Eclipse to format all the codes in that .java file, it do not understands our logical breaking and brings everything to one line. Also Spring developers might have faced it during Security Configurations with HTTP chaining.

Before formatting

List persons = new ArrayList();
String name = persons.stream()
		.filter(x -> "sovan".equals(x.toString()))
		.map(String::toLowerCase)						
		.findAny()
		.orElse("");

After formatting

List persons = new ArrayList();
String name = persons.stream().filter(x -> "sovan".equals(x.toString())).map(String::toLowerCase).findAny()
				.orElse("");

Is there a way we can stop formatting for this section of code?

Yes!!!

This feature is there only inside our developer tool.. Hidden secret!! HUH!!
Now let this secret revealed..

  • Go to Windows -> Preferences -> Java -> Code Styling -> “Formatter”
  • Click on Edit on the details section. (You can create your own development profile)
  • In the edit section, choose “On/Off tags”.
  • Check “Enable Off/On Tags”. Leave the default @formatteroff and @formatteron or you can rename the tags.
  • Done. Save your profile.

    This slideshow requires JavaScript.

Now wrap your codes around these off and on tag and eclipse will leave that section  untouched while formatting rest of the code.

If you like this post and is helpful, please do leave a comment below. If anyone knows any other better way of doing this, please do share.

Helps the community grow by sharing knowledge.
Happy Coding
Sovan

 

Java 8 – Default Methods in Interface

Java 8 arrived with new features and advances which has also interface with default methods in its arsenal too.

What we might have heard about default methods in interfaces?

  • Multiple inheritance.
  • Backward Compatibility.
  • Default methods reduces the need of abstract class.

While the first two points are correct but the last point is not though it seems to more or less works like the abstract class. I will keep it for last, and starts with some basic information.

Why we needed Default Methods inside Interface?

As it is said, when there are problems we look around for solutions , this is the case where it get fits. Think of a situation where one needs to declare a new method signature inside an interface, and that interface has been implemented in 100’s of classes.  Hhhuh!! That is a painful task as first it is mandatory to  implement it all the the new method in 100 instances and also we need to handle the situation where we don’t need that function.

What if we are allowed to write a default method definition inside interface?
Listen behind, someone whispering “Java 8 allows you to do so…

Continue reading