第一组: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 {
// 第一组 API
@Test
public void api01() throws ClassNotFoundException {
Class> personCls = Class.forName("reflection_.Person");
// 1. getName:获取全类名
System.out.println(personCls.getName());
// 2. getSimpleName:获取简单类名
System.out.println(personCls.getSimpleName());
// 3. getFields:获取所有public修饰的属性,包含本类以及父类的
Field[] fields = personCls.getFields();
for (Field field : fields) {
System.out.println("本类及父类的属性:" + field.getName());
}
// 4. getDeclaredFields:获取本类中所有属性
Field[] declaredFields = personCls.getDeclaredFields();
for (Field field : declaredFields) {
System.out.println("本类的所有属性:" + field.getName());
}
// 5. getMethods:获取所有public修饰的方法,包含本类以及父类的
Method[] methods = personCls.getMethods();
for (Method method : methods) {
System.out.println("本类及父类的所有 public 修饰的方法:" + method.getName());
}
// 6. getDeclaredMethods:获取本类中所有方法
Method[] declaredMethods = personCls.getDeclaredMethods();
for (Method method : declaredMethods) {
System.out.println("本类的所有方法:" + method.getName());
}
// 7. getConstructors:获取本类中所有public修饰的构造器
Constructor[] constructors = personCls.getConstructors();
for (Constructor constructor : constructors) {
System.out.println("本类及父类的所有 public 修饰的构造器:" + constructor.getName());
}
// 8. getDeclaredConstructors:获取本类中所有构造器
Constructor[] declaredConstructors = personCls.getDeclaredConstructors();
for (Constructor constructor : declaredConstructors) {
System.out.println("本类的所有构造器:" + constructor.getName());
}
// 9. getPackage:以Package形式返回包信息
System.out.println("以 Package 形式返回包的信息:" + personCls.getPackage());
// 10. getSuperClass:以Class形式返回父类信息
System.out.println("以 Class 形式返回父类信息:" + personCls.getSuperclass());
// 11. getInterfaces:以Class形式返回接口信息
Class>[] interfaces = personCls.getInterfaces();
for (Class aClass : interfaces) {
System.out.println("以 Class 形式返回接口信息:" + aClass);
}
// 12. getAnnotations:以Annotation形式返回注解信息
Annotation[] annotations = personCls.getAnnotations();
for (Annotation annotation : annotations) {
System.out.println("以 Annotation 形式返回注解信息:" + annotation);
}
}
// 第二组 API
@Test
public void api02() throws ClassNotFoundException {
Class> personCls = Class.forName("reflection_.Person");
// getDeclaredFields:获取本类中所有属性
Field[] declaredFields = personCls.getDeclaredFields();
for (Field field : declaredFields) {
// getModifiers():获取该属性的修饰符对应的值
// getModifiers:以 int 形式返回修饰符【说明:默认修饰符是 0,public 是 1 ,private 是 2 ,
// protected 是 4,static 是 8,final 是 16】
System.out.println("本类的所有属性:" + field.getName()
+ " 该属性的修饰符值:" + field.getModifiers()
+ " 该属性的 Class 类型:" + field.getType());
}
}
// 第三组 API
@Test
public void api03() throws ClassNotFoundException {
Class> personCls = Class.forName("reflection_.Person");
// getDeclaredMethods:获取本类中所有方法
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);
}
}
}
// 第四组 API
@Test
public void api04() throws ClassNotFoundException {
Class> personCls = Class.forName("reflection_.Person");
// getDeclaredConstructors:获取本类中所有构造器
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() {
}
}
第一组
第二组
第三组
第四组