Search in sources :

Example 1 with FieldOutline

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

the class BeanGenerator method generateFieldDecl.

/**
 * Determines the FieldRenderer used for the given FieldUse,
 * then generates the field declaration and accessor methods.
 *
 * The <code>fields</code> map will be updated with the newly
 * created FieldRenderer.
 */
private FieldOutline generateFieldDecl(ClassOutlineImpl cc, CPropertyInfo prop) {
    FieldRenderer fr = prop.realization;
    if (// none is specified. use the default factory
    fr == null) {
        fr = model.options.getFieldRendererFactory().getDefault();
    }
    FieldOutline field = fr.generate(cc, prop);
    fields.put(prop, field);
    return field;
}
Also used : FieldRenderer(com.sun.tools.xjc.generator.bean.field.FieldRenderer) FieldOutline(com.sun.tools.xjc.outline.FieldOutline)

Example 2 with FieldOutline

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

the class SignatureWriter method dump.

private void dump(ClassOutline ci) throws IOException {
    JDefinedClass cls = ci.implClass;
    StringBuilder buf = new StringBuilder();
    buf.append("interface ");
    buf.append(cls.name());
    boolean first = true;
    Iterator itr = cls._implements();
    while (itr.hasNext()) {
        if (first) {
            buf.append(" extends ");
            first = false;
        } else {
            buf.append(", ");
        }
        buf.append(printName((JClass) itr.next()));
    }
    buf.append(" {");
    println(buf.toString());
    indent++;
    // dump the field
    for (FieldOutline fo : ci.getDeclaredFields()) {
        String type = printName(fo.getRawType());
        println(type + ' ' + fo.getPropertyInfo().getName(true) + ';');
    }
    dumpChildren(cls);
    indent--;
    println("}");
}
Also used : JDefinedClass(com.sun.codemodel.JDefinedClass) JClass(com.sun.codemodel.JClass) Iterator(java.util.Iterator) FieldOutline(com.sun.tools.xjc.outline.FieldOutline)

Example 3 with FieldOutline

use of com.sun.tools.xjc.outline.FieldOutline in project jaxb-enhanced-navigation by 4Soft-de.

the class EmptyListHandlerPlugin method createBeforeMarshall.

public void createBeforeMarshall(final JCodeModel jCodeModel, final ClassOutline outline) {
    // boolean beforeMarshal(Marshaller)
    final JDefinedClass targetClass = outline.implClass;
    // targetClass.method(JMod.PUBLIC, baseType, getterName)
    final JMethod method = targetClass.method(JMod.PUBLIC, jCodeModel.BOOLEAN, "beforeMarshal");
    final JVar marshaller = method.param(Marshaller.class, "marshaller");
    final JBlock body = method.body();
    if (outline.getSuperClass() != null) {
        body.invoke(JExpr._super(), "beforeMarshal").arg(marshaller);
    }
    for (final FieldOutline f : outline.getDeclaredFields()) {
        createEmptyCheck(f, body);
    }
    body._return(JExpr.TRUE);
}
Also used : JDefinedClass(com.sun.codemodel.JDefinedClass) JBlock(com.sun.codemodel.JBlock) JMethod(com.sun.codemodel.JMethod) JVar(com.sun.codemodel.JVar) FieldOutline(com.sun.tools.xjc.outline.FieldOutline)

Example 4 with FieldOutline

use of com.sun.tools.xjc.outline.FieldOutline in project jstuff by sebthom.

the class FieldInstantiatingPlugin method run.

