2017-02-06 50 views
1

有沒有辦法讓我可以設置批註的方法裏面id ...我們可以設置註釋ID在運行時

註釋類:

import java.lang.annotation.*; 

@Retention(RetentionPolicy.RUNTIME) 
@Target(ElementType.METHOD) 
public 
@interface MyAnnotation { 
    int id(); 
} 

//Set id at runtime 
public class A { 
    @MyAnnotation(id = ?) 
    public void method1() { 
     // I want to set the id here for my annotation... 

    } 

} 

回答

0

是的,但它是一個有點不直觀。你必須使用像JavaAssist一個工具來編輯它的字節碼。

這裏是一個article描述你以後。

+0

感謝名單它幫助..我用javasist但不知道如何使用addAnnotation().... :) –

+0

如果這是正確的答案,請註明它如此。謝謝! – Keith

+0

是..它的工作對我來說完全沒有問題。現在我在runtime..Thanx能夠註釋集 –

相關問題