use of com.squareup.javapoet.TypeSpec in project storio by pushtorefresh.
the class GetResolverGenerator method generateJavaFile.
@NotNull
public JavaFile generateJavaFile(@NotNull StorIOSQLiteTypeMeta storIOSQLiteTypeMeta) {
final ClassName storIOSQLiteTypeClassName = ClassName.get(storIOSQLiteTypeMeta.packageName, storIOSQLiteTypeMeta.simpleName);
final TypeSpec getResolver = TypeSpec.classBuilder(generateName(storIOSQLiteTypeMeta)).addJavadoc("Generated resolver for Get Operation.\n").addModifiers(PUBLIC).superclass(ParameterizedTypeName.get(ClassName.get("com.pushtorefresh.storio.sqlite.operations.get", "DefaultGetResolver"), storIOSQLiteTypeClassName)).addMethod(storIOSQLiteTypeMeta.needCreator ? createMapFromCursorWithCreatorMethodSpec(storIOSQLiteTypeMeta, storIOSQLiteTypeClassName) : createMapFromCursorMethodSpec(storIOSQLiteTypeMeta, storIOSQLiteTypeClassName)).build();
return JavaFile.builder(storIOSQLiteTypeMeta.packageName, getResolver).indent(INDENT).build();
}
use of com.squareup.javapoet.TypeSpec in project storio by pushtorefresh.
the class PutResolverGenerator method generateJavaFile.
@NotNull
public JavaFile generateJavaFile(@NotNull StorIOSQLiteTypeMeta storIOSQLiteTypeMeta) {
final ClassName storIOSQLiteTypeClassName = ClassName.get(storIOSQLiteTypeMeta.packageName, storIOSQLiteTypeMeta.simpleName);
final TypeSpec putResolver = TypeSpec.classBuilder(generateName(storIOSQLiteTypeMeta)).addJavadoc("Generated resolver for Put Operation.\n").addModifiers(PUBLIC).superclass(ParameterizedTypeName.get(ClassName.get("com.pushtorefresh.storio.sqlite.operations.put", "DefaultPutResolver"), storIOSQLiteTypeClassName)).addMethod(createMapToInsertQueryMethodSpec(storIOSQLiteTypeMeta, storIOSQLiteTypeClassName)).addMethod(createMapToUpdateQueryMethodSpec(storIOSQLiteTypeMeta, storIOSQLiteTypeClassName)).addMethod(createMapToContentValuesMethodSpec(storIOSQLiteTypeMeta, storIOSQLiteTypeClassName)).build();
return JavaFile.builder(storIOSQLiteTypeMeta.packageName, putResolver).indent(INDENT).build();
}
use of com.squareup.javapoet.TypeSpec 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.TypeSpec in project RxCache by VictorAlbertos.
the class BrewJavaFile method from.
JavaFile from(ProvidersClass providersClass) throws IOException {
List<MethodSpec> methodSpecs = new ArrayList<>();
for (ProvidersClass.Method method : providersClass.methods) {
methodSpecs.add(getActionProvider(providersClass.className, method));
}
TypeSpec typeSpec = classProviders(providersClass.className, methodSpecs);
return JavaFile.builder(providersClass.className.packageName(), typeSpec).build();
}
use of com.squareup.javapoet.TypeSpec in project ActivityRouter by mzule.
the class RouterProcessor method handleRouter.
private boolean handleRouter(String genClassName, RoundEnvironment roundEnv) {
Set<? extends Element> elements = roundEnv.getElementsAnnotatedWith(Router.class);
MethodSpec.Builder mapMethod = MethodSpec.methodBuilder("map").addModifiers(Modifier.PUBLIC, Modifier.FINAL, Modifier.STATIC).addStatement("java.util.Map<String,String> transfer = null").addStatement("com.github.mzule.activityrouter.router.ExtraTypes extraTypes").addCode("\n");
for (Element element : elements) {
Router router = element.getAnnotation(Router.class);
String[] transfer = router.transfer();
if (transfer.length > 0 && !"".equals(transfer[0])) {
mapMethod.addStatement("transfer = new java.util.HashMap<String, String>()");
for (String s : transfer) {
String[] components = s.split("=>");
if (components.length != 2) {
error("transfer `" + s + "` not match a=>b format");
break;
}
mapMethod.addStatement("transfer.put($S, $S)", components[0], components[1]);
}
} else {
mapMethod.addStatement("transfer = null");
}
mapMethod.addStatement("extraTypes = new com.github.mzule.activityrouter.router.ExtraTypes()");
mapMethod.addStatement("extraTypes.setTransfer(transfer)");
addStatement(mapMethod, int.class, router.intParams());
addStatement(mapMethod, long.class, router.longParams());
addStatement(mapMethod, boolean.class, router.booleanParams());
addStatement(mapMethod, short.class, router.shortParams());
addStatement(mapMethod, float.class, router.floatParams());
addStatement(mapMethod, double.class, router.doubleParams());
addStatement(mapMethod, byte.class, router.byteParams());
addStatement(mapMethod, char.class, router.charParams());
for (String format : router.value()) {
ClassName className;
Name methodName = null;
if (element.getKind() == ElementKind.CLASS) {
className = ClassName.get((TypeElement) element);
} else if (element.getKind() == ElementKind.METHOD) {
className = ClassName.get((TypeElement) element.getEnclosingElement());
methodName = element.getSimpleName();
} else {
throw new IllegalArgumentException("unknow type");
}
if (format.startsWith("/")) {
error("Router#value can not start with '/'. at [" + className + "]@Router(\"" + format + "\")");
return false;
}
if (format.endsWith("/")) {
error("Router#value can not end with '/'. at [" + className + "]@Router(\"" + format + "\")");
return false;
}
if (element.getKind() == ElementKind.CLASS) {
mapMethod.addStatement("com.github.mzule.activityrouter.router.Routers.map($S, $T.class, null, extraTypes)", format, className);
} else {
mapMethod.addStatement("com.github.mzule.activityrouter.router.Routers.map($S, null, " + "new MethodInvoker() {\n" + " public void invoke(android.content.Context context, android.os.Bundle bundle) {\n" + " $T.$N(context, bundle);\n" + " }\n" + "}, " + "extraTypes)", format, className, methodName);
}
}
mapMethod.addCode("\n");
}
TypeSpec routerMapping = TypeSpec.classBuilder(genClassName).addModifiers(Modifier.PUBLIC, Modifier.FINAL).addMethod(mapMethod.build()).build();
try {
JavaFile.builder("com.github.mzule.activityrouter.router", routerMapping).build().writeTo(filer);
} catch (Throwable e) {
e.printStackTrace();
}
return true;
}
Aggregations