0
我嘗試使用xtend的活動註釋,我想創建一個活動註釋,它可以生成一個String[]
字段來記錄方法參數的名稱。如何使用xtend的活動註釋創建一個String []`字段?
@Target(ElementType::TYPE)
@Active(typeof(ParameterRecorderProcessor))
annotation ParameterRecorder {
}
class ParameterRecorderProcessor extends AbstractClassProcessor {
override doTransform(MutableClassDeclaration annotatedClass, extension TransformationContext context) {
var iii = 0;
// add the public methods to the interface
for (method : annotatedClass.declaredMethods) {
if (method.visibility == Visibility::PUBLIC) {
iii = iii + 1
annotatedClass.addField(method.simpleName + "_" + iii) [
type = typeof(String[]).newTypeReference // String[] doesn't work
var s = ""
for (p : method.parameters) {
if(s.length > 0) s = s + ","
s = s + "\"" + p.simpleName + "\""
}
val ss = s
initializer = [
'''[«ss»]'''
]
]
}
}
}
}
你可以看到我用typeof(String[]).newTypeReference
來定義新創建的字段的類型,但它不工作。生成的Java代碼看起來像:
private Object index_1;
它使用Object
和initializer
部分有是空的。
如何解決?