通过反射创建对应的运行时类的对象
public void test1() throws IllegalAccessException, InstantiationException {
Class<Person> clazz = Person.class;
/*
newInstance():调用此方法,创建对应的运行时类的对象
内部调用了运行时类的空参构造器
要想此方法正常的创建运行时类的对象,要求:
1.运行时类必须提供空参的构造器
2.空参的构造器的访问权限得够。通常设置为public
在javabean中要求提供一个public的空参构造器。原因:
1.便于反射,创建运行时类的对象
2.便于子类继承此运行时类时,默认调用super(),保证父类有此构造器
*/
Person obj = clazz.newInstance();//调用空参构造器
System.out.println(obj);//Person{name='null', age=0}
}
获取运行时类的方法结构:
public class MethodTest {
@Test
public void test1() {
Class clazz = Person.class;
//获取当前运行时类及其父类中声明为public权限的方法
Method[] methods = clazz.getMethods();
for (Method m : methods
) {
System.out.println(m);
}
System.out.println();
//getDeclareMethods():获取当前运行时类中声明的所有方法。(不包含父类中声明的方法)
Method[] declaredMethod = clazz.getDeclaredMethods();
for (Method m : declaredMethod
) {
System.out.println(m);
}
}
/*
权限修饰符 返回值类型 方法名(参数类型1 形参名1) throws XxxException
*/
@Test
public void test2() {
Class clazz = Person.class;
Method[] declaredMethod = clazz.getDeclaredMethods();
for (Method m : declaredMethod
) {
//1.获取方法声明的注解
Annotation[] annotations = m.getAnnotations();
for (Annotation a : annotations
) {
System.out.print(a + "\n");
}
//2.权限修饰符
System.out.print(Modifier.toString(m.getModifiers()) + "\t");
//3.返回值类型
System.out.print(m.getReturnType().getName() + "\t");
//4.方法名
System.out.print(m.getName());
System.out.print("(");
//5.形参列表
Class[] parameterTypes = m.getParameterTypes();
if (!(parameterTypes == null && parameterTypes.length == 0)) {
for (int i = 0; i < parameterTypes.length; i++) {
if (i == parameterTypes.length - 1) {
System.out.print(parameterTypes[i] + "args_" + i);
break;
}
System.out.print(parameterTypes[i] + "args_" + i + ",");
}
}
System.out.print(")");
//6.抛出的异常
Class[] exceptionTypes = m.getExceptionTypes();
if (exceptionTypes.length>0) {
System.out.println("throws" + "\t");
for (int i = 0; i < exceptionTypes.length; i++) {
if (i == exceptionTypes.length - 1) {
System.out.print(exceptionTypes[i].getName());
break;
}
System.out.println(exceptionTypes[i].getName() + ",");
}
}
System.out.println();
}
}
}
其他结构:
@Test
public void test1(){
Class clazz= Person.class;
//获取当前运行时类中声明为public的构造器
Constructor[] constructors = clazz.getConstructors();
for (Constructor c: constructors
) {
System.out.println(c);
}
System.out.println();
//获取当前运行时类中声明的所有的构造器
Constructor[] declaredConstructors = clazz.getDeclaredConstructors();
for (Constructor c: declaredConstructors
) {
System.out.println(c);
}
}
/*
获取运行时类的父类
*/
@Test
public void test2(){
Class clazz=Person.class;
Class superclass = (Class) clazz.getSuperclass();
System.out.println(superclass);
}
/*
获取运行时类带泛型的父类
*/
@Test
public void test3(){
Class clazz=Person.class;
Type genericSuperclass = clazz.getGenericSuperclass();
System.out.println(genericSuperclass);
}
/*
获取运行时类带泛型的父类的泛型
*/
@Test
public void test4(){
Class clazz=Person.class;
Type genericSuperclass = clazz.getGenericSuperclass();
ParameterizedType parameterizedType=(ParameterizedType) genericSuperclass;
//获取泛型类型
Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();
System.out.println(actualTypeArguments[0].getTypeName());
}