Search in sources :

Example 1 with Constructor

use of com.sun.tools.xjc.model.Constructor in project jaxb-ri by eclipse-ee4j.

the class ObjectFactoryGeneratorImpl method populate.

protected final void populate(ClassOutlineImpl cc, JClass sigType) {
    if (!cc.target.isAbstract()) {
        JMethod m = objectFactory.method(JMod.PUBLIC, sigType, "create" + cc.target.getSqueezedName());
        m.body()._return(JExpr._new(cc.implRef));
        // add some jdoc to avoid javadoc warnings in jdk1.4
        m.javadoc().append("Create an instance of ").append(cc.ref);
    }
    // add static factory methods for all the other constructors.
    Collection<? extends Constructor> consl = cc.target.getConstructors();
    if (consl.size() != 0) {
        // if we are going to add constructors with parameters,
        // first we need to have a default constructor.
        cc.implClass.constructor(JMod.PUBLIC);
    }
    {
        // collision check
        String name = cc.target.getSqueezedName();
        ClassOutlineImpl existing = valueFactoryNames.put(name, cc);
        if (existing != null) {
            outline.getErrorReceiver().error(existing.target.getLocator(), Messages.OBJECT_FACTORY_CONFLICT.format(name));
            outline.getErrorReceiver().error(cc.target.getLocator(), Messages.OBJECT_FACTORY_CONFLICT_RELATED.format());
            return;
        }
    }
    for (Constructor cons : consl) {
        // method on ObjectFactory
        // [RESULT]
        // Foo createFoo( T1 a, T2 b, T3 c, ... ) throws JAXBException {
        // return new FooImpl(a,b,c,...);
        // }
        JMethod m = objectFactory.method(JMod.PUBLIC, cc.ref, "create" + cc.target.getSqueezedName());
        JInvocation inv = JExpr._new(cc.implRef);
        m.body()._return(inv);
        // let's not throw this exception.
        // m._throws(codeModel.ref(JAXBException.class));
        // add some jdoc to avoid javadoc warnings in jdk1.4
        m.javadoc().append("Create an instance of ").append(cc.ref).addThrows(JAXBException.class).append("if an error occurs");
        // constructor
        // [RESULT]
        // FooImpl( T1 a, T2 b, T3 c, ... ) {
        // }
        JMethod c = cc.implClass.constructor(JMod.PUBLIC);
        for (String fieldName : cons.fields) {
            CPropertyInfo field = cc.target.getProperty(fieldName);
            if (field == null) {
                outline.getErrorReceiver().error(cc.target.getLocator(), Messages.ILLEGAL_CONSTRUCTOR_PARAM.format(fieldName));
                continue;
            }
            fieldName = camelize(fieldName);
            FieldOutline fo = outline.getField(field);
            FieldAccessor accessor = fo.create(JExpr._this());
            // declare a parameter on this factory method and set
            // it to the field
            inv.arg(m.param(fo.getRawType(), fieldName));
            JVar $var = c.param(fo.getRawType(), fieldName);
            accessor.fromRawValue(c.body(), '_' + fieldName, $var);
        }
    }
}
Also used : Constructor(com.sun.tools.xjc.model.Constructor) JAXBException(jakarta.xml.bind.JAXBException) JInvocation(com.sun.codemodel.JInvocation) JMethod(com.sun.codemodel.JMethod) FieldAccessor(com.sun.tools.xjc.outline.FieldAccessor) CPropertyInfo(com.sun.tools.xjc.model.CPropertyInfo) FieldOutline(com.sun.tools.xjc.outline.FieldOutline) JVar(com.sun.codemodel.JVar)

Aggregations

JInvocation (com.sun.codemodel.JInvocation)1 JMethod (com.sun.codemodel.JMethod)1 JVar (com.sun.codemodel.JVar)1 CPropertyInfo (com.sun.tools.xjc.model.CPropertyInfo)1 Constructor (com.sun.tools.xjc.model.Constructor)1 FieldAccessor (com.sun.tools.xjc.outline.FieldAccessor)1 FieldOutline (com.sun.tools.xjc.outline.FieldOutline)1 JAXBException (jakarta.xml.bind.JAXBException)1