@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
public @interface ClassInfo {
String author() default "Wang";
String date();
String comments();
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MethodInfo {
String description() default "No Description";
String date();
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface FieldInfo {
String type();
String name();
}
@ClassInfo(author = "wang",
date = "2016/9/13",
comments = "annotation demo")
public class AnnotationDemo {
@FieldInfo(type = "public", name = "firstField")
public int firstField;
@FieldInfo(type = "private", name = "secondField")
private String secondField;
@MethodInfo(description = "method in AnnotationDemo", name = "firstMethod")
public void firstMethod(String value) {
System.out.printf("first method involved");
}
@MethodInfo(description = "method in AnnotationDemo", name="secondMethod")
private void secondMethod() {
System.out.printf("first method involved");
}
}
public class AnnotationTest {
public static void main(String[] args) {
resolveClassAnnotationInfo(AnnotationDemo.class);
resolveFieldAnnotationInfo(AnnotationDemo.class);
resolveMethodAnnotationInfo(AnnotationDemo.class);
}
private static void resolveClassAnnotationInfo(Class<?> clz) {
// 判断该类是否有ClassInfo注解
if(clz.isAnnotationPresent(ClassInfo.class)) {
ClassInfo classInfo = (ClassInfo) clz.getAnnotation(ClassInfo.class);
System.out.println(classInfo.author() + " " + classInfo.comments() + " " + classInfo.date());
}
}
private static void resolveFieldAnnotationInfo(Class<?> clz) {
Field[] fields = clz.getDeclaredFields();
for (Field field : fields) {
if(field.isAnnotationPresent(FieldInfo.class)) {
FieldInfo fieldInfo = (FieldInfo) field.getAnnotation(FieldInfo.class);
System.out.println(fieldInfo.type() + " " + fieldInfo.name());
}
}
}
private static void resolveMethodAnnotationInfo(Class<?> clz) {
Method[] methods = clz.getDeclaredMethods();
for (Method method : methods) {
if(method.isAnnotationPresent(MethodInfo.class)) {
MethodInfo methodInfo = (MethodInfo) method.getAnnotation(MethodInfo.class);
System.out.println(methodInfo.name() + " " + methodInfo.description());
}
}
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有