Search in sources :

Example 1 with URLTemplateSource

use of com.github.jknack.handlebars.io.URLTemplateSource in project feign by OpenFeign.

the class GenerateTestStubAPT method process.

@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    System.out.println(annotations);
    System.out.println(roundEnv);
    final Map<TypeElement, List<ExecutableElement>> clientsToGenerate = annotations.stream().map(roundEnv::getElementsAnnotatedWith).flatMap(Set::stream).map(ExecutableElement.class::cast).collect(Collectors.toMap(annotatedMethod -> TypeElement.class.cast(annotatedMethod.getEnclosingElement()), ImmutableList::of, (list1, list2) -> ImmutableList.<ExecutableElement>builder().addAll(list1).addAll(list2).build()));
    System.out.println("Count: " + clientsToGenerate.size());
    System.out.println("clientsToGenerate: " + clientsToGenerate);
    final Handlebars handlebars = new Handlebars();
    final URLTemplateSource source = new URLTemplateSource("stub.mustache", getClass().getResource("/stub.mustache"));
    Template template;
    try {
        template = handlebars.with(EscapingStrategy.JS).compile(source);
    } catch (final IOException e) {
        throw new IOError(e);
    }
    clientsToGenerate.forEach((type, executables) -> {
        try {
            final String jPackage = readPackage(type);
            final String className = type.getSimpleName().toString();
            final JavaFileObject builderFile = processingEnv.getFiler().createSourceFile(jPackage + "." + className + "Stub");
            final ClientDefinition client = new ClientDefinition(jPackage, className, type.toString());
            final List<MethodDefinition> methods = executables.stream().map(method -> {
                final String methodName = method.getSimpleName().toString();
                final List<ArgumentDefinition> args = method.getParameters().stream().map(var -> new ArgumentDefinition(var.getSimpleName().toString(), var.asType().toString())).collect(Collectors.toList());
                return new MethodDefinition(methodName, method.getReturnType().toString(), method.getReturnType().getKind() == TypeKind.VOID, args);
            }).collect(Collectors.toList());
            final Context context = Context.newBuilder(template).combine("client", client).combine("methods", methods).resolver(JavaBeanValueResolver.INSTANCE, MapValueResolver.INSTANCE, FieldValueResolver.INSTANCE).build();
            final String stubSource = template.apply(context);
            System.out.println(stubSource);
            builderFile.openWriter().append(stubSource).close();
        } catch (final Exception e) {
            e.printStackTrace();
            processingEnv.getMessager().printMessage(Kind.ERROR, "Unable to generate factory for " + type);
        }
    });
    return true;
}
Also used : FieldValueResolver(com.github.jknack.handlebars.context.FieldValueResolver) java.util(java.util) MapValueResolver(com.github.jknack.handlebars.context.MapValueResolver) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) IOError(java.io.IOError) TypeKind(javax.lang.model.type.TypeKind) javax.annotation.processing(javax.annotation.processing) JavaFileObject(javax.tools.JavaFileObject) com.github.jknack.handlebars(com.github.jknack.handlebars) TypeMirror(javax.lang.model.type.TypeMirror) Kind(javax.tools.Diagnostic.Kind) URLTemplateSource(com.github.jknack.handlebars.io.URLTemplateSource) ImmutableList(com.google.common.collect.ImmutableList) Type(java.lang.reflect.Type) AutoService(com.google.auto.service.AutoService) JavaBeanValueResolver(com.github.jknack.handlebars.context.JavaBeanValueResolver) javax.lang.model.element(javax.lang.model.element) WildcardType(javax.lang.model.type.WildcardType) IOException(java.io.IOException) URLTemplateSource(com.github.jknack.handlebars.io.URLTemplateSource) IOException(java.io.IOException) JavaFileObject(javax.tools.JavaFileObject) IOError(java.io.IOError) ImmutableList(com.google.common.collect.ImmutableList)

Aggregations

com.github.jknack.handlebars (com.github.jknack.handlebars)1 FieldValueResolver (com.github.jknack.handlebars.context.FieldValueResolver)1 JavaBeanValueResolver (com.github.jknack.handlebars.context.JavaBeanValueResolver)1 MapValueResolver (com.github.jknack.handlebars.context.MapValueResolver)1 URLTemplateSource (com.github.jknack.handlebars.io.URLTemplateSource)1 AutoService (com.google.auto.service.AutoService)1 ImmutableList (com.google.common.collect.ImmutableList)1 IOError (java.io.IOError)1 IOException (java.io.IOException)1 Type (java.lang.reflect.Type)1 java.util (java.util)1 Collectors (java.util.stream.Collectors)1 javax.annotation.processing (javax.annotation.processing)1 javax.lang.model.element (javax.lang.model.element)1 TypeKind (javax.lang.model.type.TypeKind)1 TypeMirror (javax.lang.model.type.TypeMirror)1 WildcardType (javax.lang.model.type.WildcardType)1 Kind (javax.tools.Diagnostic.Kind)1 JavaFileObject (javax.tools.JavaFileObject)1