Java Reference
In-Depth Information
Listing 1-11. A Fatal Annotation Type That Can Be Used with Any Type Use
// Fatal.java
package com.jdojo.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Target;
@Target({ElementType.TYPE_USE})
public @interface Fatal {
}
Listing 1-12. A NonZero Annotation Type That Can Be Used with Any Type Use
// NonZero.java
package com.jdojo.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Target;
@Target({ElementType.TYPE_USE})
public @interface NonZero {
}
The Fatal and NonZero annotation types can be used wherever a type is used. Their uses in the following
contexts are valid:
public class Test {
public void processData() throws @Fatal Exception {
double value = getValue();
int roundedValue = (@NonZero int) value;
Test t = new @Fatal Test();
// More code goes here
}
public double getValue() {
double value = 189.98;
// More code goes here
return value;
}
}
if you do not annotate an annotation type with the Target annotation type, the annotation type can be used as a
modifier for any declaration, except a type parameter declaration.
Tip
 
Search WWH ::




Custom Search