数据准备
- import java.lang.annotation.Retention;
- import java.lang.annotation.RetentionPolicy;
- import java.lang.annotation.Target;
- import static java.lang.annotation.ElementType.*;
-
- @Target({TYPE,FIELD,METHOD,PARAMETER,CONSTRUCTOR,LOCAL_VARIABLE})
- @Retention(RetentionPolicy.RUNTIME)
-
- public @interface MyAnnotation {
-
- String value() default "hello";
- }
- public interface MyInterface {
-
- void info();
- }
- public class Creature<T> implements Serializable {
-
- private char gender;
- public double weight;
-
- private void breath() {
- System.out.println("生物呼吸");
- }
-
- private void eat() {
- System.out.println("生物吃东西");
- }
- }
- import java.util.Comparator;
-
- @MyAnnotation(value = "hi")
- public class Person extends Creature<String> implements Comparator<String> ,MyInterface{
-
- private String name;
- int age;
- public int id;
-
-
- public Person() {
- super();
- }
-
- private Person(String name) {
- this.name=name;
- }
-
- @MyAnnotation(value="abc")
- public Person(String name, int age, int id) {
- super();
- this.name = name;
- this.age = age;
- this.id = id;
- }
-
- @MyAnnotation
- private String show(String nation) {
- System.out.println("我的国籍是:"+nation);
- return nation;
- }
-
- public String display(String interests,int age)
- throws NullPointerException,ClassCastException{
- return interests+age;
- }
-
- @Override
- public void info() {
- System.out.println("我是一个人");
- }
-
- @Override
- public int compare(String o1, String o2) {
- return 0;
- }
-
-
- private static void showDesc() {
- System.out.println("我是一个可爱的人");
- }
-
- @Override
- public String toString() {
- return "Person [name=" + name + ", age=" + age + ", id=" + id + "]";
- }
-
-
- }
测试类
FieldTest:获取当前运行时类的属性结构
- import java.lang.reflect.Field;
- import java.lang.reflect.Modifier;
-
- import org.junit.jupiter.api.Test;
-
- import reflect.java2.Person;
-
-
-
- /**
- * 获取当前运行时类的属性结构
- * @author CharlieLiang
- *
- */
- public class FieldTest {
-
- @Test
- public void test() {
- Class clazz = Person.class;
-
- //获取属性结构
- // getFields():获取当前运行时类及其父类中声明为public访问权限的属性
- System.out.println("--getFields()--");
- Field[] fs= clazz.getFields();
- for(Field f: fs) {
- System.out.println(f);
- }
-
- System.out.println("---getDeclaredFields()----");
- //getDeclaredFields():获取当前类的所有属性,不包含父类
- Field[] fs2 = clazz.getDeclaredFields();
- for(Field f:fs2) {
- System.out.println(f);
- }
- }
-
- // 权限修饰符 数据类型 变量名
- @Test
- public void test2() {
- Class clazz = Person.class;
- Field[] fs = clazz.getDeclaredFields();
-
- for(Field f:fs) {
- //1. 权限修饰符 0:默认修饰符,modifer是数字,把数字转为修饰符名字
- int modifiers = f.getModifiers();
- System.out.print(Modifier.toString(modifiers)+"\t");
-
- //2. 数据类型
- Class type=f.getType();
- System.out.print(type.getName()+"\t");
-
- // 3.变量名
- String fName=f.getName();
- System.out.print(fName);
- System.out.println();
- }
-
-
- }
- }
运行结果:
MehtodTest:获取运行时类的方法
- import java.lang.annotation.Annotation;
- import java.lang.reflect.Method;
- import java.lang.reflect.Modifier;
-
- import org.junit.jupiter.api.Test;
-
- import reflect.java2.Person;
-
-
- /**
- * 获取运行时类的方法的使用
- * @author CharlieLiang
- *
- */
- public class MethodTest {
-
- @Test
- public void test() {
- Class clazz=Person.class;
- //getMethods():获取当前类及其所有父类中声明为public权限的方法
- System.out.println("-----------getMethods()-----------------");
- Method[] mds = clazz.getMethods();
- for(Method md:mds) {
- System.out.println(md);
- }
- System.out.println("-----------getDeclaredMethods()------------");
- // 获取当前类的声明的方法(不包含父类)
- Method[] dmds = clazz.getDeclaredMethods();
- for(Method md:dmds) {
- System.out.println(md);
- }
- }
-
- /**
- * @XXX
- * 权限修饰符 返回值类型 方法名(参数类型1 形参名1,....) throws XxxException()
- */
- @Test
- public void test2() {
- Class clazz=Person.class;
- Method[] dmds = clazz.getDeclaredMethods();
- for(Method md:dmds) {
- //获取注解
-
- Annotation[] annos = md.getAnnotations();
-
- for(Annotation a: annos) {
-
- System.out.println(a);
- }
-
- //2 .权限修饰符
- System.out.print(Modifier.toString(md.getModifiers())+"\t");
-
- //3. 返回值类型 getReturnType()-->class java.lang.String
- // getReturnType().getName()-->java.lang.String
- System.out.print(md.getReturnType().getName()+"\t");
- System.out.print(md.getName());
-
- System.out.print("(");
- //5. 形参列表
- Class<?>[] parameterTypes = md.getParameterTypes();
- if(!((parameterTypes==null)&&(parameterTypes.length==0))) {
- for(int i=0;i<parameterTypes.length;i++) {
- if(i==parameterTypes.length-1) {
- System.out.print(parameterTypes[i].getName()+" arg_"+i);
- break;
- }
- System.out.print(parameterTypes[i].getName()+" arg_"+i+",");
- }
- }
- System.out.print(")");
-
-
- //6.异常
- Class<?>[] exceptionTypes = md.getExceptionTypes();
- if(exceptionTypes.length>0) {
- System.out.print("throws ");
- for(int i=0;i<exceptionTypes.length;i++) {
- if(i== exceptionTypes.length-1) {
- System.out.print(exceptionTypes[i].getName());
- break;
- }
- System.out.print(exceptionTypes[i].getName()+",");
- }
-
- }
- System.out.println();
- }
- }
- }
OtherTest:其他测试
- import java.lang.annotation.Annotation;
- import java.lang.reflect.Constructor;
- import java.lang.reflect.ParameterizedType;
- import java.lang.reflect.Type;
-
- import org.junit.jupiter.api.Test;
-
- import reflect.java2.Person;
-
- /**
- *
- * @author CharlieLiang
- *
- */
- public class OtherTest {
-
- /**
- * 获取构造器结构
- */
- @Test
- public void test() {
- Class clazz=Person.class;
-
- Constructor[] constructors = clazz.getConstructors();
- System.out.println("----------getConstructors()--------------");
- // getConstructors():获取当前运行时类声明为public的构造器
- for(Constructor c: constructors) {
- System.out.println(c);
- }
- // getDeclaredConstructors():获取当前运行时类中声明的所有构造器
- System.out.println("---------getDeclaredConstructors()-----------");
- Constructor[] declaredConstructors = clazz.getDeclaredConstructors();
- for(Constructor c:declaredConstructors) {
- System.out.println(c);
- }
- }
-
- /**
- * 获取运行时类的父类
- */
- @Test
- public void test2() {
- Class clazz=Person.class;
- Class superclass = clazz.getSuperclass();
- System.out.println(superclass);
- }
-
-
- /**
- * 获取运行时类的带泛型的父类
- */
- @Test
- public void test3() {
- Class clazz=Person.class;
- Type genericSuperclass = clazz.getGenericSuperclass();
- System.out.println(genericSuperclass);
- }
-
- /**
- * 获取运行时类的父类的泛型
- */
- @Test
- public void test4() {
- Class clazz=Person.class;
- Type genericSuperclass = clazz.getGenericSuperclass();
- ParameterizedType paramType=(ParameterizedType) genericSuperclass;
- //获取泛型类型
- Type[] actualTypeArguments = paramType.getActualTypeArguments();
- System.out.println(actualTypeArguments[0]);
- System.out.println(actualTypeArguments[0].getTypeName());
- }
-
- /**
- * 获取运行时类的接口
- */
- @Test
- public void test5() {
- Class clazz = Person.class;
- Class[] interfaces = clazz.getInterfaces();
- for(Class c: interfaces) {
- System.out.println(c);
- }
-
- System.out.println();
- Class[] interfaces2 = clazz.getSuperclass().getInterfaces();
-
- for(Class c: interfaces2) {
- System.out.println(c);
- }
- }
-
- /**
- * 获取运行时类所在的包
- */
- @Test
- public void test6() {
- Class clazz = Person.class;
- Package pack = clazz.getPackage();
- System.out.println(pack);
- }
-
-
- /**
- * 获取运行时类所在的注解
- */
- @Test
- public void test7() {
- Class clazz = Person.class;
- Annotation[] as = clazz.getAnnotations();
- for(Annotation aaa:as) {
- System.out.println(aaa);
- }
- }
- }
ReflectionTest:调用运行时类指定的结构: 属性,方法,构造器
- import java.lang.reflect.Constructor;
- import java.lang.reflect.Field;
- import java.lang.reflect.Method;
-
- import org.junit.jupiter.api.Test;
-
- import reflect.java2.Person;
-
-
-
- /**
- * 调用运行时类指定的 结构: 属性,方法,构造器
- * @author CharlieLiang
- *
- */
- public class ReflectionTest {
-
- @Test
- public void testField() throws Exception {
- Class clazz=Person.class;
-
- //创建运行时类对象
- Person p= (Person) clazz.newInstance();
-
- //获取指定的属性getField():获取public变量
- Field id= clazz.getField("id");
-
- id.set(p, 1001);
-
- //获取当前属性的值
- int pid = (int) id.get(p);
- System.out.println(pid);
- }
-
- /**
- * 如何操作运行时类中的指定属性 -- 需要掌握
- * @throws Exception
- */
- @Test
- public void testField2() throws Exception {
- Class clazz=Person.class;
-
- //创建运行时类对象
- Person p= (Person) clazz.newInstance();
-
- //获取指定的属性getField():获取public变量
- //私有变量name
- Field name= clazz.getDeclaredField("name");
- //保证当前属性是可访问的
- name.setAccessible(true);
- //获取、设置指定对象的此属性值
- //set(对象,参数)
- name.set(p, "Jerry");
- System.out.println(name.get(p));
- }
-
-
- /**
- * 如何操作运行时类中的指定方法 -- 需要掌握
- *
- */
- @Test
- public void testField3() throws Exception {
- Class clazz=Person.class;
-
- //创建运行时类对象
- Person p= (Person) clazz.newInstance();
- //getDeclaredMethod(方法名,参数列表)
- Method show=clazz.getDeclaredMethod("show", String.class);
- show.setAccessible(true);
-
- //invoke(对象,参数列表),invoke有方法返回值
- Object returnVal = show.invoke(p, "CHN");
- System.out.println("返回值"+returnVal);
-
- System.out.println("----如何调用静态方法-------");
-
- //private static void showDesc()
- Method showDesc=clazz.getDeclaredMethod("showDesc");
- showDesc.setAccessible(true);
-
- //showDesc:无参数,void型的invoke返回null
- Object returnval2 = showDesc.invoke(Person.class);
- System.out.println("返回值:"+returnval2);
- }
-
-
- /**
- * 如何操作运行时类中的指定构造器
- * @throws Exception
- * @throws
- *
- */
- @Test
- public void testField4() throws Exception {
- Class clazz=Person.class;
-
- //getDeclaredConstructor():指明参数列表
- Constructor cons =clazz.getDeclaredConstructor(String.class);
-
- cons.setAccessible(true);
- //用构造器创建运行时类的对象
- Person person = (Person) cons.newInstance("Jack");
-
- System.out.println(person);
- }
-
- }