Search in sources :

Example 1 with GenericUtils

use of io.micronaut.annotation.processing.GenericUtils in project micronaut-kubernetes by micronaut-projects.

the class KubernetesApisProcessor method writeReactorClients.

private void writeReactorClients(Element e, String packageName, String simpleName) {
    final String reactorClientName = simpleName + "ReactorClient";
    final String reactorPackageName = packageName.replace(KUBERNETES_APIS_PACKAGE, MICRONAUT_APIS_PACKAGE + ".reactor");
    ClassName cn = ClassName.get(reactorPackageName, reactorClientName);
    TypeSpec.Builder builder = TypeSpec.classBuilder(cn);
    ClassName clientType = ClassName.get(packageName, simpleName);
    ClassName reactorMonoType = ClassName.get("reactor.core.publisher", "Mono");
    final AnnotationSpec.Builder requiresSpec = AnnotationSpec.builder(Requires.class).addMember("beans", "{$T.class}", clientType);
    builder.addAnnotation(requiresSpec.build());
    builder.addAnnotation(Singleton.class);
    if (Objects.equals(simpleName, "CoreV1Api")) {
        builder.addAnnotation(BootstrapContextCompatible.class);
    }
    builder.addModifiers(Modifier.PUBLIC);
    builder.addField(clientType, "client", Modifier.FINAL, Modifier.PRIVATE);
    builder.addMethod(MethodSpec.constructorBuilder().addParameter(clientType, "client").addCode("this.client = client;").build());
    TypeElement typeElement = elements.getTypeElement(clientType.reflectionName());
    if (typeElement != null) {
        ModelUtils modelUtils = new ModelUtils(elements, types) {
        };
        GenericUtils genericUtils = new GenericUtils(elements, types, modelUtils) {
        };
        AnnotationUtils annotationUtils = new AnnotationUtils(processingEnv, elements, messager, types, modelUtils, genericUtils, filer) {
        };
        JavaVisitorContext visitorContext = new JavaVisitorContext(processingEnv, messager, elements, annotationUtils, types, modelUtils, genericUtils, filer, MutableConvertibleValues.of(new LinkedHashMap<>()), TypeElementVisitor.VisitorKind.ISOLATING);
        typeElement.asType().accept(new PublicMethodVisitor<Object, Object>(visitorContext) {

            @Override
            protected void accept(DeclaredType type, Element element, Object o) {
                ExecutableElement ee = (ExecutableElement) element;
                TypeMirror returnType = ee.getReturnType();
                if (element.getSimpleName().toString().endsWith("Async")) {
                    DeclaredType dt = (DeclaredType) returnType;
                    Element e = dt.asElement();
                    if (e.getSimpleName().toString().equals("Call")) {
                        List<? extends VariableElement> parameters = ee.getParameters();
                        VariableElement fieldElement = parameters.get(parameters.size() - 1);
                        TypeMirror typeMirror = fieldElement.asType();
                        if (typeMirror instanceof DeclaredType) {
                            DeclaredType cdt = (DeclaredType) typeMirror;
                            List<? extends TypeMirror> typeArguments = cdt.getTypeArguments();
                            if (typeArguments.size() == 1) {
                                TypeMirror ctm = typeArguments.get(0);
                                if (ctm instanceof DeclaredType) {
                                    // resolve the callback response type
                                    TypeName responseType = ClassName.get(ctm);
                                    // resolve the method name by removing the Async suffix
                                    String methodName = ee.getSimpleName().toString();
                                    String finalMethodName = methodName.replace("Async", "");
                                    // prepare parameters for the method without tha _callback one
                                    List<ParameterSpec> parameterSpecs = parameters.stream().filter(va -> !va.getSimpleName().toString().equals("_callback")).filter(va -> !va.getSimpleName().toString().equals("watch")).map(va -> ParameterSpec.builder(ClassName.get(va.asType()), va.getSimpleName().toString()).build()).collect(Collectors.toList());
                                    MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder(finalMethodName).addModifiers(Modifier.PUBLIC).addParameters(parameterSpecs).returns(ParameterizedTypeName.get(reactorMonoType, responseType));
                                    methodBuilder.addCode(CodeBlock.builder().addStatement("return $T.create((sink) -> {", reactorMonoType).addStatement("try {").add("    this.client." + methodName + "(").add(parameters.stream().map(va -> {
                                        String name = va.getSimpleName().toString();
                                        if (name.equals("_callback")) {
                                            return "new AsyncCallbackSink<" + responseType + ">(sink)";
                                        } else if (name.equals("watch")) {
                                            return "Boolean.FALSE";
                                        } else {
                                            return name;
                                        }
                                    }).collect(Collectors.joining(", "))).addStatement(")").addStatement("} catch(io.kubernetes.client.openapi.ApiException e) { sink.error(e); }").addStatement("})").build());
                                    builder.addMethod(methodBuilder.build());
                                }
                            }
                        }
                    }
                }
            }
        }, null);
    }
    writeJavaFile(e, reactorPackageName, cn, builder);
}
Also used : com.squareup.javapoet(com.squareup.javapoet) AnnotationUtils(io.micronaut.annotation.processing.AnnotationUtils) GenericUtils(io.micronaut.annotation.processing.GenericUtils) Elements(javax.lang.model.util.Elements) ArrayList(java.util.ArrayList) javax.annotation.processing(javax.annotation.processing) LinkedHashMap(java.util.LinkedHashMap) PublicMethodVisitor(io.micronaut.annotation.processing.PublicMethodVisitor) TypeElementVisitor(io.micronaut.inject.visitor.TypeElementVisitor) JavaVisitorContext(io.micronaut.annotation.processing.visitor.JavaVisitorContext) BootstrapContextCompatible(io.micronaut.context.annotation.BootstrapContextCompatible) Diagnostic(javax.tools.Diagnostic) DeclaredType(javax.lang.model.type.DeclaredType) Map(java.util.Map) Requires(io.micronaut.context.annotation.Requires) MutableConvertibleValues(io.micronaut.core.convert.value.MutableConvertibleValues) NameUtils(io.micronaut.core.naming.NameUtils) javax.lang.model.element(javax.lang.model.element) Singleton(jakarta.inject.Singleton) Set(java.util.Set) IOException(java.io.IOException) Types(javax.lang.model.util.Types) Collectors(java.util.stream.Collectors) JavaFileObject(javax.tools.JavaFileObject) Objects(java.util.Objects) SourceVersion(javax.lang.model.SourceVersion) TypeMirror(javax.lang.model.type.TypeMirror) List(java.util.List) Factory(io.micronaut.context.annotation.Factory) ModelUtils(io.micronaut.annotation.processing.ModelUtils) Writer(java.io.Writer) LinkedHashMap(java.util.LinkedHashMap) ModelUtils(io.micronaut.annotation.processing.ModelUtils) TypeMirror(javax.lang.model.type.TypeMirror) ArrayList(java.util.ArrayList) List(java.util.List) Requires(io.micronaut.context.annotation.Requires) JavaVisitorContext(io.micronaut.annotation.processing.visitor.JavaVisitorContext) GenericUtils(io.micronaut.annotation.processing.GenericUtils) AnnotationUtils(io.micronaut.annotation.processing.AnnotationUtils) JavaFileObject(javax.tools.JavaFileObject) DeclaredType(javax.lang.model.type.DeclaredType)

