简体   繁体   English

作为javassist注释成员的注释数组

[英]Array of annotations as a javassist annotation member

I'm trying to create following annotation dynamically using javassist. 我正在尝试使用javassist动态创建以下注释。 I couldn't find a way to add array of annotations ( {@JoinColumn, @JoinColumn} ) as a javassist annotation member. 我找不到添加注释数组( {@JoinColumn, @JoinColumn} )作为javassist注释成员的方法。 Any suggessions? 任何建议?

@ManyToOne
@JoinColumns({
        @JoinColumn,
        @JoinColumn
})
private Parent parent;

Here are some references to build simple annotations. 以下是构建简单注释的一些参考。

AnnotationAttribute AnnotationAttribute
Dynamically Adding annotations 动态添加注释
Annotations at runtime 运行时的注释

Here I'm answering my own question. 我在这里回答我自己的问题。

/*
 * parentAnnotation, @JoinColumns object
 * memberName, "value"
 * memberValue, {@JoinColumn,...} object
 */ 
public void addMemberToAnnotation(Annotation parentAnnotation String memberName, Object memberValue){
    if(memberValue instanceof Annotation[]){
        ArrayList<AnnotationMemberValue> members = new ArrayList<AnnotationMemberValue>();
        AnnotationMemberValue annotationValue;
        for (Annotation a:(Annotation[])memberValue) {
            annotationValue =  new AnnotationMemberValue(cb.getCpool());
            annotationValue.setValue(a);
            members.add(annotationValue);
        }
        ArrayMemberValue arrayValue = new ArrayMemberValue(cb.getCpool());
        arrayValue.setValue(members.toArray(new MemberValue[0]));
        parentAnnotation.addMemberValue(memberName, arrayValue);
    } else if ( ... ){
        // Other cases
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM