You can create Java classes in your Roo project, either by using the IDE of your choice or by using the class
command. If you create a class using Roo, boilerplate code (which includes toString
, and get and setter methods for attributes) is generated automatically and managed by Spring Roo, and is kept in a separate AspectJ ITD file.
The class
command accepts the arguments listed in the following table:
As evident from the list of arguments accepted by the class
command, Spring Roo doesn't provide any argument to let you specify the interface(s) that the generated Java class implements. If you want your Java class to implement one or more interfaces, you need to manually modify your class definition.
As the output from class
command suggests, apart from FlightService.java
, Roo creates a FlightService_Roo_Serializable.aj
file—an AspectJ ITD that makes the FlightService
class implement java.io.Serializable
interface.
The AspectJ ITDs generated by Roo have the following naming convention:
<java-class-name>_Roo_<add-on-name>.aj
Where <java-class-name>
is the name of the Java class to which the AspectJ ITD applies.
<add-on-name>
is the name of Spring Roo add-on responsible for managing the AspectJ ITD
The *_Roo_*.aj
files are managed by Roo and you should not directly modify or delete them.
The following code shows how the FlightService.java
file generates the FlightService
class using the class
command:
In the given code, Roo annotations were added to the generated FlightService
class because we specified the rooAnnotations
argument in the class
command.
To simplify debugging, developers commonly override the toString
method of the java.lang.Object
class to output a string containing the value of all the attributes of the class. With Spring Roo, you are relieved of this task because if your class is annotated with @RooToString
annotation, Spring Roo takes care of creating and updating the toString
method as you add, modify, or remove attributes from your Java class.
When you add an attribute to your FlightService
class, Roo creates a FlightService_Roo_ToString.aj
—an AspectJ ITD that adds the toString
method to the FlightService
class, and a FlightService_Roo_JavaBean.aj
—an AspectJ ITD that adds getters and setters methods for the attributes defined in the FlightService
class. The creation of these aspects is triggered by the presence of @RooToString
and @RooJavaBean
annotations in the FlightService
class.
To see these two ITD files, add the following attribute to FlightService
class:
If your Roo shell is running, as soon as you save the FlightService
class, Roo will generate a FlightService_Roo_ToString.aj
file and a FlightService_Roo_JavaBean.aj
file in the same package as the FlightService
class. If you observe the Roo shell, you will find that Roo reports that it has created a FlightService_Roo_ToString.aj
and FlightService_Roo_JavaBean.aj
files, as shown here:
The following code shows how FlightService_Roo_ToString.aj
AspectJ ITD adds the toString
method to the FlightService
class:
The given code shows that FlightService_Roo_ToString
is a privileged
aspect, that is, it can access even private members of other aspects and classes. The declaration, public String FlightSerivce.toString()
, adds a public
toString
method to the FlightService
class that accepts no arguments and returns a String
. Everything inside the curly-braces is the implementation of the toString
method. Each declaration in an AspectJ ITD file identifies the target of that declaration. In the code, FlightService
in the declaration means that the FlightService
class is the target; therefore, it will add the toString
method to the FlightService
class. In the Adding fields to a Java class recipe, we will see how the toString
method is automatically updated by Spring Roo when you add more attributes to the FlightService
class.
The following figure summarizes how the FlightService_Roo_ToString.aj
file in the previous listing declares adding the toString
method to the Flight
class:
Note
In Spring Roo, AspectJ ITDs are responsible for adding fields, methods, and constructors to Java classes and to make them implement interfaces or extend from a superclass. Spring Roo is responsible for managing these ITDs and you should not directly modify or delete them.
The following code shows the FlightService_Roo_JavaBean.aj
AspectJ ITD file:
The given code shows that FlightService_Roo_JavaBean.aj
is also a privileged
aspect and it introduces two methods into the FlightService
class: getOrigin
and setOrigin
, to get and set the value of the origin
attribute.
The FlightService_Roo_Serializable.aj
AspectJ ITD defines that the FlightService
class implements the java.io.Serializable
interface, as shown here:
In the given code, the declare
parents:
FlightService
implements
Serializable
statement declares that the FlightService
class implements the java.io.Serializable
interface. The following figure summarizes what this declaration means:
The statement private
static
final
long
FlightService.serialVersionUID = 5059552858884348572L
, adds a serialVersionUID
field (it's the field which you define if your class implements the Serializable
interface) to the FlightService
class that contains it.