以下用到的自定义类型、自定义注解、自定义接口均在文件末尾附上
获取运行时类的属性
- getFields():获取当前运行时类及其所有父类当中,权限为public的属性
- getDeclaredFields():获取当前运行时类当中,不限权限的所有属性
- getModifiers():获取权限修饰符,返回值为int类型,通过Modifier.toString(int modifiers)可以转换成权限修饰符
- getType():获取数据类型
- getName():获取属性名
@Test
//获取当前运行时类的属性
public void test1(){
Class clazz = Person.class;
//getFields():获取当前运行时类及其所有父类当中,权限为public的属性
Field[] fields = clazz.getFields();
for(Field f : fields){
System.out.println(f);
}
//getDeclaredFields():获取当前运行时类当中,不限权限的所有属性
Field[] declaredFields = clazz.getDeclaredFields();
for(Field f : declaredFields){
System.out.println(f);
}
}
@Test
//获取属性对应的 权限修饰符,数据类型,属性名
public void test2(){
Class clazz = Person.class;
Field[] declaredFields = clazz.getDeclaredFields();
for(Field f : declaredFields){
//getModifiers():获取权限修饰符,返回值为int类型,通过Modifier.toString(int modifiers)可以转换成权限修饰符
int modifiers = f.getModifiers();
System.out.print(Modifier.toString(modifiers) + "\t");
//getType():获取数据类型
Class type = f.getType();
System.out.print(type + "\t");
//getName():获取属性名
String name = f.getName();
System.out.print(name + "\t");
System.out.println();
}
}
获取运行时类的方法
- getMethods():获取运行时类及其所有父类当中,权限为public的所有方法
- getDeclaredMethods():获取运行时类当中,所有权限的所有方法
- getReturnType():获取返回值类型
- getAnnotations():获取所有注解
- getParameterTypes():获取参数类型
- getExceptionTypes():获取异常
@Test
public void test1(){
Class clazz = Person.class;
//getMethods():获取运行时类及其所有父类当中,权限为public的所有方法
Method[] methods = clazz.getMethods();
for (Method m : methods){
System.out.println(m);
}
System.out.println();
//getDeclaredMethods():获取运行时类当中,所有权限的所有方法
Method[] declaredMethods = clazz.getDeclaredMethods();
for (Method m : declaredMethods){
System.out.println(m);
}
}
@Test
/*
方法的各个参数
@Xxx()
权限修饰符 返回值类型 方法名称(参数类型 参数名,参数类型 参数名。。。) throws 异常
*/
public void test2(){
Class clazz = Person.class;
Method[] declaredMethods = clazz.getDeclaredMethods();
for (Method m : declaredMethods){
//1.获取注解
Annotation[] annotations = m.getAnnotations();
for (Annotation a : annotations){
System.out.println(a);
}
//2.权限修饰符
System.out.print(Modifier.toString(m.getModifiers()) + "\t");
//3.返回值类型
System.out.print(m.getReturnType() + "\t");
//4.方法名称
System.out.print(m.getName() + "(");
//5.参数类型
Class[] parameterTypes = m.getParameterTypes();
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.print(" throws ");
for (int i = 0; i < exceptionTypes.length; i++) {
if(i == exceptionTypes.length-1){
System.out.println(exceptionTypes[i]);
break;
}
System.out.println(exceptionTypes[i] + ",");
}
}
System.out.println();
}
}
获取运行时类的其他结构
- getConstructors():获取当前运行时类,权限为public的所有构造方法
- getDeclaredConstructors():获取当前运行时类的所有构造方法
- getSuperclass():获取运行时类的父类
- getGenericSuperclass():获取运行时类的带泛型的父类
- getInterfaces():获取运行时类的接口
- getPackage():获取运行时类所在的包
- getAnnotations():获取运行时类的注解
@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 = 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());
}
@Test
//获取运行时类的接口
public void test5(){
Class clazz = Person.class;
Class[] interfaces = clazz.getInterfaces();
for (Class c : interfaces){
System.out.println(c);
}
}
@Test
//获取运行时类所在的包
public void test6(){
Class clazz = Person.class;
Package aPackage = clazz.getPackage();
System.out.println(aPackage);
}
@Test
//获取运行时类的注解
public void test7(){
Class clazz = Person.class;
Annotation[] annotations = clazz.getAnnotations();
for (Annotation a : annotations){
System.out.println(a);
}
}
最后贴一下所用到的自定义类、自定义接口、自定义注解
运行时类的父类
public class Biology<String> implements Serializable {
private boolean sex;
public int weight;
public void eat(){
System.out.println("生物吃东西");
}
private void breathing(){
System.out.println("生物呼吸");
}
}
运行时类
@MyAnnotation(value = "hi")
public class Person extends Biology<String> implements Comparable,MyInterface{
public String name;
int id;
private int age;
public Person(){}
@MyAnnotation(value = "nihao")
private Person(String name){
this.name = name;
}
Person(String name,int age,int id){
this.name = name;
this.age = age;
this.id = id;
}
@MyAnnotation
private String getNation(String nation,int age) throws NullPointerException,ClassNotFoundException{
return nation;
}
@Override
public void say() {
System.out.println("我是一个人");
}
@Override
public int compareTo(Object o) {
return 0;
}
}
自定义接口
public interface MyInterface {
void say();
}
自定义注解
@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
String value() default "hello";
}