Java学习路线-33:反射与Annotation
  TEZNKK3IfmPf 2023年11月12日 31 0

第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("发送消息");
}
}
【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月12日 0

暂无评论

推荐阅读
  TEZNKK3IfmPf   17天前   40   0   0 java
  TEZNKK3IfmPf   2024年05月31日   51   0   0 java
TEZNKK3IfmPf