第27 章 : 反射与Annotation
120 反射取得Annotation信息
JDK > 1.5
不同的Annotation 有他的存在范围
public enum RetentionPolicy {
SOURCE,
CLASS,
RUNTIME
}
import java.lang.annotation.Annotation;
@Deprecated
class Person{
}
class Demo{
public static void main(String[] args) {
Annotation[] annotations = Person.class.getAnnotations();
for (Annotation a: annotations){
System.out.println(a);
// @java.lang.Deprecated()
}
}
}
121 自定义Annotation
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
// 定义运行时策略
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation{
public String name();
public int age() default 23; // 设置默认值
}
class Message {
@MyAnnotation(name="Tom")
public void send(String msg){
System.out.println(msg);
}
}
class Demo{
public static void main(String[] args) throws Exception {
Method method = Message.class.getDeclaredMethod("send", String.class);
MyAnnotation annotation = method.getAnnotation(MyAnnotation.class);
String name = annotation.name(); // Tom
int age = annotation.age(); // 23
String msg = String.format("[%s] %s ", name, age);
method.invoke(Message.class.getDeclaredConstructor().newInstance(), msg);
// [Tom] 23
}
}
122 工厂设计模式与Annotation整合
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
// 消息接口
interface IMessage {
public void send(String msg);
}
// 消息实现类
class MessageImpl implements IMessage {
public void send(String msg) {
System.out.println("消息发送");
}
}
// 动态代理类
class MessageProxy implements InvocationHandler {
private Object target;
public Object bind(Object target) {
this.target = target;
return Proxy.newProxyInstance(
target.getClass().getClassLoader(),
target.getClass().getInterfaces(),
this
);
}
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Object obj = null;
if (this.connect()) {
obj = method.invoke(this.target, args);
}
this.close();
return obj;
}
public boolean connect() {
System.out.println("打开连接");
return true;
}
public void close() {
System.out.println("关闭连接");
}
}
// 工厂类
class Factory {
private Factory() {
}
public static <T> T getInstance(Class<T> clazz) {
try {
return (T) new MessageProxy().bind(clazz.getDeclaredConstructor().newInstance());
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
@Retention(RetentionPolicy.RUNTIME)
@interface UseMessage {
public Class<?> clazz();
}
// 利用注解实现类的使用
@UseMessage(clazz = MessageImpl.class)
class MessageService {
private IMessage message;
public MessageService() {
UseMessage use = MessageService.class.getAnnotation(UseMessage.class);
this.message = (IMessage) Factory.getInstance(use.clazz());
}
public void send(String msg) {
this.message.send(msg);
}
}
class Demo {
public static void main(String[] args) {
MessageService message = new MessageService();
message.send("发送消息");
}
}