Viewed 1k times. I am creating a custom annotation Retention RetentionPolicy. Novice User Novice User 3, 4 4 gold badges 27 27 silver badges 47 47 bronze badges. Add a comment. Active Oldest Votes. V is not null. For example: Retention RetentionPolicy. Slaw Slaw 29k 6 6 gold badges 40 40 silver badges 67 67 bronze badges.
Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown.
The Overflow Blog. Podcast Making Agile work for data science. And here we see the beauty of the Java annotation system: if the specified annotation exists, the object returned by this method implements the annotation type interface, and you can query the value of any member simply by invoking the annotation type method that defines that member.
Consider the Reviews annotation that appeared earlier in the chapter, for example. If the annotation type was declared with runtime retention, you could query it as follows:. Note that these reflective methods correctly resolve default annotation values for you. If an annotation does not include a value for a member with a default value, the default value is looked up within the annotation type itself.
An annotation type is an interface, but it is not a normal one. An annotation type differs from a normal interface in the following ways:. An annotation type is defined with the keyword interface rather than with interface.
An interface declaration implicitly extends the interface java. Annotation and may not have an explicit extends clause of its own. The methods of an annotation type must be declared with no arguments and may not throw exceptions.
These methods define annotation members: the method name becomes the member name, and the method return type becomes the member type. The return value of annotation methods may be a primitive type, a String , a Class , an enumerated type, another annotation type, or a single-dimensional array of one of those types.
Any method of an annotation type may be followed by the keyword default and a value compatible with the return type of the method. This strange new syntax specifies the default value of the annotation member that corresponds to the method. The syntax for default values is the same as the syntax used to specify member values when writing an annotation. Annotation types and their methods may not have type parameters—annotation types and members cannot be made generic.
The only valid use of generics in annotation types is for methods whose return type is Class. These methods may use a bounded wildcard to specify a constraint on the returned class.
In other ways, annotation types declared with interface are just like regular interfaces. They may include constant definitions and static member types such as enumerated type definitions.
Annotation types may also be implemented or extended just as normal interfaces are. The classes and interfaces that result from doing this are not themselves annotation types, however: annotation types can be created only with an interface declaration. We now define the annotation types used in our examples. These examples illustrate the syntax of annotation type declarations and demonstrate many of the differences between interface and interface.
We start with the simple marker annotation type Unstable. Because we used this type earlier in the chapter in a reflection example, its definition includes a meta-annotation that gives it runtime retention and makes it accessible to the reflection API.
Meta-annotations are covered below. The next annotation type defines a single member. By naming the member value , we enable a syntactic shortcut for anyone using the annotation:.
The next example is more complex. The Reviews annotation type has a single member, but the type of the member is complex: it is an array of Review annotations. The Review annotation type has three members, one of which has an enumerated type defined as a member of the Review type itself, and another of which has a default value.
Finally, suppose we wanted to annotate methods to list the unchecked exceptions but not errors that they might throw. Our annotation type would have a single member of array type. Each element of the array would be the Class of an exception. In order to enforce the requirement that only unchecked exceptions are used, we use a bounded wildcard on Class :. Annotation types can themselves be annotated. These types and their supporting classes are in the java.
That is, it specifies which program elements may have annotations of that type. If an annotation type does not have a Target meta-annotation, it can be used with any of the program elements described earlier.
Some annotation types, however, make sense only when applied to certain program elements. Override is one example: it is only meaningful when applied to a method. An Target meta-annotation applied to the declaration of the Override type makes this explicit and allows the compiler to reject an Override when it appears in an inappropriate context. The Target meta-annotation type has a single member named value.
The type of this member is java. ElementType is an enumerated type whose enumerated values represent program elements that can be annotated. We discussed annotation retention earlier in the chapter. It specifies whether an annotation is discarded by the compiler or retained in the class file, and, if it is retained in the class file, whether it is read by the VM when the class file is loaded. By default, annotations are stored in the class file but not available for runtime reflective access.
The three possible retention values source, class, and runtime are described by the enumerated type java. The Retention meta-annotation type has a single member named value whose type is RetentionPolicy. Documented is a meta-annotation type used to specify that annotations of some other type should be considered part of the public API of the annotated program element and should therefore be documented by tools like javadoc. Documented is a marker annotation: it has no members.
The Inherited meta-annotation is a marker annotation that specifies that the annotated type is an inherited one. That is, if an annotation type Inherited is used to annotate a class, the annotation applies to subclasses of that class as well. Note that Inherited annotation types are inherited only by subclasses of an annotated class. Classes do not inherit annotations from interfaces they implement, and methods do not inherit annotations from methods they override.
If you use java. Full support is expected in Java 5. Skip to main content. Java in a Nutshell, 5th Edition by David Flanagan. Start your free trial. Annotation Concepts and Terminology. Using Standard Annotations. Annotation Syntax. Annotation member types and values. Annotation targets. Annotations and defaults. Annotations and Reflection.
However you can use an array type and provide an empty array. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more.
Annotation default "null" value Ask Question. Asked 7 years, 6 months ago. Active 2 years, 4 months ago. Viewed 22k times. Is it possible to specify a annotation with a null as default? What I want to achieve is something like optional annotation attributes.
Improve this question. Walery Strauch 5, 7 7 gold badges 48 48 silver badges 54 54 bronze badges. Possible duplicate of Error setting a default null value for an annotation's field — fracz.
0コメント