這篇文章主要介紹了java反射的基本操作方法有哪些的相關(guān)知識(shí),內(nèi)容詳細(xì)易懂,操作簡(jiǎn)單快捷,具有一定借鑒價(jià)值,相信大家閱讀完這篇java反射的基本操作方法有哪些文章都會(huì)有所收獲,下面我們一起來看看吧。
成都創(chuàng)新互聯(lián)公司專注于康馬網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠為您提供康馬營銷型網(wǎng)站建設(shè),康馬網(wǎng)站制作、康馬網(wǎng)頁設(shè)計(jì)、康馬網(wǎng)站官網(wǎng)定制、微信小程序開發(fā)服務(wù),打造康馬網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供康馬網(wǎng)站排名全網(wǎng)營銷落地服務(wù)。
獲取類的名字
方法 | 描述 |
String getName() | 獲得包名+類名 |
String getSimpleName() | 獲得類的名字 |
Class cSuper=c.getSuperClass() | 獲取父類的Class對(duì)象 |
獲得類的屬性信息
方法 | 描述 |
Field getField(String fieldName) | 得到public屬性對(duì)象 |
Field[] getFields() | 得到所有public屬性對(duì)象 |
Field getDeclaredField(String fieldName) | 得到任意指定名稱的屬性對(duì)象 |
Field[] c.getDeclaredFields() | 得到所有的屬性對(duì)象 |
field.getModifiers() | 獲得屬性權(quán)限修飾符 |
field.getType() | 獲得屬性類型 |
field.getName() | 獲得屬性名稱 |
獲得類的方法
方法 | 描述 |
Method[] getDeclaredMethod() | 得到所有方法對(duì)象 |
Method[] c.getMethods() | 得到所有public方法對(duì)象 |
method.getModifiers() | 獲得方法訪問權(quán)限 |
method.getReturnType() | 獲得方法返回值類型 |
method.getName() | 獲得方法名稱 |
Class[] cs=method.getParameterTypes() | 獲取方法的參數(shù) |
c.getTypeName() | 獲得參數(shù)類型 |
Constructor[] cons=c.getConstructors() | 獲得構(gòu)造器 |
c.getConstructor(null) | 獲得無參構(gòu)造方法 |
c.getConstructor(int.class,String.class,String.class) | 獲得指定重載的構(gòu)造器 |
用反射操作對(duì)象
public class Test05 { public static void main(String[] args) throws Exception { Class c = Class.forName("demo.Person"); Constructor cons = c.getConstructor(null); Person person = (Person) cons.newInstance(); Field field = c.getDeclaredField("name"); field.setAccessible(true); //不做安全檢查,可以直接訪問 field.set(person, "王老五"); //對(duì)屬性直接賦值 System.out.println("取出name屬性值:" + field.get(person)); Method m = c.getDeclaredMethod("setName", String.class); m.invoke(person, "東施"); Method m2 = c.getDeclaredMethod("getName", null); System.out.println("通過getter獲得屬性值:" + m2.invoke(person)); } }
通過setAccessible()提高性能
setAccessible(true)可以禁用java的安全檢查。
Java中泛型僅是給編譯器javac使用的,確保數(shù)據(jù)的安全性并免去強(qiáng)制類型轉(zhuǎn)換。一旦編譯完成,所有與泛型相關(guān)的類型全部擦除(擦除到Object)。
使用泛型直接讀取泛型是不可行的,應(yīng)為反射是操作編譯以后的類的。
為操作泛型引入了ParameterizedType,GenericArrayType,TypeVariable,WildcardType(? extends Number或者? super Integer)。
public class TestGeneric { public void test01(Map<String, Person> map, List<Person> list, String s) { System.out.println("TestGeneric.test01()"); } public Map<Integer, Person> test02() { System.out.println("TestGeneric.test02()"); return null; } public String test03() { System.out.println("TestGeneric.test03()"); return null; } public static void main(String[] args) throws NoSuchMethodException { Class c = TestGeneric.class; //參數(shù)為泛型 Method test01 = c.getMethod("test01", Map.class, List.class, String.class); Type[] types = test01.getGenericParameterTypes(); System.out.println(types.length); for (Type type : types) { System.out.println("#" + type); if (type instanceof ParameterizedType) { Type[] genericType = ((ParameterizedType) type).getActualTypeArguments(); for (Type genType : genericType) { System.out.println("泛型類型:" + genType); } System.out.println("--------------"); } } System.out.println("------------------------"); //返回值為泛型 Method m2 = c.getMethod("test02", null); Type returnType = m2.getGenericReturnType(); if (returnType instanceof ParameterizedType) { Type[] types2 = ((ParameterizedType) returnType).getActualTypeArguments(); for (Type type : types2) { System.out.println("返回值的泛型類型:" + type); } } System.out.println("-----------------------"); Method m3=c.getMethod("test03",null); Type returnType3=m3.getGenericReturnType(); System.out.println(returnType3); System.out.println(returnType3 instanceof ParameterizedType); } }
原注解
@Target 注解應(yīng)用范圍
所修飾范圍 | 取值ElementType |
package包 | PACKAGE |
類,接口,枚舉,Annotation類型 | TYPE |
類型成員(方法,構(gòu)造方法,成員變量,枚舉值) | CONSTRUCTOR:用于描述構(gòu)造器;FIELD:用于描述域;METHOD:用于描述方法; |
方法參數(shù)和本地變量 | LOCAL_VARIABLE:用于描述局部變量;PARAMETER:用于描述參數(shù) |
@Retention 注解的生命周期。在什么級(jí)別保存注解信息。
取值RetentionPolicy | 作用 |
SOURCE | 在源文件中有效 |
CLASS | 在class文件中有效 |
RUNTIME | 在運(yùn)行時(shí)有效,可以被反射機(jī)制讀取 |
@Documented
@Inherited
自定義注解的語法
使用@interface定義自定義注解時(shí),自動(dòng)繼承了java.lang.annotation.Annotation接口。
@interface 用來聲明一個(gè)注解
其中每一個(gè)方法形式上聲明了一個(gè)參數(shù):
~ 方法的名稱就是參數(shù)的名稱
~ 返回值類型就是參數(shù)類型(返回值類型只能是基本數(shù)據(jù)類型,Class,String和enum)
~ 可以通過default來聲明參數(shù)的默認(rèn)值
String aName() default "";
~ 如果只有一個(gè)成員,一般參數(shù)名為value
注解元素必須要有值。自定義注解時(shí)通常使用空字符串或者0作為其默認(rèn)值。也會(huì)使用-1表示不存在。
反射讀取注解
定義ORM表注解
@Target(ElementType.TYPE) //注解的使用范圍 @Retention(RetentionPolicy.RUNTIME) //在運(yùn)行時(shí)起作用 public @interface OrmTable { String value(); }
定義ORM字段注解
@Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) public @interface OrmField { String col(); String type(); int length(); }
定義javabean
@OrmTable("tb_student") public class Student { @OrmField(col = "id", type = "int", length = 10) private int id; @OrmField(col = "stuname", type = "varchar", length = 20) private String name; @OrmField(col = "age", type = "int", length = 10) private int age; public Student() { } public Student(int id, String name, int age) { this.id = id; this.name = name; this.age = age; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
讀取javabean的注解,然后…
import orm.anno.OrmField; import orm.anno.OrmTable; import java.lang.annotation.Annotation; import java.lang.reflect.Field; public class Test { public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException { Class clazz = Class.forName("orm.entity.Student"); //獲取全部注解 Annotation[] annotations = clazz.getDeclaredAnnotations(); for (Annotation a : annotations) { System.out.println(a); } //獲取指定注解 OrmTable otb = (OrmTable) clazz.getDeclaredAnnotation(OrmTable.class); System.out.println("javabean的類注解:" + otb.value()); //獲取屬性的注解 Field field = clazz.getDeclaredField("name"); OrmField ofd = field.getDeclaredAnnotation(OrmField.class); System.out.println("[" + ofd.col() + "," + ofd.type() + "," + ofd.length() + "]"); //拼接SQL語句DDL,使用JDBC在數(shù)據(jù)庫中執(zhí)行,創(chuàng)建數(shù)據(jù)庫表...... } }
關(guān)于“java反射的基本操作方法有哪些”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對(duì)“java反射的基本操作方法有哪些”知識(shí)都有一定的了解,大家如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
名稱欄目:java反射的基本操作方法有哪些
本文URL:http://chinadenli.net/article28/jeidjp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)網(wǎng)站制作、外貿(mào)網(wǎng)站建設(shè)、外貿(mào)建站、企業(yè)建站、App開發(fā)、虛擬主機(jī)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)