Search in sources :

Example 1 with XmlTypeWriter

use of com.sun.tools.xjc.generator.annotation.spec.XmlTypeWriter in project jaxb-ri by eclipse-ee4j.

the class BeanGenerator method generateEnumBody.

private void generateEnumBody(EnumOutline eo) {
    JDefinedClass type = eo.clazz;
    CEnumLeafInfo e = eo.target;
    XmlTypeWriter xtw = type.annotate2(XmlTypeWriter.class);
    writeTypeName(e.getTypeName(), xtw, eo._package().getMostUsedNamespaceURI());
    JCodeModel cModel = model.codeModel;
    // since constant values are never null, no point in using the boxed types.
    JType baseExposedType = e.base.toType(this, EXPOSED).unboxify();
    JType baseImplType = e.base.toType(this, Aspect.IMPLEMENTATION).unboxify();
    XmlEnumWriter xew = type.annotate2(XmlEnumWriter.class);
    xew.value(baseExposedType);
    boolean needsValue = e.needsValueField();
    // for each member <m>,
    // [RESULT]
    // <EnumName>(<deserializer of m>(<value>));
    // record generated field names to detect collision
    Set<String> enumFieldNames = new HashSet<>();
    for (CEnumConstant mem : e.members) {
        String constName = mem.getName();
        if (!JJavaName.isJavaIdentifier(constName)) {
            // didn't produce a name.
            getErrorReceiver().error(e.getLocator(), Messages.ERR_UNUSABLE_NAME.format(mem.getLexicalValue(), constName));
        }
        if (!enumFieldNames.add(constName)) {
            getErrorReceiver().error(e.getLocator(), Messages.ERR_NAME_COLLISION.format(constName));
        }
        // [RESULT]
        // <Const>(...)
        // ASSUMPTION: datatype is outline-independent
        JEnumConstant constRef = type.enumConstant(constName);
        if (needsValue) {
            constRef.arg(e.base.createConstant(this, new XmlString(mem.getLexicalValue())));
        }
        if (!mem.getLexicalValue().equals(constName)) {
            constRef.annotate2(XmlEnumValueWriter.class).value(mem.getLexicalValue());
        }
        // set javadoc
        if (mem.javadoc != null) {
            constRef.javadoc().append(mem.javadoc);
        }
        eo.constants.add(new EnumConstantOutline(mem, constRef) {
        });
    }
    if (needsValue) {
        // [RESULT]
        // final <valueType> value;
        JFieldVar $value = type.field(JMod.PRIVATE | JMod.FINAL, baseExposedType, "value");
        // [RESULT]
        // public <valuetype> value() { return value; }
        type.method(JMod.PUBLIC, baseExposedType, "value").body()._return($value);
        // [RESULT]
        // <constructor>(<valueType> v) {
        // this.value=v;
        // }
        {
            JMethod m = type.constructor(0);
            m.body().assign($value, m.param(baseImplType, "v"));
        }
        // [RESULT]
        // public static <Const> fromValue(<valueType> v) {
        // for( <Const> c : <Const>.values() ) {
        // if(c.value == v)   // or equals
        // return c;
        // }
        // throw new IllegalArgumentException(...);
        // }
        {
            JMethod m = type.method(JMod.PUBLIC | JMod.STATIC, type, "fromValue");
            JVar $v = m.param(baseExposedType, "v");
            JForEach fe = m.body().forEach(type, "c", type.staticInvoke("values"));
            JExpression eq;
            if (baseExposedType.isPrimitive()) {
                eq = fe.var().ref($value).eq($v);
            } else {
                eq = fe.var().ref($value).invoke("equals").arg($v);
            }
            fe.body()._if(eq)._then()._return(fe.var());
            JInvocation ex = JExpr._new(cModel.ref(IllegalArgumentException.class));
            JExpression strForm;
            if (baseExposedType.isPrimitive()) {
                strForm = cModel.ref(String.class).staticInvoke("valueOf").arg($v);
            } else if (baseExposedType == cModel.ref(String.class)) {
                strForm = $v;
            } else {
                strForm = $v.invoke("toString");
            }
            m.body()._throw(ex.arg(strForm));
        }
    } else {
        // [RESULT]
        // public String value() { return name(); }
        type.method(JMod.PUBLIC, String.class, "value").body()._return(JExpr.invoke("name"));
        // [RESULT]
        // public <Const> fromValue(String v) { return valueOf(v); }
        JMethod m = type.method(JMod.PUBLIC | JMod.STATIC, type, "fromValue");
        m.body()._return(JExpr.invoke("valueOf").arg(m.param(String.class, "v")));
    }
}
Also used : JDefinedClass(com.sun.codemodel.JDefinedClass) XmlTypeWriter(com.sun.tools.xjc.generator.annotation.spec.XmlTypeWriter) EnumConstantOutline(com.sun.tools.xjc.outline.EnumConstantOutline) XmlEnumValueWriter(com.sun.tools.xjc.generator.annotation.spec.XmlEnumValueWriter) XmlString(com.sun.xml.xsom.XmlString) CEnumLeafInfo(com.sun.tools.xjc.model.CEnumLeafInfo) JInvocation(com.sun.codemodel.JInvocation) XmlString(com.sun.xml.xsom.XmlString) JExpression(com.sun.codemodel.JExpression) JCodeModel(com.sun.codemodel.JCodeModel) CEnumConstant(com.sun.tools.xjc.model.CEnumConstant) XmlEnumWriter(com.sun.tools.xjc.generator.annotation.spec.XmlEnumWriter) JEnumConstant(com.sun.codemodel.JEnumConstant) JFieldVar(com.sun.codemodel.JFieldVar) JMethod(com.sun.codemodel.JMethod) JForEach(com.sun.codemodel.JForEach) JType(com.sun.codemodel.JType) HashSet(java.util.HashSet) JVar(com.sun.codemodel.JVar)

