[*] Migrate to jspecify annotations

This commit is contained in:
2025-04-24 21:52:33 +02:00
parent cef5227bf1
commit c97f711c0b
100 changed files with 553 additions and 369 deletions

View File

@@ -3,8 +3,7 @@ package de.siphalor.tweed5.construct.api;
import de.siphalor.tweed5.construct.impl.TweedConstructFactoryImpl;
import org.jetbrains.annotations.CheckReturnValue;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jspecify.annotations.Nullable;
/**
* A factory that allows to construct instances of subclasses of a specific type.
@@ -20,7 +19,7 @@ public interface TweedConstructFactory<T> {
/**
* Starts building a new factory for the given base class.
*/
static <T> TweedConstructFactory.@NotNull FactoryBuilder<T> builder(Class<T> baseClass) {
static <T> TweedConstructFactory.FactoryBuilder<T> builder(Class<T> baseClass) {
return TweedConstructFactoryImpl.builder(baseClass);
}
@@ -30,7 +29,7 @@ public interface TweedConstructFactory<T> {
*/
@CheckReturnValue
@Contract(pure = true)
<C extends T> @NotNull Construct<C> construct(@NotNull Class<C> subClass);
<C extends T> Construct<C> construct(Class<C> subClass);
/**
* Builder for the factory.
@@ -40,19 +39,19 @@ public interface TweedConstructFactory<T> {
* Defines a new typed argument of the given type.
*/
@Contract(mutates = "this", value = "_ -> this")
<A> @NotNull FactoryBuilder<T> typedArg(@NotNull Class<A> argType);
<A> FactoryBuilder<T> typedArg(Class<A> argType);
/**
* Defines a new named argument with the given name and value type.
*/
@Contract(mutates = "this", value = "_, _ -> this")
<A> @NotNull FactoryBuilder<T> namedArg(@NotNull String name, @NotNull Class<A> argType);
<A> FactoryBuilder<T> namedArg(String name, Class<A> argType);
/**
* Builds the factory.
*/
@Contract(pure = true)
@NotNull TweedConstructFactory<T> build();
TweedConstructFactory<T> build();
}
/**
@@ -71,7 +70,7 @@ public interface TweedConstructFactory<T> {
* @see #namedArg(String, Object)
*/
@Contract(mutates = "this", value = "_ -> this")
<A> @NotNull Construct<C> typedArg(@NotNull A value);
<A> Construct<C> typedArg(A value);
/**
* Binds a value to a typed argument of the given type.
@@ -81,19 +80,19 @@ public interface TweedConstructFactory<T> {
* @see #namedArg(String, Object)
*/
@Contract(mutates = "this", value = "_, _ -> this")
<A> @NotNull Construct<C> typedArg(@NotNull Class<? super A> argType, @Nullable A value);
<A> Construct<C> typedArg(Class<? super A> argType, @Nullable A value);
/**
* Binds a value to a named argument.
* @see #typedArg(Object)
*/
@Contract(mutates = "this", value = "_, _ -> this")
<A> @NotNull Construct<C> namedArg(@NotNull String name, @Nullable A value);
<A> Construct<C> namedArg(String name, @Nullable A value);
/**
* Finishes the binding and actually constructs the class.
*/
@Contract(pure = true)
@NotNull C finish();
C finish();
}
}

View File

@@ -0,0 +1,4 @@
@NullMarked
package de.siphalor.tweed5.construct.api;
import org.jspecify.annotations.NullMarked;

View File