Example 2 with GenericUtils

use of io.micronaut.annotation.processing.GenericUtils in project micronaut-kubernetes by micronaut-projects.

the class KubernetesApisProcessor method writeRxJavaClients.

private void writeRxJavaClients(Element e, String packageName, String simpleName, String rxVersionSuffix, String rxJavaPackage) {
    final String rx = simpleName + "RxClient";
    final String rxPackageName = packageName.replace(KUBERNETES_APIS_PACKAGE, MICRONAUT_APIS_PACKAGE + rxVersionSuffix);
    ClassName cn = ClassName.get(rxPackageName, rx);
    TypeSpec.Builder builder = TypeSpec.classBuilder(cn);
    ClassName clientType = ClassName.get(packageName, simpleName);
    ClassName rxSingleType = ClassName.get(rxJavaPackage, "Single");
    final AnnotationSpec.Builder requiresSpec = AnnotationSpec.builder(Requires.class).addMember("beans", "{$T.class}", clientType);
    builder.addAnnotation(requiresSpec.build());
    builder.addAnnotation(Singleton.class);
    builder.addModifiers(Modifier.PUBLIC);
    builder.addField(clientType, "client", Modifier.FINAL, Modifier.PRIVATE);
    builder.addMethod(MethodSpec.constructorBuilder().addParameter(clientType, "client").addCode("this.client = client;").build());
    TypeElement typeElement = elements.getTypeElement(clientType.reflectionName());
    if (typeElement != null) {
        ModelUtils modelUtils = new ModelUtils(elements, types) {
        };
        GenericUtils genericUtils = new GenericUtils(elements, types, modelUtils) {
        };
        AnnotationUtils annotationUtils = new AnnotationUtils(processingEnv, elements, messager, types, modelUtils, genericUtils, filer) {
        };
        JavaVisitorContext visitorContext = new JavaVisitorContext(processingEnv, messager, elements, annotationUtils, types, modelUtils, genericUtils, filer, MutableConvertibleValues.of(new LinkedHashMap<>()), TypeElementVisitor.VisitorKind.ISOLATING);
        typeElement.asType().accept(new PublicMethodVisitor<Object, Object>(visitorContext) {

            @Override
            protected void accept(DeclaredType type, Element element, Object o) {
                ExecutableElement ee = (ExecutableElement) element;
                TypeMirror returnType = ee.getReturnType();
                if (element.getSimpleName().toString().endsWith("Async")) {
                    DeclaredType dt = (DeclaredType) returnType;
                    Element e = dt.asElement();
                    if (e.getSimpleName().toString().equals("Call")) {
                        List<? extends VariableElement> parameters = ee.getParameters();
                        VariableElement fieldElement = parameters.get(parameters.size() - 1);
                        TypeMirror typeMirror = fieldElement.asType();
                        if (typeMirror instanceof DeclaredType) {
                            DeclaredType cdt = (DeclaredType) typeMirror;
                            List<? extends TypeMirror> typeArguments = cdt.getTypeArguments();
                            if (typeArguments.size() == 1) {
                                TypeMirror ctm = typeArguments.get(0);
                                if (ctm instanceof DeclaredType) {
                                    // resolve the callback response type
                                    TypeName responseType = ClassName.get(ctm);
                                    // resolve the method name by removing the Async suffix
                                    String methodName = ee.getSimpleName().toString();
                                    String finalMethodName = methodName.replace("Async", "");
                                    // prepare parameters for the method without tha _callback one
                                    List<ParameterSpec> parameterSpecs = parameters.stream().filter(va -> !va.getSimpleName().toString().equals("_callback")).filter(va -> !va.getSimpleName().toString().equals("watch")).map(va -> ParameterSpec.builder(ClassName.get(va.asType()), va.getSimpleName().toString()).build()).collect(Collectors.toList());
                                    MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder(finalMethodName).addModifiers(Modifier.PUBLIC).addParameters(parameterSpecs).returns(ParameterizedTypeName.get(rxSingleType, responseType));
                                    methodBuilder.addCode(CodeBlock.builder().addStatement("return $T.create((emitter) -> {", rxSingleType).add("this.client." + methodName + "(").add(parameters.stream().map(va -> {
                                        String name = va.getSimpleName().toString();
                                        if (name.equals("_callback")) {
                                            return "new ApiCallbackEmitter<>(emitter)";
                                        } else if (name.equals("watch")) {
                                            return "Boolean.FALSE";
                                        } else {
                                            return name;
                                        }
                                    }).collect(Collectors.joining(", "))).addStatement(")").addStatement("})").build());
                                    builder.addMethod(methodBuilder.build());
                                }
                            }
                        }
                    }
                }
            }
        }, null);
    }
    writeJavaFile(e, rxPackageName, cn, builder);
}
Also used : com.squareup.javapoet(com.squareup.javapoet) AnnotationUtils(io.micronaut.annotation.processing.AnnotationUtils) GenericUtils(io.micronaut.annotation.processing.GenericUtils) Elements(javax.lang.model.util.Elements) ArrayList(java.util.ArrayList) javax.annotation.processing(javax.annotation.processing) LinkedHashMap(java.util.LinkedHashMap) PublicMethodVisitor(io.micronaut.annotation.processing.PublicMethodVisitor) TypeElementVisitor(io.micronaut.inject.visitor.TypeElementVisitor) JavaVisitorContext(io.micronaut.annotation.processing.visitor.JavaVisitorContext) BootstrapContextCompatible(io.micronaut.context.annotation.BootstrapContextCompatible) Diagnostic(javax.tools.Diagnostic) DeclaredType(javax.lang.model.type.DeclaredType) Map(java.util.Map) Requires(io.micronaut.context.annotation.Requires) MutableConvertibleValues(io.micronaut.core.convert.value.MutableConvertibleValues) NameUtils(io.micronaut.core.naming.NameUtils) javax.lang.model.element(javax.lang.model.element) Singleton(jakarta.inject.Singleton) Set(java.util.Set) IOException(java.io.IOException) Types(javax.lang.model.util.Types) Collectors(java.util.stream.Collectors) JavaFileObject(javax.tools.JavaFileObject) Objects(java.util.Objects) SourceVersion(javax.lang.model.SourceVersion) TypeMirror(javax.lang.model.type.TypeMirror) List(java.util.List) Factory(io.micronaut.context.annotation.Factory) ModelUtils(io.micronaut.annotation.processing.ModelUtils) Writer(java.io.Writer) LinkedHashMap(java.util.LinkedHashMap) ModelUtils(io.micronaut.annotation.processing.ModelUtils) TypeMirror(javax.lang.model.type.TypeMirror) ArrayList(java.util.ArrayList) List(java.util.List) Requires(io.micronaut.context.annotation.Requires) JavaVisitorContext(io.micronaut.annotation.processing.visitor.JavaVisitorContext) GenericUtils(io.micronaut.annotation.processing.GenericUtils) AnnotationUtils(io.micronaut.annotation.processing.AnnotationUtils) JavaFileObject(javax.tools.JavaFileObject) DeclaredType(javax.lang.model.type.DeclaredType)

