写在前面:本篇博客只用于学习笔记
Java内置注解有三个,分别是:
- @Override
- 定义在 java.lang.Override 中 , 此注释只适用于修辞方法 , 表示一个方法声明打算重写超类中
的另一个方法声明.
- 定义在 java.lang.Override 中 , 此注释只适用于修辞方法 , 表示一个方法声明打算重写超类中
- @Deprecated
- 定义在java.lang.Deprecated中 , 此注释可以用于修辞方法 , 属性 , 类。
- 表示不鼓励程序员使用这样的元素 , 通常是因为它很危险或者存在更好的选择 .
- @SupperssWarnings
- 定义在java.lang.SuppressWarnings中,用来抑制编译时的警告信息.
- 与前两个注释有所不同,你需要添加一个参数才能正确使用,这些参数都是已经定义好了的,我们选择性的使用就好了 .
- SuppressWarnings 常用的关键字的表格
关键字 | 解释 |
---|---|
deprecation | 使用了不赞成使用的类或方法时的警告 |
unchecked | 执行了未检查的转换时的警告,例如当使用集合时没有用泛型 (Generics) 来指定集合保存的类型。 |
fallthrough | 当 Switch 程序块直接通往下一种情况而没有 Break 时的警告。 |
path | 在类路径、源文件路径等中有不存在的路径时的警告。 |
serial | 当在可序列化的类上缺少 serialVersionUID 定义时的警告。 |
finally | 任何 finally 子句不能正常完成时的警告。 |
all | 关于以上所有情况的警告。 |
1.@Override
@Override注解源码
package java.lang;
import java.lang.annotation.*;
/**
* @author Peter von der Ahé
* @author Joshua Bloch
* @jls 9.6.1.4 @Override
* @since 1.5
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.SOURCE)
public @interface Override {
}
2.@Deprecated
@Deprecated注解源码
package java.lang;
import java.lang.annotation.*;
import static java.lang.annotation.ElementType.*;
/**
* 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.
*
* @author Neal Gafter
* @since 1.5
* @jls 9.6.3.6 @Deprecated
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(value={CONSTRUCTOR, FIELD, LOCAL_VARIABLE, METHOD, PACKAGE, PARAMETER, TYPE})
public @interface Deprecated {
}
3.@SuppressWarnings
@SuppressWarnings源码
package java.lang;
import java.lang.annotation.*;
import static java.lang.annotation.ElementType.*;
/**
* 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.
*
* <p>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.
*
* @author Josh Bloch
* @since 1.5
* @jls 4.8 Raw Types
* @jls 4.12.2 Variables of Reference Type
* @jls 5.1.9 Unchecked Conversion
* @jls 5.5.2 Checked Casts and Unchecked Casts
* @jls 9.6.3.5 @SuppressWarnings
*/
@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE})
@Retention(RetentionPolicy.SOURCE)
public @interface SuppressWarnings {
/**
* 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 <i>not</i> 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.
*
* <p> The string {@code "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.
* @return the set of warnings to be suppressed
*/
String[] value();
}
4.三注解代码
import java.util.ArrayList;
import java.util.List;
/**
* @ClassName: Test1
* @Description: //TODO
* @Author: sniper-zx
* @Date: 2020/8/3 17:42
* @Version: 1.0
**/
public class Test1 extends Object {
//@Override表示方法重写
//查看JDK帮助文档,
//同时检测写的方法名字是否是父类存在的方法名
@Override
public String toString() {
return super.toString();
}
//方法过时了, 不建议使用 , 可能存在问题 , 并不是不能使用!
// --> 查看JDK帮助文档
@Deprecated
public static void outStyle(){
System.out.println("测试@Deprecated");
}
//@SuppressWarnings 抑制警告 , 可以传参数
//--> 查看JDK帮助文档
//查看源码:发现 参数类型 和 参数名称 , 并不是方法!
@SuppressWarnings("all")
public void war(){
List list = new ArrayList();
}
public static void main(String[] args) {
outStyle();
}
}
写在后面:
参考资料:
【狂神说Java】注解和反射
JavaAnnotation