use of com.squareup.javapoet.TypeName in project storio by pushtorefresh.
the class GetResolverGenerator method createMapFromCursorWithCreatorMethodSpec.
@NotNull
private MethodSpec createMapFromCursorWithCreatorMethodSpec(@NotNull StorIOContentResolverTypeMeta storIOContentResolverTypeMeta, @NotNull ClassName storIOSQLiteTypeClassName) {
final MethodSpec.Builder builder = MethodSpec.methodBuilder("mapFromCursor").addJavadoc("{@inheritDoc}\n").addAnnotation(Override.class).addAnnotation(ANDROID_NON_NULL_ANNOTATION_CLASS_NAME).addModifiers(PUBLIC).returns(storIOSQLiteTypeClassName).addParameter(ParameterSpec.builder(ClassName.get("android.database", "Cursor"), "cursor").addAnnotation(ANDROID_NON_NULL_ANNOTATION_CLASS_NAME).build()).addCode("\n");
final StringBuilder paramsBuilder = new StringBuilder();
paramsBuilder.append("(");
boolean first = true;
for (final StorIOContentResolverColumnMeta columnMeta : storIOContentResolverTypeMeta.getOrderedColumns()) {
final String columnIndex = "cursor.getColumnIndex(\"" + columnMeta.storIOColumn.name() + "\")";
final JavaType javaType = columnMeta.javaType;
final String getFromCursor = getFromCursorString(columnMeta, javaType, columnIndex);
final TypeName name = TypeName.get(((ExecutableElement) columnMeta.element).getReturnType());
final boolean isBoxed = javaType.isBoxedType();
if (isBoxed) {
// otherwise -> if primitive and value from cursor null -> fail early
builder.addStatement("$T $L = null", name, columnMeta.getRealElementName());
builder.beginControlFlow("if (!cursor.isNull($L))", columnIndex);
builder.addStatement("$L = cursor.$L", columnMeta.getRealElementName(), getFromCursor);
builder.endControlFlow();
} else {
builder.addStatement("$T $L = cursor.$L", name, columnMeta.getRealElementName(), getFromCursor);
}
if (!first) {
paramsBuilder.append(", ");
}
first = false;
paramsBuilder.append(columnMeta.getRealElementName());
}
paramsBuilder.append(")");
builder.addCode("\n");
if (storIOContentResolverTypeMeta.creator.getKind() == ElementKind.CONSTRUCTOR) {
builder.addStatement("$T object = new $T" + paramsBuilder.toString(), storIOSQLiteTypeClassName, storIOSQLiteTypeClassName);
} else {
builder.addStatement("$T object = $T.$L", storIOSQLiteTypeClassName, storIOSQLiteTypeClassName, storIOContentResolverTypeMeta.creator.getSimpleName() + paramsBuilder.toString());
}
return builder.addCode("\n").addStatement("return object").build();
}
use of com.squareup.javapoet.TypeName in project storio by pushtorefresh.
the class GetResolverGenerator method createMapFromCursorWithCreatorMethodSpec.
@NotNull
private MethodSpec createMapFromCursorWithCreatorMethodSpec(@NotNull StorIOSQLiteTypeMeta storIOSQLiteTypeMeta, @NotNull ClassName storIOSQLiteTypeClassName) {
final MethodSpec.Builder builder = MethodSpec.methodBuilder("mapFromCursor").addJavadoc("{@inheritDoc}\n").addAnnotation(Override.class).addAnnotation(ANDROID_NON_NULL_ANNOTATION_CLASS_NAME).addModifiers(PUBLIC).returns(storIOSQLiteTypeClassName).addParameter(ParameterSpec.builder(ClassName.get("android.database", "Cursor"), "cursor").addAnnotation(ANDROID_NON_NULL_ANNOTATION_CLASS_NAME).build()).addCode("\n");
final StringBuilder paramsBuilder = new StringBuilder();
paramsBuilder.append("(");
boolean first = true;
for (final StorIOSQLiteColumnMeta columnMeta : storIOSQLiteTypeMeta.getOrderedColumns()) {
final String columnIndex = "cursor.getColumnIndex(\"" + columnMeta.storIOColumn.name() + "\")";
final JavaType javaType = columnMeta.javaType;
final String getFromCursor = getFromCursorString(columnMeta, javaType, columnIndex);
final TypeName name = TypeName.get(((ExecutableElement) columnMeta.element).getReturnType());
final boolean isBoxed = javaType.isBoxedType();
if (isBoxed) {
// otherwise -> if primitive and value from cursor null -> fail early
builder.addStatement("$T $L = null", name, columnMeta.getRealElementName());
builder.beginControlFlow("if (!cursor.isNull($L))", columnIndex);
builder.addStatement("$L = cursor.$L", columnMeta.getRealElementName(), getFromCursor);
builder.endControlFlow();
} else {
builder.addStatement("$T $L = cursor.$L", name, columnMeta.getRealElementName(), getFromCursor);
}
if (!first) {
paramsBuilder.append(", ");
}
first = false;
paramsBuilder.append(columnMeta.getRealElementName());
}
paramsBuilder.append(")");
builder.addCode("\n");
if (storIOSQLiteTypeMeta.creator.getKind() == ElementKind.CONSTRUCTOR) {
builder.addStatement("$T object = new $T" + paramsBuilder.toString(), storIOSQLiteTypeClassName, storIOSQLiteTypeClassName);
} else {
builder.addStatement("$T object = $T.$L", storIOSQLiteTypeClassName, storIOSQLiteTypeClassName, storIOSQLiteTypeMeta.creator.getSimpleName() + paramsBuilder.toString());
}
return builder.addCode("\n").addStatement("return object").build();
}
use of com.squareup.javapoet.TypeName in project tiger by google.
the class Utils method getElementKeyForBuiltinBinding.
/**
* Return {@link NewBindingKey} for element of the give {@link NewBindingKey} that
* has built-in binding, null if not built-in building.
*/
@Nullable
public static NewBindingKey getElementKeyForBuiltinBinding(NewBindingKey key) {
if (!hasBuiltinBinding(key)) {
return null;
}
ParameterizedTypeName parameterizedTypeName = (ParameterizedTypeName) key.getTypeName();
TypeName typeName = Iterables.getOnlyElement(parameterizedTypeName.typeArguments);
AnnotationSpec qualifier = key.getQualifier();
return NewBindingKey.get(typeName, qualifier);
}
use of com.squareup.javapoet.TypeName in project wire by square.
the class JavaGenerator method messageFieldsAndUnknownFieldsConstructor.
// Example:
//
// public SimpleMessage(int optional_int32, long optional_int64, ByteString unknownFields) {
// super(ADAPTER, unknownFields);
// this.optional_int32 = optional_int32;
// this.optional_int64 = optional_int64;
// }
//
private MethodSpec messageFieldsAndUnknownFieldsConstructor(NameAllocator nameAllocator, MessageType type) {
NameAllocator localNameAllocator = nameAllocator.clone();
String adapterName = localNameAllocator.get("ADAPTER");
String unknownFieldsName = localNameAllocator.newName("unknownFields");
MethodSpec.Builder result = MethodSpec.constructorBuilder().addModifiers(PUBLIC).addStatement("super($N, $N)", adapterName, unknownFieldsName);
for (OneOf oneOf : type.oneOfs()) {
if (oneOf.fields().size() < 2)
continue;
CodeBlock.Builder fieldNamesBuilder = CodeBlock.builder();
boolean first = true;
for (Field field : oneOf.fields()) {
if (!first)
fieldNamesBuilder.add(", ");
fieldNamesBuilder.add("$N", localNameAllocator.get(field));
first = false;
}
CodeBlock fieldNames = fieldNamesBuilder.build();
result.beginControlFlow("if ($T.countNonNull($L) > 1)", Internal.class, fieldNames);
result.addStatement("throw new IllegalArgumentException($S)", "at most one of " + fieldNames + " may be non-null");
result.endControlFlow();
}
for (Field field : type.fieldsAndOneOfFields()) {
TypeName javaType = fieldType(field);
String fieldName = localNameAllocator.get(field);
ParameterSpec.Builder param = ParameterSpec.builder(javaType, fieldName);
if (emitAndroid && field.isOptional()) {
param.addAnnotation(NULLABLE);
}
result.addParameter(param.build());
if (field.isRepeated() || field.type().isMap()) {
result.addStatement("this.$1L = $2T.immutableCopyOf($1S, $1L)", fieldName, Internal.class);
} else {
result.addStatement("this.$1L = $1L", fieldName);
}
}
result.addParameter(BYTE_STRING, unknownFieldsName);
return result.build();
}
use of com.squareup.javapoet.TypeName in project react4j by react4j.
the class Generator method buildCallbackBuilderMethod.
@Nonnull
private static MethodSpec.Builder buildCallbackBuilderMethod(@Nonnull final ComponentDescriptor descriptor, @Nonnull final CallbackDescriptor callback) {
final TypeName handlerType = TypeName.get(callback.getCallbackType().asType());
final MethodSpec.Builder method = MethodSpec.methodBuilder("create_" + callback.getMethod().getSimpleName()).addModifiers(Modifier.PRIVATE).addAnnotation(NONNULL_CLASSNAME).returns(handlerType);
final ExecutableElement target = callback.getCallbackMethod();
final int targetParameterCount = target.getParameters().size();
String args = 0 == targetParameterCount ? "()" : IntStream.range(0, targetParameterCount).mapToObj(i -> "arg" + i).collect(Collectors.joining(","));
if (1 < targetParameterCount) {
args = "(" + args + ")";
}
final int paramCount = callback.getMethod().getParameters().size();
final String params = 0 == paramCount ? "" : IntStream.range(0, paramCount).mapToObj(i -> "arg" + i).collect(Collectors.joining(","));
method.addStatement("final $T handler = " + args + " -> this.$N(" + params + ")", handlerType, callback.getMethod().getSimpleName());
final CodeBlock.Builder block = CodeBlock.builder();
block.beginControlFlow("if( $T.enableComponentNames() )", REACT_CONFIG_CLASSNAME);
final String code = "$T.defineProperty( $T.cast( handler ), \"name\", $T.cast( $T.of( \"value\", $S ) ) )";
block.addStatement(code, JS_OBJECT_CLASSNAME, JS_CLASSNAME, JS_CLASSNAME, JS_PROPERTY_MAP_CLASSNAME, descriptor.getName() + "." + callback.getName());
block.endControlFlow();
method.addCode(block.build());
method.addStatement("return handler");
return method;
}
Aggregations