導航:首頁 > 源碼編譯 > java註解編譯器

java註解編譯器

發布時間:2022-01-17 08:35:48

『壹』 註解的java中的註解

java.lang.annotation.Retention可以在您定義Annotation型態時,指示編譯器如何對待您的自定義 Annotation,預設上編譯器會將Annotation資訊留在class檔案中,但不被虛擬機器讀取,而僅用於編譯器或工具程式運行時提供資訊。
在使用Retention型態時,需要提供java.lang.annotation.RetentionPolicy的列舉型態:
package java.lang.annotation;
public enum RetentionPolicy {
SOURCE, //編譯器處理完Annotation資訊後就沒事了
CLASS, //編譯器將Annotation儲存於class檔中,預設
RUNTIME //編譯器將Annotation儲存於class檔中,可由VM讀入
}
RetentionPolicy為SOURCE的例子是SuppressWarnings,這個資訊的作用僅在告知編譯器抑制警訊,所以不必將這個資訊儲存於class檔案。
RetentionPolicy為RUNTIME的時機,可像是您使用Java設計一個程式碼分析工具,您要VM讀出Annotation資訊,以在分析程式中使用,搭配Reflection機制,就可以達到這個目的。
在J2SE 5.0中新增了java.lang.reflect.AnnotatedElement這個界面,當中定義有四個方法:
public Annotation getAnnotation(Class annotationType);
public Annotation[] getAnnotations();
public Annotation[] getDeclaredAnnotations();
public boolean isAnnotationPresent(Class annotationType);
Class、Constructor、Field、Method、Package等類別,都實作了AnnotatedElement這個介面,所以您可以從這些類別的實例上,分別取得標示於其上的Annotation與其資訊,如果RetentionPolicy為RUNTIME的話。
舉個例子來說,假設您設計了以下的Debug Annotation:
* Debug.java
package onlyfun.caterpillar;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
public @interface Debug {
String value();
String name();
}
由於RetentionPolicy為RUNTIME,編譯器在處理Debug Annotation時,會將之編譯至class檔中,並可以VM讀出Annotation資訊,接著我們將Debug用於程式中:
* SomeObject.java
package onlyfun.caterpillar;
public class SomeObject {
@Debug(
value = unit,
name = debug1
)
public void doSomething() {
// ....
}
}
可以設計一個工具程式來讀取Annotation資訊:
* DebugTool.java
package onlyfun.caterpillar;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
public class DebugTool {
public static void main(String[] args)
throws NoSuchMethodException {
Class<SomeObject> c = SomeObject.class;
Method method = c.getMethod(doSomething);
if(method.isAnnotationPresent(Debug.class)) {
System.out.println(@Debug is found.);
Debug debug = method.getAnnotation(Debug.class);
System.out.println( value = + debug.value());
System.out.println( name = + ());
}
else {
System.out.println(@Debug is not found.);
}
Annotation[] annotations = method.getAnnotations();
for(Annotation annotation : annotations) {
System.out.println(
annotation.annotationType().getName());
}
}
}
程式的執行結果如下:
@Debug is found.
value = unit
name = debug1
onlyfun.caterpillar.Debug

『貳』 Java 什麼是註解及註解原理詳細介紹

1、註解是針對Java編譯器的說明。

可以給Java包、類型(類、介面、枚舉)、構造器、方法、域、參數和局部變數進行註解。Java編譯器可以根據指令來解釋註解和放棄註解,或者將註解放到編譯後的生成的class文件中,運行時可用。

2、註解和註解類型

註解類型是一種特殊的介面類型,註解是註解註解類型的一個實例。

註解類型也有名稱和成員,註解中包含的信息採用鍵值對形式,可以有0個或多個。

3、Java中定義的一些註解:

@Override 告訴編譯器這個方法要覆蓋一個超類方法,防止程序員覆蓋出錯。

@Deprecated 這個標識方法或類(介面等類型)過期,警告用戶不建議使用。

@SafeVarargs JDK7新增,避免可變參數在使用泛型化時候警告」執行時期無法具體確認參數類型「,當然,也可以用@SuppressWarnings來避免檢查,顯然後者的抑制的范圍更大。

@SuppressWarnings(value={"unchecked"}) 抑制編譯警告,應用於類型、構造器、方法、域、參數以及局部變數。 value是類型數組,有效取值為:

all, to suppress all warnings

boxing, to suppress warnings relative to boxing/unboxing operations

cast, to suppress warnings relative to cast operations

dep-ann, to suppress warnings relative to deprecated annotation

deprecation, to suppress warnings relative to deprecation

fallthrough, to suppress warnings relative to missing breaks in switch statements

finally, to suppress warnings relative to finally block that don't return

hiding, to suppress warnings relative to locals that hide variable

incomplete-switch, to suppress warnings relative to missing entries in a switch statement (enum case)

javadoc, to suppress warnings relative to javadoc warnings

nls, to suppress warnings relative to non-nls string literals

null, to suppress warnings relative to null analysis

rawtypes, to suppress warnings relative to usage of raw types

restriction, to suppress warnings relative to usage of discouraged or forbidden references

serial, to suppress warnings relative to missing serialVersionUID field for a serializable class

static-access, to suppress warnings relative to incorrect static access

static-method, to suppress warnings relative to methods that could be declared as static

super, to suppress warnings relative to overriding a method without super invocations

synthetic-access, to suppress warnings relative to unoptimized access from inner classes

unchecked, to suppress warnings relative to unchecked operations

unqualified-field-access, to suppress warnings relative to field access unqualified

unused, to suppress warnings relative to unused code and dead code

