Java getDeclaredAnnotations() 方法



描述

Java Package getDeclaredAnnotations() 方法返回直接存在於此元素上的所有註釋。與該介面中的其他方法不同,此方法忽略繼承的註釋。(如果此元素上沒有直接存在註釋,則返回長度為零的陣列。)此方法的呼叫者可以自由修改返回的陣列;這不會影響返回給其他呼叫者的陣列。

宣告

以下是 java.lang.Package.getDeclaredAnnotations() 方法的宣告

public Annotation[] getDeclaredAnnotations()

引數

返回值

此方法返回直接存在於此元素上的所有註釋

異常

<

獲取所有註釋示例

以下示例顯示了 getDeclaredAnnotations() 方法的用法。在此程式中,我們已將註釋 Demo 宣告為一個介面,其中包含一個返回字串的方法和另一個返回整數值的方法。在 PackageDemo 類中,我們已將該註釋 Demo 應用於方法 example(),並帶有一些值。在 example() 方法中,我們已使用 getClass() 方法檢索了 PackageDemo 類的類。

現在使用 getMethod() 示例,我們檢索了 example 方法例項,然後使用 getDeclaredAnnotations() 方法,我們檢索了註釋陣列並列印了它們。在主方法中,呼叫此 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.getDeclaredAnnotations();

         // 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)

獲取空註釋陣列示例

以下示例顯示了 getDeclaredAnnotations() 方法的用法。在此程式中,我們已將註釋 Demo 宣告為一個介面,其中包含一個返回字串的方法和另一個返回整數值的方法。在 PackageDemo 類中,我們已將該註釋 Demo 應用於方法 example(),並帶有一些值。在 example() 方法中,我們已使用 getClass() 方法檢索了 PackageDemo 類的類。我們建立了另一個沒有應用任何註釋的方法 example1()。

現在使用 getMethod() 示例,我們檢索了 example1 方法例項,然後使用 getDeclaredAnnotations() 方法,我們檢索了註釋陣列並列印了它們。由於沒有應用任何註釋,因此陣列將為空。在主方法中,呼叫此 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.getDeclaredAnnotations();

         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.
java_lang_package.htm
廣告

© . All rights reserved.