本文导引:
通过反射机制
1 package reflection; 2 3 @AnnotationUserTable("datebaseExample") 4 public class User { 5 @AnnotationUserField(uName="name",type="varchar",length=10) 6 private String name; 7 @AnnotationUserField(uName="age",type="int",length=3) 8 private int age; 9 @AnnotationUserField(uName="sex",type="char",length=2)10 private String sex;11 12 public User() {13 super();14 }15 16 public User(String name, int age, String sex) {17 super();18 this.name = name;19 this.age = age;20 this.sex = sex;21 }22 23 public String getName() {24 return name;25 }26 public void setName() {27 this.name = "test";28 }29 public int getAge() {30 return age;31 }32 public String getSex() {33 return sex;34 }35 public void setSex(String sex) {36 this.sex = sex;37 }38 }
1 package reflection; 2 3 import java.lang.annotation.ElementType; 4 import java.lang.annotation.Retention; 5 import java.lang.annotation.RetentionPolicy; 6 import java.lang.annotation.Target; 7 8 @Target(value={ElementType.TYPE}) 9 @Retention(RetentionPolicy.RUNTIME) 10 public @interface AnnotationUserTable {11 String value();12 }
1 package reflection; 2 3 import java.lang.annotation.ElementType; 4 import java.lang.annotation.Retention; 5 import java.lang.annotation.RetentionPolicy; 6 import java.lang.annotation.Target; 7 8 @Target(value={ElementType.FIELD}) 9 @Retention(RetentionPolicy.RUNTIME)10 public @interface AnnotationUserField {11 String uName();12 String type();13 int length(); 14 }
1 package reflection; 2 3 import java.lang.reflect.Constructor; 4 import java.lang.reflect.Field; 5 import java.lang.reflect.Method; 6 7 public class Demo01 { 8 static Class c = null; 9 public static void main(String[] args) {10 try {11 c = Class.forName("reflection.User");12 } catch (ClassNotFoundException e) {13 e.printStackTrace();14 }15 test();//获取类的属性、方法等信息16 17 }18 static void test(){19 try {20 // 获取类的名称21 System.out.println("获取类的名称");22 System.out.println("getName():" + c.getName());// 获得包名+类名23 System.out.println("getSimpleName():" + c.getSimpleName());// 获得类名24 System.out.println("getCanonicalName():" + c.getCanonicalName());// 获得类名25 System.out.println("*******************************");26 // 获取属性信息27 System.out.println("获取属性信息");28 Field[] fields = c.getDeclaredFields();29 // Field[] fields = c.getFields(); 只能获取public修饰的属性信息30 for (Field f : fields) {31 String fName = f.getName();32 System.out.println(c.getDeclaredField(fName));33 }34 System.out.println("*******************************");35 // 获取方法信息36 System.out.println("获取方法信息");37 Method[] methods = c.getDeclaredMethods();38 for (Method m : methods) {39 // String mName = m.getName();40 System.out.println(m.getName() + "-->" + m);41 }42 System.out.println("通过名称单独获取对应的getName方法:" + c.getDeclaredMethod("getName"));43 System.out.println("通过名称单独获取对应的setSex方法:" + c.getDeclaredMethod("setSex", String.class));// 方法有参,必须传递参数类型44 System.out.println("*******************************");45 // 获取构造器信息46 System.out.println("获取构造器信息");47 Constructor [] constructor = c.getConstructors();48 for (Constructor cons : constructor) {49 System.out.println(cons);50 }51 } catch (NoSuchFieldException | SecurityException e) {52 e.printStackTrace();53 } catch (NoSuchMethodException e) {54 e.printStackTrace();55 }56 }57 }
output:
获取类的名称getName():reflection.UsergetSimpleName():UsergetCanonicalName():reflection.User*******************************获取属性信息private java.lang.String reflection.User.nameprivate int reflection.User.ageprivate java.lang.String reflection.User.sex*******************************获取方法信息getName-->public java.lang.String reflection.User.getName()setName-->public void reflection.User.setName()setSex-->public void reflection.User.setSex(java.lang.String)getSex-->public java.lang.String reflection.User.getSex()getAge-->public int reflection.User.getAge()通过名称单独获取对应的getName方法:public java.lang.String reflection.User.getName()通过名称单独获取对应的setSex方法:public void reflection.User.setSex(java.lang.String)*******************************获取构造器信息public reflection.User()public reflection.User(java.lang.String,int,java.lang.String)
下面的例子,是通过反射机制获取类的注解信息。
1 package reflection; 2 3 import java.lang.reflect.Field; 4 /** 5 * 获取类的属性、方法等信息 6 * 1.获取元素对象(如属性)(注意:读取类的注解,看似要少一步) 7 * 2.获取该元素对象的指定类型的注解对象 8 * 3.读取注解对象相应的值 9 */10 public class Test02 {11 static Class c = null;12 public static void main(String[] args) {13 try {14 c = Class.forName("reflection.User");15 } catch (ClassNotFoundException e) {16 e.printStackTrace();17 }18 test();19 }20 21 static void test(){22 try {23 // 获取类的指定注解24 System.out.println("***********类的指定注解**************");25 AnnotationUserTable table = (AnnotationUserTable)c.getAnnotation(AnnotationUserTable.class);26 System.out.println(table.value());27 28 // 获取属性的指定注解29 System.out.println("***********属性的指定注解*************");30 Field field = c.getDeclaredField("name");31 AnnotationUserField annoField = (AnnotationUserField)field.getAnnotation(AnnotationUserField.class);32 System.out.println(annoField.uName()+"\t"+annoField.type()+"\t"+annoField.length());33 34 // 根据获得的表名、字段的信息,拼写出DDL语句,然后通过JDBC连接数据库查询35 } catch (NoSuchFieldException e) {36 e.printStackTrace();37 } catch (SecurityException e) {38 e.printStackTrace();39 }40 }41 }
output:
***********类的指定注解**************datebaseExample***********属性的指定注解*************name varchar 10
下面的例子,是通过反射机制获取泛型信息
1 package reflection; 2 3 import java.lang.reflect.Method; 4 import java.lang.reflect.Type; 5 import java.util.List; 6 import java.util.Map; 7 8 /** 9 * 通过反射机制获取泛型10 * @author Administrator11 *12 */13 public class Test03 { 14 public static void main(String[] args) {15 Class c = Test03.class;16 try {17 System.out.println("*******获取参数值的类型**********");18 Method m1 = c.getDeclaredMethod("method01", Map.class,List.class);19 Type[] types = m1.getGenericParameterTypes();20 for(Type t:types){21 System.out.println(t.getTypeName());22 System.out.println(t.toString()); 23 }24 System.out.println("*******获取返回值的类型**********");25 Method m2 = c.getDeclaredMethod("method02");26 Type ret = m2.getGenericReturnType();27 System.out.println(ret.getTypeName());28 System.out.println(ret.toString());29 } catch (NoSuchMethodException | SecurityException e) {30 e.printStackTrace();31 }32 }33 34 public void method01(Mapargs1,List args2){35 36 }37 public Map method02(){38 return null;39 }40 }
output:
java.util.Mapjava.util.Map java.util.Map java.util.Map java.util.List java.util.List