您的位置:首页 > 编程语言 > Java开发

Java反射、注解

2017-12-07 11:24 197 查看
一.类:Class

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();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: