導航:首頁 > 編程語言 > java獲取參數的註解

java獲取參數的註解

發布時間:2022-06-26 13:03:03

A. 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();

}

B. java註解是怎麼實現的

註解的使用一般是與java的反射一起使用,下面是一個例子
註解相當於一種標記,在程序中加了註解就等於為程序打上了某種標記,沒加,則等於沒有某種標記,以後,javac編譯器,開發工具和其他程序可以用反射來了解你的類及各種元素上有無何種標記,看你有什麼標記,就去干相應的事。標記可以加在包,類,欄位,方法,方法的參數以及局部變數上。
自定義註解及其應用
1)、定義一個最簡單的註解
public @interface MyAnnotation {
//......
}
2)、把註解加在某個類上:
@MyAnnotation
public class AnnotationTest{
//......
}
以下為模擬案例
自定義註解@MyAnnotation
1 package com.ljq.test;
2
3 import java.lang.annotation.ElementType;
4 import java.lang.annotation.Retention;
5 import java.lang.annotation.RetentionPolicy;
6 import java.lang.annotation.Target;
7
8 /**
9 * 定義一個註解
10 *
11 *
12 * @author jiqinlin
13 *
14 */
15 //Java中提供了四種元註解,專門負責註解其他的註解,分別如下
16
17 //@Retention元註解,表示需要在什麼級別保存該注釋信息(生命周期)。可選的RetentionPoicy參數包括:
18 //RetentionPolicy.SOURCE: 停留在java源文件,編譯器被丟掉
19 //RetentionPolicy.CLASS:停留在class文件中,但會被VM丟棄(默認)
20 //RetentionPolicy.RUNTIME:內存中的位元組碼,VM將在運行時也保留註解,因此可以通過反射機制讀取註解的信息
21
22 //@Target元註解,默認值為任何元素,表示該註解用於什麼地方。可用的ElementType參數包括
23 //ElementType.CONSTRUCTOR: 構造器聲明
24 //ElementType.FIELD: 成員變數、對象、屬性(包括enum實例)
25 //ElementType.LOCAL_VARIABLE: 局部變數聲明
26 //ElementType.METHOD: 方法聲明
27 //ElementType.PACKAGE: 包聲明
28 //ElementType.PARAMETER: 參數聲明
29 //ElementType.TYPE: 類、介面(包括註解類型)或enum聲明
30
31 //@Documented將註解包含在JavaDoc中
32
33 //@Inheried允許子類繼承父類中的註解
34
35
36 @Retention(RetentionPolicy.RUNTIME)
37 @Target({ElementType.METHOD, ElementType.TYPE})
38 public @interface MyAnnotation {
39 //為註解添加屬性
40 String color();
41 String value() default "我是林計欽"; //為屬性提供默認值
42 int[] array() default {1, 2, 3};
43 Gender gender() default Gender.MAN; //添加一個枚舉
44 MetaAnnotation metaAnnotation() default @MetaAnnotation(birthday="我的出身日期為1988-2-18");
45 //添加枚舉屬性
46
47 }
註解測試類AnnotationTest
1 package com.ljq.test;
2
3 /**
4 * 註解測試類
5 *
6 *
7 * @author jiqinlin
8 *
9 */
10 //調用註解並賦值
11 @MyAnnotation(metaAnnotation=@MetaAnnotation(birthday = "我的出身日期為1988-2-18"),color="red", array={23, 26})
12 public class AnnotationTest {
13
14 public static void main(String[] args) {
15 //檢查類AnnotationTest是否含有@MyAnnotation註解
16 if(AnnotationTest.class.isAnnotationPresent(MyAnnotation.class)){
17 //若存在就獲取註解
18 MyAnnotation annotation=(MyAnnotation)AnnotationTest.class.getAnnotation(MyAnnotation.class);
19 System.out.println(annotation);
20 //獲取註解屬性
21 System.out.println(annotation.color());
22 System.out.println(annotation.value());
23 //數組
24 int[] arrs=annotation.array();
25 for(int arr:arrs){
26 System.out.println(arr);
27 }
28 //枚舉
29 Gender gender=annotation.gender();
30 System.out.println("性別為:"+gender);
31 //獲取註解屬性
32 MetaAnnotation meta=annotation.metaAnnotation();
33 System.out.println(meta.birthday());
34 }
35 }
36 }
枚舉類Gender,模擬註解中添加枚舉屬性
1 package com.ljq.test;
2 /**
3 * 枚舉,模擬註解中添加枚舉屬性
4 *
5 * @author jiqinlin
6 *
7 */
8 public enum Gender {
9 MAN{
10 public String getName(){return "男";}
11 },
12 WOMEN{
13 public String getName(){return "女";}
14 }; //記得有「;」
15 public abstract String getName();
16 }
註解類MetaAnnotation,模擬註解中添加註解屬性
1 package com.ljq.test;
2
3 /**
4 * 定義一個註解,模擬註解中添加註解屬性
5 *
6 * @author jiqinlin
7 *
8 */
9 public @interface MetaAnnotation {
10 String birthday();
11 }

