The BCI has decided to cater to its customers, long interested in this form of trade but reluctant to make large investments. By exploiting the new opportunities provided by the software developed with the Open Source philosophy, we have found some solutions at the forefront and, surprisingly, often exploited by some of the greatest leaders in the industry.

Write less code with Java Lombok PDF Print E-mail

Reduce the lines of code for the implementation of a program is a desire of almost all developers, regardless of the language used. To this end, we report for the Java project worthy of note, who has received the attention of developerWorks, IBM's Lombok.

Like almost all projects Java, Lombok is physically include a JAR archive in the projects should be used, but it is easily integrated with Eclipse, thanks to the wizard included in the same JAR. To work Lombok uses custom annotation.

The most powerful and safe annotation @ Data, which includes in itself the operations of other annotation of Lombok:

 

  • @ Getter / Setter @ to create the usual getter and setter with the usual notation prefix before the variable name for the functions involved;
  • @ ToStringper the classic function toString ();
  • @ EqualsAndHashCode for creating 'HashCode class and function equals.

 

So a file POJO can be written as follows:

  1. import lombok.Data;
  2.  
  3. public @Data class Mountain 
  4. { 
  5. 	private final String name; 
  6. 	private final double latitude, longitude; 
  7. 	private String country; 
  8. }

 

Once built it, what is created is as shown in disassembler javap:

  1. javap Mountain
  2. Compiled from "Mountain.java"
  3. public class Mountain extends java.lang.Object{
  4.     public Mountain(java.lang.String, double, double);
  5.     public java.lang.String getName();
  6.     public double getLatitude();
  7.     public double getLongitude();
  8.     public java.lang.String getCountry();
  9.     public void setCountry(java.lang.String);
  10.     public boolean equals(java.lang.Object);
  11.     public int hashCode();
  12.     public java.lang.String toString();
  13. }

 

This default configuration can be changed: for example we can change something in the behavior of toString () excluding one variable at our option.

  1.  import lombok.Data ; import lombok.Data; 
  2. import lombok.ToString ; import lombok.ToString; 
  3.  @ToString ( exclude = "country" ) @ ToString (exclude = "country") 
  4.  public @Data class Mountain public class @ Data Mountain 
  5.  private final String name ; private final String name; 
  6.  private final double latitude, longitude ; private final double latitude, longitude; 
  7.  private String country ; private String country; 
  8. 	public static void main ( String [ ] args ) { public static void main (String [] args) ( 
  9.     Mountain m = new Mountain ( "Etna" , 37.44 , 15.0 ) ; M = new Mountain Mountain ( "Etna", 37.44, 15.0); 
  10.  System . out . println ( m ) ; System. Out. Println (m); 
  11.  } ) 
  12.  } ) 
  13.  //esempio output standard / / example output standard 
  14. Mountain ( name = Etna, latitude = 37.44 , longitude = 15.0 , country = null ) Mountain (name = Etna, latitude = 37.44, longitude = 15.0, country = null) 
  15.  //output con esclusione / / output with exception 
  16.  Mountain ( name = Etna, latitude = 37.44 , longitude = 15.0 ) Mountain (name = Etna, latitude = 37.44, longitude = 15.0) 

 

Other features of interest are: the annotation @ Cleanup to avoid writing the try / catch and the various close () required in the finally block; @ sychronized, used, as can be imagined, for the definition of protected methods note directly from the same name; and finally @ SneakyThrows, which avoids the use of the directive throws.

 

Select language

Site visit

045776
TodayToday73
YesterdayYesterday237
WeekWeek310
MonthMonth1468
IP:38.107.191.119
Bossoni Consulenza Informatica di Omar Bososni - Quinzano d'Oglio (BS) Via Gramsci, 11 - P.IVA 01935580983
BCI Software & Solutions Template by Ahadesign Powered by BCI!
Banner