Package com.sun.codemodel

Examples of com.sun.codemodel.JConditional


                        if (wrapperElement != null && (property.isRequired() || property.isNillable())) {
                            outerBlock.add(builder.getWriteStartElement(wrapperElement));
                        }

                        // if collection is not null, process it; otherwise write xsi:nil
                        JConditional nullCond = outerBlock._if(outerVar.ne(JExpr._null()));
                        if (property.isNillable()) {
                            nullCond._else().add(builder.getXSW().invoke("writeXsiNil"));
                        }

                        // if wrapper tag is not required, start the tag inside of the null block
                        if (wrapperElement != null && !property.isRequired() && !property.isNillable()) {
                            nullCond._then().add(builder.getWriteStartElement(wrapperElement));
                        }

                        JType itemType;
                        if (!toClass(property.getComponentType()).isPrimitive()) {
                            itemType = context.getGenericType(property.getComponentType());
                        } else {
                            itemType = context.toJType((Class<?>) toClass(property.getComponentType()));
                        }

                        // if xml list add code to properly space items
                        if (property.isXmlList()) {
                            firstVar = nullCond._then().decl(context.toJType(boolean.class), builder.getWriteVariableManager().createId(property.getName() + "First"), JExpr.TRUE);
                        }

                        String itemName = builder.getWriteVariableManager().createId(property.getName() + "Item");
                        JForEach each = nullCond._then().forEach(itemType, itemName, outerVar);

                        // write wraper element closing tag
                        if (wrapperElement != null) {
                            if (property.isRequired() || property.isNillable()) {
                                outerBlock.add(builder.getXSW().invoke("writeEndElement"));
                            } else {
                                nullCond._then().add(builder.getXSW().invoke("writeEndElement"));
                            }
                        }

                        outerBlock = each.body();
                        outerVar = each.var();
                    }
                    Class propertyType = toClass(property.getComponentType());

                    // process value through adapter
                    outerVar = writeAdapterConversion(builder, outerBlock, property, outerVar);
                    if (property.getAdapterType() != null) {
                        propertyType = property.getComponentAdaptedType();
                    }

                    // determine types that may be substuited for this value
                    Map<Class, ElementMapping> expectedTypes = new TreeMap<Class, ElementMapping>(new ClassComparator());
                    for (ElementMapping mapping : property.getElementMappings()) {
                        if (mapping.getComponentType() != null) {
                            expectedTypes.put(toClass(mapping.getComponentType()), mapping);
                        } else {
                            expectedTypes.put(toClass(property.getType()), mapping);
                        }
                    }

                    if (expectedTypes.size() == 1 && !property.isMixed()) {
                        ElementMapping mapping = property.getElementMappings().iterator().next();

                        // null check for non-nillable elements
                        JBlock block = outerBlock;
                        JConditional nullCond = null;
                        if (!mapping.isNillable() && !propertyType.isPrimitive()) {
                            nullCond = outerBlock._if(outerVar.ne(JExpr._null()));
                            block = nullCond._then();
                        }

                        // add space (' ') separator for XmlList
                        if (property.isXmlList()) {
                            // if (fooFirst) {
                            //    writer.writeCharacters(" ");
                            // }
                            // fooFirst = false;
                            block._if(firstVar.not())._then().add(builder.getXSW().invoke("writeCharacters").arg(" "));
                            block.assign(firstVar, JExpr.FALSE);
                        }

                        // write element
                        writeElement(builder, block, mapping, outerVar, propertyType, mapping.isNillable(), property.isXmlList());

                        // property is required and does not support nill, then an error is reported if the value was null
                        if (property.isRequired() && !mapping.isNillable() && nullCond != null) {
                            nullCond._else().invoke(builder.getWriteContextVar(), "unexpectedNullValue").arg(builder.getWriteObject()).arg(property.getName());
                        }
                    } else {
                        JIfElseBlock conditional = new JIfElseBlock();
                        outerBlock.add(conditional);

                        if (property.isMixed()) {
                            // add instance of check
                            JExpression isInstance = outerVar._instanceof(context.toJClass(String.class));
                            JBlock block = conditional.addCondition(isInstance);

                            // declare item variable
                            JVar itemVar;
                            if (toClass(property.getComponentType()) == String.class) {
                                itemVar = outerVar;
                            } else {
                                String itemName = builder.getWriteVariableManager().createId("string");
                                itemVar = block.decl(context.toJClass(String.class), itemName, JExpr.cast(context.toJClass(String.class), outerVar));
                            }
                            writeSimpleTypeElement(builder, itemVar, String.class, block);
                        }

                        ElementMapping nilMapping = null;
                        for (Map.Entry<Class, ElementMapping> entry : expectedTypes.entrySet()) {
                            Class itemType = entry.getKey();
                            ElementMapping mapping = entry.getValue();

                            if (mapping.isNillable()) {
                                if (nilMapping != null && nilMapping != mapping) {
                                    throw new BuildException("Property " + property + " mappings " + mapping.getXmlName() + " and " + nilMapping + " are both nillable.  Only one mapping may of an property may be nilable");
                                }
                                nilMapping = mapping;
                            }


                            // add instance of check
                            JExpression isInstance = outerVar._instanceof(context.toJClass(itemType));
                            JBlock block = conditional.addCondition(isInstance);

                            // add space (' ') separator for XmlList
                            if (property.isXmlList()) {
                                // if (fooFirst) {
                                //    writer.writeCharacters(" ");
                                // }
                                // fooFirst = false;
                                block._if(firstVar.not())._then().add(builder.getXSW().invoke("writeCharacters").arg(" "));
                                block.assign(firstVar, JExpr.FALSE);
                            }

                            // declare item variable
                            JVar itemVar;
                            if (toClass(property.getComponentType()) == itemType) {
                                itemVar = outerVar;
                            } else {
                                String itemName = builder.getWriteVariableManager().createId(itemType.getSimpleName());
                                itemVar = block.decl(context.toJClass(itemType), itemName, JExpr.cast(context.toJClass(itemType), outerVar));
                            }
                            writeElement(builder, block, mapping, itemVar, itemType, false, property.isXmlList());
                        }

                        // if item was null, write xsi:nil or report an error
                        JBlock nullBlock = conditional.addCondition(outerVar.eq(JExpr._null()));
                        if (nilMapping != null) {
                            // write start element
                            QName name = nilMapping.getXmlName();
                            nullBlock.add(builder.getWriteStartElement(name));

                            // write xsi:nil
                            nullBlock.add(builder.getXSW().invoke("writeXsiNil"));

                            // close element
                            nullBlock.add(builder.getXSW().invoke("writeEndElement"));
                        } else {
                            nullBlock.invoke(builder.getWriteContextVar(), "unexpectedNullValue").arg(builder.getWriteObject()).arg(property.getName());
                        }

                        // if not a recogonized type or null, report unknown type error
                        JInvocation unexpected = conditional._else().invoke(builder.getWriteContextVar(), "unexpectedElementType").arg(builder.getXSW()).arg(builder.getWriteObject()).arg(property.getName()).arg(outerVar);
                        for (Class expectedType : expectedTypes.keySet()) {
                            unexpected.arg(context.dotclass(expectedType));
                        }
                    }
                    break;
                case ELEMENT_REF:
                    JBlock block = builder.getWriteMethod().body();

                    JVar itemVar = propertyVar;
                    if (property.isCollection()) {
                        JBlock collectionNotNull = block._if(propertyVar.ne(JExpr._null()))._then();

                        JType itemType;
                        if (!toClass(property.getComponentType()).isPrimitive()) {
                            itemType = context.getGenericType(property.getComponentType());
                        } else {
                            itemType = context.toJType((Class<?>) toClass(property.getComponentType()));
                        }

                        String itemName = builder.getWriteVariableManager().createId( property.getName() + "Item");
                        JForEach each = collectionNotNull.forEach(itemType, itemName, propertyVar);

                        JBlock newBody = each.body();
                        block = newBody;
                        itemVar = each.var();
                    }

                    // process value through adapter
                    itemVar = writeAdapterConversion(builder, block, property, itemVar);

                    if (property.isMixed()) {
                        // add instance of check
                        JExpression isInstance = itemVar._instanceof(context.toJClass(String.class));
                        JConditional conditional = block._if(isInstance);

                        // declare item variable
                        JVar stringVar;
                        if (toClass(property.getComponentType()) == String.class) {
                            stringVar = itemVar;
                        } else {
                            String itemName = builder.getWriteVariableManager().createId("string");
                            stringVar = conditional._then().decl(context.toJClass(String.class), itemName, JExpr.cast(context.toJClass(String.class), itemVar));
                        }
                        writeSimpleTypeElement(builder, stringVar, String.class, conditional._then());

                        block = conditional._else();
                    }


                    if (!property.isXmlAny()) {
                        block.invoke(builder.getWriteContextVar(), "unexpectedElementRef").arg(builder.getXSW()).arg(builder.getWriteObject()).arg(property.getName()).arg(itemVar);
View Full Code Here


            // the written type is always a non-nillable String
            type = String.class;
            nillable = false;

            // if (id != null) write the value
            JConditional nullCond = block._if(itemVar.ne(JExpr._null()));
            block = nullCond._then();
        }

        // write start element
        if (!xmlList) {
            QName name = mapping.getXmlName();
            block.add(builder.getWriteStartElement(name));
        }

        // if nillable, we need to write xsi:nil when value is null
        JBlock elementWriteBlock = block;
        if (nillable && !type.isPrimitive() && !xmlList) {
            JConditional nilCond = block._if(itemVar.ne(JExpr._null()));
            elementWriteBlock = nilCond._then();
            nilCond._else().add(builder.getXSW().invoke("writeXsiNil"));
        }

        // write element
        Bean targetBean = model.getBean(type);
        if (targetBean == null || targetBean.getType().isEnum()) {
View Full Code Here

            // the written type is always String
            type = String.class;

            // if (id != null) write the value
            JConditional nullCond = itemBlock._if(itemVar.ne(JExpr._null()));
            itemBlock = nullCond._then();
        }

        if(isBuiltinType(type)) {
            itemVar = toString(builder, itemVar, type);
        } else if (type.equals(byte[].class)) {
View Full Code Here

        method.javadoc().append(doc);

        JFieldRef fr = JExpr.ref(fieldName);
        if (dvExpr != null) {
            JExpression test = JOp.eq(JExpr._null(), fr);
            JConditional jc =  method.body()._if(test);
            jc._then()._return(dvExpr);
            jc._else()._return(fr);
        } else {
            method.body()._return(fr);
        }
    }
