Search in sources :

Example 1 with JStringLiteral

use of com.sun.codemodel.JStringLiteral in project jaxb-ri by eclipse-ee4j.

the class TypeUseImpl method createConstant.

@Override
public JExpression createConstant(Outline outline, XmlString lexical) {
    if (isCollection())
        return null;
    if (adapter == null)
        return coreType.createConstant(outline, lexical);
    // [RESULT] new Adapter().unmarshal(CONSTANT);
    JExpression cons = coreType.createConstant(outline, lexical);
    Class<? extends XmlAdapter> atype = adapter.getAdapterIfKnown();
    // try to run the adapter now rather than later.
    if (cons instanceof JStringLiteral && atype != null) {
        JStringLiteral scons = (JStringLiteral) cons;
        XmlAdapter a = ClassFactory.create(atype);
        try {
            Object value = a.unmarshal(scons.str);
            if (value instanceof String) {
                return JExpr.lit((String) value);
            }
        } catch (Exception e) {
        // assume that we can't eagerly bind this
        }
    }
    return JExpr._new(adapter.getAdapterClass(outline)).invoke("unmarshal").arg(cons);
}
Also used : JStringLiteral(com.sun.codemodel.JStringLiteral) JExpression(com.sun.codemodel.JExpression) XmlString(com.sun.xml.xsom.XmlString) XmlAdapter(jakarta.xml.bind.annotation.adapters.XmlAdapter)

Aggregations

JExpression (com.sun.codemodel.JExpression)1 JStringLiteral (com.sun.codemodel.JStringLiteral)1 XmlString (com.sun.xml.xsom.XmlString)1 XmlAdapter (jakarta.xml.bind.annotation.adapters.XmlAdapter)1