java 反射機制
更新時間:2017年02月06日 09:46:25 作者:fhw
本文主要介紹了java反射機制的相關(guān)知識,具有一定的參考價值,下面跟著小編一起來看下吧
本文導引:
通過反射機制
- 獲取類的基本信息
- 獲取類的注解信息
- 獲取泛型信息
package reflection; @AnnotationUserTable("datebaseExample") public class User { @AnnotationUserField(uName="name",type="varchar",length=10) private String name; @AnnotationUserField(uName="age",type="int",length=3) private int age; @AnnotationUserField(uName="sex",type="char",length=2) private String sex; public User() { super(); } public User(String name, int age, String sex) { super(); this.name = name; this.age = age; this.sex = sex; } public String getName() { return name; } public void setName() { this.name = "test"; } public int getAge() { return age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } } bean:User
package reflection; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target(value={ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) public @interface AnnotationUserTable { String value(); } 自定義注解:類注解
package reflection; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target(value={ElementType.FIELD}) @Retention(RetentionPolicy.RUNTIME) public @interface AnnotationUserField { String uName(); String type(); int length(); } 自定義注解:屬性注解
package reflection; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.Method; public class Demo01 { static Class<?> c = null; public static void main(String[] args) { try { c = Class.forName("reflection.User"); } catch (ClassNotFoundException e) { e.printStackTrace(); } test();//獲取類的屬性、方法等信息 } static void test(){ 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 (Field f : fields) { String fName = f.getName(); System.out.println(c.getDeclaredField(fName)); } System.out.println("*******************************"); // 獲取方法信息 System.out.println("獲取方法信息"); Method[] methods = c.getDeclaredMethods(); for (Method m : methods) { // String mName = m.getName(); System.out.println(m.getName() + "-->" + m); } System.out.println("通過名稱單獨獲取對應的getName方法:" + c.getDeclaredMethod("getName")); System.out.println("通過名稱單獨獲取對應的setSex方法:" + c.getDeclaredMethod("setSex", String.class));// 方法有參,必須傳遞參數(shù)類型 System.out.println("*******************************"); // 獲取構(gòu)造器信息 System.out.println("獲取構(gòu)造器信息"); Constructor<?>[] constructor = c.getConstructors(); for (Constructor<?> cons : constructor) { System.out.println(cons); } } catch (NoSuchFieldException | SecurityException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } } } main1:通過反射機制獲取類的基本信息
output:
獲取類的名稱 getName():reflection.User getSimpleName():User getCanonicalName():reflection.User ******************************* 獲取屬性信息 private java.lang.String reflection.User.name private int reflection.User.age private 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) ******************************* 獲取構(gòu)造器信息 public reflection.User() public reflection.User(java.lang.String,int,java.lang.String) View Console
下面的例子,是通過反射機制獲取類的注解信息。
package reflection; import java.lang.reflect.Field; /** * 獲取類的屬性、方法等信息 * 1.獲取元素對象(如屬性)(注意:讀取類的注解,看似要少一步) * 2.獲取該元素對象的指定類型的注解對象 * 3.讀取注解對象相應的值 */ public class Test02 { static Class<?> c = null; public static void main(String[] args) { try { c = Class.forName("reflection.User"); } catch (ClassNotFoundException e) { e.printStackTrace(); } test(); } static void test(){ try { // 獲取類的指定注解 System.out.println("***********類的指定注解**************"); AnnotationUserTable table = (AnnotationUserTable)c.getAnnotation(AnnotationUserTable.class); System.out.println(table.value()); // 獲取屬性的指定注解 System.out.println("***********屬性的指定注解*************"); Field field = c.getDeclaredField("name"); AnnotationUserField annoField = (AnnotationUserField)field.getAnnotation(AnnotationUserField.class); System.out.println(annoField.uName()+"\t"+annoField.type()+"\t"+annoField.length()); // 根據(jù)獲得的表名、字段的信息,拼寫出DDL語句,然后通過JDBC連接數(shù)據(jù)庫查詢 } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (SecurityException e) { e.printStackTrace(); } } }
output:
***********類的指定注解************** datebaseExample ***********屬性的指定注解************* name varchar 10
下面的例子,是通過反射機制獲取泛型信息
package reflection; import java.lang.reflect.Method; import java.lang.reflect.Type; import java.util.List; import java.util.Map; /** * 通過反射機制獲取泛型 * @author Administrator * */ public class Test03 { public static void main(String[] args) { Class<?> c = Test03.class; try { System.out.println("*******獲取參數(shù)值的類型**********"); Method m1 = c.getDeclaredMethod("method01", Map.class,List.class); Type[] types = m1.getGenericParameterTypes(); for(Type t:types){ System.out.println(t.getTypeName()); System.out.println(t.toString()); } System.out.println("*******獲取返回值的類型**********"); Method m2 = c.getDeclaredMethod("method02"); Type ret = m2.getGenericReturnType(); System.out.println(ret.getTypeName()); System.out.println(ret.toString()); } catch (NoSuchMethodException | SecurityException e) { e.printStackTrace(); } } public void method01(Map<String,String> args1,List<Integer> args2){ } public Map<String,String> method02(){ return null; } } 通過反射機制獲取泛型信息
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> View Console
以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學習或者工作能帶來一定的幫助,同時也希望多多支持腳本之家!
相關(guān)文章
完美解決Server?returned?HTTP?response?code:403?for?URL報錯問題
在調(diào)用某個接口的時候,突然就遇到了Server?returned?HTTP?response?code:?403?for?URL報錯這個報錯,導致獲取不到接口的數(shù)據(jù),下面小編給大家分享解決Server?returned?HTTP?response?code:403?for?URL報錯問題,感興趣的朋友一起看看吧2023-03-03SpringBoot+OCR實現(xiàn)PDF內(nèi)容識別的示例代碼
在SpringBoot中,您可以結(jié)合OCR庫來實現(xiàn)對PDF文件內(nèi)容的識別和提取,本文就來介紹一下如何使用 Tesseract 和 pdf2image 對 PDF 文件進行OCR識別和提取,具有一定的參考價值,感興趣的可以了解一下2023-12-12關(guān)于使用MyBatis簡化JDBC開發(fā)和解決SQL語句警告的問題
這篇文章主要介紹了關(guān)于使用MyBatis簡化JDBC開發(fā)和解決SQL語句警告的問題,如果idea和數(shù)據(jù)庫沒有建立鏈接,idea不識別表的信息,就會出現(xiàn)SQL語句的警告,需要的朋友可以參考下2023-05-05SpringBoot文件上傳同時接收復雜參數(shù)的過程詳解
這篇文章主要介紹了SpringBoot文件上傳同時,接收復雜參數(shù),本文通過示例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-12-12