java.lang.Class类

String className = "a.b.c.Person";
/*获取类*/
Class c = Class.forName(className);
Object o = c.newInstance();

Class<?>[] classes = c.getClasses();
Class<?>[] declaredClasses = c.getDeclaredClasses();
Class<?> declaringClass = Person.Tree.class.getDeclaringClass();
Class<?> enclosingClass = Person.Tree.class.getEnclosingClass();
Class superclass = c.getSuperclass();
Class subclass =c.asSubclass(o.getClass());

/*获取构造函数*/
Constructor<?>[] constructors = c.getConstructors();
Constructor<?>[] declaredConstructors = c.getDeclaredConstructors();
Constructor<?> constructor = c.getConstructor(String.class);
Constructor<?> declaredConstructor = c.getDeclaredConstructor();
Constructor<?> enclosingConstructor = c.getEnclosingConstructor();

Object tom = constructor.newInstance("Tom");
constructor.getDeclaredAnnotations();

/*获取属性*/
Field[] publicFields = c.getFields();
Field[] fields = c.getDeclaredFields();
Field cupField = c.getField("cup");
Field nameField = c.getDeclaredField("name");

/*获取方法*/
Method[] publicMethods = c.getMethods();
Method[] methods = c.getDeclaredMethods();
Method getName = c.getDeclaredMethod("getName");
Method setName = c.getMethod("setName", String.class);
Method enclosingMethod = c.getEnclosingMethod();

/*获取注解*/
Annotation[] annotations = c.getAnnotations();
Annotation[] declaredAnnotations = c.getDeclaredAnnotations();
Annotation[] annotationsByTypes = c.getAnnotationsByType(AnnotationType.class);
Annotation[] declaredAnnotationsByTypes = c.getDeclaredAnnotationsByType(AnnotationType.class);
Annotation annotation = c.getAnnotation(AnnotationType.class);
Annotation declaredAnnotation = c.getDeclaredAnnotation(AnnotationType.class);

AnnotatedType[] annotatedInterfaces = c.getAnnotatedInterfaces();
AnnotatedType annotatedType = c.getAnnotatedSuperclass();

Type[] types = c.getGenericInterfaces();
Type type = c.getGenericSuperclass();
/*其他*/
Object[] enums = c.getEnumConstants();
Object[] signers = c.getSigners();
Object cast = c.cast(o);
TypeVariable<Class>[] typeVariables = c.getTypeParameters();
URL url = c.getResource("xxx");

/*判断*/
boolean desiredAssertionStatus = c.desiredAssertionStatus();
boolean isAnnotation = c.isAnnotation();
boolean isAnnotationPresent = c.isAnnotationPresent(AnnotationType.class);
boolean isAnonymousClass = c.isAnonymousClass();
boolean isArray = c.isArray();
boolean isAssignableFrom = c.isAssignableFrom(Human.class);
boolean isEnum = c.isEnum();
boolean isInstance = c.isInstance(o);
boolean isInterface = c.isInterface();
boolean isLocalClass = c.isLocalClass();
boolean isMemberClass = c.isMemberClass();
boolean isPrimitive = c.isPrimitive();
boolean isSynthetic = c.isSynthetic();
int modifiers = c.getModifiers();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值