Search in sources :

Example 31 with TypeSpec

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();
}
Also used : ClassName(com.squareup.javapoet.ClassName) TypeSpec(com.squareup.javapoet.TypeSpec) NotNull(org.jetbrains.annotations.NotNull)

Example 32 with TypeSpec

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();
}
Also used : ClassName(com.squareup.javapoet.ClassName) TypeSpec(com.squareup.javapoet.TypeSpec) NotNull(org.jetbrains.annotations.NotNull)

Example 33 with TypeSpec

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);
}
Also used : MethodSpec(com.squareup.javapoet.MethodSpec) CodeBlock(com.squareup.javapoet.CodeBlock) FieldSpec(com.squareup.javapoet.FieldSpec) ClassName(com.squareup.javapoet.ClassName) List(java.util.List) ArrayList(java.util.ArrayList) TypeSpec(com.squareup.javapoet.TypeSpec)

Example 34 with TypeSpec

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();
}
Also used : MethodSpec(com.squareup.javapoet.MethodSpec) ArrayList(java.util.ArrayList) TypeSpec(com.squareup.javapoet.TypeSpec)

Example 35 with TypeSpec

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;
}
Also used : MethodSpec(com.squareup.javapoet.MethodSpec) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) TypeElement(javax.lang.model.element.TypeElement) Router(com.github.mzule.activityrouter.annotation.Router) ClassName(com.squareup.javapoet.ClassName) Name(javax.lang.model.element.Name) ClassName(com.squareup.javapoet.ClassName) TypeSpec(com.squareup.javapoet.TypeSpec)

Aggregations

TypeSpec (com.squareup.javapoet.TypeSpec)44 ClassName (com.squareup.javapoet.ClassName)26 MethodSpec (com.squareup.javapoet.MethodSpec)20 ParameterizedTypeName (com.squareup.javapoet.ParameterizedTypeName)10 TypeName (com.squareup.javapoet.TypeName)8 NotNull (org.jetbrains.annotations.NotNull)8 FieldSpec (com.squareup.javapoet.FieldSpec)7 ArrayList (java.util.ArrayList)6 Type (com.squareup.wire.schema.Type)5 TypeElement (javax.lang.model.element.TypeElement)5 ByteString (okio.ByteString)5 WireField (com.squareup.wire.WireField)4 JavaGenerator (com.squareup.wire.java.JavaGenerator)4 Field (com.squareup.wire.schema.Field)4 MessageType (com.squareup.wire.schema.MessageType)4 ProtoType (com.squareup.wire.schema.ProtoType)4 Schema (com.squareup.wire.schema.Schema)4 CodeBlock (com.squareup.javapoet.CodeBlock)3 EnclosingType (com.squareup.wire.schema.EnclosingType)3 EnumType (com.squareup.wire.schema.EnumType)3