2015-02-09 55 views
0

全部 我正在將我的項目從xtend 2.4.3遷移到2.7.3用xtend 2.7.3設置註釋值?

我遇到了一些問題。 這裏是代碼適用於2.4.3

val AttrType = findTypeGlobally(typeof(Attr)); 
val fld = Cls.addField(Pkt::getMemberName(m)) 
[ 
    val annot = addAnnotation(AttrType) 
    annot.set("value", GenHelper::getAnnot(m)) 
    visibility = Visibility::PUBLIC 
] 
setFieldType(m, fld, context) 

在2.7.3,addAnnotation返回AnnotationReference。

沒有辦法將值設置到AnnotationReference中。 如何解決它?

謝謝。

回答

1

使用addAnnotation方法,該方法使用lambda來初始化註釋引用。在該lambda中,您可以訪問AnnotationReferenceBuildContext來設置值。

val AttrType = findTypeGlobally(typeof(Attr)) 
val fld = Cls.addField(Pkt::getMemberName(m)) 
[ 
    val annot = addAnnotation(AttrType) [ 
     set("value", GenHelper::getAnnot(m)) 
    ] 
    visibility = Visibility::PUBLIC 
] 
... 

還要注意:

  1. typeOf已經過時了。您可以直接使用Attr來引用該類。
  2. ::現在可以用簡單的.代替靜態成員訪問。
  3. 如果類Attr位於活動註釋項目和客戶端項目的類路徑上,則不必使用`findTypeGlobally`。
  4. 靜態(擴展)導入可以幫助您編寫更簡潔的代碼,例如

所以,你的代碼將變得

import static extension <whereever>.Pkt.* 
import static extension <whereever>.GenHelper.* 
import static org.xtend.lib.macro.declaration.Visibility.* 
... 

Cls.addField(m.memberName) [ 
    Attr.addAnnotation [ 
     'value'.set(m.annot) 
    ] 
    visibility = PUBLIC 
    type = m.fieldType(context) // might need further refactoring 
] 

...