Refine search
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; }
private void handleMultiValue(JVar values) { // [RESULT] // List<S> values = dom.leafElements("..."); // <packer init> // 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()); }
JBlock onTabSelectedBody = onTabSelectedMethod.body(); JVar positionVar = onTabSelectedBody.decl(jCodeModel.INT, "position", tabParam.invoke("getPosition")); onTabSelectedBody.invoke(pagerField, "setCurrentItem").arg(positionVar); "public void onTabReselected(Tab tab, FragmentTransaction ft) {}"); JInvocation getSupportActionBar = JExpr.invoke("getSupportActionBar"); JFieldRef navigationModeList = ref.sActionBar().staticRef("NAVIGATION_MODE_TABS"); configureActionBarBody.invoke(getSupportActionBar, "setNavigationMode").arg(navigationModeList); JForEach forEachLocation = configureActionBarBody.forEach(ref.string(), "location", locationsField); JVar location = forEachLocation.var(); JBlock forEachLocationBody = forEachLocation.body(); JVar tab = forEachLocationBody.decl(ref.sTab(), "tab", getSupportActionBar.invoke("newTab")); forEachLocationBody.invoke(tab, "setText").arg(location); forEachLocationBody.invoke(tab, "setTabListener").arg(JExpr._this()); forEachLocationBody.invoke(getSupportActionBar, "addTab").arg(tab);
public void toRawValue(JBlock block, JVar $var) { JCodeModel cm = outline().getCodeModel(); JClass elementType = ei.toType(outline(),EXPOSED).boxify(); // [RESULT] // $var = new ArrayList(); // for( JAXBElement e : [core.toRawValue] ) { // if(e==null) // $var.add(null); // else // $var.add(e.getValue()); // } 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")); }
type.method(JMod.PUBLIC, baseExposedType, "value" ).body()._return($value); m.body().assign( $value, m.param( baseImplType, "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(codeModel.ref(IllegalArgumentException.class)); strForm = codeModel.ref(String.class).staticInvoke("valueOf").arg($v); } else if(baseExposedType==codeModel.ref(String.class)){ strForm = $v; } else { strForm = $v.invoke("toString"); m.body()._throw(ex.arg(strForm)); type.method(JMod.PUBLIC, String.class, "value" ).body()._return(JExpr.invoke("name")); m.body()._return( JExpr.invoke("valueOf").arg(m.param(String.class,"v")));
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); final BuilderOutline childBuilderOutline = getBuilderDeclaration(elementType); final JMethod addMethod; if (childBuilderOutline != null && !childBuilderOutline.getClassOutline().getImplClass().isAbstract()) { 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())); ? 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)); 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));
String message = builder.toString(); body._if(dataVar._instanceof(targetClass).not())._then()._throw( JExpr._new(validationExceptionClass).arg(JExpr.lit(message))); JVar sourceVar = body.decl(targetClass, "sourceVar").init(JExpr.cast(targetClass, dataVar)); JVar fieldValue = body.decl(propertyType, fieldName).init( JExpr._this().invoke("value").arg(sourceVar.invoke(getterName)).arg(propertyType.staticRef("class")) ); JClass narrowCollection = collectionClass.narrow(propertyType.wildcard()); .forEach(propertyType, fieldName, property); JVar fieldValue = forEach.var(); forEach.body() .add(jacksonType.writeType(model, jsonVar, fieldValue)); .forEach(propertyType, fieldName, property); JVar fieldValue = forEach.var(); forEach.body() .add(writerClass.staticInvoke("instance").invoke("write").arg(fieldValue).arg(jsonVar));
JVar fieldValue = body.decl(propertyType, fieldName).init( JExpr._this().invoke("value").arg(sourceVar.invoke(getterName)).arg(propertyType.staticRef("class")) ); JInvocation statement = jsonVar.invoke("writeStringField").arg(JExpr.lit(fieldName)).arg(fieldValue.invoke("stringValue")); body._if(fieldValue.ne(JExpr._null()))._then() .add(statement); } else { JClass narrowCollection = collectionClass.narrow(propertyType.wildcard()); .forEach(propertyType, fieldName, property); JVar fieldValue = forEach.var(); JInvocation statement = jsonVar.invoke("writeString").arg(fieldValue.invoke("stringValue")); forEach.body() .add(statement);
stringBuilder = block.decl(context.toJClass(StringBuilder.class), valueName, JExpr._new(context.toJClass(StringBuilder.class))); JForEach each = block.forEach(itemType, itemName, propertyVar); JBlock newBody = each.body(); itemBlock = newBody; itemVar = each.var(); itemBlock._if(stringBuilder.invoke("length").ne(JExpr.lit(0)))._then().invoke(stringBuilder, "append").arg(" "); propertyVar = stringBuilder.invoke("toString"); JConditional nullCond = itemBlock._if(itemVar.ne(JExpr._null())); itemBlock = nullCond._then(); itemVar = toString(builder, itemVar, type); } else if (type.equals(byte[].class)) { itemVar = context.toJClass(Base64.class).staticInvoke("encode").arg(itemVar); } else if (type.equals(QName.class)) { itemVar = builder.getXSW().invoke("getQNameAsString").arg(itemVar); } else if (type.equals(DataHandler.class) || type.equals(Image.class)) {
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(); JVar adapterVar = builder.getAdapter(property.getAdapterType()); JVar xmlValueVar = block.decl(context.toJClass(String.class), builder.getReadVariableManager().createId(propertyName + "Raw"), value); block.add(new JBlankLine()); JVar valueVar = block.decl(context.toJClass(targetType), propertyName, JExpr._null()); tryBlock.body().assign(valueVar, adapterVar.invoke("unmarshal").arg(xmlValueVar)); .arg(xsrVar) .arg(context.dotclass(property.getAdapterType())) } else if (!property.isCollection() && (targetType.equals(Byte.class) || targetType.equals(byte.class))) { toSet = JExpr._null(); } else { logger.severe("Could not map element value " + propertyName + " of type " + property.getType());
if (property.getXmlStyle() == Property.XmlStyle.ATTRIBUTE) continue; builder.getWriteMethod().body().add(new JBlankLine()); builder.getWriteMethod().body().add(new JLineComment(property.getXmlStyle() + ": " + property.getName())); outerBlock.add(builder.getWriteStartElement(wrapperElement)); outerBlock = each.body(); outerVar = each.var(); block._if(firstVar.not())._then().add(builder.getXSW().invoke("writeCharacters").arg(" ")); block.assign(firstVar, JExpr.FALSE); nullCond._else().invoke(builder.getWriteContextVar(), "unexpectedNullValue").arg(builder.getWriteObject()).arg(property.getName()); JForEach each = collectionNotNull.forEach(itemType, itemName, propertyVar); JBlock newBody = each.body(); block = newBody; itemVar = each.var(); JForEach each = collectionNotNull.forEach(itemType, itemName, propertyVar); JBlock newBody = each.body(); block = newBody; itemVar = each.var();
public void fromRawValue(JBlock block, String uniqueName, JExpression $var) { JCodeModel cm = outline().getCodeModel(); JClass elementType = ei.toType(outline(),EXPOSED).boxify(); // [RESULT] // $t = new ArrayList(); // for( Type e : $var ) { // $var.add(new JAXBElement(e)); // } // [core.fromRawValue] JClass col = cm.ref(ArrayList.class).narrow(elementType); JVar $t = block.decl(col,uniqueName+"_col",JExpr._new(col)); JForEach loop = block.forEach(itemType(), uniqueName+"_i", $t); loop.body().invoke($var,"add").arg(createJAXBElement(loop.var())); acc.fromRawValue(block, uniqueName, $t); } }
.staticInvoke("status") .arg(JExpr.lit(statusCode)); builderArgument = builderArgument.invoke("header") .arg(HttpHeaders.CONTENT_TYPE) .arg(responseMimeType.getType()); builderArgument = builderArgument.invoke("header").arg(headerName).arg(JExpr.ref(argumentName)); final JVar builderVariable = responseBuilderMethodBody.decl( types.getGeneratorType(ResponseBuilder.class), "responseBuilder", builderArgument); final JClass listOfObjectsClass = types.getGeneratorClass(List.class).narrow(Object.class); final JClass headersArgument = types.getGeneratorClass(Map.class).narrow( types.getGeneratorClass(String.class), listOfObjectsClass); builderArgument = responseBuilderMethodBody.invoke("headers") .arg(JExpr.ref(MULTIPLE_RESPONSE_HEADERS_ARGUMENT_NAME)) .arg(builderVariable); responseBuilderMethodBody.invoke(builderVariable, "entity").arg( JExpr.ref(GENERIC_PAYLOAD_ARGUMENT_NAME)); responseBuilderMethod.param(types.getResponseEntityClass(responseMimeType), JBlock body = responseBuilderMethod.body().forEach(context.getGeneratorType(Types.getJavaType(header)), "h", JExpr.ref(argumentName)).body(); body.add(JExpr.invoke(JExpr.ref("responseBuilder"), "header").arg(headerName).arg(JExpr.ref("h"))); responseBuilderMethodBody._return(JExpr._new(responseClass).arg(builderVariable.invoke("build")));
List<JClass> typeParams = returnJClass.getTypeParameters(); assert typeParams.size() == 1; JClass typeParam = typeParams.get(0); JConditional cond = body._if( jvarParam.ne( JExpr._null())); JForEach forEach = cond._then() .forEach( typeParam, VALUE, JExpr.ref(VALUES)); JInvocation addInvocation = forEach.body() .invoke( JExpr.invoke(listGetterMethod), "add"); addInvocation.arg( JExpr.ref(VALUE));
listClass = listClass.narrow(valueClass); JVar localVar = block.decl(listClass, fieldName, subjectVar.invoke(getterName)); block._if(JOp.cor(localVar.eq(JExpr._null()), JOp.lt(localVar.invoke("size"), JExpr.lit(minCount)))) ._then()._throw( JExpr._new(dataWriter.getValidationExceptionClass()).arg( JExpr.lit(subjectClass.getLocalName() + " " + fieldName + " must contain at least " + minCount + value) JBlock thenBlock = block._if(localVar.ne(JExpr._null()))._then(); thenBlock.invoke(jsonVar, "writeFieldName").arg(JExpr.lit(p.getPredicate().getLocalName())); thenBlock.invoke(jsonVar, "writeStartArray"); dataWriter.writeSimpleValue(forEach.body(), jsonVar, fieldJavaType, forEach.var()); } else if (valueShape != null) { forEach.body().invoke(writerVar, "write").arg(forEach.var()).arg(outVar); } else if (p.getValueClass() instanceof URI) { JClass namespacesClass = ref(dataWriter.getJavaNamer().namespacesClass()); JVar elementIdVar = forEach.body().decl(uriClass, elementName + "Id", forEach.var().invoke("getId")); forEach.body()._if(JOp.eq(elementIdVar, JExpr._null()))._then()._throw( JExpr._new(dataWriter.getValidationExceptionClass()).arg( JExpr.lit(subjectClass.getLocalName() + " " + elementName + " must have a URI as an Id ") )); forEach.body().invoke(jsonVar, "writeString").arg(namespacesClass.staticInvoke("curie").arg(elementIdVar));
JVar fieldValue = body.decl(propertyType, fieldName).init(sourceVar.invoke(getterName)); JBlock notNull = body._if(fieldValue.ne(JExpr._null()))._then(); notNull.add(jsonVar.invoke("writeFieldName").arg(JExpr.lit(fieldName))); .add(writerClass.staticInvoke("instance").invoke("write") .arg(fieldValue).arg(jsonVar)); JClass setType = model.ref(Set.class).narrow(propertyType.wildcard()); JVar setVar = body.decl(setType, fieldName + "Set").init(sourceVar.invoke(getterName)); JBlock notNull = body._if(setVar.ne(JExpr._null()))._then(); notNull.add(jsonVar.invoke("writeArrayFieldStart").arg(JExpr.lit(fieldName))); JForEach forEach = notNull.forEach(propertyType, fieldName, setVar); JVar elemVar = forEach.var(); JBlock forEachBody = forEach.body();
@Override public JStatement marshall(EntityDescriptionGenerator.ToRawEntityHolder holder, PropertyMirror propertyMirror) { JClass listType = CodeModelTypes.ref(typeHelper.singleGenericParameter(propertyMirror.getType()).toString()); JBlock block = new JBlock(); JVar items = block.decl(CodeModelTypes.LIST.narrow(listType), "items", JExpr._new(CodeModelTypes.ARRAY_LIST.narrow(listType))); JExpression propertyItems = propertyMirror.getGetter().getValue(holder.entity); JConditional isLazyList = block._if(propertyItems._instanceof(CodeModelTypes.LAZY_ARRAY_LIST)); isLazyList._then().assign(items, JExpr.cast(CodeModelTypes.LAZY_ARRAY_LIST.narrow(listType), propertyItems)); JForEach forEachItem = isLazyList._else().forEach(listType, "item", propertyItems); forEachItem.body().add(items.invoke("add").arg(forEachItem.var())); block.add(holder.torchFactory.invoke("begin").invoke("save").invoke("entities").arg(items)); return block; }
final JClass jaxbElementClass = outline.getCodeModel().ref(JAXBElement.class); if (propertyOutline.isIndirect()) { final JClass propertyType = ((JClass)rawType).getTypeParameters().get(0); if (propertyOutline.isCollection() && !rawType.isArray()) { getMaker = new F1<JExpression, JVar>() { infoClass = propertyType.name().startsWith("?") ? IndirectCollectionPropertyInfo.class : IndirectPrimitiveCollectionPropertyInfo.class; typeArg = propertyType.name().startsWith("?") ? propertyType._extends() : propertyType; propertyWrapperClass = propertyType.name().startsWith("?") ? IndirectCollectionProperty.class : IndirectPrimitiveCollectionProperty.class; fieldType = outline.getCodeModel().ref(List.class).narrow(jaxbElementClass.narrow(propertyType)); final JClass propertyCategory = typeArg == null ? pluginContext.codeModel.ref(propertyWrapperClass).narrow(fieldOutline.parent().implClass) : pluginContext.codeModel.ref(propertyWrapperClass).narrow(fieldOutline.parent().implClass, typeArg); final boolean generatedClass = typeArg != null && isVisitable(pluginContext, typeArg); final JInvocation call = visitorParam.invoke("visit").arg(JExpr._new(propertyCategory).arg(metaClass.staticRef(staticField)).arg(JExpr._this())); if (generatedClass) { final JFieldRef field = JExpr._this().ref(fieldOutline.getPropertyInfo().getName(false)); final JConditional ifContinue = block._if(call.cand(field.ne(JExpr._null()))); if (propertyOutline.isCollection() && !rawType.isArray()) { final JForEach forEach = ifContinue._then().forEach(rawType, "_item_", field); final JConditional ifNotNull = forEach.body()._if(forEach.var().ne(JExpr._null())); ifNotNull._then().add((propertyOutline.isIndirect() ? forEach.var().invoke("getValue") : forEach.var()).invoke(this.visitMethodName).arg(visitorParam)); } else { ifContinue._then().add((propertyOutline.isIndirect() ? field.invoke("getValue") : field).invoke(this.visitMethodName).arg(visitorParam));
if (field.type().isReference()) { final JClass fieldType = (JClass) field.type(); if (this.pluginContext.collectionClass.isAssignableFrom(fieldType)) { final JClass elementType = fieldType.getTypeParameters().get(0); if (this.pluginContext.partialCopyableInterface.isAssignableFrom(elementType)) { final JForEach forLoop = this.pluginContext.loop(currentBlock, sourceField, elementType, targetField, elementType); forLoop.body().invoke(targetField, "add").arg(nullSafe(forLoop.var(), this.pluginContext.castOnDemand(elementType, treeVarGenerator.generatePartialArgs(forLoop.var().invoke(this.pluginContext.copyMethodName))))); } else if (this.pluginContext.copyableInterface.isAssignableFrom(elementType)) { final JForEach forLoop = this.pluginContext.loop(currentBlock, sourceField, elementType, targetField, elementType); forLoop.body().invoke(targetField, "add").arg(nullSafe(forLoop.var(), this.pluginContext.castOnDemand(elementType, forLoop.var().invoke(this.pluginContext.copyMethodName)))); } else if (this.pluginContext.cloneableInterface.isAssignableFrom(elementType)) { final JBlock maybeTryBlock = this.pluginContext.catchCloneNotSupported(currentBlock, elementType); final JForEach forLoop = this.pluginContext.loop(maybeTryBlock, sourceField, elementType, targetField, elementType); forLoop.body().invoke(targetField, "add").arg(nullSafe(forLoop.var(), this.pluginContext.castOnDemand(elementType, forLoop.var().invoke(this.pluginContext.cloneMethodName)))); } else { currentBlock.assign(targetField, nullSafe(sourceField, this.pluginContext.newArrayList(elementType).arg(sourceField)));
final JBlock body = cloneMethod.body(); final JVar newObjectVar; newObjectVar = body.decl(JMod.FINAL, definedClass, PluginContext.NEW_OBJECT_VAR_NAME, null); final JBlock superMaybeTryBlock = pluginContext.catchCloneNotSupported(body, definedClass._extends()); superMaybeTryBlock.assign(newObjectVar, JExpr.cast(definedClass, JExpr._super().invoke(pluginContext.cloneMethodName))); boolean cloneNotSupportedExceptionPossible = false; for (final FieldOutline fieldOutline : classOutline.getDeclaredFields()) { if (field.type().isReference()) { final JClass fieldType = (JClass) field.type(); final JFieldRef newField = JExpr.ref(newObjectVar, field); final JFieldRef fieldRef = JExpr._this().ref(field); if (pluginContext.collectionClass.isAssignableFrom(fieldType)) { final JClass elementType = fieldType.getTypeParameters().get(0); if (pluginContext.cloneableInterface.isAssignableFrom(elementType)) { final JBlock maybeTryBlock = this.cloneThrows ? body : pluginContext.catchCloneNotSupported(body, elementType); cloneNotSupportedExceptionPossible |= pluginContext.mustCatch(elementType); final JForEach forLoop = pluginContext.loop(maybeTryBlock, fieldRef, elementType, newField, elementType); forLoop.body().invoke(newField, "add").arg(nullSafe(forLoop.var(), pluginContext.castOnDemand(elementType, forLoop.var().invoke(pluginContext.cloneMethodName)))); } else { body.assign(newField, nullSafe(fieldRef, pluginContext.newArrayList(elementType).arg(fieldRef)));