Indicates that a method declaration is intended to override a method declaration in a supertype. If a method is annotated with this annotation type compilers are required to generate an error message unless at least one of the following conditions hold:
The method does override or implement a method declared in a supertype.
The method has a signature that is override-equivalent to that of any public method declared in Object.
@Override public String toString(){ return"Test{}"; }
@Deprecated
A program element annotated @Deprecated is one that programmers are discouraged from using, typically because it is dangerous, or because a better alternative exists. Compilers warn when a deprecated program element is used or overridden in non-deprecated code.
Indicates that the named compiler warnings should be suppressed in the annotated element (and in all program elements contained in the annotated element). Note that the set of warnings suppressed in a given element is a superset of the warnings suppressed in all containing elements. For example, if you annotate a class to suppress one warning and annotate a method to suppress another, both warnings will be suppressed in the method.
As a matter of style, programmers should always use this annotation on the most deeply nested element where it is effective. If you want to suppress a warning in a particular method, you should annotate that method rather than its class.
The set of warnings that are to be suppressed by the compiler in the annotated element. Duplicate names are permitted. The second and successive occurrences of a name are ignored. The presence of unrecognized warning names is not an error: Compilers must ignore any warning names they do not recognize. They are, however, free to emit a warning if an annotation contains an unrecognized warning name.
The string “unchecked” is used to suppress unchecked warnings. Compiler vendors should document the additional warning names they support in conjunction with this annotation type. They are encouraged to cooperate to ensure that the same names work across multiple compilers.
Returns: the set of warnings to be suppressed
关键字
用途
all
to suppress all warnings
boxing
to suppress warnings relative to boxing/unboxing operations
cast
to suppress warnings relative to cast operations
dep-ann
to suppress warnings relative to deprecated annotation
deprecation
to suppress warnings relative to deprecation
fallthrough
to suppress warnings relative to missing breaks in switch statements
finally
to suppress warnings relative to finally block that don’t return
hiding
to suppress warnings relative to locals that hide variable
incomplete-switch
to suppress warnings relative to missing entries in a switch statement (enum case)
nls
to suppress warnings relative to non-nls string literals
null
to suppress warnings relative to null analysis
rawtypes
to suppress warnings relative to un-specific types when using generics on class params
restriction
to suppress warnings relative to usage of discouraged or forbidden references
serial
to suppress warnings relative to missing serialVersionUID field for a serializable class
static-access
o suppress warnings relative to incorrect static access
synthetic-access
to suppress warnings relative to unoptimized access from inner classes
unchecked
to suppress warnings relative to unchecked operations
unqualified-field-access
to suppress warnings relative to field access unqualified
unused
to suppress warnings relative to unused code
实例
1 2 3 4 5
@SuppressWarnings(value={"unchecked", "rawtypes"}) publicvoidaddItems(String item){ List items = new ArrayList(); items.add(item); }
@Retention:表示需要在什么级别保存该注释信息,用于描述注解的生命周期(SOURCE < CLASS < RUNTIME)
@Document:说明该注解将被包含在javadoc中。
@Inherited:说明子类可以继承父类中的该注解。
@Target
Indicates the contexts in which an annotation type is applicable. The declaration contexts and type contexts in which an annotation type may be applicable are specified in JLS 9.6.4.1, and denoted in source code by enum constants of java.lang.annotation.ElementType.
Indicates how long annotations with the annotated type are to be retained. If no Retention annotation is present on an annotation type declaration, the retention policy defaults to RetentionPolicy.CLASS.
A Retention meta-annotation has effect only if the meta-annotated type is used directly for annotation. It has no effect if the meta-annotated type is used as a member type in another annotation type.
Indicates that annotations with a type are to be documented by javadoc and similar tools by default. This type should be used to annotate the declarations of types whose annotations affect the use of annotated elements by their clients. If a type declaration is annotated with Documented, its annotations become part of the public API of the annotated elements.
Indicates that an annotation type is automatically inherited. If an Inherited meta-annotation is present on an annotation type declaration, and the user queries the annotation type on a class declaration, and the class declaration has no annotation for this type, then the class’s superclass will automatically be queried for the annotation type. This process will be repeated until an annotation for this type is found, or the top of the class hierarchy (Object) is reached. If no superclass has an annotation for this type, then the query will indicate that the class in question has no such annotation.
Note that this meta-annotation type has no effect if the annotated type is used to annotate anything other than a class. Note also that this meta-annotation only causes annotations to be inherited from superclasses; annotations on implemented interfaces have no effect.