第一组 api0_1 Java.lang.Class 类
第二组 api0_2 Java.lang.reflect.Field 类
- getModifiers:以 int 形式返回修饰符(默认修饰符是0,public 是1,private 是2,protected 是4,static 是8,final 是16)
- getType:以 Class 形式返回类型
- getName:返回属性名
第三组 api0_3 Java.lang.reflect.Method 类
- getModifiers:以 int 形式返回修饰符(默认修饰符是0,public 是1,private 是2,protected 是4,static 是8,final 是16)
- getReturnType:以 Class 形式获取返回类型
- getName:返回方法名
- getParameterTypes:以 Class[ ] 形式获取参数类型数组
第四组 api_04 Java.lang.reflect.Constructor 类
- getModifiers:以 int 形式返回修饰符(默认修饰符是0,public 是1,private 是2,protected 是4,static 是8,final 是16)
- getName:返回构造器名(全类名)
- getParameterTypes:以 Class[ ] 形式获取参数类型数组
package com.ftn.reflection.question;
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 {
public static void main(String[] args) {
}
@Test
public void api_01() throws ClassNotFoundException {
Class<?> personClass = Class.forName("com.ftn.reflection.question.Person");
System.out.println(personClass.getName());
System.out.println("--------------");
System.out.println(personClass.getSimpleName());
System.out.println("--------------");
Field[] fields = personClass.getFields();
for (Field field : fields) {
System.out.println("本类及父类的属性=" + field.getName());
}
System.out.println("--------------");
Field[] declaredFields = personClass.getDeclaredFields();
for (Field declaredField : declaredFields) {
System.out.println("本类中所有属性="+declaredField.getName());
}
System.out.println("--------------");
Method[] methods = personClass.getMethods();
for (Method method : methods) {
System.out.println("本类及父类的方法="+method.getName());
}
System.out.println("--------------");
Method[] declaredMethods = personClass.getDeclaredMethods();
for (Method declaredMethod : declaredMethods) {
System.out.println("本类中所有方法="+declaredMethod.getName());
}
System.out.println("--------------");
Constructor<?>[] constructors = personClass.getConstructors();
for (Constructor<?> constructor : constructors) {
System.out.println("本类及父类中所有构造器="+constructor.getName());
}
System.out.println("--------------");
Constructor<?>[] declaredConstructors = personClass.getDeclaredConstructors();
for (Constructor<?> declaredConstructor : declaredConstructors) {
System.out.println("本类中所有构造器="+declaredConstructor.getName());
}
System.out.println("--------------");
System.out.println(personClass.getPackage());
System.out.println("--------------");
Class<?> superclass = personClass.getSuperclass();
System.out.println("父类的Class对象="+superclass);
System.out.println("--------------");
Class<?>[] interfaces = personClass.getInterfaces();
for (Class<?> anInterface : interfaces) {
System.out.println("接口信息="+anInterface.getName());
}
System.out.println("--------------");
Annotation[] annotations = personClass.getAnnotations();
for (Annotation annotation : annotations) {
System.out.println("注解信息="+annotation);
}
}
@Test
public void api_02() throws ClassNotFoundException {
Class<?> personClass = Class.forName("com.ftn.reflection.question.Person");
Field[] declaredFields = personClass.getDeclaredFields();
for (Field declaredField : declaredFields) {
System.out.println("本类的属性=" + declaredField.getName() + "\t修饰符的int值=" + declaredField.getModifiers());
System.out.println( "本类属性类型=" + declaredField.getType());
}
}
@Test
public void api_03() throws ClassNotFoundException {
Class<?> personClass = Class.forName("com.ftn.reflection.question.Person");
Method[] declaredMethods = personClass.getDeclaredMethods();
for (Method declaredMethod : declaredMethods) {
System.out.println("本类中所有方法="+declaredMethod.getName() + "\t修饰符的int值=" + declaredMethod.getModifiers());
System.out.println("返回值类型=" + declaredMethod.getReturnType());
Class<?>[] parameterTypes = declaredMethod.getParameterTypes();
for (Class<?> parameterType : parameterTypes) {
System.out.println("参数类型=" + parameterType);
}
System.out.println("------------------");
}
}
@Test
public void api_04() throws ClassNotFoundException {
Class<?> personClass = Class.forName("com.ftn.reflection.question.Person");
Constructor<?>[] declaredConstructors = personClass.getDeclaredConstructors();
for (Constructor<?> declaredConstructor : declaredConstructors) {
System.out.println("本类中所有构造器="+declaredConstructor.getName() + "\t修饰符的int值=" + declaredConstructor.getModifiers());
Class<?>[] parameterTypes = declaredConstructor.getParameterTypes();
for (Class<?> parameterType : parameterTypes) {
System.out.println("构造器参数类型=" + parameterType);
}
System.out.println("-----------------------");
}
}
}
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){
}
private Person(String name,int age){
}
public void m1(){
}
protected void m2(String name){
}
void m3(String name,int age){
}
private void m4(){
}
}