A simple example will demonstrate Project Lombok's usefulness. Suppose we add the following minimal Robot
class to the example app's W2A.java
source file:
class Robot
{
private String name;
}
The idea with this class is that we'll store the name of the robot being animated and potentially any other information that might be useful, and display this information as a toast (a small message that appears like a tool tip).
We could take a few minutes to add a constructor and getter/setter methods to this code. However, a better option is to let Lombok add this boilerplate for us. Here are the steps:
- Right-click on
Robot
. - Select Refactor from the pop-up menu.
- Select Lombok from the next pop-up menu.
- Select Default @Data from the next pop-up menu.
You should now observe an @lombok.Data
annotation prefixed to the class Robot
header. This annotation causes the constructor, getter/setter methods, and other boilerplate code to be auto-generated. You can see all of this boilerplate by repeating the previous steps, but this time select Delombok instead of Lombok. You'll see that Lombok has generated something like the following:
class Robot
{
private String name;
public Robot() {
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public boolean equals(final Object o) {
if (o == this) return true;
if (!(o instanceof Robot)) return false;
final Robot other = (Robot) o;
if (!other.canEqual((Object) this)) return false;
final Object this$name = this.getName();
final Object other$name = other.getName();
if (this$name == null ? other$name != null : !this$name.equals(other$name)) return false;
return true;
}
protected boolean canEqual(final Object other) {
return other instanceof Robot;
}
public int hashCode() {
final int PRIME = 59;
int result = 1;
final Object $name = this.getName();
result = result * PRIME + ($name == null ? 43 : $name.hashCode());
return result;
}
public String toString() {
return "Robot(name=" + this.getName() + ")";
}
}
Conclusion to Part 4
Android Studio is an amazing (albeit somewhat buggy) environment for developing Android apps. In this short series I've showed you how to download and install Android Studio, walked you through a short Android app-development tutorial, showed you how to build and run your app on a device emulator, and introduced you to a number of useful tools and plugins for Android development.
There's much more for you to learn. For example, you might want to play with Android Profiler to monitor your app's performance and discover any hidden performance bottlenecks. You might also want to install and explore additional plugins to boost your coding productivity.
I hope you'll use Android Studio with both its built-in tools and extensible plugin architecture to develop your Android mobile apps.
This story, "Android Studio for beginners, Part 4: Debugging tools and productivity plugins" was originally published by JavaWorld.