java_反射与Annotation

在java.lang.reflect.AccessibleObject(java.lang.Class)类中提供有如下与Annotation有关的方法:

  • 取得全部Annotation: public Annotation[] getAnnotations()

  • 取得指定的Annotation: public T getAnnotation
    (Class annotationClass)
    取得定义在类上的Annotation:

    package www.bittech.java;
    import java.io.Serializable;
    import java.lang.annotation.Annotation;
    @SuppressWarnings(“serial”)
    @Deprecated
    class Member implements Serializable {}
    public class Test{
    public static void main(String[] args) {
    Annotation[] ant = Member.class.getAnnotations() ;
    for (Annotation a: ant) {
    System.out.println(a);
    }
    }
    }

在方法上使用Annotation:

   package www.bittech.java;
    import java.io.Serializable;
    import java.lang.annotation.Annotation;
    @SuppressWarnings("serial")
    @Deprecated
    class Member implements Serializable {
        @Deprecated
        @Override
        public String toString() {
            return super.toString();
        }
    }public class Test{
        public static void main(String[] args) {
            Annotation[] ant = new Annotation[0];
            try {
                ant = Member.class.getMethod("toString").getAnnotations();
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            }
            for (Annotation a: ant) {
                System.out.println(a);
            }
        }
    }

自定义Annotation

注解定义:
1.注解是接口interface修饰,并且interface前加@
2.定义抽象方法(可选),提供默认值,如果不提供使用时就得设置值
3.保存范围
4.作用的位置

要想自定义Annotation,首先需要解决的就是Annotation的作用范围。通过上面的范例我们可以知道,不同的Annotation有自己的运行范围,而这些范围就在一个枚举类(java.lang.annotation.RetentionPolicy)中定义:

  • SOURCE:在源代码中出现的Annotation
  • CLASS:在*.class中出现的Annotation
  • RUNTIME:在类执行的时候出现的Annotation

定义一个在运行时生效的Annotation:

package www.bittech.java;

import java.io.Serializable;
import java.lang.annotation.Annotation;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
// 自定义一个Annotation
 
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation{
    public String name() ;
    public int age() ;
}
@Deprecated
@MyAnnotation(name = "zhangsan" , age = 18)
class Member implements Serializable {
}
public class Test {
    public static void main(String[] args) {
        Annotation[] ant = new Annotation[0];
        ant = Member.class.getAnnotations();
        for (Annotation a: ant) {
            System.out.println(a);
        }
    }
}

此时自定义的Annotation强制要求设置name和age属性内容,这样不方便用户使用。所以可以设置默认值,在定义Annotation时使用default设置默认值。

使用默认值自定义Annotation:

package www.bittech.java;

import java.io.Serializable;
import java.lang.annotation.Annotation;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
/**
 * 自定义一个Annotation
 */
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation{
    public String name() default "zhangsan" ;
    public int age() default 18 ;
}
@Deprecated
@MyAnnotation
class Member implements Serializable {
}
public class Test {
    public static void main(String[] args) {
        Annotation[] ant = new Annotation[0];
        ant = Member.class.getAnnotations();
        for (Annotation a: ant) {
            System.out.println(a);
        }
    }
}

取得某个具体的Annotation:

package www.bittech.java;

import java.io.Serializable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
//自定义一个Annotation
 
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation{
    public String name() default "zhangsan" ;
    public int age() default 18 ;
}
@Deprecated
@MyAnnotation
class Member implements Serializable {
}
public class Test {
    public static void main(String[] args) {
        MyAnnotation ma = Member.class.getAnnotation(MyAnnotation.class) ;
        System.out.println("姓名 :" +ma.name()) ;
        System.out.println("年龄 :"+ma.age()) ;
    }
}

Annotation与工厂设计模式
用注解的形式配置工厂类:

package www.bittech.java;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
/**
 * 自定义一个Annotation
 */
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation{
    public Class<?> target() ;
}
interface IFruit {
    public void eat() ;
}
class Apple implements IFruit {
    @Override
    public void eat() {
        System.out.println("吃苹果") ;
    }
}
@MyAnnotation(target = Apple.class)
class Factory {
    public static <T> T getInstance() throws Exception{
        MyAnnotation mt = Factory.class.getAnnotation(MyAnnotation.class) ;
        return (T) mt.target().newInstance() ;
    }
}
public class Test {
    public static void main(String[] args) throws Exception{
        IFruit fruit = Factory.getInstance() ;
        fruit.eat() ;
    }
}

程序可以通过Annotation配置的信息来实现不同的操作效果。
注解和工厂模式结合,主要是在框架中使用。

猜你喜欢

转载自blog.csdn.net/qq_44149554/article/details/89536421
今日推荐