java 反射機制
本文導(dǎo)引:
通過反射機制
- 獲取類的基本信息
- 獲取類的注解信息
- 獲取泛型信息
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("通過名稱單獨獲取對應(yīng)的getName方法:" + c.getDeclaredMethod("getName"));
System.out.println("通過名稱單獨獲取對應(yīng)的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() 通過名稱單獨獲取對應(yīng)的getName方法:public java.lang.String reflection.User.getName() 通過名稱單獨獲取對應(yīng)的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.讀取注解對象相應(yīng)的值
*/
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)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時也希望多多支持腳本之家!
相關(guān)文章
IDEA的Terminal無法執(zhí)行g(shù)it命令問題
這篇文章主要介紹了IDEA的Terminal無法執(zhí)行g(shù)it命令問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-09-09
詳解Java中的迭代迭代器Iterator與枚舉器Enumeration
Iterator與Enumeration分別是實現(xiàn)迭代器和枚舉器類的接口,下面就帶大家來詳解Java中的迭代迭代器Iterator與枚舉器Enumeration,以及它們之間的區(qū)別.2016-05-05
解決java讀取EXCEL數(shù)據(jù)變成科學(xué)計數(shù)法的問題
這篇文章主要介紹了解決java讀取EXCEL數(shù)據(jù)變成科學(xué)計數(shù)法的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-04-04
JavaWeb頁面中防止點擊Backspace網(wǎng)頁后退情況
當(dāng)鍵盤敲下后退鍵(Backspace)后怎么防止網(wǎng)頁后退情況呢?今天小編通過本文給大家詳細介紹下,感興趣的朋友一起看看吧2016-11-11
基于java SSM springboot實現(xiàn)景區(qū)行李寄存管理系統(tǒng)
這篇文章主要介紹了基于java SSM springboot實現(xiàn)的景區(qū)行李寄存管理系統(tǒng),本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-08-08
徹底搞懂java并發(fā)ThreadPoolExecutor使用
這篇文章主要為大家介紹了徹底搞懂java并發(fā)ThreadPoolExecutor使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-02-02

