"Java EE Spring" 17, annotated @suppresswarnings

Source: Internet
Author: User
Tags deprecated

Note @suppresswarnings

The last annotation provided by J2SE is @SuppressWarnings. The purpose of this annotation is to give the compiler a directive that tells it to remain silent about some of the warnings inside the code element being annotated.

@SuppressWarnings annotations allow you to selectively suppress warnings in specific snippets (that is, classes or methods). The idea is that when you see a warning, you'll investigate it, and if you're sure it's not an issue, you can add a @SuppressWarnings comment so you don't see the warning again. While it may sound like it will block potential errors, it will actually improve code security because it will prevent you from being indifferent to warnings-every warning you see will be worth noting.

Http://zhidao.baidu.com/link?url=srtJ0lOakSMsQotStFzuI46L9ne5k3wUfDT_ediXFlf-H24dR0KVo99qQO1cyWG1oMfVwRVbFv2yWM_oE2-yP_

Keyword usage
Deprecation warning when using a class or method that is deprecated
Unchecked a warning when an unchecked conversion is performed, such as when using a collection without generics (generics) to specify the type of collection to save.
Fallthrough a warning when a Switch block directly leads to the next situation without a break.
The path is a warning when there are no paths in the classpath, source file path, and so on.
Serial a warning when a SERIALVERSIONUID definition is missing on a serializable class.
Finally, a warning when any finally clause does not complete properly.
All warnings about all of these cases.

@SuppressWarnings annotations allow you to selectively suppress warnings in specific snippets (that is, classes or methods). The idea is that when you see a warning, you'll investigate it, and if you're sure it's not an issue, you can add a @SuppressWarnings comment so you don't see the warning again. While it may sound like it will block potential errors, it will actually improve code security because it will prevent you from being indifferent to warnings-every warning you see will be worth noting.

Here is an example of using the @SuppressWarnings to cancel the deprecation warning:

publicclass DeprecatedExample2 {  @Deprecated  publicstaticvoidfoo() {  }}publicclass DeprecatedUser2 {  @SuppressWarnings(value={"deprecation"})publicstaticvoidmain(String[] args) {    DeprecatedExample2.foo();  }}

@SuppressWarnings annotation receives a "value" variable, which is an array of strings that indicates the warning that will be canceled. The collection of legitimate strings varies with the compiler, but on the JDK, the same set of keywords that can be passed to-xlint (very handy). and ask the compiler to ignore any keywords they don't recognize, which is handy when you're using a few different compilers.

Because the @SuppressWarnings annotation only receives one parameter and uses a special name of "value" for the parameter, you can choose to omit value= as a handy abbreviation:

publicclass DeprecatedUser2 {  @SuppressWarnings({"deprecation"})publicstaticvoidmain(String[] args) {    DeprecatedExample2.foo();  }}

You can pass any number of string values in a single array parameter to the annotation and place the annotation at any level. For example, the following example code indicates that the deprecation warning for the entire class will be canceled, and only the unchecked and Fallthrough warnings are canceled within the main () method code:

Importjava.util.*;@SuppressWarnings({"Deprecation"}) Public  class nongenerics {  @SuppressWarnings({"Unchecked","Fallthrough"}) Public Static void Main(string[] args)    {runtime.runfinalizersonexit (); List List =NewArrayList (); List.add ("foo"); } Public Static void Foo() {List List =NewArrayList (); List.add ("foo"); }}

is @SuppressWarnings more useful than the first two annotations? That's absolutely true. However, this annotation is not fully supported in JDK 1.5.0, and if you try it with 1.5.%, then it will resemble a no-action instruction. Calling-xlint:-deprecation also has no effect. Sun does not state when it will add support, but it implies that it will be implemented in a dot version that is about to be launched.

Further

If you try to view these properties in the Javadocs page, you may find it difficult to locate them. They are located in the core Java.lang package, but are somewhat hidden, and they appear at the bottom of the Javadoc class, listed behind Exceptions and Errors.

Have you noticed the unfamiliar annotations @Target and @Retention appended to the suppresswarnings annotation? These are called metadata annotations, and they describe where the annotation applies. I'll cover them in the second article in this series and explain how to apply metadata annotations to your own annotations.

Http://zhidao.baidu.com/link?url=eB0Hyrhazjmnw4INMYlVF4A93hFK42ctoTrdzV5OrFSiJBJceIQlxWvNycq6ESEy8XZFgsyU_9Vd06R0hEHNSK

"Java EE Spring" 17, annotated @suppresswarnings

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.