JavaSe5
开始内置了三种注解,定义在Java.lang中
@Override 表示覆盖超类的方法(父类)
@Deprecated 编译器发出警告
@SuppressWarnings 关闭不当的编译器警告信息
另外还有四种元注解
@Target 表示该注解可以用在什么地方
@Retention 表示在什么级别保存该注解信息
@Documented 将此注解包含在Javadoc中
@Inherited 允许子类继承父类的注解
一般来说用到注解的地方很大可能会用到反射以及枚举
因此我举例的时候直接把三者融合在一起
定义注解
@Inherited
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface XXXX{
public int id();
public String name()default "zhou";
}
那么既然定义了注解必然要使用注解的
public class Testable(){
@XXXX(id = 56,name = "blake")
public void isDemo(){
System.out.println("..............getAddress()");
}
}
同样的道理如果用到了注解,那么必须有注解处理器呀!
Class<Testable> testAble = Testable.class;
Method m = testAble.getDeclaredMethod("isDemo", null);
//因为没有参数嘛!假如有参数并且恰好是String,则null改 为String.class
//重点来了
XXXX xx = m.getAnnotation(XXXX.class);
//调用即可
System.out.println("name:" + xx.name());
下面我把编好的例子copy上来
四个自定义注解
FruitAddress
@Inherited
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface FruitAddress {
public enum Address {
A1("越南"), A2("缅甸"), A3("菲律宾"), A4("中国海南"), A5("柬埔寨"), A6("文莱"), A7("老挝"), A8("泰国"), A9("新加坡"), A10("马来西亚"), A11("东帝汶"), A12("印度尼西亚");
private String nameAddress;
Address(String nameAddress) {
this.nameAddress = nameAddress;
}
/**
* 获取地址
* @return
*/
public String getNameAddress() {
return nameAddress;
}
}
public Address fruitAddress() default Address.A2;
}
FruitColor
@Documented
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface FruitColor {
/**
* 颜色枚举
*/
public enum Color {
BLUE, GREEN, RED
}
/**
* 颜色属性
*
* @return
*/
public Color fruitColor() default Color.BLUE;
}
FruitName
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface FruitName {
public String value() default "";
}
FruitProvider
@Documented
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface FruitProvider {
/**
* 供应商编号
*
* @return
*/
public int id() default 01;
/**
* 供应商名称
*
* @return
*/
public String name() default "";
/**
* 供应商地址
*
* @return
*/
public String address() default "";
}
使用四个注解的class
@FruitProvider(id = 23, name = "blake", address = "china")
public class Apple {
@FruitName("apple")
private String appleName;
@FruitColor(fruitColor = FruitColor.Color.RED)
private String appleColor;
@FruitAddress(fruitAddress = FruitAddress.Address.A5)
public void getAddress() {
System.out.println("..............getAddress()");
}
public Apple() {
}
public void displayName() {
System.out.println("水果的名字是:苹果!");
}
}
注解处理器的测试 (测试和注解处理器并没有分开都在这个例子中)
public class Demo {
public static void main(String[] args) {
try {
isRun();
isStart();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void isStart() {
Class<Apple> appleClass = Apple.class;
Thread thread = new Thread() {
@Override
public void run() {
try {
//获取指定成员变量(字段)
Field f1 = appleClass.getDeclaredField("appleName");
Field f2 = appleClass.getDeclaredField("appleColor");
System.out.println(f1.getName() + " " + f2.getName());
//获取成员变量上面的注解
FruitName fruitName = f1.getAnnotation(FruitName.class);
FruitColor fruitColor = f2.getAnnotation(FruitColor.class);
System.out.println(fruitName.value() + "---->");
System.out.println(fruitColor.fruitColor().name() + "<----");
//获取指定方法
Method m1;
m1 = appleClass.getDeclaredMethod("getAddress", null);
m1 = appleClass.getMethod("getAddress",null); //ps 因为没有参数,如果有传入参数的class
System.out.println("method Name:" + m1.getName());
//获取方法上的注解
FruitAddress fruitAddress = m1.getAnnotation(FruitAddress.class);
System.out.println(".." + fruitAddress);
if (fruitAddress != null) {
System.out.println("address:" + fruitAddress.fruitAddress());
} else {
System.out.println("point exception");
}
;
//获取类或者说接口上的注解
FruitProvider fruitProvider = appleClass.getAnnotation(FruitProvider.class);
if (fruitProvider != null)
System.out.println(fruitProvider.address() + " " + fruitProvider.name() + " " + fruitProvider.id());
} catch (NoSuchFieldException e) {
System.out.println("exception:" + e.getMessage());
e.printStackTrace();
} catch (NoSuchMethodException ex) {
System.out.println("exception:" + ex.getMessage());
ex.printStackTrace();
}
}
};
thread.start();
}
public static void isRun() {
Class<Apple> appleClass = Apple.class;
Field[] fields = appleClass.getDeclaredFields();
for (Field f : fields) {//遍历
FruitColor fruitColor = f.getAnnotation(FruitColor.class);//获取注解在字段(成员变量)上的注解对象
FruitName fruitName = f.getAnnotation(FruitName.class);
if (fruitColor != null) {
System.out.println("color:" + fruitColor.fruitColor());
}
if (fruitName != null) {
System.out.println("name:" + fruitName.value());
}
}
}
}
基本上用到了Java的线程,注解,反射,枚举这四个基础知识.如有问题请留言!