在使用动态创建的@XmlJavaTypeAdapter批注进行封送时,不会调用XML适配器

时间:2016-01-14 18:27:26

标签: java jaxb marshalling xmladapter

我有一个简单的XMLAdapter:

public class CDataXMLAdapter extends XmlAdapter<String, String> {

@Override
public String marshal(String arg0) throws Exception {
    return "<![CDATA[" + arg0 + "]]>";
}
@Override
public String unmarshal(String arg0) throws Exception {
    return arg0;
}
}

以下代码背后的想法是:在我的RuntimeAnnotationReader实现中,我在运行时创建一个@XmlJavaTypeAdapter注释,并在我的@XmlDetectCDATA注释之外返回它

public class DetectAnnotationReader implements RuntimeAnnotationReader{
...
@Override
public Annotation[] getAllMethodAnnotations(Method method, Locatable srcPos) {
    Annotation[] allMethodAnnotations = annotationReader.getAllMethodAnnotations(method, srcPos);
    if(method.getAnnotation(XmlDetectCDATA.class) != null){
        Annotation an = new  XmlJavaTypeAdapter(){

            @Override
            public Class annotationType() {
                return XmlJavaTypeAdapter.class;
            }

            @Override
            public Class type() {
                return null;
            }

            @Override
            public Class value() {
                return CDataXMLAdapter.class;
            }
        };
        allMethodAnnotations = extendAnnotationArray(allMethodAnnotations);
        allMethodAnnotations[allMethodAnnotations.length-1] = an;
    }
    return allMethodAnnotations;
}
...
}

问题:编组时不调用CDataXMLAdapter适配器。有任何想法吗?感谢

0 个答案:

没有答案