Aggregations

com.squareup.javapoet (com.squareup.javapoet)2 AnnotationUtils (io.micronaut.annotation.processing.AnnotationUtils)2 GenericUtils (io.micronaut.annotation.processing.GenericUtils)2 ModelUtils (io.micronaut.annotation.processing.ModelUtils)2 PublicMethodVisitor (io.micronaut.annotation.processing.PublicMethodVisitor)2 JavaVisitorContext (io.micronaut.annotation.processing.visitor.JavaVisitorContext)2 BootstrapContextCompatible (io.micronaut.context.annotation.BootstrapContextCompatible)2 Factory (io.micronaut.context.annotation.Factory)2 Requires (io.micronaut.context.annotation.Requires)2 MutableConvertibleValues (io.micronaut.core.convert.value.MutableConvertibleValues)2 NameUtils (io.micronaut.core.naming.NameUtils)2 TypeElementVisitor (io.micronaut.inject.visitor.TypeElementVisitor)2 Singleton (jakarta.inject.Singleton)2 IOException (java.io.IOException)2 Writer (java.io.Writer)2 ArrayList (java.util.ArrayList)2 LinkedHashMap (java.util.LinkedHashMap)2 List (java.util.List)2 Map (java.util.Map)2 Objects (java.util.Objects)2