第 12 节 反射与Annotation
获取Annotation信息
public class Application {
public static void main(String[] args) throws Exception {
{
// 获取接口上全部的Annotation信息
Annotation[] annotations = IMessage.class.getAnnotations();
for (Annotation annotation : annotations) {
System.out.println(annotation);
}
}
System.out.println("=========================================");
{
// 获取MessageImpl子类上的Annotation
Annotation[] annotations = MessageImpl.class.getAnnotations();
for (Annotation annotation : annotations) {
System.out.println(annotation);
}
}
System.out.println("=========================================");
{
// 获取MessageImpl.send()方法上的Annotation
Method method = MessageImpl.class.getDeclaredMethod("send", String.class);
Annotation[] annotations = method.getAnnotations();
for (Annotation annotation : annotations) {
System.out.println(annotation);
}
}
}
}
@FunctionalInterface
@Deprecated(since = "1.0")
interface IMessage {
void send(String msg);
}
@SuppressWarnings("serial")
class MessageImpl implements IMessage, Serializable {
@Override
public void send(String msg) {
System.out.println("[SEND] " + msg);
}
}自定义Annotation
工厂设计模式与Annotation整合
最后更新于