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();
java.lang.Class类
最新推荐文章于 2024-04-23 17:00:00 发布