| Start | ||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Defining annotation types Annotation types are defined with Default valuesMeta-AnnotationsMeta-annotations are annotations that can be applied to annotation types. Special predefined meta-annotation define how annotation types can be used. @TargetThe Multiple values can be added using array notation, e.g. Available Values
@RetentionThe Available values
@DocumentedThe @InheritedThe
Note that only the super-classes are queried: any annotations attached to interfaces in the classes hierarchy will be ignored. @RepeatableThe |
||||||||||||||||||||||||||||||||||||||||||||
| Runtime annotation checks via reflection Java's Reflection API allows the programmer to perform various checks and operations on class fields, methods and annotations during runtime. However, in order for an annotation to be at all visible at runtime, the |
||||||||||||||||||||||||||||||||||||||||||||
| Built-in annotations The Standard Edition of Java comes with some annotations predefined. You do not need to define them by yourself and you can use them immediately. They allow the compiler to enable some fundamental checking of methods, classes and code. @Override This annotation applies to a method and says that this method must override a superclass' method or implement an abstract superclass' method definition. If this annotation is used with any other kind of method, the compiler will throw an error. Concrete superclass Abstract class Does not work The main purpose is to catch mistyping, where you think you are overriding a method, but are actually defining a new one. Note that the meaning of
(This can occasionally cause problems when back-porting code to Java 5.) @Deprecated This marks the method as deprecated. There can be several reasons for this: The specific reason for deprecation can usually be found in the documentation of the API. The annotation will cause the compiler to emit an error if you use it. IDEs may also highlight this method somehow as deprecated @SuppressWarnings In almost all cases, when the compiler emits a warning, the most appropriate action is to fix the cause. In some instances (Generics code using untype-safe pre-generics code, for example) this may not be possible and it's better to suppress those warnings that you expect and cannot fix, so you can more clearly see unexpected warnings. This annotation can be applied to a whole class, method or line. It takes the category of warning as a parameter. It is better to limit the scope of the annotation as much as possible, to prevent unexpected warnings also being suppressed. For example, confining the scope of the annotation to a single-line: The warnings supported by this annotation may vary from compiler to compiler. Only the @SafeVarargs Because of type erasure, There are instances where the use is safe, in which case you can annotate the method with the @FunctionalInterface This is an optional annotation used to mark a FunctionalInterface. It will cause the compiler to complain if it does not conform to the FunctionalInterface spec (has a single abstract method) |
||||||||||||||||||||||||||||||||||||||||||||
| The idea behind Annotations The Java Language Specification (opens new window) describes Annotations as follows: An annotation is a marker which associates information with a program construct, but has no effect at run time. Annotations may appear before types or declarations. It is possible for them to appear in a place where they could apply to both a type or a declaration. Annotations are used for a multitude of purposes. Frameworks like Spring and Spring-MVC make use of annotations to define where Dependencies should be injected or where requests should be routed. Other frameworks use annotations for code-generation. Lombok and JPA are prime examples, that use annotations to generate Java (and SQL) code. This topic aims to provide a comprehensive overview of: |
||||||||||||||||||||||||||||||||||||||||||||
| Compile time processing using annotation processor This example demonstrates how to do compile time checking of an annotated element. The annotationThe The annotation processorThe PackagingTo be applied by the compiler, the annotation processor needs to be made available to the SPI (see ServiceLoader (opens new window)). To do this a text file We'll assume the jar file is called Example annotated classThe following class is example class in the default package with the annotations being applied to the correct elements according to the retention policy. However only the annotation processor only considers the second method a valid annotation target. Using the annotation processor with javacIf the annotation processor is discovered using the SPI, it is automatically used to process annotated elements. E.g. compiling the yields the following output instead of compiling normally. No This could be prevented by specifying the IDE integrationNetbeansAnnotation processors can be used in the NetBeans editor. To do this the annotation processor needs to be specified in the project settings: Result |
||||||||||||||||||||||||||||||||||||||||||||
| Repeating Annotations Until Java 8, two instances of the same annotation could not be applied to a single element. The standard workaround was to use a container annotation holding an array of some other annotation: Java 8 provides a cleaner, more transparent way of using container annotations, using the This tells Java to treat multiple |
||||||||||||||||||||||||||||||||||||||||||||
| Inherited Annotations By default class annotations do not apply to types extending them. This can be changed by adding the ExampleConsider the following 2 Annotations: and If three classes are annotated like this: running this code will print a result similar to this (depending on the packages of the annotation): Note that annotations can only be inherited from classes, not interfaces. |
||||||||||||||||||||||||||||||||||||||||||||
| Getting Annotation values at run-time You can fetch the current properties of the Annotation by using Reflection (opens new window) to fetch the Method or Field or Class which has an Annotation applied to it, and then fetching the desired properties. The output will be |
||||||||||||||||||||||||||||||||||||||||||||
| Annotations for 'this' and receiver parameters When Java annotations were first introduced there was no provision for annotating the target of an instance method or the hidden constructor parameter for an inner classes constructor. This was remedied in Java 8 with addition of receiver parameter declarations; see JLS 8.4.1 (opens new window). The receiver parameter is an optional syntactic device for an instance method or an inner class's constructor. For an instance method, the receiver parameter represents the object for which the method is invoked. For an inner class's constructor, the receiver parameter represents the immediately enclosing instance of the newly constructed object. Either way, the receiver parameter exists solely to allow the type of the represented object to be denoted in source code, so that the type may be annotated. The receiver parameter is not a formal parameter; more precisely, it is not a declaration of any kind of variable (ยง4.12.3), it is never bound to any value passed as an argument in a method invocation expression or qualified class instance creation expression, and it has no effect whatsoever at run time. The following example illustrates the syntax for both kinds of receiver parameter: The sole purpose of receiver parameters is to allow you to add annotations. For example, you might have a custom annotation At one level, the
|
||||||||||||||||||||||||||||||||||||||||||||
| Add multiple annotation values An Annotation parameter can accept multiple values if it is defined as an array. For example the standard annotation The If you only need to set a single value, the brackets can be omitted: Syntax
RemarksParameter typesOnly constant expressions of following types are allowed for parameters, as well as arrays of these types:
|
Read Article
https://develop.horje.com/learn/1434/reference