Annotations in Java is a very powerful enhancement that can save a lot of time for developers and programmers. Basically, it enables programmers to avoid creating bunch of configurations based on XML, database records, etc. Instead they can annotate certain classes or fields to achieve different configurations. That is how now we don't declare our servlets in web.xml with its class and URL pattern; instead we annotate them by typing @WebServlet in the Java file.
OK, the use case: Let's assume that we are developing a tax processing program. We have an entity class called Citizen.java which looks something like following:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Citizen { | |
private String name; | |
private Date registrationDate; | |
private Date birthDate; | |
private Double income; | |
} |
Obviously, some of the fields are irrelevant for formula designing. So, we define an annotation and annotate those fields which are relevant.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.lang.annotation.ElementType; | |
import java.lang.annotation.Retention; | |
import java.lang.annotation.RetentionPolicy; | |
import java.lang.annotation.Target; | |
@Retention(RetentionPolicy.RUNTIME) | |
@Target({ElementType.FIELD, ElementType.PARAMETER}) | |
public @interface Calculated { | |
String name(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Citizen { | |
private String name; | |
@Calculated(name="Registration date") | |
private Date registrationDate; | |
private Date birthDate; | |
@Calculated(name="Income") | |
private Double income; | |
} |
Finally, what and why are doing it is that we are building drop-down menus for setting variables in each formula we define. We are not able to fetch those fields annotated with @Calculated to fill those drop-down menus.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Field [] allFields = Citizen.class.getFields(); | |
List<Field> calculatedFields = new ArrayList<Field>(); | |
for (Field field : allFields) { | |
if (field.isAnnotationPresent(Calculated.class)) { | |
calculatedFields.add(field); | |
} | |
} |
The ways to using these kind of functionality are left for your imaginations.
Of course, using reflections in run-time may make your program run slower. However, you can always cache the annotated fields list in your favorite embedded key-value store database.