Java Tutorial/Language/Annotations Types
Содержание
A marker annotation
<source lang="java">
import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.reflect.Method; @Retention(RetentionPolicy.RUNTIME) @interface MyMarker { } class Marker {
@MyMarker public static void myMeth() { Marker ob = new Marker(); try { Method m = ob.getClass().getMethod("myMeth"); if (m.isAnnotationPresent(MyMarker.class)) System.out.println("MyMarker is present."); } catch (NoSuchMethodException exc) { System.out.println("Method Not Found."); } } public static void main(String args[]) { myMeth(); }
}</source>
Annotations and Annotation Types
Annotations are notes in Java programs to instruct the Java compiler to do something. Java provides three standard annotations and four standard meta-annotations.
- An annotation type is a special interface type.
- An annotation is an instance of an annotation type.
- An annotation type has a name and members.
- The information contained in an annotation takes the form of key/value pairs.
- There can be zero or multiple pairs and each key has a specific type.
- It can be a String, int, or other Java types.
- Annotation types with no key/value pairs are called marker annotation types.
- Those with one key/value pair are referred to single-value annotation types.
- There are three annotation types in Java 5: Deprecated, Override, and Suppress Warnings.
- There are four other annotation types that are part of the java.lang.annotation package: Documented, Inherited, Retention, and Target.
- These four annotation types are used to annotate annotations,
Creates and uses a single-member annotation
<source lang="java">
import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.reflect.Method; @Retention(RetentionPolicy.RUNTIME) @interface MySingle {
int value(); // this variable name must be value
} class Single {
@MySingle(100) public static void myMeth() { Single ob = new Single(); try { Method m = ob.getClass().getMethod("myMeth"); MySingle anno = m.getAnnotation(MySingle.class); System.out.println(anno.value()); // displays 100 } catch (NoSuchMethodException exc) { System.out.println("Method Not Found."); } } public static void main(String args[]) { myMeth(); }
}</source>
Marker Annotations
- Marker Annotations are used to mark a declaration.
- A marker annotation is a special kind of annotation.
- A marker annotation contains no members.
- Using isAnnotationPresent( ) to determine if the marker is present.
<source lang="java">
import java.lang.annotation.Annotation; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.reflect.Method;
@Retention(RetentionPolicy.RUNTIME) @interface MyAnnotation { } @MyAnnotation public class MainClass {
// Annotate a method. @MyAnnotation() public static void myMethod() { } public static void main(String[] arg) { try { MainClass ob = new MainClass(); Method m = ob.getClass( ).getMethod("myMethod"); Annotation[] annos = m.getAnnotations(); System.out.println("All annotations for myMeth:"); for(Annotation a : annos) System.out.println(a); } catch (Exception exc) { } }
}</source>
All annotations for myMeth: @MyAnnotation()
Single-Member Annotations
- Single-Member Annotations contains only one member.
- Single-Member Annotations allow a shorthand form of specifying the value.
- The name of the member must be value.
<source lang="java">
import java.lang.annotation.Annotation; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.reflect.Method;
@Retention(RetentionPolicy.RUNTIME) @interface MyAnnotation {
int value();
} @MyAnnotation(102) public class MainClass {
// Annotate a method. @MyAnnotation(101) public static void myMethod() { } public static void main(String[] arg) { try { MainClass ob = new MainClass(); Method m = ob.getClass( ).getMethod("myMethod"); Annotation[] annos = m.getAnnotations(); System.out.println("All annotations for myMeth:"); for(Annotation a : annos) System.out.println(a); } catch (Exception exc) { } }
}</source>
All annotations for myMeth: @MyAnnotation(value=101)
Some Restrictions
- No annotation can inherit another.
- All methods declared by an annotation must be without parameters.
- Annotations cannot be generic.
- They cannot specify a throws clause.
They must return one of the following:
<source lang="java">
A simple type, such as int or double,
An object of type String or Class An enum type Another annotation type An array of one of the preceding types</source>
The Annotation Interface
- An annotation type is a Java interface.
- All annotation types are subinterfaces of the java.lang.annotation.Annotation interface.
- It has one method, annotation Type, that returns an java.lang.Class object.
<source lang="java">
java.lang.Class<? extends Annotation> annotationType()</source>
The single-value syntax
Using the single-value syntax when applying an annotation that has other members, if other members all have default values.
<source lang="java">
@interface SomeAnno {
int value(); int xyz() default 0;
}
@SomeAnno(88)</source>
All annotations for myMeth: @MyAnnotation(defaultValue=100, value=101)
Using Default Values
- Annotation default values is used if no value is specified.
- A default value is specified by adding a default clause.
- Default value must be of a type compatible with type.
Here is @MyAnnotation rewritten to include default values:
<source lang="java">
@Retention(RetentionPolicy.RUNTIME) @interface MyAnnotation {
String stringValue() default "defaultString"; int intValue() default 101;
}</source>
All annotations for myMeth: @MyAnnotation(intValue=100, stringValue=defaultString)