Search in sources :

Example 1 with FieldAccessor

use of com.sun.tools.xjc.outline.FieldAccessor 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)

Example 2 with FieldAccessor

use of com.sun.tools.xjc.outline.FieldAccessor in project jaxb-ri by eclipse-ee4j.

the class IsSetField method generate.

private void generate(ClassOutlineImpl outline, CPropertyInfo prop) {
    // add isSetXXX and unsetXXX.
    MethodWriter writer = outline.createMethodWriter();
    JCodeModel codeModel = outline.parent().getCodeModel();
    FieldAccessor acc = core.create(JExpr._this());
    if (generateIsSetMethod) {
        // [RESULT] boolean isSetXXX()
        JExpression hasSetValue = acc.hasSetValue();
        if (hasSetValue == null) {
            // issue an error
            throw new UnsupportedOperationException();
        }
        writer.declareMethod(codeModel.BOOLEAN, "isSet" + this.prop.getName(true)).body()._return(hasSetValue);
    }
    if (generateUnSetMethod) {
        // [RESULT] void unsetXXX()
        acc.unsetValues(writer.declareMethod(codeModel.VOID, "unset" + this.prop.getName(true)).body());
    }
}
Also used : MethodWriter(com.sun.tools.xjc.generator.bean.MethodWriter) JExpression(com.sun.codemodel.JExpression) JCodeModel(com.sun.codemodel.JCodeModel) FieldAccessor(com.sun.tools.xjc.outline.FieldAccessor)

Aggregations

FieldAccessor (com.sun.tools.xjc.outline.FieldAccessor)2 JCodeModel (com.sun.codemodel.JCodeModel)1 JExpression (com.sun.codemodel.JExpression)1 JInvocation (com.sun.codemodel.JInvocation)1 JMethod (com.sun.codemodel.JMethod)1 JVar (com.sun.codemodel.JVar)1 MethodWriter (com.sun.tools.xjc.generator.bean.MethodWriter)1 CPropertyInfo (com.sun.tools.xjc.model.CPropertyInfo)1 Constructor (com.sun.tools.xjc.model.Constructor)1 FieldOutline (com.sun.tools.xjc.outline.FieldOutline)1 JAXBException (jakarta.xml.bind.JAXBException)1