Annotation parameters

Annotations, as we saw, can have parameters. To declare these parameters in the @interface declaration of the annotation, we use methods. These methods have a name and a return value, but they should not have an argument. You may try to declare some parameters, but the Java compiler will be strict and will not compile your code.

The values can be defined at the place where the annotation is used, using the name of the method and with the = character, assigning to them some value that is compatible with the type of the method. For example, let's suppose that we modify the declaration of the annotation PoweredDevice to the following:

public @interface ParameteredPoweredDevice { 
String myParameter();
}

In such a case, at the use of the annotation, we should specify some value for the parameter, such as the following:

@Component 
@ParameteredPoweredDevice(myParameter = "1966")
public class NeedPowercord implements ConsistencyChecker {
...

If the name of the parameter is a value and at the place of use of the annotation there is no other parameter defined, then the name, "value", may be skipped. For example, modifying the code as follows is a handy shorthand when we have only one parameter:

public @interface ParameteredPoweredDevice{ 
String value();
}
...
@Component
@ParameteredPoweredDevice("1966")
public class NeedPowercord implements ConsistencyChecker {
...

We can define optional parameters also using the default keyword following the method declaration. In this case, we have to define a default value for the parameter. Modifying the sample annotation we have further, we still can, but need not, specify the value. In the latter case, it will be an empty string:

public @interface ParameteredPoweredDevice { 
String value() default "";
}

Since the value we specify should be constant and calculable during compile time, there is not much use of complex types. Annotation parameters are usually strings, integers, and sometimes, doubles, or other primitive types. The exact list of the types given by the language specification is as follows:

  • Primitive (double, int, and so on)
  • String
  • Class
  • An enum
  • Another annotation
  • An array of any of the aforementioned types

We have seen examples of String and also that enum:Retention and Target both have enum parameters. The interesting part we want to focus on is the last two items of the preceding list.

When the value of the parameter is an array, the value can be specified as comma-separated values between the { and} characters. For example:

String[] value();

This can then be added to the @interface annotation we can write:

@ParameteredPoweredDevice({"1966","1967","1991"})

However, in case there is only one value we want to pass as the parameter value, we can still use the format:

@ParameteredPoweredDevice("1966")

In this case, the value of the attribute will be an array of length 1. When the value of an annotation is an array of annotation types, things get a bit more complex. We create an @interface annotation (note the plural in the name):

@Retention(RetentionPolicy.RUNTIME) 
public @interface PoweredDevices {
ParameteredPoweredDevice[] value() default {};
}

The use of this annotation could be as follows:

@PoweredDevices( 
{@ParameteredPoweredDevice("1956"), @ParameteredPoweredDevice({"1968", "2018"})}
)

Note that this is not the same as having the ParameteredPoweredDevice annotation with three parameters. This is an annotation that has two parameters. Each parameter is an annotation. The first has one string parameter and the second has two.

As you can see, annotations can be fairly complex, and some of the frameworks (or rather the programmers who created them) ran amok using them. Before you start writing a framework, research to see whether there is already a framework that you can use. Also, check whether there is some other way to solve your problem. 99% of annotation handling code could be avoided and made simpler. The less code we write for the same functionality, the happier we are. We programmers are the lazy types and this is the way it has to be.

The last example, where the parameter of the annotation is an array of annotations, is important to understand how we can create repeatable annotations.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.141.219