Posted on 2011-10-07 15:05
leekiang 閱讀(505)
評論(0) 編輯 收藏 所屬分類:
java
1,SuppressWarnings的作用是抑制編譯器產生警告信息。
@SuppressWarnings("unused")
@SuppressWarnings("unchecked")
eclipse支持的
SuppressWarning
的值如下,其他開發工具略有差異。
- 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
- 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 to 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
2,注解
注解是加入到java源代碼中的一些描述性的數據,本身不能執行。可利用反射(當RetentionPolicy=
RUNTIME)或文本解析取得注解信息。
@Target,@Retention為元注解。
SuppressWarnings的源碼如下:
@Target({TYPE,?FIELD,?METHOD,?PARAMETER,?CONSTRUCTOR,?LOCAL_VARIABLE})
@Retention(RetentionPolicy.SOURCE)
public?@interface?SuppressWarnings?{
????String[]?value();
}
package?java.lang.annotation;
public?enum?RetentionPolicy?{
????/**
?????*?Annotations?are?to?be?discarded?by?the?compiler.
?????*/
????SOURCE,
????/**
?????*?Annotations?are?to?be?recorded?in?the?class?file?by?the?compiler
?????*?but?need?not?be?retained?by?the?VM?at?run?time.??This?is?the?default
?????*?behavior.
?????*/
????CLASS,
????/**
?????*?Annotations?are?to?be?recorded?in?the?class?file?by?the?compiler?and
?????*?retained?by?the?VM?at?run?time,?so?they?may?be?read?reflectively.
?????*
?????*?@see?java.lang.reflect.AnnotatedElement
?????*/
????RUNTIME
}
package?java.lang.annotation;
public?enum?ElementType?{
????TYPE,
????FIELD,
????METHOD,
????PARAMETER,
????CONSTRUCTOR,
????LOCAL_VARIABLE,
????ANNOTATION_TYPE,
????PACKAGE
}
3,泛型
????public?<T>?T?testT(List<T>??list){
????????T?t?=(T)?list;
????????return?t;
????}
4,自動裝箱
(AutoBoxing)
關于row type
http://stackoverflow.com/questions/2770321/what-is-a-raw-type-and-why-shouldnt-we-use-it/
http://www.tkk7.com/sevenguin/archive/2011/04/20/348628.html
轉:J2SE5中的最新注釋功能SuppressWarningshttp://wenku.baidu.com/view/9d20440f844769eae009edf2.html
實戰篇:設計自己的AnnotationSupported Values for @SuppressWarnings