类注解:

package com.cglibs;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; // 适用类、接口(包括注解类型)或枚举
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface ClassInfo {
String value();
}
field属性注解
package com.cglibs;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; // 适用field属性,也包括enum常量
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface FieldInfo {
int[] value();
}

方法注解

package com.cglibs;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; // 适用方法
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MethodInfo {
String name() default "long";
String data();
int age() default 27;
}

参数注解

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
// package com.cglibs; import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; @Target({ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface AutoUserModel {
boolean value() default true;
boolean require() default true;
String message() default "user is null";
}

测试类

package com.cglibs;

/**
* 测试运行时注解
*/
@ClassInfo("Test Class")
public class RuntimeAnnotation { @FieldInfo(value = {1, 2})
public String fieldInfo = "FiledInfo"; @FieldInfo(value = {10086})
public int i = 100; @MethodInfo(name = "BlueBird", data = "Big")
public static String getMethodInfo(@AutoUserModel(value = false,require = true) @AutoPlatformModel String a) {
System.out.println("RuntimeAnnotation.getMethodInfo "+a);
return RuntimeAnnotation.class.getSimpleName();
}
}

运行

package com.cglibs;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Arrays; /**
* 测试运行时注解
*/
public class TestRuntimeAnnotation {
/**
* 测试运行时注解
*/
public static void main(String[] args) {
StringBuilder sb = new StringBuilder();
Class<?> cls = RuntimeAnnotation.class;
// 获取指定类型的注解
sb.append("Class注解:").append("\n");
ClassInfo classInfo = cls.getAnnotation(ClassInfo.class);
if (classInfo != null) {
sb.append(Modifier.toString(cls.getModifiers())).append(" ")
.append(cls.getSimpleName()).append("\n");
sb.append("注解值: ").append(classInfo.value()).append("\n\n");
} sb.append("Field注解:").append("\n");
Field[] fields = cls.getDeclaredFields();
for (Field field : fields) {
FieldInfo fieldInfo = field.getAnnotation(FieldInfo.class);
if (fieldInfo != null) {
sb.append(Modifier.toString(field.getModifiers())).append(" ")
.append(field.getType().getSimpleName()).append(" ")
.append(field.getName()).append("\n");
sb.append("注解值: ").append(Arrays.toString(fieldInfo.value())).append("\n\n");
}
} sb.append("Method注解:").append("\n");
Method[] methods = cls.getDeclaredMethods();
for (Method method : methods) {
MethodInfo methodInfo = method.getAnnotation(MethodInfo.class);
if (methodInfo != null) {
sb.append(Modifier.toString(method.getModifiers())).append(" ")
.append(method.getReturnType().getSimpleName()).append(" ")
.append(method.getName()).append("\n");
sb.append("注解值: ").append("\n");
sb.append("name: ").append(methodInfo.name()).append("\n");
sb.append("data: ").append(methodInfo.data()).append("\n");
sb.append("age: ").append(methodInfo.age()).append("\n");
Annotation[][] parameterAnnotations = method.getParameterAnnotations();
for (Annotation[] a : parameterAnnotations
) {
if (a != null) {
for (Annotation b : a
) {
if (AutoUserModel.class.isAssignableFrom(b.annotationType())) {
AutoUserModel b1 = (AutoUserModel) b;
sb.append("找到了 AutoUserModel =").append(b1.value()).append("\n");
}
if (AutoPlatformModel.class.isAssignableFrom(b.annotationType())) {
AutoPlatformModel b1 = (AutoPlatformModel) b;
sb.append("找到了 AutoPlatformModel =").append(b1.value()).append("\n");
}
}
} }
}
}
System.out.print(sb.toString());
}
}

最新文章

  1. ejoy2d源码阅读笔记1
  2. C++ 11 中的右值引用
  3. [python面向对象]--基础篇
  4. IIS部署ASP.NET常见错误
  5. Tomcat部署记事
  6. 9.9,新iPhone要来了,是欢呼,还是墙角画圈,一会儿见分晓
  7. WPF在XAML中Binding使用StringFormat属性
  8. js版iphone通讯录分组列表效果
  9. 微信小程序教程(第二篇)
  10. Spring Boot 学习笔记
  11. Kendo UI 使用小知识点汇总
  12. dubbo框架的web端(war)和server端(tar.gz)结合jenkins打包方式
  13. Java当中的IO一
  14. JDBC-Oracle连接教程
  15. 复制命令(XCOPY)
  16. libgdx学习记录11——平铺地图TiledMap
  17. 报错:-bash: locate: command not found
  18. Python collections.OrderedDict解决dict元素顺序问题
  19. 一些ios牛人的博客
  20. MySQL查看一个表的创建文本以及删除表某列的索引

热门文章

  1. PHP程序员的技术成长规划(转载)
  2. WPF多值绑定及多值转换(MultiBinding和IMultiValueConverter)
  3. Property or method &quot;openPageOffice&quot; is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option, or for class-based components, by
  4. c语言1博客作业05
  5. css 禁止点击事件触发
  6. Oracle 物理结构(七) 文件-归档日志文件
  7. GO111MODULE的设置(及GOPROXY)
  8. (转)python正向连接后门
  9. Mybatis 返回值 返回Map的为空的值
  10. super关键字和调用父类构造方法