use of com.squareup.javapoet.ParameterizedTypeName in project kripton by xcesco.
the class MapBindTransformation method generateParseOnXml.
@Override
public void generateParseOnXml(BindTypeContext context, MethodSpec.Builder methodBuilder, String parserName, TypeName beanClass, String beanName, BindProperty property) {
// define key and value type
ParameterizedTypeName mapTypeName = (ParameterizedTypeName) property.getPropertyType().getTypeName();
TypeName keyTypeName = mapTypeName.typeArguments.get(0);
TypeName valueTypeName = mapTypeName.typeArguments.get(1);
// @formatter:off
methodBuilder.beginControlFlow("");
methodBuilder.addStatement("$T<$T, $T> collection=new $T<>()", defineMapClass(mapTypeName), keyTypeName, valueTypeName, defineMapClass(mapTypeName));
BindTransform transformKey = BindTransformer.lookup(keyTypeName);
BindProperty elementKeyProperty = BindProperty.builder(keyTypeName, property).inCollection(true).xmlType(property.xmlInfo.mapEntryType.toXmlType()).elementName(property.mapKeyName).build();
BindTransform transformValue = BindTransformer.lookup(valueTypeName);
BindProperty elementValueProperty = BindProperty.builder(valueTypeName, property).xmlType(property.xmlInfo.mapEntryType.toXmlType()).elementName(property.mapValueName).build();
methodBuilder.addStatement("$T key", elementKeyProperty.getPropertyType().getTypeName());
methodBuilder.addStatement("$T value", elementValueProperty.getPropertyType().getTypeName());
if (property.xmlInfo.mapEntryType == MapEntryType.ATTRIBUTE) {
methodBuilder.addStatement("int attributeIndex");
}
if (property.xmlInfo.isWrappedCollection()) {
methodBuilder.beginControlFlow("while ($L.nextTag() != $T.END_TAG && $L.getName().toString().equals($S))", parserName, XmlPullParser.class, parserName, property.xmlInfo.labelItem);
} else {
methodBuilder.addCode("// add first element\n");
switch(property.xmlInfo.mapEntryType) {
case TAG:
methodBuilder.addStatement("$L.nextTag()", parserName);
transformKey.generateParseOnXml(context, methodBuilder, parserName, null, "key", elementKeyProperty);
methodBuilder.addStatement("$L.nextTag()", parserName);
methodBuilder.beginControlFlow("if ($L.isEmptyElement())", parserName);
methodBuilder.addStatement("value=null");
methodBuilder.addStatement("$L.nextTag()", parserName);
methodBuilder.nextControlFlow("else");
transformValue.generateParseOnXml(context, methodBuilder, parserName, null, "value", elementValueProperty);
methodBuilder.endControlFlow();
methodBuilder.addStatement("$L.nextTag()", parserName);
break;
case ATTRIBUTE:
methodBuilder.addStatement("attributeIndex=$L.getAttributeIndex(null, $S)", parserName, property.mapKeyName);
transformKey.generateParseOnXml(context, methodBuilder, parserName, null, "key", elementKeyProperty);
methodBuilder.addStatement("attributeIndex=$L.getAttributeIndex(null, $S)", parserName, property.mapValueName);
transformValue.generateParseOnXml(context, methodBuilder, parserName, null, "value", elementValueProperty);
break;
}
methodBuilder.addStatement("collection.put(key, value)");
methodBuilder.beginControlFlow("while ($L.nextTag() != $T.END_TAG && $L.getName().toString().equals($S))", parserName, XmlPullParser.class, parserName, property.xmlInfo.labelItem);
}
switch(property.xmlInfo.mapEntryType) {
case TAG:
methodBuilder.addStatement("$L.nextTag()", parserName);
transformKey.generateParseOnXml(context, methodBuilder, parserName, null, "key", elementKeyProperty);
methodBuilder.addStatement("$L.nextTag()", parserName);
methodBuilder.beginControlFlow("if ($L.isEmptyElement())", parserName);
methodBuilder.addStatement("value=null");
methodBuilder.addStatement("$L.nextTag()", parserName);
methodBuilder.nextControlFlow("else");
transformValue.generateParseOnXml(context, methodBuilder, parserName, null, "value", elementValueProperty);
methodBuilder.endControlFlow();
methodBuilder.addStatement("$L.nextTag()", parserName);
break;
case ATTRIBUTE:
methodBuilder.addStatement("attributeIndex=$L.getAttributeIndex(null, $S)", parserName, property.mapKeyName);
transformKey.generateParseOnXml(context, methodBuilder, parserName, null, "key", elementKeyProperty);
methodBuilder.addStatement("attributeIndex=$L.getAttributeIndex(null, $S)", parserName, property.mapValueName);
transformValue.generateParseOnXml(context, methodBuilder, parserName, null, "value", elementValueProperty);
break;
}
methodBuilder.addStatement("collection.put(key, value)");
methodBuilder.endControlFlow();
methodBuilder.addStatement(setter(beanClass, beanName, property, "collection"));
if (!property.xmlInfo.isWrappedCollection()) {
methodBuilder.addStatement("read=false");
}
methodBuilder.endControlFlow();
// @formatter:on
}
use of com.squareup.javapoet.ParameterizedTypeName in project kripton by xcesco.
the class MapBindTransformation method generateParseOnJacksonInternal.
public void generateParseOnJacksonInternal(BindTypeContext context, Builder methodBuilder, String parserName, TypeName beanClass, String beanName, BindProperty property, boolean onString) {
// define key and value type
ParameterizedTypeName mapTypeName = (ParameterizedTypeName) property.getPropertyType().getTypeName();
TypeName keyTypeName = mapTypeName.typeArguments.get(0);
TypeName valueTypeName = mapTypeName.typeArguments.get(1);
// @formatter:off
methodBuilder.beginControlFlow("if ($L.currentToken()==$T.START_ARRAY)", parserName, JsonToken.class);
methodBuilder.addStatement("$T<$T, $T> collection=new $T<>()", defineMapClass(mapTypeName), keyTypeName, valueTypeName, defineMapClass(mapTypeName));
BindTransform transformKey = BindTransformer.lookup(keyTypeName);
BindProperty elementKeyProperty = BindProperty.builder(keyTypeName, property).inCollection(false).xmlType(property.xmlInfo.mapEntryType.toXmlType()).elementName(property.mapKeyName).nullable(false).build();
BindTransform transformValue = BindTransformer.lookup(valueTypeName);
BindProperty elementValueProperty = BindProperty.builder(valueTypeName, property).inCollection(false).xmlType(property.xmlInfo.mapEntryType.toXmlType()).elementName(property.mapValueName).nullable(true).build();
methodBuilder.addStatement("$T key=$L", elementKeyProperty.getPropertyType().getTypeName(), DEFAULT_VALUE);
methodBuilder.addStatement("$T value=$L", elementValueProperty.getPropertyType().getTypeName(), DEFAULT_VALUE);
if (onString) {
methodBuilder.addStatement("$T current", JsonToken.class);
methodBuilder.addStatement("String tempValue=null");
}
methodBuilder.beginControlFlow("while ($L.nextToken() != $T.END_ARRAY)", parserName, JsonToken.class);
if (onString) {
// on string
methodBuilder.addStatement("current=$L.currentToken()", parserName);
methodBuilder.beginControlFlow("for (int i=0; i<2 ;i++)");
methodBuilder.beginControlFlow("while (current != $T.FIELD_NAME)", JsonToken.class);
methodBuilder.addStatement("current=$L.nextToken()", parserName);
methodBuilder.endControlFlow();
methodBuilder.addStatement("$L.nextValue()", parserName);
//
methodBuilder.addCode("switch(jacksonParser.getCurrentName()) {\n");
methodBuilder.addCode("case $S:$>\n", property.mapKeyName);
transformKey.generateParseOnJacksonAsString(context, methodBuilder, parserName, null, "key", elementKeyProperty);
methodBuilder.addStatement("$<break");
methodBuilder.addCode("case $S:$>\n", property.mapValueName);
methodBuilder.addStatement("tempValue=$L.getValueAsString()", parserName);
methodBuilder.beginControlFlow("if ($L.currentToken()==$T.VALUE_STRING && \"null\".equals(tempValue))", parserName, JsonToken.class);
methodBuilder.addStatement("value=$L", DEFAULT_VALUE);
methodBuilder.nextControlFlow("else");
transformValue.generateParseOnJacksonAsString(context, methodBuilder, parserName, null, "value", elementValueProperty);
methodBuilder.endControlFlow();
methodBuilder.addStatement("$<break");
methodBuilder.addCode("}\n");
//
methodBuilder.endControlFlow();
} else {
// key
methodBuilder.addStatement("$L.nextValue()", parserName);
transformKey.generateParseOnJackson(context, methodBuilder, parserName, null, "key", elementKeyProperty);
// value
methodBuilder.addStatement("$L.nextValue()", parserName);
transformValue.generateParseOnJackson(context, methodBuilder, parserName, null, "value", elementValueProperty);
}
methodBuilder.addStatement("collection.put(key, value)");
methodBuilder.addStatement("key=$L", DEFAULT_VALUE);
methodBuilder.addStatement("value=$L", DEFAULT_VALUE);
methodBuilder.addStatement("$L.nextToken()", parserName);
methodBuilder.endControlFlow();
methodBuilder.addStatement(setter(beanClass, beanName, property, "collection"));
methodBuilder.endControlFlow();
// @formatter:on
}
use of com.squareup.javapoet.ParameterizedTypeName in project kripton by xcesco.
the class MapBindTransformation method generateSerializeOnXml.
@Override
public void generateSerializeOnXml(BindTypeContext context, MethodSpec.Builder methodBuilder, String serializerName, TypeName beanClass, String beanName, BindProperty property) {
// define key and value type
ParameterizedTypeName mapTypeName = (ParameterizedTypeName) property.getPropertyType().getTypeName();
TypeName keyTypeName = mapTypeName.typeArguments.get(0);
TypeName valueTypeName = mapTypeName.typeArguments.get(1);
// @formatter:off
methodBuilder.beginControlFlow("if ($L!=null) ", getter(beanName, beanClass, property));
if (property.xmlInfo.isWrappedCollection()) {
methodBuilder.addCode("// write wrapper tag\n");
methodBuilder.addStatement("$L.writeStartElement($S)", serializerName, property.label);
}
BindTransform transformKey = BindTransformer.lookup(keyTypeName);
// key can not be null
// not in collection, it's included in an element
BindProperty elementKeyProperty = BindProperty.builder(keyTypeName, property).inCollection(false).nullable(false).xmlType(property.xmlInfo.mapEntryType.toXmlType()).elementName(property.mapKeyName).build();
BindTransform transformValue = BindTransformer.lookup(valueTypeName);
// not in collection, it's included in an element
BindProperty elementValueProperty = BindProperty.builder(valueTypeName, property).inCollection(false).xmlType(property.xmlInfo.mapEntryType.toXmlType()).elementName(property.mapValueName).build();
methodBuilder.beginControlFlow("for ($T<$T, $T> item: $L.entrySet())", Entry.class, keyTypeName, valueTypeName, getter(beanName, beanClass, property));
methodBuilder.addStatement("$L.writeStartElement($S)$>", serializerName, property.xmlInfo.labelItem);
transformKey.generateSerializeOnXml(context, methodBuilder, serializerName, null, "item.getKey()", elementKeyProperty);
if (elementValueProperty.isNullable()) {
methodBuilder.beginControlFlow("if (item.getValue()==null)");
methodBuilder.addStatement("$L.writeEmptyElement($S)", serializerName, property.mapValueName);
methodBuilder.nextControlFlow("else");
transformValue.generateSerializeOnXml(context, methodBuilder, serializerName, null, "item.getValue()", elementValueProperty);
methodBuilder.endControlFlow();
} else {
transformValue.generateSerializeOnXml(context, methodBuilder, serializerName, null, "item.getValue()", elementValueProperty);
}
methodBuilder.addStatement("$<$L.writeEndElement()", serializerName);
methodBuilder.endControlFlow();
if (property.xmlInfo.isWrappedCollection()) {
methodBuilder.addStatement("$L.writeEndElement()", serializerName);
}
methodBuilder.endControlFlow();
// @formatter:on
}
use of com.squareup.javapoet.ParameterizedTypeName in project kripton by xcesco.
the class BindDataSourceBuilder method generatExecuteBatchRx.
public void generatExecuteBatchRx(ClassName dataSourceName, String daoFactory, RxType rxType) {
String parameterName = "batch";
// @formatter:off
ParameterizedTypeName returnTypeName = ParameterizedTypeName.get(ClassName.get(rxType.clazz), TypeVariableName.get("T"));
ParameterizedTypeName observableTypeName = ParameterizedTypeName.get(TypeUtility.className(rxType.clazz.getPackage().getName(), rxType.clazz.getSimpleName() + "OnSubscribe"), TypeVariableName.get("T"));
ParameterizedTypeName emitterTypeName = ParameterizedTypeName.get(TypeUtility.className(rxType.clazz.getPackage().getName(), rxType.clazz.getSimpleName() + "Emitter"), TypeVariableName.get("T"));
TypeSpec innerEmitter = TypeSpec.anonymousClassBuilder("").addSuperinterface(observableTypeName).addMethod(MethodSpec.methodBuilder("subscribe").addAnnotation(Override.class).addModifiers(Modifier.PUBLIC).addParameter(emitterTypeName, "emitter").returns(Void.TYPE).addStatement("boolean needToOpened=writeMode?!$L.this.isOpenInWriteMode(): !$L.this.isOpen()", dataSourceName.simpleName(), dataSourceName.simpleName()).addCode("if (needToOpened) { if (writeMode) { openWritableDatabase(); } else { openReadOnlyDatabase(); }}\n", SQLiteDatabase.class).addStatement("$L currentDaoFactory=new DataSourceSingleThread()", DATA_SOURCE_SINGLE_THREAD_NAME).addStatement("currentDaoFactory.onSessionOpened()").beginControlFlow("try").addCode("if ($L != null) { $L.onExecute(currentDaoFactory, emitter); }\n", parameterName, parameterName).addStatement(rxType.onComplete ? "emitter.onComplete()" : "// no onComplete").nextControlFlow("catch($T e)", Throwable.class).addStatement("$T.error(e.getMessage())", Logger.class).addStatement("e.printStackTrace()").addStatement("emitter.onError(e)").nextControlFlow("finally").addCode("if (needToOpened) { close(); }\n").addStatement("currentDaoFactory.onSessionClosed()").endControlFlow().addStatement("return").build()).build();
{
MethodSpec.Builder executeMethod = MethodSpec.methodBuilder("executeBatch").addModifiers(Modifier.PUBLIC).addTypeVariable(TypeVariableName.get("T")).addParameter(ParameterizedTypeName.get(TypeUtility.className(dataSourceName.toString(), rxType.clazz.getSimpleName() + "Batch"), TypeVariableName.get("T")), parameterName, Modifier.FINAL).addParameter(TypeName.BOOLEAN, "writeMode", Modifier.FINAL).returns(returnTypeName);
executeMethod.addStatement("$T emitter=$L", observableTypeName, innerEmitter);
if (rxType == RxType.FLOWABLE) {
executeMethod.addStatement("$T result=$T.create(emitter, $T.BUFFER)", returnTypeName, rxType.clazz, BackpressureStrategy.class);
} else {
executeMethod.addStatement("$T result=$T.create(emitter)", returnTypeName, rxType.clazz);
}
executeMethod.addStatement("if (globalSubscribeOn!=null) result.subscribeOn(globalSubscribeOn)");
executeMethod.addStatement("if (globalObserveOn!=null) result.observeOn(globalObserveOn)");
executeMethod.addStatement("return result");
classBuilder.addMethod(executeMethod.build());
}
{
MethodSpec.Builder executeMethod = MethodSpec.methodBuilder("executeBatch").addModifiers(Modifier.PUBLIC).addTypeVariable(TypeVariableName.get("T")).addParameter(ParameterizedTypeName.get(TypeUtility.className(dataSourceName.toString(), rxType.clazz.getSimpleName() + "Batch"), TypeVariableName.get("T")), parameterName, Modifier.FINAL).returns(returnTypeName);
executeMethod.addStatement("return executeBatch($L, false)", parameterName);
classBuilder.addMethod(executeMethod.build());
}
// @formatter:on
}
use of com.squareup.javapoet.ParameterizedTypeName in project kripton by xcesco.
the class SelectBeanListenerHelper method generateSpecializedPart.
/*
* (non-Javadoc)
*
* @see com.abubusoft.kripton.processor.sqlite.SQLiteSelectBuilder.SelectCodeGenerator#generate(com.squareup.javapoet.MethodSpec.Builder)
*/
@Override
public void generateSpecializedPart(SQLiteModelMethod method, TypeSpec.Builder classBuilder, MethodSpec.Builder methodBuilder, Set<JQLProjection> fieldList, boolean mapFields) {
SQLiteDaoDefinition daoDefinition = method.getParent();
SQLiteEntity entity = daoDefinition.getEntity();
// LiteralType listenerType = LiteralType.of(OnReadBeanListener.class, entity.getElement());
ParameterizedTypeName listenerType = ParameterizedTypeName.get(ClassName.get(OnReadBeanListener.class), TypeName.get(entity.getElement().asType()));
// List<SQLProperty> fields = fieldList.value1;
TypeName entityClass = typeName(entity.getElement());
int counter = SqlBuilderHelper.countParameterOfType(method, listenerType);
if (counter == 0) {
// non listener found
throw (new InvalidMethodSignException(method, "there is no parameter of type \"ReadCursorListener\""));
}
if (counter > 1) {
// more than one listener found
throw (new InvalidMethodSignException(method, "there are more than one parameter of type \"ReadCursorListener\""));
}
String listenerName = SqlSelectBuilder.getNameParameterOfType(method, listenerType);
methodBuilder.addCode("$T resultBean=new $T();", entityClass, entityClass);
methodBuilder.addCode("\n");
// methodBuilder.beginControlFlow("try");
methodBuilder.beginControlFlow("if (_cursor.moveToFirst())");
// generate index from columns
methodBuilder.addCode("\n");
{
int i = 0;
for (JQLProjection a : fieldList) {
SQLProperty item = a.property;
methodBuilder.addStatement("int index$L=_cursor.getColumnIndex($S)", (i++), item.columnName);
if (item.hasTypeAdapter()) {
methodBuilder.addStatement("$T $LAdapter=$T.getAdapter($T.class)", item.typeAdapter.getAdapterTypeName(), item.getName(), SQLTypeAdapterUtils.class, item.typeAdapter.getAdapterTypeName());
}
}
}
methodBuilder.addCode("\n");
methodBuilder.addCode("int rowCount=_cursor.getCount();\n");
methodBuilder.beginControlFlow("do\n");
// reset mapping
methodBuilder.addCode("// reset mapping\n");
{
int i = 0;
for (SQLProperty item : entity.getCollection()) {
if (item.isNullable()) {
SQLTransformer.resetBean(methodBuilder, entityClass, "resultBean", item, "_cursor", "index" + i + "");
methodBuilder.addCode(";");
methodBuilder.addCode("\n");
} else {
methodBuilder.addCode("// " + item.getName() + " does not need reset\n");
}
i++;
}
}
methodBuilder.addCode("\n");
// generate mapping
methodBuilder.addCode("// generate mapping\n");
{
int i = 0;
for (JQLProjection a : fieldList) {
SQLProperty item = a.property;
if (item.isNullable()) {
methodBuilder.addCode("if (!_cursor.isNull(index$L)) { ", i);
}
SQLTransformer.cursor2Java(methodBuilder, typeName(entity.getElement()), item, "resultBean", "_cursor", "index" + i + "");
methodBuilder.addCode(";");
if (item.isNullable()) {
methodBuilder.addCode(" }");
}
methodBuilder.addCode("\n");
i++;
}
}
methodBuilder.addCode("\n");
methodBuilder.addCode("$L.onRead(resultBean, _cursor.getPosition(), rowCount);\n", listenerName);
methodBuilder.endControlFlow("while (_cursor.moveToNext())");
// close try { open cursor
methodBuilder.endControlFlow();
// close method
methodBuilder.endControlFlow();
}
Aggregations