/***************************************************************************************/
此博客主要是在观看张孝祥老师的教学视频的过程中,自己所做的学习笔记,在此以博客的形式分享出来,方便大家学习
,建议大家观看视频,然后以此笔记作为资料回顾复习。
参考资料
张孝祥2010年贺岁视频:Java高新技术
视频下载
疯狂Java讲义
/***********************************
33节视频*******************************************/
注解
注解相当于一种标记,加了注解就相当于为程序加了某种标记,没加就相当于没有加标记,以后javac编译器, 开发工具和其他程序可以用反射来了解你的类,以及各种元素上是否有无何种标记,看你有什么标记,就去做相应的事。
默认情况下标记可以加在包,类,字段,方法,方法的参数以及局部变量上面。
@Override 覆盖方法
@Deprecated 提示过时
@SuppressWarnings("deprecation") 不提示方法过时
@SafeVarargs 不显示
堆污染警告
堆污染
当把一个不带泛型的对象赋给一个带泛型的变量时,往往会发生堆污染。
List list = new ArrayList<Integer>();
list.add(20);//
添加元素引发unchecked警告
List<String> ls = list;//引起“未经检查的转换”的警告编译运行时完全正常
System.out.print(ls.get(0));//引发运行时异常
对于形参个数可变的方法,该形参的类型又是泛型,更容易引发堆污染
public void faultyMethod(List<String>...listStrArray){
List[] listArray = listStrArray;
List<Intefer> myList = new ArrayList<Intefer>();
myList.add(new Random().nextInt(100));
listArray[0] = myList;
Sring s = listStrArray[0].get(0);
}
如果想要“抑制”这个警告,可以使用如下的方式
使用@SafeVarargs修饰引发该警告的方法或者构造器
使用@SuppressWarnings("unchecked")修饰
检查视图
List<String> strList = new ArrayList<>;
List list = strList;
list.add(new Date());//并不会报错,但是取值得时候会报错
list.get(0);//报错
List<String> strList = new ArrayList<>;
List<String> safeList = Collections.checkedList(strList,String.class);
List list = strList;
list.add(new Date());//报错
/***********************************
34节视频
*******************************************/
注解类-----应用了注解的类------对添加了注解的类进行反射操作的的类
信息的信息-----元信息
注解的生命周期三个阶段:java源文件--------class文件------内存中的字节码
javac在把源代码编译成class文件的时候可能会把注解去掉
类加载器把class文件加载到内存中的时候,也有可能会把注解去掉
设计注解的时候,加上
@Retention(
RetentionPolicy.RUNTIME
),
提示注解的声明周期在哪一个阶段
RetentionPolicy.RUNTIME 运行阶段(
只能通过反射获得生命周期为此的注解)
RetentionPolicy.CLASS class阶段(
默认阶段)
RetentionPolicy.SOURCE source阶段
@Override
source阶段
@Deprecated
运行阶段
@SuppressWarnings
source阶段
@Target(
{ElementType.METHOD,ElementType.TYPE
}) 指明该注解只能加在方法跟类上面,如果需要指明该注解既可以作用在方法上也可以作用在类上,则需要指明一个数组,来指明注解作用的对象
ElementType.TYPE //接口、类、枚举、注解
ElementType.FIELD //字段、枚举的常量
ElementType.METHOD //方法
ElementType.PARAMETER //方法参数
ElementType.CONSTRUCTOR //构造函数
ElementType.LOCAL_VARIABLE//局部变量
ElementType.ANNOTATION_TYPE//注解
ElementType.PACKAGE ///包
@Documented
@Documented用于指定被该注解修饰的Annotation类将被javadoc工具提取成文档,如果定义Annotation类时使用了@Documented修饰,则所有使用该Annotation修饰的程序元素的API文档中将会包含该Annotation说明,下面会有示例代码。
自己创建注解类,并应用
自己创建的注解类
@Retention(RetentionPolicy.RUNTIME)//指明该注解存在于运行阶段
@Target({ElementType.METHOD,ElementType.TYPE})//指明该注解既可以加在方法上,也可以加在类上面
public @interface ItcastAnnotation {
String color() default "blue";
String value();
int[] arrayAttr() default {3,4,4};
EnumTest.TrafficLamp lamp() default EnumTest.TrafficLamp.RED;
MetaAnnotation annotationAttr() default @MetaAnnotation("lhm");
}
应用了自定义注解的类
@ItcastAnnotation("abc")
public class AnnotationTest {
/**
* @param args
*/
@SuppressWarnings("deprecation")
@ItcastAnnotation("xyz")
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
System.runFinalizersOnExit(true);
if(AnnotationTest.class.isAnnotationPresent(ItcastAnnotation.class)){//判断这个类是否应用了注解ItcastAnnotation
ItcastAnnotation annotation = (ItcastAnnotation)AnnotationTest.class.getAnnotation(ItcastAnnotation.class);//如果应用了注解,通过反射获得应用在这个类上面的注解
System.out.println(annotation);
}
}
}
访问注解信息的三个方法
getAnnotation(Class<t> class) 返回改程序元素上指定类型的注解,如果该类型的注解不存在,则返回null
Annotation[] getAnnotations() 返回改程序元素上的所有注解
boolean isAnnotationPresent(Class<? extends Annotation> class) 判断改程序元素上是否存在指定类型的注解,如果存在则返回true否则返回false
@Documented作用示例代码
@Documented
public @interface ItcastAnnotation {
}
public class First{
@ItcastAnnotation
@ItcastAnnotation
public void get(){
}
}
使用javac工具为First.java生成文档。如下所示
public void get()
如果去掉注解中的@Documented
public @interface ItcastAnnotation {
}
public class First{
@ItcastAnnotation
@ItcastAnnotation
public void get(){
}
}
使用javac工具为First.java生成文档。如下所示
public void get()
为注解添加属性
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD,ElementType.TYPE})
public @interface ItcastAnnotation {
String color() default "blue";//default 设定属性的默认值,这样如果在应用注解的时候没有给该属性赋值得话,并不会报错,该属性值采用这个默认值
String value();//特殊的属性,当只需要给这一个属性赋值的时候,可以直接省略掉等号,例: @ItcastAnnotation("xyz")
int[] arrayAttr() default {3,4,4};//数组属性,当数组属性有多个元素时,arrayAttr={1,2,3} 当数组属性只有一个元素时,arrayAttr=1 或者arrayAttr={1}
EnumTest.TrafficLamp lamp() default EnumTest.TrafficLamp.RED;//枚举属性,EnumTest.TrafficLamp是一个自定义的枚举类
MetaAnnotation annotationAttr() default @MetaAnnotation("lhm");// 这个属性的的类型是“注解” MetaAnnotation是一个自定义的注解,其具有value属性,且只需要给这个属性赋值
}
根据注解是否包含成员变量,可以将注解分为一下两类
标记Annotation :一个没有定义成员变量的Annotation类型被称为标记。这种注解仅利用自身的存在与否为我们提供信息,如前面介绍的@Override @Test等Annotation
元数据Annotation
:包含成员变量的Annotation因为他们可以接受更多的元数据,所以也被成为元数据Annotation
@ItcastAnnotation(annotationAttr=@MetaAnnotation("flx"),color="red",value="abc",arrayAttr=1)
//当数组属性有多个元素时,arrayAttr={1,2,3} 当数组属性只有一个元素时,arrayAttr=1 或者arrayAttr={1}
public class AnnotationTest {
@SuppressWarnings("deprecation") //不提示方法过时警告
@ItcastAnnotation("xyz") //当只需要给value属性赋值的时候可以直接省略掉“=”
public static void main(String[] args) throws Exception{
System.runFinalizersOnExit(true);
if(
AnnotationTest.class.isAnnotationPresent(ItcastAnnotation.class)){
ItcastAnnotation annotation = (ItcastAnnotation)AnnotationTest.class.getAnnotation(ItcastAnnotation.class);
System.out.println(annotation.color());
System.out.println(annotation.value());
System.out.println(annotation.arrayAttr().length);
System.out.println(annotation.lamp().nextLamp().name());
System.out.println(annotation.annotationAttr().value());
}
Method mainMethod = AnnotationTest.class.getMethod("main", String[].class);
ItcastAnnotation annotation2 = (ItcastAnnotation)mainMethod.getAnnotation(ItcastAnnotation.class);
System.out.println(annotation2.value());
}
}
怎么看文档视频里有
/*********************************************************************************/
编译时处理Annotation 见疯狂Java讲义P650