Package com.sun.codemodel

Examples of com.sun.codemodel.JForEach


        JFieldVar lookupMap = _enum.field(JMod.PRIVATE | JMod.STATIC, lookupType, "constants");

        JClass lookupImplType = _enum.owner().ref(HashMap.class).narrow(_enum.owner().ref(String.class), _enum);
        lookupMap.init(JExpr._new(lookupImplType));

        JForEach forEach = _enum.init().forEach(_enum, "c", JExpr.invoke("values"));
        JInvocation put = forEach.body().invoke(lookupMap, "put");
        put.arg(forEach.var().ref("value"));
        put.arg(forEach.var());

        return lookupMap;
    }
View Full Code Here


                        // get the value to evaluate
                        JExpression value;
                        if (!property.isCollection()) {
                            value = builder.getAttributeVar().invoke("getValue");
                        } else {
                            JForEach forEach = block.forEach(context.toJClass(String.class), builder.getReadVariableManager().createId(property.getName() + "Item"), builder.getAttributeVar().invoke("getXmlListValue"));
                            block = forEach.body();
                            value = forEach.var();
                        }
                       
                        // read and set
                        JExpression toSet = handleAttribute(builder, block, property, value);
                        doSet(builder, block, property, beanVar, toSet, collectionVar);
                    } else {
                        handleAnyAttribute(builder, property, beanVar);
                    }

                }
                break;

                case ELEMENT:
                case ELEMENT_REF: {
                    JAXBObjectBuilder elementBuilder = builder;
                    JVar parentVar = beanVar;
                    if (property.getXmlName() != null && !property.isXmlList()) {
                        elementBuilder = builder.expectWrapperElement(property.getXmlName(), beanVar, property.getName(), property.isMixed());
                    }

                    // create collection var if necessary
                    JVar collectionVar = handleCollection(elementBuilder, property, parentVar);

                    for (ElementMapping mapping : property.getElementMappings()) {
                        // create element block
                        JBlock block = elementBuilder.expectElement(mapping.getXmlName());

                        // add comment for readability
                        block.add(new JLineComment(property.getXmlStyle() + ": " + property.getName()));

                        // get the value to evaluate
                        JVar xsrVar = builder.getChildElementVar();
                        if (!property.isXmlList()) {
                            // read and set
                            JExpression toSet = handleElement(builder, xsrVar, block, property, mapping.isNillable(), mapping.getComponentType());
                            doSet(builder, block, property, parentVar, toSet, collectionVar);
                        } else {
                            JForEach forEach = block.forEach(context.toJClass(String.class), builder.getReadVariableManager().createId(property.getName() + "Item"), xsrVar.invoke("getElementAsXmlList"));
                            block = forEach.body();
                            JExpression value = forEach.var();

                            // read and set
                            String propertyName = property.getName();
                            if (property.isCollection()) propertyName += "Item";
                            propertyName = builder.getReadVariableManager().createId(propertyName);
View Full Code Here

        // get the value to evaluate
        JExpression value;
        if (!property.isCollection()) {
            value = xsrVar.invoke("getElementText");
        } else {
            JForEach forEach = block.forEach(context.toJClass(String.class), builder.getReadVariableManager().createId(property.getName() + "Item"), builder.getXSR().invoke("getElementAsXmlList"));
            block = forEach.body();
            value = forEach.var();
        }

        // read and set value
        Class targetType = toClass(property.getComponentType());
View Full Code Here

                    if (needsCast) {
                        propertyVar = JExpr.cast(context.toJClass(Map.class).narrow(context.toJClass(QName.class), context.getGenericType(property.getComponentType())), propertyVar);
                    }

                    JForEach each = nullCond._then().forEach(context.toJClass(Map.Entry.class).narrow(context.toJClass(QName.class), context.getGenericType(property.getComponentType())), entryName, propertyVar.invoke("entrySet"));
                    writeSimpleTypeAttribute(builder, each.body(), each.var().invoke("getKey"), toClass(property.getComponentType()), each.var().invoke("getValue"));
                }
            }
        }
    }
View Full Code Here

                        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);
                    } else {
                        block.invoke(builder.getWriteContextVar(), "writeXmlAny").arg(builder.getXSW()).arg(builder.getWriteObject()).arg(property.getName()).arg(itemVar);
                    }
                    break;
                case VALUE:
                    block = builder.getWriteMethod().body();

                    itemVar = propertyVar;
                    firstVar = null;
                    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()));
                        }

                        firstVar = collectionNotNull.decl(context.toJType(boolean.class), builder.getWriteVariableManager().createId(property.getName() + "First"), JExpr.TRUE);

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

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

                    // add space (' ') separator for XmlList
                    if (property.isCollection()) {
                        // if (fooFirst) {
View Full Code Here

            String valueName = builder.getWriteVariableManager().createId( property.getName() + "Value");
            stringBuilder = block.decl(context.toJClass(StringBuilder.class), valueName, JExpr._new(context.toJClass(StringBuilder.class)));

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

            JBlock newBody = each.body();
            itemBlock = newBody;
            itemVar = each.var();

            // if (booleanArrayAttribute.length != 0) booleanArrayAttributeValue.append(" ");
            itemBlock._if(stringBuilder.invoke("length").ne(JExpr.lit(0)))._then().invoke(stringBuilder, "append").arg(" ");

            // the final value assigned to the property is stringBuilder.toString()
View Full Code Here

                // for( S v : values ) {
                //   <packer set>(<as>(v));
                // }
                //  ... assign ...
                packer.start(values.invoke("size"));
                JForEach forEach = body.forEach(conv.sourceType(), id(), values);
                packer.pack(forEach.body(),conv.as(forEach.var(),packer.itemType()), forEach.var());
                assign(packer.end());
            }
View Full Code Here

                // for( S v : values ) {
                //   <packer set>(<as>(v));
                // }
                //  ... assign ...
                packer.start(values.invoke("size"));
                JForEach forEach = body.forEach(conv.sourceType(), id(), values);
                packer.pack(forEach.body(),conv.as(forEach.var(),packer.itemType()), forEach.var());
                assign(packer.end());
            }
View Full Code Here

            //   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()) {
View Full Code Here

            // }

            block.assign($var,JExpr._new(cm.ref(ArrayList.class).narrow(itemType().boxify())));
            JVar $col = block.decl(core.getRawType(), "col" + hashCode());
            acc.toRawValue(block,$col);
            JForEach loop = block.forEach(elementType, "v" + hashCode()/*unique string handling*/, $col);

            JConditional cond = loop.body()._if(loop.var().eq(JExpr._null()));
            cond._then().invoke($var,"add").arg(JExpr._null());
            cond._else().invoke($var,"add").arg(loop.var().invoke("getValue"));
        }
View Full Code Here

TOP

Related Classes of com.sun.codemodel.JForEach

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.