@@ -4,8 +4,7 @@ import de.siphalor.tweed5.construct.api.ConstructParameter;
import de.siphalor.tweed5.construct.api.TweedConstruct;
import de.siphalor.tweed5.construct.api.TweedConstructFactory;
import lombok.*;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jspecify.annotations.Nullable;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
@@ -37,11 +36,11 @@ public class TweedConstructFactoryImpl<T> implements TweedConstructFactory<T> {
}
@Override
public <C extends T> TweedConstructFactory.@NotNull Construct<C> construct(@NotNull Class<C> subClass) {
public <C extends T> TweedConstructFactory.Construct<C> construct(Class<C> subClass) {
return new Construct<>(getConstructTarget(subClass));
}
private <C extends T> @NotNull ConstructTarget<C> getConstructTarget(Class<C> type) {
private <C extends T> ConstructTarget<C> getConstructTarget(Class<C> type) {
ConstructTarget<C> cachedConstructTarget = readConstructTargetFromCache(type);
if (cachedConstructTarget != null) {
return cachedConstructTarget;
@@ -164,7 +163,7 @@ public class TweedConstructFactoryImpl<T> implements TweedConstructFactory<T> {
return new ConstructTarget<>(type, argOrder, createInvokerFromCandidate(type, executable));
}
private <C> Function<Object[], C> createInvokerFromCandidate(Class<C> type, Executable executable) {
private <C> Function<@Nullable Object[], C> createInvokerFromCandidate(Class<C> type, Executable executable) {
MethodHandle handle;
try {
if (executable instanceof Method) {
@@ -263,7 +262,7 @@ public class TweedConstructFactoryImpl<T> implements TweedConstructFactory<T> {
private final Map<String, Class<?>> namedArgs = new HashMap<>();
@Override
public <A> TweedConstructFactory.@NotNull FactoryBuilder<T> typedArg(@NotNull Class<A> argType) {
public <A> TweedConstructFactory.FactoryBuilder<T> typedArg(Class<A> argType) {
argType = boxClass(argType);
if (typedArgs.contains(argType)) {
throw new IllegalArgumentException("Argument for type " + argType + " has already been registered");
@@ -273,10 +272,7 @@ public class TweedConstructFactoryImpl<T> implements TweedConstructFactory<T> {
}
@Override
public <A> TweedConstructFactory.@NotNull FactoryBuilder<T> namedArg(
@NotNull String name,
@NotNull Class<A> argType
) {
public <A> TweedConstructFactory.FactoryBuilder<T> namedArg(String name, Class<A> argType) {
Class<?> existingArgType = namedArgs.get(name);
if (existingArgType != null) {
throw new IllegalArgumentException(
@@ -290,7 +286,7 @@ public class TweedConstructFactoryImpl<T> implements TweedConstructFactory<T> {
}
@Override
public @NotNull TweedConstructFactory<T> build() {
public TweedConstructFactory<T> build() {
return new TweedConstructFactoryImpl<>(
constructBaseClass,
typedArgs,
@@ -302,18 +298,18 @@ public class TweedConstructFactoryImpl<T> implements TweedConstructFactory<T> {
@RequiredArgsConstructor
private class Construct<C> implements TweedConstructFactory.Construct<C> {
private final ConstructTarget<C> target;
private final Map<Class<?>, Object> typedArgValues = new HashMap<>();
private final Map<String, Object> namedArgValues = new HashMap<>();
private final Map<Class<?>, @Nullable Object> typedArgValues = new HashMap<>();
private final Map<String, @Nullable Object> namedArgValues = new HashMap<>();
@Override
public <A> TweedConstructFactory.@NotNull Construct<C> typedArg(@NotNull A value) {
public <A> TweedConstructFactory.Construct<C> typedArg(A value) {
requireTypedArgExists(value.getClass(), value);
typedArgValues.put(value.getClass(), value);
return this;
}
@Override
public <A> TweedConstructFactory.@NotNull Construct<C> typedArg(@NotNull Class<? super A> argType, @Nullable A value) {
public <A> TweedConstructFactory.Construct<C> typedArg(Class<? super A> argType, @Nullable A value) {
argType = boxClass(argType);
if (value != null && !argType.isAssignableFrom(value.getClass())) {
throw new IllegalArgumentException(
@@ -327,7 +323,7 @@ public class TweedConstructFactoryImpl<T> implements TweedConstructFactory<T> {
return this;
}
private <A> void requireTypedArgExists(@NotNull Class<?> type, @Nullable A value) {
private <A> void requireTypedArgExists(Class<?> type, @Nullable A value) {
if (!typedArgs.contains(type)) {
throw new IllegalArgumentException(
"Typed argument for type " + type.getName() + " does not exist, value: " + value
@@ -336,7 +332,7 @@ public class TweedConstructFactoryImpl<T> implements TweedConstructFactory<T> {
}
@Override
public <A> TweedConstructFactory.@NotNull Construct<C> namedArg(@NotNull String name, @Nullable A value) {
public <A> TweedConstructFactory.Construct<C> namedArg(String name, @Nullable A value) {
Class<?> argType = namedArgs.get(name);
if (argType == null) {
throw new IllegalArgumentException(
@@ -353,10 +349,10 @@ public class TweedConstructFactoryImpl<T> implements TweedConstructFactory<T> {
}
@Override
public @NotNull C finish() {
public C finish() {
checkAllArgsFilled();
Object[] argValues = new Object[target.argOrder.length];
@Nullable Object[] argValues = new Object[target.argOrder.length];
for (int i = 0; i < target.argOrder.length; i++) {
Object arg = target.argOrder[i];
if (arg instanceof Class<?>) {
@@ -459,6 +455,6 @@ public class TweedConstructFactoryImpl<T> implements TweedConstructFactory<T> {
private static class ConstructTarget<C> {
Class<?> type;
Object[] argOrder;
Function<Object[], C> invoker;
Function<@Nullable Object[], C> invoker;
}
}

View File

@@ -1,4 +1,6 @@
@ApiStatus.Internal
@NullMarked
package de.siphalor.tweed5.construct.impl;
import org.jetbrains.annotations.ApiStatus;
import org.jspecify.annotations.NullMarked;