[weaver-pojo] Implement first prototype of POJO weaving

This commit is contained in:
2024-10-20 21:30:00 +02:00
parent 37d64502ad
commit 002f59ebd0
40 changed files with 2144 additions and 31 deletions

View File

@@ -0,0 +1,48 @@
package de.siphalor.tweed5.data.extension.api;
import de.siphalor.tweed5.data.extension.api.readwrite.TweedEntryReaderWriter;
import lombok.RequiredArgsConstructor;
/**
* An interface that allows to register {@link TweedEntryReader}s and {@link TweedEntryWriter}s.
* Implementing classes should be Java services, e.g. using {@link com.google.auto.service.AutoService}.
*/
public interface TweedReaderWriterProvider {
void provideReaderWriters(ProviderContext context);
/**
* The context where reader and writer factories may be registered.<br />
* The reader and writer ids must be globally unique. It is therefore recommended to scope custom reader and writer ids in your own namespace (e.g. "de.siphalor.custom.blub")
*/
interface ProviderContext {
void registerReaderFactory(String id, ReaderWriterFactory<? extends TweedEntryReader<?, ?>> readerFactory);
void registerWriterFactory(String id, ReaderWriterFactory<? extends TweedEntryWriter<?, ?>> writerFactory);
default void registerReaderWriterFactory(String id, ReaderWriterFactory<? extends TweedEntryReaderWriter<?, ?>> readerWriterFactory) {
registerReaderFactory(id, readerWriterFactory);
registerWriterFactory(id, readerWriterFactory);
}
}
/**
* A factory that creates a new reader or writer using delegate readers/writers as its arguments.
* @param <T>
*/
@FunctionalInterface
interface ReaderWriterFactory<T> {
T create(T... delegateReaderWriters);
}
@RequiredArgsConstructor
final class StaticReaderWriterFactory<T> implements ReaderWriterFactory<T> {
private final T readerWriter;
@SafeVarargs
@Override
public final T create(T... delegateReaderWriters) {
if (delegateReaderWriters.length != 0) {
throw new IllegalArgumentException("Reader writer factory must not be passed any delegates as arguments");
}
return readerWriter;
}
}
}

View File

@@ -0,0 +1,30 @@
package de.siphalor.tweed5.data.extension.impl;
import com.google.auto.service.AutoService;
import de.siphalor.tweed5.data.extension.api.TweedReaderWriterProvider;
import static de.siphalor.tweed5.data.extension.api.readwrite.TweedEntryReaderWriters.*;
@AutoService(TweedReaderWriterProvider.class)
public class DefaultTweedEntryReaderWriterImplsProvider implements TweedReaderWriterProvider {
@Override
public void provideReaderWriters(ProviderContext context) {
context.registerReaderWriterFactory("boolean", new StaticReaderWriterFactory<>(booleanReaderWriter()));
context.registerReaderWriterFactory("byte", new StaticReaderWriterFactory<>(byteReaderWriter()));
context.registerReaderWriterFactory("short", new StaticReaderWriterFactory<>(shortReaderWriter()));
context.registerReaderWriterFactory("int", new StaticReaderWriterFactory<>(intReaderWriter()));
context.registerReaderWriterFactory("long", new StaticReaderWriterFactory<>(longReaderWriter()));
context.registerReaderWriterFactory("float", new StaticReaderWriterFactory<>(floatReaderWriter()));
context.registerReaderWriterFactory("double", new StaticReaderWriterFactory<>(doubleReaderWriter()));
context.registerReaderWriterFactory("string", new StaticReaderWriterFactory<>(stringReaderWriter()));
context.registerReaderWriterFactory("coherent_collection", new StaticReaderWriterFactory<>(coherentCollectionReaderWriter()));
context.registerReaderWriterFactory("compound", new StaticReaderWriterFactory<>(compoundReaderWriter()));
context.registerReaderWriterFactory("nullable", delegateReaderWriters -> {
if (delegateReaderWriters.length != 1) {
throw new IllegalArgumentException("Nullable reader writer requires a single delegate argument, got " + delegateReaderWriters.length);
}
return nullableReaderWriter(delegateReaderWriters[0]);
});
}
}