@Override
public boolean run(final Outline outline, final Options options, final ErrorHandler errorHandler) throws SAXException {
    // collect all types defined in the XSD
    final List<JType> typeDefs = new ArrayList<>();
    for (final ClassOutline classDef : outline.getClasses()) {
        typeDefs.add(classDef.implClass);
    }
    // scan all XSD based classes for field references to other XSD based classes
    for (final ClassOutline classDef : outline.getClasses()) {
        for (final JFieldVar fieldDecl : classDef.implClass.fields().values()) {
            /*
             * @XmlElementRefs({
             *    @XmlElementRef(name = "bike", namespace = "my-config", type = JAXBElement.class, required = false),
             *    @XmlElementRef(name = "car", namespace = "my-config", type = JAXBElement.class, required = false)
             * })
             * private List<JAXBElement<?>> bikesAndCars;
             */
            final Field memberValueFields = Fields.find(JAnnotationUse.class, "memberValues");
            for (final JAnnotationUse a : fieldDecl.annotations()) {
                if (// 
                jakarta.xml.bind.annotation.XmlElementRefs.class.getName().equals(a.getAnnotationClass().binaryName()) || // 
                "javax.xml.bind.annotation.XmlElementRefs".equals(a.getAnnotationClass().binaryName())) {
                    for (final JAnnotationUse xmlElementRefAnno : ((JAnnotationArrayMember) a.getAnnotationMembers().get("value")).annotations()) {
                        final JAnnotationValue requiredAttribute = xmlElementRefAnno.getAnnotationMembers().get("required");
                        if (requiredAttribute != null) {
                            ((Map<?, ?>) Fields.read(xmlElementRefAnno, memberValueFields)).remove("required");
                        }
                    }
                }
            }
            if (!typeDefs.contains(fieldDecl.type())) {
                continue;
            }
            FieldOutline fieldDef = null;
            for (final FieldOutline f : classDef.getDeclaredFields()) {
                if (f.getPropertyInfo().getName(false).equals(fieldDecl.name())) {
                    fieldDef = f;
                }
            }
            if (fieldDef == null)
                throw new IllegalStateException("FieldOutline not found for " + fieldDecl.name());
            boolean doInstantiate = false;
            for (final CPluginCustomization pc : findCustomizations(fieldDef.getPropertyInfo().getCustomizations(), CUSTOMIZATION_ENABLED_TAG)) {
                pc.markAsAcknowledged();
                doInstantiate = true;
            }
            // initialize field
            if (doInstantiate) {
                LOG.info("%s#%s = new %s()", classDef.implClass.name(), fieldDecl.name(), fieldDecl.type().name());
                fieldDecl.init(JExpr._new(fieldDecl.type()));
            } else {
                LOG.info("Not instantiating %s#%s", classDef.implClass.name(), fieldDecl.name());
            }
        }
    }
    return true;
}
Also used : CPluginCustomization(com.sun.tools.xjc.model.CPluginCustomization) ArrayList(java.util.ArrayList) JAnnotationArrayMember(com.sun.codemodel.JAnnotationArrayMember) ClassOutline(com.sun.tools.xjc.outline.ClassOutline) Field(java.lang.reflect.Field) JFieldVar(com.sun.codemodel.JFieldVar) JAnnotationUse(com.sun.codemodel.JAnnotationUse) JAnnotationValue(com.sun.codemodel.JAnnotationValue) Map(java.util.Map) JType(com.sun.codemodel.JType) FieldOutline(com.sun.tools.xjc.outline.FieldOutline)

Example 5 with FieldOutline

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

FieldOutline (com.sun.tools.xjc.outline.FieldOutline)7 JDefinedClass (com.sun.codemodel.JDefinedClass)3 JAnnotationUse (com.sun.codemodel.JAnnotationUse)2 JMethod (com.sun.codemodel.JMethod)2 JVar (com.sun.codemodel.JVar)2 JAnnotationArrayMember (com.sun.codemodel.JAnnotationArrayMember)1 JAnnotationValue (com.sun.codemodel.JAnnotationValue)1 JBlock (com.sun.codemodel.JBlock)1 JClass (com.sun.codemodel.JClass)1 JFieldVar (com.sun.codemodel.JFieldVar)1 JInvocation (com.sun.codemodel.JInvocation)1 JType (com.sun.codemodel.JType)1 FieldRenderer (com.sun.tools.xjc.generator.bean.field.FieldRenderer)1 CCustomizations (com.sun.tools.xjc.model.CCustomizations)1 CPluginCustomization (com.sun.tools.xjc.model.CPluginCustomization)1 CPropertyInfo (com.sun.tools.xjc.model.CPropertyInfo)1 Constructor (com.sun.tools.xjc.model.Constructor)1 ClassOutline (com.sun.tools.xjc.outline.ClassOutline)1 FieldAccessor (com.sun.tools.xjc.outline.FieldAccessor)1 JAXBException (jakarta.xml.bind.JAXBException)1