调用运行时类的指定结构
调用指定属性
@Test
public void test() throws Exception {
Class<Person> personClass = Person.class;
Person person = personClass.newInstance();
//获取指定变量名属性:
Field name = personClass.getDeclaredField("name");
//设置当前属性可以访问
name.setAccessible(true);
//设置属性
name.set(person,"lc_666");
System.out.println(person);
//获取属性
Object o = name.get(person);
System.out.println(o);
}
调用指定的方法
@Test
public void test1() throws Exception {
Class<Person> personClass = Person.class;
Person person = personClass.newInstance();
//非静态方法
//获取指定的某个方法
Method display = personClass.getDeclaredMethod("display", String.class);
//设置当前属性可以访问
display.setAccessible(true);
//调用方法
Object movie = display.invoke(person, "movie");
System.out.println(movie);
//静态方法
Method test = personClass.getDeclaredMethod("test");
test.setAccessible(true);
Object invoke = test.invoke(personClass);
System.out.println(invoke);
}
创建运行时类对象
@Test
public void test() throws IllegalAccessException, InstantiationException {
Class<Person> personClass = Person.class;
Person person = personClass.newInstance();
System.out.println(person);//Person{name='null', age=null}
}
- 使用
newInstance()
来创建对象,调用了空参构造方法;
- 需要提供权限为
public
的空参构造方法;
获取运行时类的属性
@Test
public void test1(){
Class<Person> personClass = Person.class;
//获取当前运行时类及其父类中访问权限为public的属性
Field[] fields = personClass.getFields();
for (Field field:fields){
System.out.println(field);
}
// 获取当前运行时类中所有声明的属性,不包含父类的属性
Field[] declaredFields = personClass.getDeclaredFields();
for (Field field:declaredFields){
System.out.println(field);
//权限修饰符
int modifiers = field.getModifiers();
System.out.println(modifiers);
System.out.println(Modifier.toString(modifiers));
//数据类型
Class<?> type = field.getType();
System.out.println(type.getName());
//变量名
System.out.println(field.getName());
}
}
获取运行时类的方法
@Test
public void test3(){
Class<Person> personClass = Person.class;
//获取当前运行时类及其所有父类中声明为public的方法
Method[] methods = personClass.getMethods();
for(Method method:methods){
System.out.println(method);
}
//获取当前运行时类中所有的方法,不包含父类的方法
Method[] declaredMethods = personClass.getDeclaredMethods();
for(Method method:declaredMethods){
System.out.println(method);
//获取注解
Annotation[] annotations = method.getAnnotations();
//获取访问权限
int modifiers = method.getModifiers();
//返回值类型
Class<?> returnType = method.getReturnType();
//方法名
System.out.println(method.getName());
//形参列表
Class<?>[] parameterTypes = method.getParameterTypes();
//获取抛出的异常类型
Class<?>[] exceptionTypes = method.getExceptionTypes();
}
}
获取构造器
@Test
public void test4() {
Class<Person> personClass = Person.class;
//获取当前运行时类中声明为public的构造器
Constructor<?>[] constructors = personClass.getConstructors();
for (Constructor constructor : constructors) {
System.out.println(constructor);
}
//获取当前运行时类中所有的构造器
//获取某个构造器的属性参照获取方法属性
Constructor<?>[] declaredConstructors = personClass.getDeclaredConstructors();
for (Constructor constructor : declaredConstructors) {
System.out.println(constructor);
}
}
获取运行时类的父类
@Test
public void test5() {
Class<Person> personClass = Person.class;
// 获取运行时类带泛型的父类
Class<? super Person> superclass = personClass.getSuperclass();
System.out.println(superclass);
Type genericSuperclass = personClass.getGenericSuperclass();
System.out.println(genericSuperclass);
//获取泛型类型
ParameterizedType parameterizedType = (ParameterizedType) genericSuperclass;
Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();
for (Type type:actualTypeArguments){
System.out.println(type.getTypeName());
}
}
获取运行时类实现的接口
@Test
public void test6() {
Class<Person> personClass = Person.class;
//获取运行时类实现的接口
Class<?>[] interfaces = personClass.getInterfaces();
for (Class c : interfaces) {
System.out.println(c);
}
}
获取运行时类所在包
@Test
public void test7() {
Class<Person> personClass = Person.class;
Package aPackage = personClass.getPackage();
System.out.println(aPackage);
}
获取运行时类注解
@Test
public void test8() {
Class<Person> personClass = Person.class;
Annotation[] annotations = personClass.getAnnotations();
for (Annotation annotation:annotations){
System.out.println(annotation);
}
}
网友评论