java 反射机制
本文导引:
通过反射机制
- 获取类的基本信息
- 获取类的注解信息
- 获取泛型信息
packagereflection; @AnnotationUserTable("datebaseExample") publicclassUser{ @AnnotationUserField(uName="name",type="varchar",length=10) privateStringname; @AnnotationUserField(uName="age",type="int",length=3) privateintage; @AnnotationUserField(uName="sex",type="char",length=2) privateStringsex; publicUser(){ super(); } publicUser(Stringname,intage,Stringsex){ super(); this.name=name; this.age=age; this.sex=sex; } publicStringgetName(){ returnname; } publicvoidsetName(){ this.name="test"; } publicintgetAge(){ returnage; } publicStringgetSex(){ returnsex; } publicvoidsetSex(Stringsex){ this.sex=sex; } } bean:User
packagereflection; importjava.lang.annotation.ElementType; importjava.lang.annotation.Retention; importjava.lang.annotation.RetentionPolicy; importjava.lang.annotation.Target; @Target(value={ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) public@interfaceAnnotationUserTable{ Stringvalue(); } 自定义注解:类注解
packagereflection; importjava.lang.annotation.ElementType; importjava.lang.annotation.Retention; importjava.lang.annotation.RetentionPolicy; importjava.lang.annotation.Target; @Target(value={ElementType.FIELD}) @Retention(RetentionPolicy.RUNTIME) public@interfaceAnnotationUserField{ StringuName(); Stringtype(); intlength(); } 自定义注解:属性注解
packagereflection; importjava.lang.reflect.Constructor; importjava.lang.reflect.Field; importjava.lang.reflect.Method; publicclassDemo01{ staticClass<?>c=null; publicstaticvoidmain(String[]args){ try{ c=Class.forName("reflection.User"); }catch(ClassNotFoundExceptione){ e.printStackTrace(); } test();//获取类的属性、方法等信息 } staticvoidtest(){ try{ //获取类的名称 System.out.println("获取类的名称"); System.out.println("getName():"+c.getName());//获得包名+类名 System.out.println("getSimpleName():"+c.getSimpleName());//获得类名 System.out.println("getCanonicalName():"+c.getCanonicalName());//获得类名 System.out.println("*******************************"); //获取属性信息 System.out.println("获取属性信息"); Field[]fields=c.getDeclaredFields(); //Field[]fields=c.getFields();只能获取public修饰的属性信息 for(Fieldf:fields){ StringfName=f.getName(); System.out.println(c.getDeclaredField(fName)); } System.out.println("*******************************"); //获取方法信息 System.out.println("获取方法信息"); Method[]methods=c.getDeclaredMethods(); for(Methodm:methods){ //StringmName=m.getName(); System.out.println(m.getName()+"-->"+m); } System.out.println("通过名称单独获取对应的getName方法:"+c.getDeclaredMethod("getName")); System.out.println("通过名称单独获取对应的setSex方法:"+c.getDeclaredMethod("setSex",String.class));//方法有参,必须传递参数类型 System.out.println("*******************************"); //获取构造器信息 System.out.println("获取构造器信息"); Constructor<?>[]constructor=c.getConstructors(); for(Constructor<?>cons:constructor){ System.out.println(cons); } }catch(NoSuchFieldException|SecurityExceptione){ e.printStackTrace(); }catch(NoSuchMethodExceptione){ e.printStackTrace(); } } } main1:通过反射机制获取类的基本信息
output:
获取类的名称 getName():reflection.User getSimpleName():User getCanonicalName():reflection.User ******************************* 获取属性信息 privatejava.lang.Stringreflection.User.name privateintreflection.User.age privatejava.lang.Stringreflection.User.sex ******************************* 获取方法信息 getName-->publicjava.lang.Stringreflection.User.getName() setName-->publicvoidreflection.User.setName() setSex-->publicvoidreflection.User.setSex(java.lang.String) getSex-->publicjava.lang.Stringreflection.User.getSex() getAge-->publicintreflection.User.getAge() 通过名称单独获取对应的getName方法:publicjava.lang.Stringreflection.User.getName() 通过名称单独获取对应的setSex方法:publicvoidreflection.User.setSex(java.lang.String) ******************************* 获取构造器信息 publicreflection.User() publicreflection.User(java.lang.String,int,java.lang.String) ViewConsole
下面的例子,是通过反射机制获取类的注解信息。
packagereflection; importjava.lang.reflect.Field; /** *获取类的属性、方法等信息 *1.获取元素对象(如属性)(注意:读取类的注解,看似要少一步) *2.获取该元素对象的指定类型的注解对象 *3.读取注解对象相应的值 */ publicclassTest02{ staticClass<?>c=null; publicstaticvoidmain(String[]args){ try{ c=Class.forName("reflection.User"); }catch(ClassNotFoundExceptione){ e.printStackTrace(); } test(); } staticvoidtest(){ try{ //获取类的指定注解 System.out.println("***********类的指定注解**************"); AnnotationUserTabletable=(AnnotationUserTable)c.getAnnotation(AnnotationUserTable.class); System.out.println(table.value()); //获取属性的指定注解 System.out.println("***********属性的指定注解*************"); Fieldfield=c.getDeclaredField("name"); AnnotationUserFieldannoField=(AnnotationUserField)field.getAnnotation(AnnotationUserField.class); System.out.println(annoField.uName()+"\t"+annoField.type()+"\t"+annoField.length()); //根据获得的表名、字段的信息,拼写出DDL语句,然后通过JDBC连接数据库查询 }catch(NoSuchFieldExceptione){ e.printStackTrace(); }catch(SecurityExceptione){ e.printStackTrace(); } } }
output:
***********类的指定注解************** datebaseExample ***********属性的指定注解************* namevarchar10
下面的例子,是通过反射机制获取泛型信息
packagereflection; importjava.lang.reflect.Method; importjava.lang.reflect.Type; importjava.util.List; importjava.util.Map; /** *通过反射机制获取泛型 *@authorAdministrator * */ publicclassTest03{ publicstaticvoidmain(String[]args){ Class<?>c=Test03.class; try{ System.out.println("*******获取参数值的类型**********"); Methodm1=c.getDeclaredMethod("method01",Map.class,List.class); Type[]types=m1.getGenericParameterTypes(); for(Typet:types){ System.out.println(t.getTypeName()); System.out.println(t.toString()); } System.out.println("*******获取返回值的类型**********"); Methodm2=c.getDeclaredMethod("method02"); Typeret=m2.getGenericReturnType(); System.out.println(ret.getTypeName()); System.out.println(ret.toString()); }catch(NoSuchMethodException|SecurityExceptione){ e.printStackTrace(); } } publicvoidmethod01(Map<String,String>args1,List<Integer>args2){ } publicMap<String,String>method02(){ returnnull; } } 通过反射机制获取泛型信息
output:
java.util.Map<java.lang.String,java.lang.String> java.util.Map<java.lang.String,java.lang.String> java.util.Map<java.lang.String,java.lang.String> java.util.Map<java.lang.String,java.lang.String> java.util.List<java.lang.Integer> java.util.List<java.lang.Integer> ViewConsole
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持毛票票!