Example 2 with XmlTypeWriter

use of com.sun.tools.xjc.generator.annotation.spec.XmlTypeWriter in project jaxb-ri by eclipse-ee4j.

the class BeanGenerator method generateClassBody.

/**
 * Generates the body of a class.
 */
private void generateClassBody(ClassOutlineImpl cc) {
    CClassInfo target = cc.target;
    // used to simplify the generated annotations
    String mostUsedNamespaceURI = cc._package().getMostUsedNamespaceURI();
    // [RESULT]
    // @XmlType(name="foo", targetNamespace="bar://baz")
    XmlTypeWriter xtw = cc.implClass.annotate2(XmlTypeWriter.class);
    writeTypeName(cc.target.getTypeName(), xtw, mostUsedNamespaceURI);
    // @XmlSeeAlso
    Iterator<CClassInfo> subclasses = cc.target.listSubclasses();
    if (subclasses.hasNext()) {
        XmlSeeAlsoWriter saw = cc.implClass.annotate2(XmlSeeAlsoWriter.class);
        while (subclasses.hasNext()) {
            CClassInfo s = subclasses.next();
            saw.value(getClazz(s).implRef);
        }
    }
    if (target.isElement()) {
        String namespaceURI = target.getElementName().getNamespaceURI();
        String localPart = target.getElementName().getLocalPart();
        // [RESULT]
        // @XmlRootElement(name="foo", targetNamespace="bar://baz")
        XmlRootElementWriter xrew = cc.implClass.annotate2(XmlRootElementWriter.class);
        xrew.name(localPart);
        if (// only generate if necessary
        !namespaceURI.equals(mostUsedNamespaceURI)) {
            xrew.namespace(namespaceURI);
        }
    }
    if (target.isOrdered()) {
        for (CPropertyInfo p : target.getProperties()) {
            if (!(p instanceof CAttributePropertyInfo)) {
                if (!((p instanceof CReferencePropertyInfo) && ((CReferencePropertyInfo) p).isDummy())) {
                    xtw.propOrder(p.getName(false));
                }
            }
        }
    } else {
        // produce empty array
        xtw.getAnnotationUse().paramArray("propOrder");
    }
    for (CPropertyInfo prop : target.getProperties()) {
        generateFieldDecl(cc, prop);
    }
    if (target.declaresAttributeWildcard()) {
        generateAttributeWildcard(cc);
    }
    // generate some class level javadoc
    cc.ref.javadoc().append(target.javadoc);
    cc._package().objectFactoryGenerator().populate(cc);
}
Also used : XmlRootElementWriter(com.sun.tools.xjc.generator.annotation.spec.XmlRootElementWriter) CClassInfo(com.sun.tools.xjc.model.CClassInfo) XmlTypeWriter(com.sun.tools.xjc.generator.annotation.spec.XmlTypeWriter) XmlString(com.sun.xml.xsom.XmlString) XmlSeeAlsoWriter(com.sun.tools.xjc.generator.annotation.spec.XmlSeeAlsoWriter) CAttributePropertyInfo(com.sun.tools.xjc.model.CAttributePropertyInfo) CReferencePropertyInfo(com.sun.tools.xjc.model.CReferencePropertyInfo) CPropertyInfo(com.sun.tools.xjc.model.CPropertyInfo)

Aggregations

XmlTypeWriter (com.sun.tools.xjc.generator.annotation.spec.XmlTypeWriter)2 XmlString (com.sun.xml.xsom.XmlString)2 JCodeModel (com.sun.codemodel.JCodeModel)1 JDefinedClass (com.sun.codemodel.JDefinedClass)1 JEnumConstant (com.sun.codemodel.JEnumConstant)1 JExpression (com.sun.codemodel.JExpression)1 JFieldVar (com.sun.codemodel.JFieldVar)1 JForEach (com.sun.codemodel.JForEach)1 JInvocation (com.sun.codemodel.JInvocation)1 JMethod (com.sun.codemodel.JMethod)1 JType (com.sun.codemodel.JType)1 JVar (com.sun.codemodel.JVar)1 XmlEnumValueWriter (com.sun.tools.xjc.generator.annotation.spec.XmlEnumValueWriter)1 XmlEnumWriter (com.sun.tools.xjc.generator.annotation.spec.XmlEnumWriter)1 XmlRootElementWriter (com.sun.tools.xjc.generator.annotation.spec.XmlRootElementWriter)1 XmlSeeAlsoWriter (com.sun.tools.xjc.generator.annotation.spec.XmlSeeAlsoWriter)1 CAttributePropertyInfo (com.sun.tools.xjc.model.CAttributePropertyInfo)1 CClassInfo (com.sun.tools.xjc.model.CClassInfo)1 CEnumConstant (com.sun.tools.xjc.model.CEnumConstant)1 CEnumLeafInfo (com.sun.tools.xjc.model.CEnumLeafInfo)1