第一组:java.lang.Class 类
- getName:获取全类名
- getSimpleName:获取简单类名
- getFields:获取所有public修饰的属性,包含本类以及父类的
- getDeclaredFields:获取本类中所有属性
- getMethods:获取所有public修饰的方法,包含本类以及父类的
- getDeclaredMethods:获取本类中所有方法
- getConstructors:获取本类中所有public修饰的构造器
- getDeclaredConstructors:获取本类中所有构造器
- getPackage:以Package形式返回包信息
- getSuperClass:以Class形式返回父类信息
- getInterfaces:以Class形式返回接口信息
- getAnnotations:以Annotation形式返回注解信息
第二组:java.lang.reflect.Field 类
getName
:返回属性名getModifiers
:以 int
形式返回修饰符【说明:默认修饰符是 0,public
是 1 ,private
是 2 ,protected
是 4,static
是 8,final
是 16】getType
:返回该属性对应的类 的 Class
对象
第三组:java.lang.reflect.Method 类
getModifiers()
:以 int 形式返回修饰符【说明:默认修饰符是 0,public
是 1 ,private
是 2 ,protected
是 4,static
是 8 ,final
是 16】getReturnType()
:以 Class
形式获取返回类型getName
:返回方法名getParameterTypes
:以 Class[]
返回参数类型数组
第四组:java.lang.reflect.Constructor 类
getModifiers
:以int形式返回修饰符getName
:返回构造器名(全类名)getParameterTypes
:以Class返回参数类型数组
以上四组演示:
package reflection_;
import org.junit.Test;
import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class ReflectionUtils {
@Test
public void api01() throws ClassNotFoundException {
Class<?> personCls = Class.forName("reflection_.Person");
System.out.println(personCls.getName());
System.out.println(personCls.getSimpleName());
Field[] fields = personCls.getFields();
for (Field field : fields) {
System.out.println("本类及父类的属性:" + field.getName());
}
Field[] declaredFields = personCls.getDeclaredFields();
for (Field field : declaredFields) {
System.out.println("本类的所有属性:" + field.getName());
}
Method[] methods = personCls.getMethods();
for (Method method : methods) {
System.out.println("本类及父类的所有 public 修饰的方法:" + method.getName());
}
Method[] declaredMethods = personCls.getDeclaredMethods();
for (Method method : declaredMethods) {
System.out.println("本类的所有方法:" + method.getName());
}
Constructor[] constructors = personCls.getConstructors();
for (Constructor constructor : constructors) {
System.out.println("本类及父类的所有 public 修饰的构造器:" + constructor.getName());
}
Constructor[] declaredConstructors = personCls.getDeclaredConstructors();
for (Constructor constructor : declaredConstructors) {
System.out.println("本类的所有构造器:" + constructor.getName());
}
System.out.println("以 Package 形式返回包的信息:" + personCls.getPackage());
System.out.println("以 Class 形式返回父类信息:" + personCls.getSuperclass());
Class<?>[] interfaces = personCls.getInterfaces();
for (Class aClass : interfaces) {
System.out.println("以 Class 形式返回接口信息:" + aClass);
}
Annotation[] annotations = personCls.getAnnotations();
for (Annotation annotation : annotations) {
System.out.println("以 Annotation 形式返回注解信息:" + annotation);
}
}
@Test
public void api02() throws ClassNotFoundException {
Class<?> personCls = Class.forName("reflection_.Person");
Field[] declaredFields = personCls.getDeclaredFields();
for (Field field : declaredFields) {
System.out.println("本类的所有属性:" + field.getName()
+ " 该属性的修饰符值:" + field.getModifiers()
+ " 该属性的 Class 类型:" + field.getType());
}
}
@Test
public void api03() throws ClassNotFoundException {
Class<?> personCls = Class.forName("reflection_.Person");
Method[] declaredMethods = personCls.getDeclaredMethods();
for (Method method : declaredMethods) {
System.out.println("本类的所有方法:" + method.getName()
+ " 该方法的访问修饰符值:" + method.getModifiers()
+ " 该方法的返回类型:" + method.getReturnType());
Class<?>[] parameterTypes = method.getParameterTypes();
for (Class aClass : parameterTypes) {
System.out.println("当前方法的形参数组:" + aClass);
}
}
}
@Test
public void api04() throws ClassNotFoundException {
Class<?> personCls = Class.forName("reflection_.Person");
Constructor[] declaredConstructors = personCls.getDeclaredConstructors();
for (Constructor constructor : declaredConstructors) {
System.out.println("============");
System.out.println("本类的所有构造器:" + constructor.getName()
+ " 本类所有构造器的修饰符值:" + constructor.getModifiers());
Class[] parameterTypes = constructor.getParameterTypes();
for (Class aClass : parameterTypes) {
System.out.println(" 本类所有构造器的形参:" + aClass);
}
}
}
}
class A {
public String hobby;
public void hi() {
}
public A() {
}
}
interface IA {
}
interface IB {
}
@Deprecated
class Person extends A implements IA, IB {
public String name;
protected int age;
String job;
private double sal;
public Person() {
}
public Person(String name) {
this.name = name;
}
private Person(String name, int age) {
this.name = name;
this.age = age;
}
public void m1(String name, int age, double sal) {
}
protected String m2() {
return null;
}
void m3() {
}
private void m4() {
}
}
第一组
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/56bfb45756ca5a5773c575b0eea724b9.png)
第二组
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/cc4d07a3b840ef7e8ffea3d88817e039.png)
第三组
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/8260001ce0dc4fd207c0de0a9ca1e902.png)
第四组
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/20975d5e0500ef6fbd5b3cb81a78446b.png)