C. 請問JAVA 註解中是如何獲取參數註解值的

通過反射機制來完成的!

D. Java如何獲取方法參數中的名稱

一、從註解中獲取
使用註解方式,我們需要自定義一個註解,在註解中指定參數名,然後通過反射機制,獲取方法參數上的註解,從而獲取到相應的註解信息。這里自定義的註解是Param,通過value參數指定參數名,定義了一個工具類ParameterNameUtils來獲取指定方法的參數名列表,這里獲取測試類ParameterNameTest中定義的方法method1的參數名列表表,下面是具體的代碼。

E. Java 註解的讀取註解信息的方法

屬於重點,在系統中用到註解許可權時非常有用,可以精確控制許可權的粒度
注意:要想使用反射去讀取註解,必須將Retention的值選為Runtime Java代碼importjava.lang.annotation.Annotation;importjava.lang.reflect.Method;//讀取註解信息{publicstaticvoidmain(String[]args)throwsException{//測試AnnotationTest類,得到此類的類對象Classc=Class.forName(com.iwtxokhtd.annotation.AnnotationTest);//獲取該類所有聲明的方法Method[]methods=c.getDeclaredMethods();//聲明註解集合Annotation[]annotations;//遍歷所有的方法得到各方法上面的註解信息for(Methodmethod:methods){//獲取每個方法上面所聲明的所有註解信息annotations=method.getDeclaredAnnotations();//再遍歷所有的註解,列印其基本信息System.out.println(method.getName());for(Annotationan:annotations){System.out.println(方法名為:+method.getName()+其上面的註解為:+an.annotationType().getSimpleName());Method[]meths=an.annotationType().getDeclaredMethods();//遍歷每個註解的所有變數for(Methodmeth:meths){System.out.println(註解的變數名為:+meth.getName());}}}}}

F. java自定義註解怎麼獲取註解的方法參數

不需要指明具體名稱,Java註解是附加在代碼中的一些元信息,用於一些工具在編譯、運行時進行解析和使用,起到說明、配置的功能

閱讀全文

與java獲取參數的註解相關的資料

熱點內容
android文件分區 瀏覽:368
南京開通數控螺紋編程 瀏覽:290
伺服器與ups用什麼線 瀏覽:967
unix網路命令 瀏覽:48
程序員表白代碼大全可復制 瀏覽:365
手機如何共享web伺服器 瀏覽:956
php介面有什麼用 瀏覽:382
iis如何安裝php 瀏覽:791
k5嗜血魔鍵安卓怎麼調好用 瀏覽:834
建行app中如何添加銀行卡 瀏覽:281
簡便演算法100點 瀏覽:161
如何創新我的世界伺服器 瀏覽:882
戰地怎麼看伺服器地址 瀏覽:348
vue怎麼打包放上伺服器 瀏覽:165
為什麼安卓服夏日活動沒有兔子頭 瀏覽:894
pubg為什麼顯示伺服器連接失敗 瀏覽:650
阿里雲掃碼登錄伺服器 瀏覽:971
化學基礎pdf 瀏覽:896
51單片機晶碼管 瀏覽:281
怎麼查伺服器假死原因日誌在哪看 瀏覽:277