Annotations are nothing but meta-data added to the Java Elements. Annotations define a java type which is similar to classes, Enums, Interfaces and they can be applied to several Java Elements.
Annotations is a form of metadata which provides data about a program but itself is not part of the program. Annotations have no direct effect on the operation of the code they annotate. Annotations have a number of uses, of which some of them are as follows :
Information for the compiler — Annotations can be used by the compiler either to detect errors or suppress warnings.
Compile-time and deployment-time processing — Software tools can process annotation information to generate code, XML files and so on.
Runtime processing — Some annotations are available to be examined at runtime.
'Deprecated annotation' indicates that the marked element is deprecated and should no longer be used. The compiler generates a warning whenever a program uses a method, class or a field with the 'Deprecated annotation'.
'Override annotation' informs the compiler that the element is meant to override an element declared in a superclass.
'SuppressWarnings annotation' tells the compiler to suppress specific warnings which would be generated otherwise.
'SafeVarargs annotation', when applied to a method or a constructor, asserts that the code does not perform potentially unsafe operations on its 'varargsparameter'. When this annotation type is used, unchecked warnings relating to 'varargs' usage are suppressed.
'FunctionalInterface annotation', introduced in Java SE 8, indicates that the type declaration is intended to be a 'functional interface', as defined by the 'Java Language Specification'.
Annotations were introduced with 5th version of Java.
Let's say for instance a software group traditionally starts the body of every class with comments providing important information:
public class Generation3List extends Generation2List {
// Author: Java by kiran
// Date: 12/15/2019
// Current revision: 1
// Last modified: 3/13/2020
// By: Java by kiran
// Reviewers: Kiran
// class code goes here
}
To add this same metadata with an annotation, you must first define the annotation type. The syntax for doing this is :
@interface ClassPreamble {
String author();
String date();
int currentRevision() default 1;
String lastModified() default "N/A";
String lastModifiedBy() default "N/A";
// Note use of array
String[] reviewers();
}
The annotation type definition looks similar to an interface definition where the keyword interface is preceded by the at sign (@) (@ = AT, as in annotation type). Annotation types are a form of interface, which will be covered in a later lesson. For the moment, you do not need to understand interfaces.
The body of the previous annotation definition contains annotation type element declarations, which look a lot like methods. Note that they can define optional default values.
After the annotation type is defined, you can use annotations of that type, with the values filled in, like this :
@ClassPreamble (
author = "Java by kiran",
date = "12/15/2019",
currentRevision = 1,
lastModified = "3/13/2020",
lastModifiedBy = "Java by kiran",
// Note array notation
reviewers = {"Tom", "Hilton"}
)
'Retention annotation' specifies how the marked annotation is stored.
'Documented annotation' indicates that whenever the specified annotation is used those elements should be documented using the Javadoc tool. (By default, annotations are not included in Javadoc.)
'Target annotation' marks another annotation to restrict what kind of Java elements the annotation can be applied to.
'Inherited annotation' indicates that the annotation type can be inherited from the super class (though this is not true by default). When the user query's the annotation type and if the class has no annotation for this type, the class's superclass is queried for the annotation type. This annotation applies only to class declarations.
'Repeatable annotation' that was introduced in Java SE 8, indicates that the marked annotation can be applied more than once to the same 'declaration' or 'type' in use. For more information, refer 'Repeating Annotations'.
Annotations that apply to other annotations are called "meta-annotations".
Entity
Table
Id
Column
Temporal
Basic
Enumerated
Access
Embeddable
Lob
AttributeOverride
Embedded
GeneratedValue
ElementCollection
JoinTable
JoinColumn
CollectionId
GenericGenerator
OneToOne
OneToMany
ManyToOne
ManyToMany
NotFound
What is the difference between the below given two annotations ?
Entity
Entity (name="EMPLOYEES")
The first annotation will try to map the Class with the Table having the same name as Class whereas the second annotation will specify the Entity name as "EMPLOYEES" and hence will try to map with Table Name "EMPLOYEES".
We will configure Entity classes having annotated mappings.
Test Annotation
The 'Test annotation' indicates that the public void method to which it is attached can be run as a test case.
Before Annotation
The 'Before annotation' indicates that this method must be executed before each test in the class, so as to execute some pre-conditions necessary for the test.
BeforeClass Annotation
The 'BeforeClass annotation' indicates that the static method to which it is attached must be executed once and before all tests in the class.
After Annotation
The 'After annotation' indicates that this method gets executed after execution of each test.
AfterClass Annotation
The 'AfterClass annotation' can be used when a method needs to be executed after executing all the tests in a JUnit Test Case class so as to clean-up the set-up.
Ignore Annotation
The 'Ignore annotation' can be used when you want temporarily disable the execution of a specific test.
Auto , Identity , Sequence and Table are some of the strategies that are used with the @GeneratedValue annotation.
Using an annotation Test with the argument gives us an expected exception.
Test (expected = Exception.class)
We can remove @Test from the respective test in order to avoid its execution. Alternatively we can put @Ignore annotation on the Junit file if we want to ignore all tests in a particular file.
We can either increase the test method's visibility and mark them with annotation @VisibleForTesting or we can use reflection to individually test those methods.
Which of the following annotation is used to avoid executing Junits ?
@explicit
@ignore
@avoid
@NoTest
Answer is "b. @ignore"
This is an informative annotation which specify that the interface is a functional interface. A 'Functional Interface' has only one 'abstract method' and many 'default methods'.
Compiler generates an error if the interface specified with the annotation doesn't abide by the specifications for functional interface.
java.text
is the package name for an annotation class.
'Object' is said to be the parent class of annotation class.
Annotations that are once defined and used in some class, can be read using 'reflection package' methods like getAnnotations(). We have to first obtain the reference to the class which contains or uses the Annotations and then we can write code as shown below:
Class classObj_JBK = MyAnnotedClass.class;
Annotation[] annotations = classObj_JBK.getAnnotations();
for (Annotation annotation : annotations) {
}