4、註解的定義

使用 @interface 關鍵字聲明一個註解

public @interface MyAnnotation1

註解中可以定義屬性

String name default 「defval」;

value是註解中的特殊屬性

註解中定義的屬性如果名稱為 value, 此屬性在使用時可以省寫屬性名

例如,聲明一個註解:

@Retention(RetentionPolicy.RUNTIME)

public @interface MyAnno1 {

String msg();

int value();

}

『叄』 如何實現自定義Java編譯時註解功能

自定義註解,可以應用到反射中,比如自己寫個小框架。
如實現實體類某些屬性不自動賦值,或者驗證某個對象屬性完整性等
本人自己用過的驗證屬性值完整性:

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface IgnoreProperty {
}
然後實體類中:
public class TarResearch implements Serializable{

@IgnoreProperty
private static final long serialVersionUID = 1L;

@IgnoreProperty
private Integer researchId;

@IgnoreProperty
private TarUser userId;

private String version;

private String grade;
....
}

然後action類中
// 驗證數據完整性

Class<TarResearch > userClass = TarResearch .class;

Field[] field = userClass.getDeclaredFields();

for (int i = 0; i < field.length; i++) {

if (field[i].getAnnotation(IgnoreProperty.class) != null) {

continue;

}

String fie = field[i].getName().substring(0, 1).toUpperCase()

+ field[i].getName().substring(1);

Method method = userClass.getMethod("get" + fie);

Object obj = method.invoke(u);

if (obj == null) {

sendResponseMsg(response, "數據錯誤");

return null;

}

}

『肆』 Java編譯時註解和運行時註解有什麼區別

重寫,重載,泛型,分別是在運行時還是編譯時執行的

1. 方法重載是在編譯時執行的,因為,在編譯的時候,如果調用了一個重載的方法,那麼編譯時必須確定他調用的方法是哪個。如:

當調用evaluate("hello")時候,我們在編譯時就可以確定他調用的method #1.

2.
方法的重寫是在運行時進行的。這個也常被稱為運行時多態的體現。編譯器是沒有辦法知道它調用的到底是那個方法,相反的,只有在jvm執行過程中,才知曉到底是父子類中的哪個方法被調用了當有如下一個介面的時候,我們是無法確定到底是調用父類還是子類的方法

3.
泛型(類型檢測),這個發生在編譯時。編譯器會在編譯時對泛型類型進行檢測,並吧他重寫成實際的對象類型(非泛型代碼),這樣就可以被JVM執行了。這個過程被稱為"類型擦除"。

類型擦除的關鍵在於從泛型類型中清除類型參數的相關信息,並且再必要的時候添加類型檢查和類型轉換的方法。

類型擦除可以簡單的理解為將泛型java代碼轉換為普通java代碼,只不過編譯器更直接點,將泛型java代碼直接轉換成普通java位元組碼。類型擦除的主要過程如下:

1). 將所有的泛型參數用其最左邊界(最頂級的父類型)類型替換。

2). 移除所有的類型參數。

在編譯後變成:

4. 註解。註解即有可能是運行時也有可能是編譯時。

如java中的@Override註解就是典型的編譯時註解,他會在編譯時會檢查一些簡單的如拼寫的錯誤(與父類方法不相同)等

同樣的@Test註解是junit框架的註解,他是一個運行時註解,他可以在運行時動態的配置相關信息如timeout等。

5. 異常。異常即有可能是運行時異常,也有可能是編譯時異常。

RuntimeException是一個用於指示編譯器不需要檢查的異常。RuntimeException
是在jvm運行過程中拋出異常的父類。對於運行時異常是不需要再方法中顯示的捕獲或者處理的。

已檢查的異常是被編譯器在編譯時候已經檢查過的異常,這些異常需要在try/catch塊中處理的異常。

6. AOP. Aspects能夠在編譯時,預編譯時以及運行時使用。

1).
編譯時:當你擁有源碼的時候,AOP編譯器(AspectJ編譯器)能夠編譯源碼並生成編織後的class。這些編織進入的額外功能是在編譯時放進去的。

2). 預編譯時:織入過程有時候也叫二進制織入,它是用來織入到哪些已經存在的class文件或者jar中的。

3). 運行時:當被織入的對象已經被載入如jvm中後,可以動態的織入到這些類中一些信息。

7. 繼承:繼承是編譯時執行的,它是靜態的。這個過程編譯後就已經確定

8. 代理(delegate):也稱動態代理,是在運行時執行。

閱讀全文

與java註解編譯器相關的資料

熱點內容
工作三年的大專程序員 瀏覽:728
java畢業設計文獻 瀏覽:143
籌碼集中度指標源碼 瀏覽:482
listsortjava 瀏覽:186
plc閃光電路編程實例 瀏覽:299
socket編程試題 瀏覽:206
華為的伺服器怎麼設置從光碟機啟動 瀏覽:871
程序員真的累嗎 瀏覽:328
學信網app為什麼刷臉不了 瀏覽:874
天蠍vs程序員 瀏覽:996
單片機下載口叫什麼 瀏覽:190
程序員的道 瀏覽:926
雲伺服器不實名違法嗎 瀏覽:558
怎樣查看文件夾圖片是否重復 瀏覽:995
文件怎麼導成pdf文件 瀏覽:808
打開sql表的命令 瀏覽:103
安卓手機如何面部支付 瀏覽:38
天元數學app為什麼登錄不上去 瀏覽:825
明日之後為什麼有些伺服器是四個字 瀏覽:104
安卓系統l1是什麼意思 瀏覽:26