- Java.lang 包類
- Java.lang - 首頁
- Java.lang - Boolean
- Java.lang - Byte
- Java.lang - Character
- Java.lang - Character.Subset
- Java.lang - Character.UnicodeBlock
- Java.lang - Class
- Java.lang - ClassLoader
- Java.lang - Compiler
- Java.lang - Double
- Java.lang - Enum
- Java.lang - Float
- Java.lang - InheritableThreadLocal
- Java.lang - Integer
- Java.lang - Long
- Java.lang - Math
- Java.lang - Number
- Java.lang - Object
- Java.lang - Package
- Java.lang - Process
- Java.lang - ProcessBuilder
- Java.lang - Runtime
- Java.lang - RuntimePermission
- Java.lang - SecurityManager
- Java.lang - Short
- Java.lang - StackTraceElement
- Java.lang - StrictMath
- Java.lang - String
- Java.lang - StringBuffer
- Java.lang - StringBuilder
- Java.lang - System
- Java.lang - Thread
- Java.lang - ThreadGroup
- Java.lang - ThreadLocal
- Java.lang - Throwable
- Java.lang - Void
- Java.lang 包額外內容
- Java.lang - 介面
- Java.lang - 錯誤
- Java.lang - 異常
- Java.lang 包有用資源
- Java.lang - 有用資源
- Java.lang - 討論
Java 包 getAnnotations() 方法
描述
Java 包 getAnnotations() 方法返回此元素上存在的所有註釋。(如果此元素沒有註釋,則返回長度為零的陣列。)此方法的呼叫者可以自由修改返回的陣列;這不會影響返回給其他呼叫者的陣列。
宣告
以下是java.lang.Package.getAnnotations() 方法的宣告
public Annotation[] getAnnotations()
引數
無
返回值
此方法返回此元素上存在的所有註釋
異常
無
獲取所有註釋示例
以下示例演示了 getAnnotations() 方法的用法。在這個程式中,我們聲明瞭一個註釋 Demo 作為介面,它包含一個返回字串的方法和另一個返回整數值的方法。在 PackageDemo 類中,我們在一個名為 example() 的方法上應用了該 Demo 註釋,並賦予了一些值。在 example() 方法中,我們使用 getClass() 方法檢索了 PackageDemo 類的類。
現在使用 getMethod() 示例,我們檢索 example 方法例項,然後使用 getAnnotations() 方法檢索註釋陣列並列印它們。在主方法中,呼叫此 example() 方法並列印結果。
package com.tutorialspoint;
import java.lang.annotation.Annotation;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Method;
// declare a new annotation
@Retention(RetentionPolicy.RUNTIME)
@interface Demo {
String str();
int val();
}
public class PackageDemo {
// set values for the annotation
@Demo(str = "Demo Annotation", val = 100)
// a method to call in the main
public static void example() {
PackageDemo ob = new PackageDemo();
try {
Class c = ob.getClass();
// get the method example
Method m = c.getMethod("example");
// get the annotations
Annotation[] annotation = m.getAnnotations();
// print the annotation
for (int i = 0; i < annotation.length; i++) {
System.out.println(annotation[i]);
}
} catch (NoSuchMethodException exc) {
exc.printStackTrace();
}
}
public static void main(String args[]) {
example();
}
}
輸出
讓我們編譯並執行上述程式,這將產生以下結果:
@com.tutorialspoint.Demo(str=Demo Annotation, val=100)
獲取空註釋陣列示例
以下示例演示了 getAnnotations() 方法的用法。在這個程式中,我們聲明瞭一個註釋 Demo 作為介面,它包含一個返回字串的方法和另一個返回整數值的方法。在 PackageDemo 類中,我們在一個名為 example() 的方法上應用了該 Demo 註釋,並賦予了一些值。在 example() 方法中,我們使用 getClass() 方法檢索了 PackageDemo 類的類。我們建立了另一個名為 example1() 的方法,該方法沒有應用任何註釋。
現在使用 getMethod() 示例,我們檢索 example1 方法例項,然後使用 getAnnotations() 方法檢索註釋陣列並列印它們。由於沒有應用任何註釋,因此陣列將為空。在主方法中,呼叫此 example() 方法並列印結果。
package com.tutorialspoint;
import java.lang.annotation.Annotation;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Method;
// declare a new annotation
@Retention(RetentionPolicy.RUNTIME)
@interface Demo {
String str();
int val();
}
public class PackageDemo {
// set values for the annotation
@Demo(str = "Demo Annotation", val = 100)
// a method to call in the main
public static void example() {
PackageDemo ob = new PackageDemo();
try {
Class c = ob.getClass();
// get the method example
Method m = c.getMethod("example1");
// get the annotations
Annotation[] annotation = m.getAnnotations();
if(annotation.length != 0) {
// print the annotation
for (int i = 0; i < annotation.length; i++) {
System.out.println(annotation[i]);
}
}else {
System.out.println("No annotations present.");
}
} catch (NoSuchMethodException exc) {
exc.printStackTrace();
}
}
public static void main(String args[]) {
example();
}
public static void example1() {
// method with no annotations
}
}
輸出
讓我們編譯並執行上述程式,這將產生以下結果:
No annotations present.