GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||
GeezerVison is showing jenkov.com — Java Annotations Java Language
Java Annotations
Java annotations are used to provide meta data for your Java code. Being meta data, Java annotations do not directly affect the execution of your code, although some types of annotations can actually be used for that purpose. Java annotations were added to Java from Java 5. This text covers Java annotations as they look in Java 8, Java 9 and beyond. As far as I know, Java annotations have not changed in later Java version, so this text should be valid for Java 8, 9, 10 and 11 programmers too. Java Annotations Video TutorialI have created a set of videos that explain how Java Annotations work, and how to create your own Java Annotations. Here is a link to the YouTube playlist: Java Annotations - Playlist Here is the first video in the Java annotations video tutorial playlist: Java Annotation PurposesJava annotations are typically used for the following purposes:
Java has 3 built-in annotations that you can use to give instructions to the Java compiler. These annotations are explained in more detail later in this text. Java annotations can be be used at build-time, when you build your software project. The build process includes generating source code, compiling the source, generating XML files (e.g. deployment descriptors), packaging the compiled code and files into a JAR file etc. Building the software is typically done by an automatic build tool like Apache Ant or Apache Maven. Build tools may scan your Java code for specific annotations and generate source code or other files based on these annotations. Accessing Java Annotations via Java ReflectionNormally, Java annotations are not present in your Java code after compilation. It is possible, however, to define your own annotations that are available at runtime. These annotations can then be accessed via Java Reflection, and used to give instructions to your program, or some third party API. I have covered how to access Java annotations via reflection in my Java Reflection and Annotations tutorial. Annotation BasicsA Java annotation in its shortest form looks like this: @Entity
The Annotation ElementsA Java annotation can have elements for which you can set values. An element is like an attribute or parameter. Here is an example of a Java annotation with an element: @Entity(tableName = "vehicles")
The annotation in this example contains a single element named An annotation can contain multiple elements. Here is a multiple element Java annotation example: @Entity(tableName = "vehicles", primaryKey = "id")
In case an annotation contains just a single element, it is convention to name that element
@InsertNew(value = "yes")
When an annotation just contains a single element named @InsertNew("yes")
Annotation PlacementYou can place Java annotations above classes, interfaces, methods, method parameters, fields and local variables. Here is an example annotation added above a class definition: @Entity
public class Vehicle {
}
The annotation starts with the Here is a bigger example with annotations above both the class, fields, methods, parameters and local variables: @Entity
public class Vehicle {
@Persistent
protected String vehicleName = null;
@Getter
public String getVehicleName() {
return this.vehicleName;
}
public void setVehicleName(@Optional vehicleName) {
this.vehicleName = vehicleName;
}
public List addVehicleNameToList(List names) {
@Optional
List localNames = names;
if(localNames == null) {
localNames = new ArrayList();
}
localNames.add(getVehicleName());
return localNames;
}
}
The annotations are again just annotations I have made up. They have no specific meaning in Java. Built-in Java AnnotationsJava comes with three built-in annotations which are used to give the Java compiler instructions. These annotations are:
Each of these annotations are explained in the following sections. @Deprecated
The @Deprecated
public class MyComponent {
}
The use of the
You can also use the
When you use the @Deprecated
/**
@deprecated Use MyNewComponent instead.
*/
public class MyComponent {
}
@Override
The
The
Here is an public class MySuperClass {
public void doTheThing() {
System.out.println("Do the thing");
}
}
public class MySubClass extends MySuperClass{
@Override
public void doTheThing() {
System.out.println("Do it differently");
}
}
In case the method @SuppressWarnings
The
Here is a @SuppressWarnings
public void methodWithWarning() {
}
@Contended
The Creating Your Own Java AnnotationsIt is possible to create your own (custom) Java annotations. Annotations are defined in their own file, just like a Java class or interface. I have created a video tutorial about creating your own Java Annotations, by the way. You can find it just below this paragraph. If you prefer a textual explanation, just scroll down over the video and continue reading. Create Your Own Java Annotations Video TutorialCustom Java Annotation ExampleHere is custom Java annotation example: @interface MyAnnotation {
String value();
String name();
int age();
String[] newNames();
}
This example defines an annotation called Notice that each element is defined similarly to a method definition in an interface. It has a data type and a name. You can use all primitive data types as element data types. You can also use arrays as data type. You cannot use complex objects as data type. To use the above annotation, you could use code like this: @MyAnnotation(
value="123",
name="Jakob",
age=37,
newNames={"Jenkov", "Peterson"}
)
public class MyClass {
}
As you can see, I have to specify values for all elements of the Element Default ValuesYou can specify default values for an element. That way the element becomes optional and can be left out. Here is an example of how the annotation definition looks with a default value for an element: @interface MyAnnotation {
String value() default "";
String name();
int age();
String[] newNames();
}
The @MyAnnotation(
name="Jakob",
age=37,
newNames={"Jenkov", "Peterson"}
)
public class MyClass {
}
Notice that the @Retention
You can specify for your custom annotation if it should be available at runtime, for inspection
via reflection. You do so by annotating your annotation definition with the import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation {
String value() default "";
}
Notice the @Retention(RetentionPolicy.RUNTIME) This is what signals to the Java compiler and JVM that the annotation should be available via reflection at runtime. Accessing annotations at runtime is covered in my Java Reflection and Annotations tutorial, which is part of my Java Reflection Tutorial.
The
@Target
You can specify which Java elements your custom annotation can be used to annotate.
You do so by annotating your annotation definition with the import java.lang.annotation.ElementType;
import java.lang.annotation.Target;
@Target({ElementType.METHOD})
public @interface MyAnnotation {
String value();
}
This example shows a Java annotation that can only be used to annotate methods.
The
Most of these are self explaining, but a few are not. Therefore I will explain the targets which are not obvious.
The
The @Inherited
The java.lang.annotation.Inherited
@Inherited
public @interface MyAnnotation {
}
@MyAnnotation
public class MySuperClass { ... }
public class MySubClass extends MySuperClass { ... }
In this example the class @Documented
The import java.lang.annotation.Documented;
@Documented
public @interface MyAnnotation {
}
@MyAnnotation
public class MySuperClass { ... }
When generating JavaDoc for the
You will not use the Next: Java Lambda Expressions
Close TOC
All Trails Trail TOC Page TOC Previous Next | ||||||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. | ||||||