Simple Java annotations
There are only three simple annotation types provided by JDK5. These three are used to prevent errors or provide reminders, namely:
1.Override
2.Deprecated
3.Suppresswarnings
It is important to note that JDK5 (another term, Tiger) does not actually have many built-in annotations; instead, it allows core Java to support the ability of annotation features. It is strictly stated in JSR-175 that it is used to define metadata functions. It needs to be written by the programmer Custom annotation types, other JSR standards have also written a series of standard annotation types. The following will use examples to explain these three simple annotations in depth.
Override annotation
The Override annotation indicates that the annotated method needs to override the method in the super class.
If a method uses this annotation but does not override the method in the superclass (for example, the capitalization is wrong, or the parameters are wrong, or the method is defined by the subclass itself), the compiler will generate an error.
(Note: Override annotations cannot be used in JRE5 when implementing methods in interfaces. JRE6 allows it. Many times JRE5 will report this error).
Example 1 demonstrates overriding annotations:
Java annotation example 1:
Copy the code code as follows:
public class Test_Override {
@Override
public String toString() {
return super.toString() + "Test using 'Override' annotation";
}
}
What happens if the method name is misspelled? For example, if you rename the toString method to "tostring" (all lowercase), you will get an error message similar to the following when compiling:
Copy the code code as follows:
Compiling 1 source file to D:tempNew Folder (2)
TestJavaApplication1buildclasses
D:tempNew Folder (2)TestJavaApplication1srctest
myannotationTest_Override.java:24: method does not override
a method from its superclass
@Override
1 error
BUILD FAILED (total time: 0 seconds)
Of course, Eclipse will directly report a red cross. Now that IDEs have become very easy to use, beginners should not mess with the JDK command line.
Deprecated annotation
This annotation indicates that the compiler should display a warning message if the program calls a deprecated (Deprecated, obsolete, obsolete) element. Example 2 shows how to use the Deprecated annotation.
Java annotation example 2
First, create a class and mark a method as deprecated like this:
Copy the code code as follows:
public class Test_Deprecated {
@Deprecated
public void doSomething() {
System.out.println("Test using deprecated annotation: 'Deprecated'");
}
}
Next, try calling this method from another class:
Copy the code code as follows:
public class TestAnnotations {
public static void main(String arg[]) throws Exception {
new TestAnnotations();
}
public TestAnnotations() {
Test_Deprecated t2=new Test_Deprecated();
t2.doSomething();
}
The doSomething() method in this example is declared as a deprecated method. Therefore, this method should not be called under normal circumstances. There will be no warning message when compiling the Test_Deprecated.java file. But when compiling TestAnnotations.java, the compiler A warning message similar to this will be given (Eclipse will warn):
Copy the code code as follows:
Compiling 1 source file to D:tempNew Folder
(2)TestJavaApplication1buildclasses
D:tempNew Folder
(2)TestJavaApplication1srctestmyannotation
TestAnnotations.java:27:
warning: [deprecation] doSomething() in
test.myannotation.Test_Deprecated has been deprecated
t2.doSomething();
1 warning
Suppresswarnings annotation
This annotation tells the compiler that warning messages for the annotated element and all sub-elements should be suppressed. All warning messages for a set of elements and sub-elements will be suppressed. For example, suppose you use the Suppresswarnings annotation on a class to suppress a warning. If you use the Suppresswarnings annotation on one method to suppress another warning, both warnings will be suppressed at the method level. See Example 3.
Java annotation example 3:
Copy the code code as follows:
public class TestAnnotations {
public static void main(String arg[]) throws Exception {
new TestAnnotations().doSomeTestNow();
}
@SuppressWarnings({"deprecation"})
public void doSomeTestNow() {
Test_Deprecated t2 = new Test_Deprecated();
t2.doSomething();
}
}
In this example, @SuppressWarnings is used to suppress the deprecation warning message shown in Example 2. Because this type of warning for this method is suppressed, you will no longer see the "deprecation" warning.
Note: It is better to use this annotation on the innermost element. Therefore, if you only want to suppress a warning on a specific method, you should annotate it on the method rather than using the annotation on the class.
Meta-Annotations (Java annotation type)
Meta-annotations, actually called annotations, include four types. They are:
1.Target
2.Retention
3.Documented
4.Inherited
Target annotation
The Target annotation indicates which target element the annotation type applies to. It contains the following enumeration type values:
1.@Target(ElementType.TYPE)--can be applied to elements of any class
2.@Target(ElementType.FIELD) - only applies to fields or properties
3.@Target(ElementType.METHOD)--only applicable to method annotations
4.@Target(ElementType.PARAMETER) - only applies to method parameters
5.@Target(ElementType.CONSTRUCTOR)--only applicable to constructors
6.@Target(ElementType.LOCAL_VARIABLE)--only applicable to local variables
7.@Target(ElementType.ANNOTATION_TYPE)——Indicates that the declared type itself is an annotation type
Example 4 demonstrates the Target annotation:
Java annotation example 4
First, an annotation type named Test_Target is defined, with the @Target meta-annotation, as shown below:
Copy the code code as follows:
@Target(ElementType.METHOD)
public @interface Test_Target {
public String doTestTarget();
}
Next, create a class that will be annotated with Test_Target:
Copy the code code as follows:
public class TestAnnotations {
public static void main(String arg[]) {
new TestAnnotations().doTestTarget();
}
// Use annotations on methods, OK.
// There can be no line breaks in the middle, 2 lines, etc. Java ignores redundant line breaks.
@Test_Target(doTestTarget="Hello World!")
public void doTestTarget() {
System.out.printf("Testing Target annotation");
}
}
The @Target(ElementType.METHOD) annotation indicates that the annotation type can only be used to annotate methods. If you compile this code, no warning message will be displayed. However, what will happen if this annotation is declared on a string variable? What? Like this:
Copy the code code as follows:
public class TestAnnotations {
// This is a wrong approach, and the compilation will not pass because the annotation is at the wrong level.
// Meta-annotations indicate that only methods can be annotated, and they cannot be used to annotate properties.
@Test_Target(doTestTarget="Hello World!")
private String str;
public static void main(String arg[]) {
new TestAnnotations().doTestTarget();
}
public void doTestTarget() {
System.out.printf("Testing Target annotation");
}
}
The only change is that the annotation declaration moves from the method level to the field level, which is incorrect. Because you have defined the annotation @Test_Target only applies at the method level, if you try to compile this class, you may get an error message like this:
Copy the code code as follows:
"TestAnnotations.java":
D:R_AND_DTestAnnotationsrctestmyannotation
TestAnnotations.java:16:
annotation type not applicable to this kind of declaration at line
16, column 0
@Test_Target(doTestTarget="Hello World!")
^
Error in javac compilation