java-commons/commons-serialize-generator/src/main/java/io/gitlab/jfronny/commons/serialize/generator/adapter/Adapters.java

66 lines
4.0 KiB
Java

package io.gitlab.jfronny.commons.serialize.generator.adapter;
import com.squareup.javapoet.CodeBlock;
import com.squareup.javapoet.TypeSpec;
import com.squareup.javapoet.TypeVariableName;
import io.gitlab.jfronny.commons.serialize.generator.AdapterRef;
import io.gitlab.jfronny.commons.serialize.generator.SerializableClass;
import io.gitlab.jfronny.commons.serialize.generator.adapter.impl.*;
import io.gitlab.jfronny.commons.serialize.generator.core.value.Property;
import javax.annotation.processing.Messager;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.Element;
import javax.lang.model.type.TypeMirror;
import javax.tools.Diagnostic;
import java.util.List;
import java.util.Set;
import java.util.function.Consumer;
public class Adapters {
public static final List<Adapter<?>> ADAPTERS = List.of(
new DeclaredAdapter(),
new InferredAdapter(),
new PrimitiveAdapter(),
new StringAdapter(),
new DateAdapter(),
new EnumAdapter(),
new ArrayAdapter(),
new CollectionAdapter(),
new MapAdapter(),
new OtherSerializableAdapter(),
new ReflectAdapter()
);
public static void generateRead(Property<?> prop, TypeSpec.Builder klazz, CodeBlock.Builder code, List<TypeVariableName> typeVariables, Set<SerializableClass> otherAdapters, Set<AdapterRef> refs, Messager message) {
withAdapter(prop, klazz, code, typeVariables, otherAdapters, refs, message, Adapter.Hydrated::generateRead);
}
public static void generateWrite(Property<?> prop, TypeSpec.Builder klazz, CodeBlock.Builder code, List<TypeVariableName> typeVariables, Set<SerializableClass> otherAdapters, Set<AdapterRef> refs, Messager message, Runnable writeGet) {
withAdapter(prop, klazz, code, typeVariables, otherAdapters, refs, message, adapter -> adapter.generateWrite(writeGet));
}
private static void withAdapter(Property<?> prop, TypeSpec.Builder klazz, CodeBlock.Builder code, List<TypeVariableName> typeVariables, Set<SerializableClass> otherAdapters, Set<AdapterRef> refs, Messager message, Consumer<Adapter<?>.Hydrated> action) {
withAdapter(klazz, code, typeVariables, otherAdapters, refs, prop.getType(), prop.getName(), prop.getAnnotations(), prop.getElement(), message, action);
}
public static void generateRead(TypeSpec.Builder klazz, CodeBlock.Builder code, List<TypeVariableName> typeVariables, Set<SerializableClass> other, Set<AdapterRef> refs, TypeMirror type, String propName, List<? extends AnnotationMirror> annotations, Element sourceElement, Messager message) {
withAdapter(klazz, code, typeVariables, other, refs, type, propName, annotations, sourceElement, message, Adapter.Hydrated::generateRead);
}
public static void generateWrite(TypeSpec.Builder klazz, CodeBlock.Builder code, List<TypeVariableName> typeVariables, Set<SerializableClass> other, Set<AdapterRef> refs, TypeMirror type, String propName, List<? extends AnnotationMirror> annotations, Element sourceElement, Messager message, Runnable writeGet) {
withAdapter(klazz, code, typeVariables, other, refs, type, propName, annotations, sourceElement, message, adapter -> adapter.generateWrite(writeGet));
}
private static void withAdapter(TypeSpec.Builder klazz, CodeBlock.Builder code, List<TypeVariableName> typeVariables, Set<SerializableClass> other, Set<AdapterRef> refs, TypeMirror type, String propName, List<? extends AnnotationMirror> annotations, Element sourceElement, Messager message, Consumer<Adapter<?>.Hydrated> action) {
for (Adapter<?> adapter : Adapters.ADAPTERS) {
Adapter<?>.Hydrated hydrated = adapter.hydrate(klazz, code, typeVariables, other, refs, type, propName, annotations, sourceElement);
if (hydrated.applies()) {
action.accept(hydrated);
return;
}
}
message.printMessage(Diagnostic.Kind.ERROR, "Could not find applicable adapter for property " + propName, sourceElement);
}
}