use of com.squareup.javapoet.MethodSpec in project glide by bumptech.
the class RootModuleGenerator method generateRegisterComponents.
private static MethodSpec generateRegisterComponents(Set<String> childGlideModuleClassNames, Set<String> excludedGlideModuleClassNames) {
MethodSpec.Builder registerComponents = MethodSpec.methodBuilder("registerComponents").addModifiers(Modifier.PUBLIC).addAnnotation(Override.class).addParameter(ClassName.get("android.content", "Context"), "context").addParameter(ClassName.get("com.bumptech.glide", "Registry"), "registry");
for (String glideModule : childGlideModuleClassNames) {
if (excludedGlideModuleClassNames.contains(glideModule)) {
continue;
}
ClassName moduleClassName = ClassName.bestGuess(glideModule);
registerComponents.addStatement("new $T().registerComponents(context, registry)", moduleClassName);
}
// Order matters here. The RootGlideModule must be called last.
registerComponents.addStatement("rootGlideModule.registerComponents(context, registry)");
return registerComponents.build();
}
use of com.squareup.javapoet.MethodSpec in project DeepLinkDispatch by airbnb.
the class DeepLinkProcessor method generateDeepLinkLoader.
private void generateDeepLinkLoader(String packageName, String className, List<DeepLinkAnnotatedElement> elements) throws IOException {
CodeBlock.Builder initializer = CodeBlock.builder().add("$T.unmodifiableList($T.asList(\n", CLASS_COLLECTIONS, CLASS_ARRAYS).indent();
int totalElements = elements.size();
for (int i = 0; i < totalElements; i++) {
DeepLinkAnnotatedElement element = elements.get(i);
String type = "DeepLinkEntry.Type." + element.getAnnotationType().toString();
ClassName activity = ClassName.get(element.getAnnotatedElement());
Object method = element.getMethod() == null ? null : element.getMethod();
String uri = element.getUri();
initializer.add("new DeepLinkEntry($S, $L, $T.class, $S)$L\n", uri, type, activity, method, (i < totalElements - 1) ? "," : "");
}
FieldSpec registry = FieldSpec.builder(ParameterizedTypeName.get(List.class, DeepLinkEntry.class), "REGISTRY", Modifier.PUBLIC, Modifier.FINAL, Modifier.STATIC).initializer(initializer.unindent().add("))").build()).build();
MethodSpec parseMethod = MethodSpec.methodBuilder("parseUri").addModifiers(Modifier.PUBLIC).addAnnotation(AnnotationSpec.builder(Override.class).build()).addParameter(String.class, "uri").returns(DeepLinkEntry.class).beginControlFlow("for (DeepLinkEntry entry : REGISTRY)").beginControlFlow("if (entry.matches(uri))").addStatement("return entry").endControlFlow().endControlFlow().addStatement("return null").build();
TypeSpec deepLinkLoader = TypeSpec.classBuilder(className + "Loader").addModifiers(Modifier.PUBLIC, Modifier.FINAL).addSuperinterface(ClassName.get(Parser.class)).addField(registry).addMethod(parseMethod).build();
JavaFile.builder(packageName, deepLinkLoader).build().writeTo(filer);
}
use of com.squareup.javapoet.MethodSpec in project epoxy by airbnb.
the class EpoxyProcessor method addDefaultLayoutMethodIfNeeded.
/**
* If there is no existing implementation of getDefaultLayout we can generate an implementation.
* This relies on a layout res being set in the @EpoxyModelClass annotation.
*/
private void addDefaultLayoutMethodIfNeeded(TypeElement originalClassElement, List<MethodSpec> methods) {
MethodSpec getDefaultLayoutMethod = MethodSpec.methodBuilder(GET_DEFAULT_LAYOUT_METHOD_NAME).addAnnotation(Override.class).addAnnotation(LayoutRes.class).addModifiers(Modifier.PROTECTED).returns(TypeName.INT).build();
if (implementsMethod(originalClassElement, getDefaultLayoutMethod, typeUtils)) {
return;
}
EpoxyModelClass annotation = findClassAnnotationWithLayout(originalClassElement);
if (annotation == null) {
logError("Model must use %s annotation if it does not implement %s. (class: %s)", EpoxyModelClass.class, GET_DEFAULT_LAYOUT_METHOD_NAME, originalClassElement.getSimpleName());
return;
}
int layoutRes;
try {
layoutRes = annotation.layout();
} catch (AnnotationTypeMismatchException e) {
logError("Invalid layout value in %s annotation. (class: %s). %s: %s", EpoxyModelClass.class, originalClassElement.getSimpleName(), e.getClass().getSimpleName(), e.getMessage());
return;
}
if (layoutRes == 0) {
logError("Model must specify a valid layout resource in the %s annotation. (class: %s)", EpoxyModelClass.class, originalClassElement.getSimpleName());
return;
}
AndroidResource layoutResource = resourceProcessor.getResourceForValue(layoutRes);
getDefaultLayoutMethod = getDefaultLayoutMethod.toBuilder().addStatement("return $L", layoutResource.code).build();
methods.add(getDefaultLayoutMethod);
}
use of com.squareup.javapoet.MethodSpec in project storio by pushtorefresh.
the class GetResolverGenerator method createMapFromCursorMethodSpec.
@NotNull
private MethodSpec createMapFromCursorMethodSpec(@NotNull StorIOContentResolverTypeMeta storIOContentResolverTypeMeta, @NotNull ClassName storIOContentResolverTypeClassName) {
final MethodSpec.Builder builder = MethodSpec.methodBuilder("mapFromCursor").addJavadoc("{@inheritDoc}\n").addAnnotation(Override.class).addAnnotation(ANDROID_NON_NULL_ANNOTATION_CLASS_NAME).addModifiers(PUBLIC).returns(storIOContentResolverTypeClassName).addParameter(ParameterSpec.builder(ClassName.get("android.database", "Cursor"), "cursor").addAnnotation(ANDROID_NON_NULL_ANNOTATION_CLASS_NAME).build()).addStatement("$T object = new $T()", storIOContentResolverTypeClassName, storIOContentResolverTypeClassName).addCode("\n");
for (final StorIOContentResolverColumnMeta columnMeta : storIOContentResolverTypeMeta.columns.values()) {
final String columnIndex = "cursor.getColumnIndex(\"" + columnMeta.storIOColumn.name() + "\")";
final JavaType javaType = columnMeta.javaType;
final String getFromCursor = getFromCursorString(columnMeta, javaType, columnIndex);
final boolean isBoxed = javaType.isBoxedType();
if (isBoxed) {
// otherwise -> if primitive and value from cursor null -> fail early
builder.beginControlFlow("if (!cursor.isNull($L))", columnIndex);
}
builder.addStatement("object.$L = cursor.$L", columnMeta.elementName, getFromCursor);
if (isBoxed) {
builder.endControlFlow();
}
}
return builder.addCode("\n").addStatement("return object").build();
}
use of com.squareup.javapoet.MethodSpec in project storio by pushtorefresh.
the class PutResolverGenerator method createMapToContentValuesMethodSpec.
@NotNull
private MethodSpec createMapToContentValuesMethodSpec(@NotNull final StorIOContentResolverTypeMeta storIOContentResolverTypeMeta, @NotNull final ClassName storIOContentResolverClassName) {
MethodSpec.Builder builder = MethodSpec.methodBuilder("mapToContentValues").addJavadoc("{@inheritDoc}\n").addAnnotation(Override.class).addAnnotation(ANDROID_NON_NULL_ANNOTATION_CLASS_NAME).addModifiers(PUBLIC).returns(ClassName.get("android.content", "ContentValues")).addParameter(ParameterSpec.builder(storIOContentResolverClassName, "object").addAnnotation(ANDROID_NON_NULL_ANNOTATION_CLASS_NAME).build()).addStatement("ContentValues contentValues = new ContentValues($L)", storIOContentResolverTypeMeta.columns.size()).addCode("\n");
for (final StorIOContentResolverColumnMeta columnMeta : storIOContentResolverTypeMeta.columns.values()) {
final boolean ignoreNull = columnMeta.storIOColumn.ignoreNull();
if (ignoreNull) {
builder.beginControlFlow("if (object.$L != null)", columnMeta.elementName + (columnMeta.isMethod() ? "()" : ""));
}
builder.addStatement("contentValues.put($S, object.$L)", columnMeta.storIOColumn.name(), columnMeta.elementName + (columnMeta.isMethod() ? "()" : ""));
if (ignoreNull) {
builder.endControlFlow();
}
}
return builder.addCode("\n").addStatement("return contentValues").build();
}
Aggregations