获取运行时类的完整结构
package Reflection2;
package Reflection2;
//Person的父类
import java.io.Serializable;
public class Creature<T> implements Serializable
{
private char gendr;
public double weight;
private void breath()
{
System.out.println("生物呼吸");
}
public void eat()
{
System.out.println("生物进食");
}
}
package Reflection2;
//Porson类的接口
public interface MyInterface
{
void info();
}
package Reflection2;
//自定义注解
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.ElementType.LOCAL_VARIABLE;
/*
注解
*/
package Reflection3;
import Reflection2.Person;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
/*
获取当前运行时类的属性结构
*/
public class FieldTest
{
public void test1()
{
Class clazz= Person.class;
//获取属性结构
//getFields():获取当前运行时类及其父类中声明为public访问权限的属性
Field[] fields = clazz.getFields();//获取所有丰富的属性
for (Field f:fields)
{
System.out.println(f);
}
System.out.println();
//getDeclaredFields(): 获取当前运行时类及其父类中声明所有属性。(不包含父类中声明的属性)
Field[] declaredFields = clazz.getDeclaredFields();
for (Field f:declaredFields)
{
System.out.println(f);
}
}
//权限修饰符 数据类型 变量名
public void test2()
{
Class clazz= Person.class;
Field[] declaredFields = clazz.getDeclaredFields();
for (Field f:declaredFields)
{
//权限修饰符
int modifiers = f.getModifiers();
System.out.print(Modifier.toString(modifiers)+"\t");
//数据类型
Class type=f.getType();
System.out.print(type.getName()+"\t");
//变量名
String name = f.getName();
System.out.print(name);
System.out.println();
}
}
public static void main(String[] args)
{
new FieldTest().test1();
new FieldTest().test2();
}
}
package Reflection3;
import Reflection2.Person;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
/*
获取运行时类的方法结构
*/
public class MethodTest
{
public void test1()
{
Class clzz= Person.class;
//getMethods():获取当前运行时类及其所有父类中声明为public权限的方法
Method[] methods = clzz.getMethods();
for (Method method : methods)
{
System.out.println(method);
}
System.out.println();
//getDeclaredMethods():获取当前运行时类中声明的所有方法。(不包含父类中声明的方法)
Method[] declaredMethods = clzz.getDeclaredMethods();
for (Method m: declaredMethods)
{
System.out.println(m);
}
}
/*
@Xxxx
权限修饰符 返回值类型 方法名(参数类型1 形参1,....)throws xxxException{}
*/
public void test2()
{
//获取方法声明的注解
Class clzz= Person.class;
Method[] declaredMethods = clzz.getDeclaredMethods();
for (Method m: declaredMethods)
{
Annotation[] annotations = m.getAnnotations();
for (Annotation a:annotations)//真假for循环
{
System.out.println(a);
}
//2.权限修饰符
System.out.print(Modifier.toString(m.getModifiers())+"\t");
//返回类型
System.out.print(m.getReturnType().getName()+"\t");
System.out.println();
//方法名
System.out.println(m.getName());
System.out.print("(");
//形参列表
Class[] parameterTypes = m.getParameterTypes();
if (!(parameterTypes==null && parameterTypes.length==0))//判断方法参数是否为空
{
for (int i=0;i<parameterTypes.length;i++)
{
if (i==parameterTypes.length-1)
{