View Full Code Here

        } else if (cls.equals(boolean.class) || cls.equals(Boolean.class)) {
            JExpression var = JExpr.direct("_attValue");
            JBlock b = method.body();
            JVar retVar = method.body().decl(model._ref(boolean.class), "value");
           
            JConditional cond = b._if(JExpr.lit("1").invoke("equals").arg(var).cor(JExpr.lit("true").invoke("equals").arg(var)));
            JClass boolClass = (JClass) model._ref(Boolean.class);
           
            cond._then().assign(retVar, boolClass.staticRef("TRUE"));
            cond._else().assign(retVar, boolClass.staticRef("FALSE"));
           
            return retVar;
        }
        throw new UnsupportedOperationException();
    }
View Full Code Here

    public ElementWriterBuilder newCondition(JExpression condition) {
        return newCondition(condition, objectVar.type());
    }
   
    public ElementWriterBuilder newCondition(JExpression condition, JType type) {
        JConditional conditional = currentBlock._if(condition);
        JBlock block = conditional._then();
       
        JMethod m = buildContext.getNextWriteMethod(writerClass);
        addBasicArgs(m);
        JVar newObjectVar = m.param(type, "_" + type.name().replaceAll("\\[", "").replace("]", ""));
       
View Full Code Here

    public ElementWriterBuilder writeElement(QName name) {
        return writeElement(name, objectVar.type(), objectVar);
    }

    public ElementWriterBuilder writeElement(QName name, JExpression condition, JType type, JExpression var) {
        JConditional conditional = currentBlock._if(condition);
        JBlock block = conditional._then();
       
        return writeElement(name, type, var, block);
    }
View Full Code Here

        return new AttributeWriterBuilder(this, name, m, newObjectVar);
    }

    public void writeNilIfNull() {
        JBlock ifBlock = currentBlock;
        JConditional cond2 = ifBlock._if(getObject().eq(JExpr._null()));
        JBlock nullBlock2 = cond2._then();
        nullBlock2.add(xswVar.invoke("writeXsiNil"));
        nullBlock2._return();
    }
View Full Code Here

    }
   
    public void writeAs(Class cls, boolean nillable) {
        if (!cls.isPrimitive()) {
            JBlock block = currentBlock;
            JConditional cond = block._if(getObject().ne(JExpr._null()));
            JBlock newBlock = cond._then();
            setCurrentBlock(newBlock);
           
            writeAs(cls);
           
            if (nillable) {
                newBlock = cond._else();
                newBlock.add(xswVar.invoke("writeXsiNil"));
                setCurrentBlock(newBlock);
            }
            setCurrentBlock(block);
        } else {
View Full Code Here

    private JVar createVar(String value, Class<?> cls, boolean nillable) {
        JVar var;
        if (nillable) {
            var = method.body().decl(model._ref(cls), "value", JExpr._null());
            JConditional cond = method.body()._if(xsrVar.invoke("isXsiNil").not());
           
            JInvocation invocation = xsrVar.invoke(value);
            cond._then().assign(var, invocation);
        } else {
            var = method.body().decl(model._ref(cls), "value", xsrVar.invoke(value));
        }
       
        return var;
View Full Code Here

TOP

Related Classes of com.sun.codemodel.JConditional

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.