Java反射、注解
2017-12-07 11:24
197 查看
一.类:Class
二.属性:Field
三.方法:Method
四.构造方法:Constructor
五.注解:Annotation
public void test01() throws Exception { //类名字符串 → 类 Class c = Class.forName("反射.Son"); //类名 → 类 Class c2 = Son.class; //对象 → 类 Son son = new Son(); Class c3 = son.getClass(); //类 → 类名 String name = c.getName(); String simpleName = c.getSimpleName(); //类 → 父类, 类 → 接口 Class superClass = c.getSuperclass(); Class[] interfaces = c.getInterfaces(); //类 → 对象 Object obj = c.newInstance(); }
二.属性:Field
public void test02() throws Exception { Class c = Son.class; Son son = new Son(); //类 → 属性 //范围:类自己的所有属性 c.getDeclaredFields(); c.getDeclaredField("id"); //范围:类及所有父类和接口的public的属性 c.getFields(); c.getField("type"); Field field =c.getDeclaredField("id"); //变量修饰符,变量类型,变量名 String modifier = Modifier.toString(field.getModifiers()); Class type = field.getType(); Type gType = field.getGenericType(); String name = field.getName(); //设置私有属性允许访问 field.setAccessible(true); //获取,设置属性的值 Object o = field.get(son); field.set(son, 520); }
三.方法:Method
public void test03() throws Exception { Class c = Son.class; Son son = new Son(); //类 → 属性 //范围:类自己的所有方法 c.getDeclaredMethods(); c.getDeclaredMethod("add"); //范围:类及所有父类和接口的public的属性 c.getMethods(); c.getMethod("add"); Method method = c.getDeclaredMethod("delete",String.class,int.class); //方法修饰符,返回值类型,参数类型,参数个数,方法名 String modifier = Modifier.toString(method.getModifiers()); Class type = method.getReturnType(); Type gtype = method.getGenericReturnType(); Class[] params = method.getParameterTypes(); Type[] gparams = method.getGenericParameterTypes(); int count = method.getParameterCount(); String name = method.getName(); //设置私有方法允许访问,执行方法 method.setAccessible(true); Object result = method.invoke(son, "哈哈哈"); }
四.构造方法:Constructor
public void test04() throws Exception { Class c = Son.class; Son son = new Son(); //范围:所有的构造方法 c.getDeclaredConstructors(); c.getDeclaredConstructor(int.class); //范围:public的构造方法 c.getConstructors(); c.getConstructor(String.class); Constructor con = c.getDeclaredConstructor(int.class); //构造方法修饰符,参数类型,参数个数,方法名 String modifier = Modifier.toString(con.getModifiers()); Class[] params = con.getParameterTypes(); Type[] types = con.getGenericParameterTypes(); int count = con.getParameterCount(); String name = con.getName(); //设置私有构造方法允许访问,执行构造方法创建对象 con.setAccessible(true); Object obj = con.newInstance(1); }
五.注解:Annotation
@Documented @Inherited @Retention(RetentionPolicy.RUNTIME) public @interface ResponseBody { /* 元注解 @Documented 允许文档化 @Inherited 自动继承 @Target 注解的使用范围 (多选): ElementType.XX PACKAGE 包 TYPE 类,接口,注解,枚举 CONSTRUCTOR 构造器 FIELD 属性 METHOD 方法 PARAMETER 参数 LOCAL_VARIABLE 局部变量 @Retention 生命周期(单选): RetentionPolicy.XX SOURCE 源文件有效 CLASS 编译时有效 RUNTIME 运行时有效 */ /* 1.方法返回类型: 基本类型 String Class Enum Annotation 数组 2.继承只用于类上面的注解 3.当只有value属性无默认值时,可以直接写值 */ @ResponseBody("") public String name() default ""; public String value(); public Request type() default @Request; }
public class Test extends Animal{ private String color; @ResponseBody("") public void fly(@ResponseBody("") @Request String a,String b) { @ResponseBody("") String c = ""; } public static void main(String[] args) throws Exception { Class<Test> c = Test.class; Method method = c.getMethod("fly",String.class,String.class); //判断是否拥有某个注解 method.isAnnotationPresent(ResponseBody.class); //获取方法的某个或所有注解 method.getAnnotation(ResponseBody.class); method.getAnnotations(); method.getDeclaredAnnotation(ResponseBody.class); method.getDeclaredAnnotations(); //获取类的某个或所有注解 c.getAnnotation(ResponseBody.class); c.getAnnotations(); //获取类或父类的某个或所有注解 c.getDeclaredAnnotation(ResponseBody.class); c.getDeclaredAnnotations(); Parameter[] param = method.getParameters(); //获取参数的注解(参数只能拥有一个有效注解) param[0].getAnnotations(); //获取参数的某个注解,不存在返回null param[0].getAnnotation(Request.class); System.out.println(); } }
相关文章推荐
- Java反射+注解实现自动持久化小例子(代码未优化)
- java--反射和注解
- Java注解(Annotation)用法:利用注解和反射机制指定列名导出数据库数据
- java反射以及自定义注解二者的使用
- Java中利用反射查找使用指定注解的类---找到指定包下的指定注解类
- Java开发框架时编写的小功能(反射获取Method参数描述及注解描述)
- Java反射—结合框架谈注解
- 黑马程序员----JAVA基础反射与注解
- Java反射学习总结终(使用反射和注解模拟JUnit单元测试框架)
- Java自定义注解和运行时靠反射获取注解
- JAVA反射与注解实例
- Android简易注解View(java反射实现)
- Java中通过注解+反射拿到对象的属性和方法
- 【转】JAVA反射与注解
- Java使用反射处理注解
- 深入理解Java5的反射与注解机制
- JAVA面试-基础加强与巩固:反射、注解、泛型等
- Java中对比两个对象中属性值[反射、注解]
- java 注解annotation的使用,以及反射如何获取注解
- Java使用反射和注解实现参数校验