Java 类com.sun.codemodel.JForEach 实例源码

项目:GitHub    文件:EnumRule.java   
private JFieldVar addQuickLookupMap(JDefinedClass _enum, JType backingType) {

        JClass lookupType = _enum.owner().ref(Map.class).narrow(backingType.boxify(), _enum);
        JFieldVar lookupMap = _enum.field(JMod.PRIVATE | JMod.STATIC | JMod.FINAL, lookupType, "CONSTANTS");

        JClass lookupImplType = _enum.owner().ref(HashMap.class).narrow(backingType.boxify(), _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;
    }
项目:org.ops4j.ramler    文件:EnumGenerator.java   
private void generateEnumFromStringMethod(JDefinedClass klass, JFieldVar valueField) {
    JMethod converter = klass.method(JMod.PUBLIC | JMod.STATIC, klass, "fromString");
    JVar param = converter.param(String.class, VALUE);

    JBlock body = converter.body();
    JForEach forEach = body.forEach(klass, "v", klass.staticInvoke("values"));
    JBlock loopBlock = forEach.body();
    loopBlock._if(forEach.var().ref(valueField).invoke("equals").arg(param))._then()
        ._return(forEach.var());
    body._throw(JExpr._new(codeModel._ref(IllegalArgumentException.class)).arg(param));
}
项目:android-modules    文件:BusHandler.java   
private void registerAll(EComponentHolder holder, Element element, String methodName, JClass eventOrRequestClass) {
    JMethod method = getMethodForRegistering(holder);
    final JClass moduleProviderClass = refClass(ModuleObjectsShare.class);
    final JInvocation modulesNames = moduleProviderClass.staticInvoke("modulesNames");
    final JConditional isEmpty = method.body()._if(modulesNames.invoke("isEmpty"));
    register(holder, element, methodName, eventOrRequestClass, lit(""), isEmpty._then());
    final JForEach forEach = isEmpty._else().forEach(refClass(String.class), "moduleName", modulesNames);
    forEach.body().directStatement("// we need to store module name in some final variable to use it in the listener");
    final JVar moduleNameToPass = forEach.body().decl(JMod.FINAL, refClass(String.class), "moduleNameToPass", forEach.var());
    register(holder, element, methodName, eventOrRequestClass, moduleNameToPass, forEach.body());
}
项目:springmvc-raml-plugin    文件:EnumBuilder.java   
public EnumBuilder withValueField(Class<?> type) {
    pojoCreationCheck();
    if (!this.pojo.fields().containsKey("value")) {
        //Create Field
        valueField = this.pojo.field(JMod.PRIVATE | JMod.FINAL, type, "value");

        //Create private Constructor
        JMethod constructor =  this.pojo.constructor(JMod.PRIVATE);
        JVar valueParam = constructor.param(type, "value");
        constructor.body().assign(JExpr._this().ref(valueField), valueParam);

        //add values to map
        JClass lookupType = this.pojo.owner().ref(Map.class).narrow(valueField.type().boxify(), this.pojo);
        lookupMap = this.pojo.field(JMod.PRIVATE | JMod.STATIC | JMod.FINAL, lookupType, "VALUE_CACHE");

        JClass lookupImplType = this.pojo.owner().ref(HashMap.class).narrow(valueField.type().boxify(), this.pojo);
        lookupMap.init(JExpr._new(lookupImplType));

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

        //Add method to retrieve value
        JMethod fromValue = this.pojo.method(JMod.PUBLIC, valueField.type(), "value");
        fromValue.body()._return(JExpr._this().ref(valueField));

        addFromValueMethod();
        addToStringMethod();
    }
       return this;
}
项目:kola    文件:StatementAdapter.java   
@Override
    public void caseAEnhancedForStatement(AEnhancedForStatement node) {
        context.pushStatementScope();
        try {
            // Formal Parameter
            AFormalParameter paramNode = (AFormalParameter) node.getVariable();
            FormalParameter param = new FormalParameter();
            FieldModifierAdapter fma = new FieldModifierAdapter(context);
            paramNode.apply(fma);
            param.mods = fma.getMods();
            TypeAdapter ta = new TypeAdapter(context);
            paramNode.getType().apply(ta);
            param.type = ta.type;
            AVariableDeclaratorId avdid = (AVariableDeclaratorId) paramNode.getVariableDeclaratorId();
            param.id = avdid.getIdentifier().getText();
            param.dim = avdid.getDim().size();


            // Iterable
            ExpressionAdapter ea = new ExpressionAdapter(new JExprParent(), context);
            node.getIterable().apply(ea);

            JForEach loop = parent.forEach(param.type, param.id, ea.expr);
            context.addField(param.id, loop.var(), Field.Type.NORMAL);

            // Do
            JBlock tmpBlock = new JBlock();
            StatementAdapter sa = new StatementAdapter(new JBlockParent(tmpBlock, context), context);
            node.getDo().apply(sa);
            JStatement stmt = (JStatement) tmpBlock.getContents().get(0);
//        if (stmt instanceof JBlock) {
//            JBlock stmtBlock = (JBlock) stmt;
//            stmtBlock.setNewlineRequired(false);
//        }
            loop.setBody(stmt);
        } finally {
            context.popScope();
        }
    }
项目:jsonschema2pojo    文件:EnumRule.java   
private JFieldVar addQuickLookupMap(JDefinedClass _enum) {

        JClass lookupType = _enum.owner().ref(Map.class).narrow(_enum.owner().ref(String.class), _enum);
        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", _enum.staticInvoke("values"));
        JInvocation put = forEach.body().invoke(lookupMap, "put");
        put.arg(forEach.var().ref("value"));
        put.arg(forEach.var());

        return lookupMap;
    }
项目:kola    文件:JBlockParent.java   
@Override
public JForEach forEach(JType type, String id, JExpression iterable) {
    return block.forEach(type, id, iterable);
}
项目:jaxb2-rich-contract-plugin    文件:PluginContext.java   
public JForEach loop(final JBlock block, final JExpression source, final JType sourceElementType, final JAssignmentTarget target, final JType targetElementType) {
    final JConditional ifNull = block._if(source.eq(JExpr._null()));
    ifNull._then().assign(target, JExpr._null());
    ifNull._else().assign(target, JExpr._new(this.arrayListClass.narrow(targetElementType)));
    return ifNull._else().forEach(sourceElementType, "_item", source);
}
项目:jaxb2-rich-contract-plugin    文件:BuilderGenerator.java   
private void generateAddMethods(final PropertyOutline propertyOutline,
                                final QName elementName, final JType jType) {
    final JClass elementType = jType.boxify();
    final JClass iterableType = this.pluginContext.iterableClass.narrow(elementType.wildcard());
    final String fieldName = this.pluginContext.outline.getModel().getNameConverter().toVariableName(elementName.getLocalPart());
    final String propertyName = this.pluginContext.outline.getModel().getNameConverter().toPropertyName(elementName.getLocalPart());
    final JMethod addIterableMethod = this.builderClass.raw.method(JMod.PUBLIC, this.builderClass.type, PluginContext.ADD_METHOD_PREFIX + propertyName);
    final JVar addIterableParam = addIterableMethod.param(JMod.FINAL, iterableType, fieldName + "_");
    generateAddMethodJavadoc(addIterableMethod, addIterableParam);
    final JMethod addVarargsMethod = this.builderClass.raw.method(JMod.PUBLIC, this.builderClass.type, PluginContext.ADD_METHOD_PREFIX + propertyName);
    final JVar addVarargsParam = addVarargsMethod.varParam(elementType, fieldName + "_");
    generateAddMethodJavadoc(addVarargsMethod, addVarargsParam);
    final BuilderOutline childBuilderOutline = getBuilderDeclaration(elementType);
    final JMethod addMethod;
    if (childBuilderOutline != null && !childBuilderOutline.getClassOutline().getImplClass().isAbstract()) {
        final JClass builderWithMethodReturnType = childBuilderOutline.getBuilderClass().narrow(this.builderClass.type.wildcard());
        addMethod = this.builderClass.raw.method(JMod.PUBLIC, builderWithMethodReturnType, PluginContext.ADD_METHOD_PREFIX + propertyName);
        generateBuilderMethodJavadoc(addMethod, "add", fieldName);
    } else {
        addMethod = null;
    }
    if (this.implement) {
        final BuilderOutline choiceChildBuilderOutline = getBuilderDeclaration(propertyOutline.getElementType());
        final JClass childBuilderType = childBuilderOutline == null ? this.pluginContext.buildableInterface : childBuilderOutline.getBuilderClass().narrow(this.builderClass.type);
        final JClass builderFieldElementType = choiceChildBuilderOutline == null ? this.pluginContext.buildableInterface : choiceChildBuilderOutline.getBuilderClass().narrow(this.builderClass.type);
        final JClass builderArrayListClass = this.pluginContext.arrayListClass.narrow(builderFieldElementType);
        final JFieldVar builderField = this.builderClass.raw.fields().get(propertyOutline.getFieldName());
        addVarargsMethod.body()._return(JExpr.invoke(addIterableMethod).arg(this.pluginContext.asList(addVarargsParam)));
        if (addMethod == null) {
            addIterableMethod.body()._return(JExpr.invoke(PluginContext.ADD_METHOD_PREFIX + propertyOutline.getBaseName()).arg(addIterableParam));
        } else {
            final JConditional addIterableIfParamNull = addIterableMethod.body()._if(addIterableParam.ne(JExpr._null()));
            final JConditional addIterableIfNull = addIterableIfParamNull._then()._if(JExpr._this().ref(builderField).eq(JExpr._null()));
            addIterableIfNull._then().assign(JExpr._this().ref(builderField), JExpr._new(builderArrayListClass));
            final JForEach addIterableForEach = addIterableIfParamNull._then().forEach(elementType, BuilderGenerator.ITEM_VAR_NAME, addIterableParam);
            final JExpression builderCreationExpression = JExpr._new(childBuilderType).arg(JExpr._this()).arg(addIterableForEach.var()).arg(this.settings.isCopyAlways() ? JExpr.TRUE : JExpr.FALSE);
            addIterableForEach.body().add(JExpr._this().ref(builderField).invoke("add").arg(builderCreationExpression));
            addIterableMethod.body()._return(JExpr._this());

            final JConditional addIfNull = addMethod.body()._if(JExpr._this().ref(builderField).eq(JExpr._null()));
            addIfNull._then().assign(JExpr._this().ref(builderField), JExpr._new(builderArrayListClass));
            final JVar childBuilderVar = addMethod.body().decl(JMod.FINAL, childBuilderType, fieldName + this.settings.getBuilderFieldSuffix(), JExpr._new(childBuilderType).arg(JExpr._this()).arg(JExpr._null()).arg(JExpr.FALSE));
            addMethod.body().add(JExpr._this().ref(builderField).invoke("add").arg(childBuilderVar));
            addMethod.body()._return(childBuilderVar);
        }
    }
}
项目:jaxb2-rich-contract-plugin    文件:BuilderGenerator.java   
private void generateCollectionProperty(final JBlock initBody, final JVar productParam, final PropertyOutline propertyOutline, final JClass elementType) {
    final String fieldName = propertyOutline.getFieldName();
    final String propertyName = propertyOutline.getBaseName();
    final JClass iterableType = this.pluginContext.iterableClass.narrow(elementType.wildcard());
    final JMethod addIterableMethod = this.builderClass.raw.method(JMod.PUBLIC, this.builderClass.type, PluginContext.ADD_METHOD_PREFIX + propertyName);
    final JVar addIterableParam = addIterableMethod.param(JMod.FINAL, iterableType, fieldName);
    generateAddMethodJavadoc(addIterableMethod, addIterableParam);
    final JMethod withIterableMethod = this.builderClass.raw.method(JMod.PUBLIC, this.builderClass.type, PluginContext.WITH_METHOD_PREFIX + propertyName);
    final JVar withIterableParam = withIterableMethod.param(JMod.FINAL, iterableType, fieldName);
    generateWithMethodJavadoc(withIterableMethod, withIterableParam);
    final JMethod addVarargsMethod = this.builderClass.raw.method(JMod.PUBLIC, this.builderClass.type, PluginContext.ADD_METHOD_PREFIX + propertyName);
    final JVar addVarargsParam = addVarargsMethod.varParam(elementType, fieldName);
    generateAddMethodJavadoc(addVarargsMethod, addVarargsParam);
    final JMethod withVarargsMethod = this.builderClass.raw.method(JMod.PUBLIC, this.builderClass.type, PluginContext.WITH_METHOD_PREFIX + propertyName);
    final JVar withVarargsParam = withVarargsMethod.varParam(elementType, fieldName);
    generateWithMethodJavadoc(withVarargsMethod, withVarargsParam);
    final BuilderOutline childBuilderOutline = getBuilderDeclaration(elementType);
    final JMethod addMethod;
    if (childBuilderOutline != null && !childBuilderOutline.getClassOutline().getImplClass().isAbstract()) {
        final JClass builderWithMethodReturnType = childBuilderOutline.getBuilderClass().narrow(this.builderClass.type.wildcard());
        addMethod = this.builderClass.raw.method(JMod.PUBLIC, builderWithMethodReturnType, PluginContext.ADD_METHOD_PREFIX + propertyName);
        generateBuilderMethodJavadoc(addMethod, "add", propertyName);
    } else {
        addMethod = null;
    }
    if (this.implement) {
        final JClass childBuilderType = childBuilderOutline == null ? this.pluginContext.buildableInterface : childBuilderOutline.getBuilderClass().narrow(this.builderClass.type);
        final JClass builderArrayListClass = this.pluginContext.arrayListClass.narrow(childBuilderType);
        final JClass builderListClass = this.pluginContext.listClass.narrow(childBuilderType);
        final JFieldVar builderField = this.builderClass.raw.field(JMod.PRIVATE, builderListClass, fieldName);
        addVarargsMethod.body().invoke(addIterableMethod).arg(this.pluginContext.asList(addVarargsParam));
        addVarargsMethod.body()._return(JExpr._this());
        withVarargsMethod.body().invoke(withIterableMethod).arg(this.pluginContext.asList(withVarargsParam));
        withVarargsMethod.body()._return(JExpr._this());
        final JConditional addIterableIfParamNull = addIterableMethod.body()._if(addIterableParam.ne(JExpr._null()));
        final JConditional addIterableIfNull = addIterableIfParamNull._then()._if(JExpr._this().ref(builderField).eq(JExpr._null()));
        addIterableIfNull._then().assign(JExpr._this().ref(builderField), JExpr._new(builderArrayListClass));
        final JForEach jForEach = addIterableIfParamNull._then().forEach(elementType, BuilderGenerator.ITEM_VAR_NAME, addIterableParam);
        final JExpression builderCreationExpression = childBuilderOutline == null
                ? JExpr._new(this.pluginContext.buildableClass).arg(jForEach.var())
                : JExpr._new(childBuilderType).arg(JExpr._this()).arg(jForEach.var()).arg(this.settings.isCopyAlways() ? JExpr.TRUE : JExpr.FALSE);
        jForEach.body().add(JExpr._this().ref(builderField).invoke("add").arg(builderCreationExpression));
        addIterableMethod.body()._return(JExpr._this());
        final JConditional withIterableIfNull = withIterableMethod.body()._if(JExpr._this().ref(builderField).ne(JExpr._null()));
        withIterableIfNull._then().add(JExpr._this().ref(builderField).invoke("clear"));
        withIterableMethod.body()._return(JExpr.invoke(addIterableMethod).arg(withIterableParam));
        final JConditional ifNull = initBody._if(JExpr._this().ref(builderField).ne(JExpr._null()));
        final JVar collectionVar = ifNull._then().decl(JMod.FINAL, this.pluginContext.listClass.narrow(elementType), fieldName, JExpr._new(this.pluginContext.arrayListClass.narrow(elementType)).arg(JExpr._this().ref(builderField).invoke("size")));
        final JForEach initForEach = ifNull._then().forEach(childBuilderType, BuilderGenerator.ITEM_VAR_NAME, JExpr._this().ref(builderField));
        final JInvocation buildMethodInvocation = initForEach.var().invoke(this.settings.getBuildMethodName());
        final JExpression buildExpression = childBuilderOutline == null ? JExpr.cast(elementType, buildMethodInvocation) : buildMethodInvocation;
        initForEach.body().add(collectionVar.invoke("add").arg(buildExpression));
        ifNull._then().assign(productParam.ref(fieldName), collectionVar);
        if (addMethod != null) {
            final JConditional addIfNull = addMethod.body()._if(JExpr._this().ref(builderField).eq(JExpr._null()));
            addIfNull._then().assign(JExpr._this().ref(builderField), JExpr._new(builderArrayListClass));
            final JVar childBuilderVar = addMethod.body().decl(JMod.FINAL, childBuilderType, fieldName + this.settings.getBuilderFieldSuffix(), JExpr._new(childBuilderType).arg(JExpr._this()).arg(JExpr._null()).arg(JExpr.FALSE));
            addMethod.body().add(JExpr._this().ref(builderField).invoke("add").arg(childBuilderVar));
            addMethod.body()._return(childBuilderVar);
        }
        this.pluginContext.generateImmutableFieldInit(initBody, productParam, propertyOutline);
    }
}
项目:jaxb2-rich-contract-plugin    文件:BuilderGenerator.java   
JForEach loop(final JBlock block, final JExpression source, final JType sourceElementType, final JAssignmentTarget target, final JType targetElementType) {
    final JConditional ifNull = block._if(source.eq(JExpr._null()));
    ifNull._then().assign(target, JExpr._null());
    ifNull._else().assign(target, JExpr._new(this.pluginContext.arrayListClass.narrow(targetElementType)));
    return ifNull._else().forEach(sourceElementType, BuilderGenerator.ITEM_VAR_NAME, source);
}
项目:serket    文件:GenerateScpdClasses.java   
private static JType getArgumentsType(JCodeModel cm, JDefinedClass cls, String name, Element[] args, boolean out) throws XPathExpressionException, JClassAlreadyExistsException {
        JDefinedClass returnClass = cls._class(JMod.PUBLIC | JMod.STATIC , name + (out ? "Response" : "Request"));
        returnClass._extends(SOAPSerializable.class);

        // public void parse(SOAPMessage soapmessage) throws SOAPException;
        JMethod parse = returnClass.method(JMod.PUBLIC, cm.VOID, "parse");
        JVar soapMessageParam = parse.param(SOAPMessage.class, "soapMessage");

        // public SOAPMessage format()
        JMethod format = returnClass.method(JMod.PUBLIC, SOAPMessage.class, "format");
        format._throws(SOAPException.class);
        JVar formatRetVal = format.body().decl(cm.ref(SOAPMessage.class), "retVal").init(returnClass.staticInvoke("createMessage"));
        JInvocation formatBodyElementInvocation = formatRetVal.invoke("getSOAPBody").invoke("addBodyElement").arg(JExpr._new(cm.ref(QName.class)).arg(JExpr.ref("URI")).arg(JExpr.lit(returnClass.name())).arg(JExpr.lit("u")));

        JClass xmlUtilClass = cm.ref(XmlUtil.class);
        if (args.length > 0) {
            parse.annotate(SuppressWarnings.class).param("value", "unchecked");
            parse._throws(SOAPException.class);

//            for (Element e : XmlUtil.getChildElements(XmlUtil.getChildElements(body).get(0))) {

//            for (Element e : XmlUtil.getChildElements(XmlUtil.getChildElements(soapMessage.getSOAPBody()).get(0))) {

            JInvocation collection = xmlUtilClass.staticInvoke("getChildElements").arg(xmlUtilClass.staticInvoke("getChildElements").arg(soapMessageParam.invoke("getSOAPBody")).invoke("get").arg(JExpr.lit(0)));


            JForEach parseFor = parse.body().forEach(cm._ref(Element.class), "e", collection);
            JVar parseForElement = parseFor.var();
            JVar parseForName = parseFor.body().decl(cm.ref(String.class), "name", parseForElement.invoke("getNodeName"));
            JVar formatElement = format.body().decl(cm.ref(SOAPBodyElement.class), "soapBodyElement",formatBodyElementInvocation);   
            for (Element outArg : args) {

                String relatedStateVariable = XPathUtil.getStringValue(outArg, "relatedStateVariable").trim();
                String elementName = XPathUtil.getStringValue(outArg, "name").trim(); 
                String fieldName = Character.toLowerCase(elementName.charAt(0)) + elementName.substring(1);
                JType fieldType = getStateVariableType(cm, cls, relatedStateVariable, outArg.getOwnerDocument());
                JFieldVar fieldVar = returnClass.field(JMod.PUBLIC, fieldType, fieldName);

                // element.addChildElement("Result").setTextContent(resultDidl);
                JInvocation formatSetText = formatElement.invoke("addChildElement").arg(JExpr.lit(elementName)).invoke("setTextContent");
                format.body().add(formatSetText);
                // if ("Result".equals(name)) {
                JBlock ifBody = parseFor.body()._if(JExpr.lit(elementName).invoke("equals").arg(parseForName))._then();
                // String value = e.getTextContent();
                if (fieldType.isPrimitive()) {
                    formatSetText.arg(cm.ref(Integer.class).staticInvoke("toString").arg(fieldVar));
                } else {
                    formatSetText.arg(fieldVar.invoke("toString"));
                }

                if (fieldType.equals(cm.ref(String.class))) {
                    // TransferID = e.getTextContent();
                    ifBody.assign(fieldVar, parseForElement.invoke("getTextContent"));
                } else if (fieldType.equals(cm.ref(Integer.class)) || fieldType.equals(cm.INT)) {
                    // TransferID = Integer.valueOf(e.getTextContent());
                    ifBody.assign(fieldVar, cm.ref(Integer.class).staticInvoke("valueOf").arg(parseForElement.invoke("getTextContent")));
                } else if (fieldType.equals(cm.ref(Boolean.class)) || fieldType.equals(cm.BOOLEAN)) {
                    // TransferID = Integer.valueOf(e.getTextContent());
                    ifBody.assign(fieldVar, cm.ref(Boolean.class).staticInvoke("valueOf").arg(parseForElement.invoke("getTextContent")));
                } else if (fieldType instanceof JClass) {
                    JClass fieldCls = (JClass) fieldType;
                    // TransferStatus = org.saintandreas.serket.scpd.TransferStatus.valueOf(e.getTextContent());
                    ifBody.assign(fieldVar, fieldCls.staticInvoke("valueOf").arg(parseForElement.invoke("getTextContent")));
                } else {
                    System.out.println("unparsed type " + fieldType);
                }
                //            JVar value = ifBody.decl(cm.ref(String.class), "value", );
                ifBody._continue();
            }
        } else {
            format.body().add(formatBodyElementInvocation);
        }

        format.body()._return(formatRetVal);
        return returnClass;
    }
项目:podio-java-codegen    文件:EnumGenerator.java   
/**
 * Generates an enum for a given field
 * 
 * @param f
 * @param name
 *            of enum in UpperCamelCase
 * @return
 * @throws JClassAlreadyExistsException
 */
public JDefinedClass generateEnum(ApplicationField f, String name) throws JClassAlreadyExistsException {
    JDefinedClass result = jp != null ? jp._enum(name) : jc._package("")._enum(name);
    result._implements(PodioCategory.class);

    // fields:
    JFieldVar podioId = result.field(JMod.PRIVATE, jc.INT, "podioId");
    JFieldVar value = result.field(JMod.PRIVATE, jc.ref(String.class), "value");

    // constructor:
    JMethod constructor = result.constructor(JMod.PRIVATE);
    JVar constructorPodioIdParam = constructor.param(jc.INT, "podioId");
    JVar constructorToStringParam = constructor.param(jc.ref(String.class), "value");
    constructor.body().assign(JExpr._this().ref(podioId), constructorPodioIdParam);
    constructor.body().assign(JExpr._this().ref(value), constructorToStringParam);

    // toString:
    result.method(JMod.PUBLIC, String.class, "toString").body()._return(value);

    // getId:
    result.method(JMod.PUBLIC, jc.INT, "getPodioId").body()._return(podioId);

    // static byId:
    JMethod byId = result.method(JMod.PUBLIC | JMod.STATIC, result, "byId");
    byId.javadoc().addReturn().add("{@code null}, if no element with given {@code id} exists.");
    JVar byIdParam = byId.param(jc.INT, "podioId");
    JForEach forEach = byId.body().forEach(result, "e", result.staticInvoke("values"));
    forEach.body()._if(byIdParam.eq(forEach.var().invoke("getPodioId")))._then()._return(forEach.var());
    byId.body()._return(JExpr._null());

    // literals:
    result.enumConstant("NONE").arg(JExpr.lit(0)).arg(JExpr.lit("--"));
    for (CategoryOption option : f.getConfiguration().getSettings().getOptions()) {
        String constantName = JavaNames.createValidJavaTypeName(option.getText(), name);
        String finalConstantName = CaseFormat.UPPER_CAMEL.to(CaseFormat.UPPER_UNDERSCORE,
                constantName);
        JEnumConstant constant = result.enumConstant(finalConstantName);
        constant.arg(JExpr.lit(option.getId())).arg(JExpr.lit(option.getText()));
        if (option.getStatus().equals(CategoryOptionStatus.DELETED)) {
            constant.annotate(Deprecated.class);
        }
    }

    return result;
}
项目:jpmml-evaluator    文件:OperationProcessor.java   
static
private void createFormatMethod(JDefinedClass clazz, JPrimitiveType type){
    JCodeModel codeModel = clazz.owner();

    JClass numberClazz = codeModel.ref(Number.class);
    JClass stringBuilderClazz = codeModel.ref(StringBuilder.class);

    JType numberListClazz;

    try {
        numberListClazz = codeModel.parseType("java.util.List<? extends java.lang.Number>");
    } catch(ClassNotFoundException cnfe){
        throw new RuntimeException(cnfe);
    }

    JMethod method = clazz.method(JMod.STATIC | JMod.PRIVATE, String.class, "format");

    JVar valuesParameter = method.param(numberListClazz, "values");

    JBlock body = method.body();

    JVar sbVariable = body.decl(stringBuilderClazz, "sb", JExpr._new(stringBuilderClazz).arg(valuesParameter.invoke("size").mul(JExpr.lit(32))));

    JForEach forStatement = body.forEach(numberClazz, "value", valuesParameter);

    JBlock forBody = forStatement.body();

    forBody.add(createReportInvocation(clazz, sbVariable, "${0}", Collections.singletonList(forStatement.var()), type));

    body._return(sbVariable.invoke("toString"));
}
项目:kola    文件:StatementAdapter.java   
public JForEach forEach(JType type, String id, JExpression iterable);