About ◎ suppresswarnings ("unchecked ")

Source: Internet
Author: User

Explanation 1:

Block warning information during compilation
The compiler will give a warning when the type conversion is forced.
Add

Program code @ suppresswarnings ("unchecked ")

No warning.

Explanation 2:

Annotation type

When your code may have a warning, such as a security warning, you can use it to eliminate it.

This is described in the API.

Indicates that the specified compiler warning should be removed from the annotation element (and all program elements contained in the annotation element. Note: The warning set that is undisplayed in a given element is a superset that contains all warnings that are undisplayed in the element. For example, if you annotate a class to cancel displaying a warning and annotate a method to cancel displaying another warning, both warnings will be canceled in this method.

Depending on the style, the programmer should always use this annotation on the nested element at the innermost layer, which is valid only when used. If you want to cancel displaying a warning in a specific method, you should comment out the method instead of its class.

 

Explanation 3:

@ Suppresswarnings

The last annotation provided by j2se is@ Suppresswarnings. The purpose of this annotation is to give the compiler an instruction to tell it to remain silent for some internal warnings of the annotated code element.

A little background: j2se 5.0 has added several new features for the Java language, added many new warnings together with them, and promised to add more warnings in the future. You can add-XlintParameters to control whether to report these warnings (as shown above@ Deprecated).

By default, the sun compiler outputs warnings in two simple rows. Add-Xlint:KeywordMark (for example-Xlint: Finally), You can get a complete description of the keyword type error. Add a break number before the keyword and write it-Xlint:-KeywordYou can cancel the warning. (-XlintThe complete list of supported keywords can be found on .) The following is a list:

Keywords Purpose
Deprecation Warning when an unsupported class or method is used
Unchecked Warning when an unchecked conversion is executed. For example, when using a set, you do not use generic to specify the type of the set to be saved.
Fallthrough Warning when the switch block directly leads to the following situation without a break.
Path Warning when a path does not exist in the class path or source file path.
Serial Warning when serialversionuid definition is missing in serializable classes.
Finally Warning when any finally clause cannot be completed normally.
All Warning about all the above situations.

 

@ SuppresswarningsAnnotations allow you to selectively cancel warnings in a specific code segment (that is, a class or method. The idea is that when you see a warning, you will investigate it. If you are sure it is not a problem, you can add@ SuppresswarningsAnnotate so that you no longer see the warning. Although it sounds like it will block potential errors, it will actually improve code security as it will prevent you from being indifferent to warnings
-Every warning you see is worth noting.

Use@ SuppresswarningsTo cancel the deprecation warning:

Public class deprecatedexample2 {@ deprecated public static void Foo () {}} public class deprecateduser2 {@ suppresswarnings (value = {"deprecation"}) public static void main (string [] ARGs) {deprecatedexample2.foo ();}}

 

@ SuppresswarningsAnnotation receives a "value" variable, which is a string array that indicates a warning that will be canceled. The set of valid strings varies with the compiler, but on JDK, it can be passed-XlintIs the same keyword set (very convenient ). It also requires the compiler to ignore any keywords that they cannot recognize, which is very convenient when you use different compilers.

Because@ SuppresswarningsThe annotation only receives one parameter and uses a special name "value" for this parameter. Therefore, you can omit value = as a convenient Abbreviation:

 

Public class deprecateduser2 {@ suppresswarnings ({"deprecation"}) public static void main (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 sample code indicates that the deprecation warning of the entire class will be canceledMain ()The unchecked and fallthrough warnings are canceled in the method code:

Import Java. util. *; @ suppresswarnings ({"deprecation"}) public class nongenerics {@ suppresswarnings ({"unchecked", "fallthrough"}) public static void main (string [] ARGs) {runtime. runfinalizersonexit (); List list = new arraylist (); list. add ("foo");} public static
Void Foo () {list = new arraylist (); list. Add ("foo ");}}

@ SuppresswarningsIs it more useful than the first two annotations? Absolutely. However, JDK 1.5.0 does not fully support this annotation. If you use 1.5.0 to try it, it will be similar to no operation command. Calling-xlint:-deprecation has no effect. Sun does not declare when support will be added, but it implies that this will be implemented in an upcoming dot version.

Further steps

If you try to view these attributes on the javadocs page, it may be difficult to find them. They are located in the core Java. lang package, but a little concealed. They appear at the bottom of the javadoc class and are listed behind the exceptions and errors.

 

Note the strange annotations appended to the suppresswarnings Annotation@ TargetAnd@ Retention? These are called metadata annotations, which describe where the annotation applies. I will introduce them in the second article in this series and how to apply metadata annotations to your own annotations.



Original reprint: http://blog.csdn.net/cfhacker007/article/details/3971357

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.