利用反射查看类的声明信息

package test.my.chap0302;

import java.lang.annotation.Annotation;
import java.lang.reflect.Modifier;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;

public class ClassDeclarationView {

/**
* @param args
* @throws ClassNotFoundException
*/
@SuppressWarnings("unchecked")
public static void main(String[] args) throws ClassNotFoundException {

Class<?> classz = Class.forName("java.util.ArrayList");
System.out.println("标准的类名:"+classz.getCanonicalName());
System.out.println("标准的修饰符:"+Modifier.toString(classz.getModifiers()));
TypeVariable[] typeVariable = classz.getTypeParameters();
//输出类的泛型参数
if(typeVariable.length!=0){
for(TypeVariable type :typeVariable){
System.out.println("泛型参数:\t"+type);
}
}else{
System.out.println("没有参数!");
}

Type[] interfaces = classz.getInterfaces();
if(interfaces.length!=0){
for(Type ty:interfaces){
System.out.println("实现的所有接口:\t"+ty);
}
}else{
System.out.println("\t"+"空");
}
//输出类的直接继承类
Type supers = classz.getGenericSuperclass();
if(supers!=null){
System.out.println("直接继承类是:\t"+supers);
}else{
System.out.println("null");
}
//输出所有的注解信息
Annotation[] annotation = classz.getAnnotations();
if(annotation.length!=0){
for(Annotation at:annotation){
System.out.println("\t"+at);
}
}else{
System.out.println("没有注解信息!");
}
}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值