生活随笔
收集整理的這篇文章主要介紹了
注解反射
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
jdk1.8
下載地址
https://www.oracle.com/java/technologies/javase/javase-jdk8-downloads.html
下載ecipase
https://www.eclipse.org/downloads/
注解和反射
- Annotation是從JDK5.0開始引用的新技術
- Annotation的作用:
- 不是程序本身,可以對程序作出解釋
- 可以被其他程序(比如編譯器)讀取
- Annotation的格式:
- 注解是以"@注釋名"在代碼中存在的,還可以添加一些參數值。
- Annotation在哪里使用?
- 可以附加在package,class,method,fild等上面,相當于給他們添加了額外的輔助信息,我們可以通過反射機制編程實現對這些元素的訪問
package cn
.org
.mubai
;import java
.lang
.reflect
.Array
;
import java
.util
.ArrayList
;
import java
.util
.List
;public class Test1 {@Deprecatedpublic static void Test() {System
.out
.println("notRecommended");}public static void main(String
[] args
) {Test();}@SuppressWarnings("all") public static void test2(){List list
=new ArrayList();}
}
@SuppressWarnings:定義在java.lang.SuppressWarnings中,用來抑制編譯時的警告信息,
元注解
package cn
.org
.mubai
;import java
.lang
.annotation
.*
;public class Test2 {
}
@Target(value
= {ElementType
.METHOD
, ElementType
.TYPE
})
@Retention(value
= RetentionPolicy
.RUNTIME
)
@Documented
@Inherited
@
interface MyAnnotation {
}
反射機制
動態語言
- 是一類在運行時可以改變其結構的語言:例如新的函數、對象、 甚至代碼可以被引進,已有的函數可以被刪除或是其他結構上的變化。
- 主要動態語言:Object-C、C#、JavaScript、PHP、Python等
靜態語言
- 與動態語言相對應的,運行時結構不可變得語言就是靜態語言
- Java不是動態語言,但java可以稱之為“準動態語言”。即java有一定的動態性,我們可以利用反射機制獲得類似動態語言的特性。
Java Reflection
-
Refletion (反射) 是java被視為動態語言的關鍵,反射機制允許程序在執行期間借助于Reflection API取得任何類的內部信息,并能直接操作任意對象的內部屬性及方法,
Class c=Class.forName();
-
加載外類之后,在堆內存的方法區就產生了一個Class類型的對象(一個類只有一個Class對象),這個對象就包含了完整的類的結構信息。我們可以通過這個對象看到類的結構。這個對象就像一面鏡子,透過這個鏡子看到類的結構,所以,我們形象稱之為:反射
正常方式:引入需要的“包類”名稱–》通過new實例化–》取得實例化對象
反射方式:實例化對象–》getClass()方法–》得到完整的“包類”名稱
反射機制提供的功能
- 在運行時判斷任意一個對象所屬的類
- 在運行時構造任意一個類的對象
- 在運行時判斷任意一個類所屬的成員變量和方法
- 在運行時獲取泛型信息
- 在運行時調用任意一個對象的成員變量和方法
- 在運行時處理注解
- 生成動態代理
- 。。。。
package cn.org.mubai;public class Test4 {public static void main(String[] args) throws ClassNotFoundException {
// 通過反射獲取類的class對象Class c1 = Class.forName("cn.org.mubai.User");System.out.println(c1);Class c2 = Class.forName("cn.org.mubai.User");Class c3 = Class.forName("cn.org.mubai.User");Class c4 = Class.forName("cn.org.mubai.User");// 一個類的內存中只有一個class對象
// 一個類被加載之后,類的整個結構都會被封裝在class對象中System.out.println(c2.hashCode());System.out.println(c3.hashCode());System.out.println(c4.hashCode());}
}class User {private String name;private int age;private int id;public User() {}public User(String name, int age, int id) {this.name = name;this.age = age;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;}public int getId() {return id;}public void setId(int id) {this.id = id;}
}
Class類
package cn
.org
.mubai
;public class Test5 {public static void main(String
[] args
) throws ClassNotFoundException
{person person
=new student();System
.out
.println(person
.name
);Class
c1 = person
.getClass();System
.out
.println(c1
.hashCode());Class
c2 = Class
.forName("cn.org.mubai.student");System
.out
.println(c2
.hashCode());Class
c3=student
.class;System
.out
.println(c3
.hashCode());Class
c4=Integer
.TYPE
;System
.out
.println(c4
);System
.out
.println(c4
.hashCode());Class
c5=c1
.getSuperclass();System
.out
.println(c5
);}
}
class person{String name
;
}
class student extends person{public student() {this.name
="student";}
}class teacher extends person{public teacher() {this.name
="teacher";}
}
java內存分析
package cn
.org
.mubai
;import java
.lang
.reflect
.Constructor
;
import java
.lang
.reflect
.Field
;
import java
.lang
.reflect
.InvocationTargetException
;
import java
.lang
.reflect
.Method
;public class Test7 {public static void main(String
[] args
) throws ClassNotFoundException
, IllegalAccessException
, InstantiationException
, NoSuchMethodException
, InvocationTargetException
, NoSuchFieldException
{
Class
c1 = Class
.forName("cn.org.mubai.User");User user
= (User
) c1
.newInstance();System
.out
.println(user
);Constructor deconstructor
= c1
.getDeclaredConstructor(String
.class, int.class, int.class);User newInstance2
= (User
) deconstructor
.newInstance("白", 1, 1);System
.out
.println(newInstance2
.getName());User newInstance3
= (User
) c1
.newInstance();
Method setName
= c1
.getDeclaredMethod("setName", String
.class);Method setId
= c1
.getDeclaredMethod("setId", int.class);
setName
.invoke(newInstance3
, "mubai");setId
.invoke(newInstance3
, 0001);System
.out
.println(newInstance3
.getName());System
.out
.println(newInstance3
.getId());User newInstance4
= (User
) c1
.newInstance();Field age
= c1
.getDeclaredField("age");age
.setAccessible(true);age
.set(newInstance4
, 18);System
.out
.println(newInstance4
.getAge());}
}
package cn
.org
.mubai
;import java
.lang
.reflect
.InvocationTargetException
;
import java
.lang
.reflect
.Method
;public class Test8 {public static void main(String
[] args
) throws NoSuchMethodException
, IllegalAccessException
, InvocationTargetException
{Test1();Test2();Test3();}public static void Test1(){User user
= new User();long timeMillis1
= System
.currentTimeMillis();for (int i
= 0; i
< 1000000000; i
++) {user
.getName();}long timeMillis2
= System
.currentTimeMillis();System
.out
.println("普通方法調用"+(timeMillis2
-timeMillis1
)+"ms");}public static void Test2() throws NoSuchMethodException
, InvocationTargetException
, IllegalAccessException
{User user
= new User();Class
c1 = user
.getClass();Method getName
= c1
.getDeclaredMethod("getName", null
);long timeMillis1
= System
.currentTimeMillis();for (int i
= 0; i
< 1000000000; i
++) {getName
.invoke(user
,null
);user
.getName();}long timeMillis2
= System
.currentTimeMillis();System
.out
.println("反射方法調用"+(timeMillis2
-timeMillis1
)+"ms");}public static void Test3() throws NoSuchMethodException
, InvocationTargetException
, IllegalAccessException
{User user
= new User();Class
c1 = user
.getClass();Method getName
= c1
.getDeclaredMethod("getName", null
);long timeMillis1
= System
.currentTimeMillis();getName
.setAccessible(true);for (int i
= 0; i
< 1000000000; i
++) {getName
.invoke(user
,null
);user
.getName();}long timeMillis2
= System
.currentTimeMillis();System
.out
.println("關閉檢測"+(timeMillis2
-timeMillis1
)+"ms");}}
練習ORM
package cn
.org
.mubai
;import java
.lang
.annotation
.*
;
import java
.lang
.reflect
.Field
;
import java
.lang
.reflect
.Method
;public class Test9 {public static void main(String
[] args
) throws ClassNotFoundException
, NoSuchMethodException
, NoSuchFieldException
{Class
c1 = Class
.forName("cn.org.mubai.student1");
Annotation
[] annotations
= c1
.getAnnotations();for (Annotation annotation
:annotations
){System
.out
.println(annotation
);}
mubai mubai
= (mubai
)c1
.getAnnotation(mubai
.class);String value
= mubai
.value();System
.out
.println(value
);
Field id
= c1
.getDeclaredField("id");Fielmubai fielmubai
= id
.getAnnotation(Fielmubai
.class);System
.out
.println(fielmubai
.columnName());System
.out
.println(fielmubai
.length());System
.out
.println(fielmubai
.type());}
}
@mubai("mubai")
class student1{@Fielmubai(columnName
= "name",type
="int",length
=10)private String name
;@Fielmubai(columnName
= "age",type
="int",length
=10)private int age
;@Fielmubai(columnName
= "id",type
="int",length
=10)private int id
;public student1() {}public student1(String name
, int age
, int id
) {this.name
= name
;this.age
= age
;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
;}public int getId() {return id
;}public void setId(int id
) {this.id
= id
;}
}@Target(ElementType
.TYPE
)
@Retention(RetentionPolicy
.RUNTIME
)
@
interface mubai{String
value();
}@Target(ElementType
.FIELD
)
@Retention(RetentionPolicy
.RUNTIME
)
@
interface Fielmubai{String
columnName();String
type();int length();
}
@cn.org
.mubai
.mubai(value
=mubai
)
mubai
id
10
int
總結
以上是生活随笔為你收集整理的注解反射的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。