com.sun.codemodel.JForEach Java Examples
The following examples show how to use
com.sun.codemodel.JForEach.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JaxRsEnumRule.java From apicurio-studio with Apache License 2.0 | 5 votes |
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; }
Example #2
Source File: EnumBuilder.java From springmvc-raml-plugin with Apache License 2.0 | 5 votes |
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; }
Example #3
Source File: FastSerializerGenerator.java From avro-util with BSD 2-Clause "Simplified" License | 4 votes |
private void processMap(final Schema mapSchema, JExpression mapExpr, JBlock body) { final JClass mapClass = schemaAssistant.classFromSchema(mapSchema); JClass keyClass = schemaAssistant.findStringClass(mapSchema); body.invoke(JExpr.direct(ENCODER), "writeMapStart"); final JExpression emptyMapCondition = mapExpr.eq(JExpr._null()).cor(JExpr.invoke(mapExpr, "isEmpty")); final JConditional emptyMapIf = body._if(emptyMapCondition); final JBlock emptyMapBlock = emptyMapIf._then(); emptyMapBlock.invoke(JExpr.direct(ENCODER), "setItemCount").arg(JExpr.lit(0)); final JBlock nonEmptyMapBlock = emptyMapIf._else(); nonEmptyMapBlock.invoke(JExpr.direct(ENCODER), "setItemCount").arg(JExpr.invoke(mapExpr, "size")); final JForEach mapKeysLoop = nonEmptyMapBlock.forEach(keyClass, getUniqueName("key"), JExpr.invoke(JExpr.cast(mapClass, mapExpr), "keySet")); final JBlock forBody = mapKeysLoop.body(); forBody.invoke(JExpr.direct(ENCODER), "startItem"); JVar keyStringVar; if (SchemaAssistant.hasStringableKey(mapSchema)) { keyStringVar = forBody.decl(codeModel.ref(String.class), getUniqueName("keyString"), mapKeysLoop.var().invoke("toString")); } else { keyStringVar = mapKeysLoop.var(); } final Schema valueSchema = mapSchema.getValueType(); forBody.invoke(JExpr.direct(ENCODER), "writeString").arg(keyStringVar); JVar containerVar; if (SchemaAssistant.isComplexType(valueSchema)) { containerVar = declareValueVar(valueSchema.getName(), valueSchema, forBody); forBody.assign(containerVar, JExpr.invoke(JExpr.cast(mapClass, mapExpr), "get").arg(mapKeysLoop.var())); processComplexType(valueSchema, containerVar, forBody); } else { processSimpleType(valueSchema, mapExpr.invoke("get").arg(mapKeysLoop.var()), forBody); } body.invoke(JExpr.direct(ENCODER), "writeMapEnd"); }
Example #4
Source File: FastSerializerGenerator.java From avro-fastserde with Apache License 2.0 | 4 votes |
private void processMap(final Schema mapSchema, JExpression mapExpr, JBlock body) { final JClass mapClass = schemaAssistant.classFromSchema(mapSchema); JClass keyClass = schemaAssistant.keyClassFromMapSchema(mapSchema); body.invoke(JExpr.direct(ENCODER), "writeMapStart"); final JExpression emptyMapCondition = mapExpr.eq(JExpr._null()) .cor(JExpr.invoke(mapExpr, "isEmpty")); final JConditional emptyMapIf = body._if(emptyMapCondition); final JBlock emptyMapBlock = emptyMapIf._then(); emptyMapBlock.invoke(JExpr.direct(ENCODER), "setItemCount").arg(JExpr.lit(0)); final JBlock nonEmptyMapBlock = emptyMapIf._else(); nonEmptyMapBlock.invoke(JExpr.direct(ENCODER), "setItemCount") .arg(JExpr.invoke(mapExpr, "size")); final JForEach mapKeysLoop = nonEmptyMapBlock.forEach(keyClass, getVariableName("key"), JExpr.invoke(JExpr.cast(mapClass, mapExpr), "keySet")); final JBlock forBody = mapKeysLoop.body(); forBody.invoke(JExpr.direct(ENCODER), "startItem"); JVar keyStringVar; if (SchemaAssistant.hasStringableKey(mapSchema)) { keyStringVar = forBody.decl(string, getVariableName("keyString"), mapKeysLoop.var().invoke("toString")); } else { keyStringVar = mapKeysLoop.var(); } final Schema valueSchema = mapSchema.getValueType(); forBody.invoke(JExpr.direct(ENCODER), "writeString").arg(keyStringVar); JVar containerVar; if (SchemaAssistant.isComplexType(valueSchema)) { containerVar = declareValueVar(valueSchema.getName(), valueSchema, forBody); forBody.assign(containerVar, JExpr.invoke(JExpr.cast(mapClass, mapExpr), "get").arg(mapKeysLoop.var())); processComplexType(valueSchema, containerVar, forBody); } else { processSimpleType(valueSchema, mapExpr.invoke("get").arg(mapKeysLoop.var()), forBody); } body.invoke(JExpr.direct(ENCODER), "writeMapEnd"); }
Example #5
Source File: PluginContext.java From jaxb2-rich-contract-plugin with MIT License | 4 votes |
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); }
Example #6
Source File: BuilderGenerator.java From jaxb2-rich-contract-plugin with MIT License | 4 votes |
private void generateAddMethods(final PropertyOutline propertyOutline, final QName elementName, final JType jType, final String schemaAnnotation) { final JClass elementType = jType.boxify(); final JClass iterableType = this.pluginContext.iterableClass.narrow(elementType.wildcard()); final String fieldName = this.pluginContext.toVariableName(elementName.getLocalPart()); final String propertyName = this.pluginContext.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, schemaAnnotation); 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, schemaAnnotation); 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, schemaAnnotation); } 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); } } }
Example #7
Source File: BuilderGenerator.java From jaxb2-rich-contract-plugin with MIT License | 4 votes |
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, propertyOutline.getSchemaAnnotationText().orElse(null)); 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, propertyOutline.getSchemaAnnotationText().orElse(null)); 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, propertyOutline.getSchemaAnnotationText().orElse(null)); 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, propertyOutline.getSchemaAnnotationText().orElse(null)); 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, propertyOutline.getSchemaAnnotationText().orElse(null)); } 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); } }
Example #8
Source File: BuilderGenerator.java From jaxb2-rich-contract-plugin with MIT License | 4 votes |
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); }
Example #9
Source File: OperationProcessor.java From jpmml-evaluator with GNU Affero General Public License v3.0 | 4 votes |
static private void createFormatMethod(JDefinedClass clazz, JPrimitiveType type){ JCodeModel codeModel = clazz.owner(); JClass numberClazz = codeModel.ref(Number.class); JClass stringBuilderClazz = codeModel.ref(StringBuilder.class); JMethod method = clazz.method(JMod.STATIC | JMod.PRIVATE, String.class, "format"); JVar valuesParameter = method.varParam(numberClazz, "values"); JBlock body = method.body(); JVar sbVariable = body.decl(stringBuilderClazz, "sb", JExpr._new(stringBuilderClazz).arg(valuesParameter.ref("length").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")); }