diff --git a/src/main/java/carpet/script/annotation/SimpleTypeConverter.java b/src/main/java/carpet/script/annotation/SimpleTypeConverter.java index b5229071..aef8f363 100644 --- a/src/main/java/carpet/script/annotation/SimpleTypeConverter.java +++ b/src/main/java/carpet/script/annotation/SimpleTypeConverter.java @@ -25,6 +25,7 @@ import carpet.script.value.ValueConversions; *

{@link SimpleTypeConverter}s are reused whenever asked for one, since they don't have any complexity.

* * @see #registerType(Class, Class, Function, String) + * @see #registerType(Class, Class, BiFunction, String) * * @param The type of the required input {@link Value} * @param The type that this converter converts to @@ -52,14 +53,29 @@ public final class SimpleTypeConverter implements ValueConve private final String typeName; /** - *

The default constructor for {@link SimpleTypeConverter}.

+ * Same as {@link #SimpleTypeConverter(Class, BiFunction, String)}, but without the converter function taking a {@link Context} + * + * @param inputType The required type for the input {@link Value} + * @param converter The function to convert an instance of inputType into R, with the context of the call. + * @param typeName The name of the type for error messages + * + * @see #SimpleTypeConverter(Class, BiFunction, String) + */ + public SimpleTypeConverter(final Class inputType, final Function converter, final String typeName) + { + this(inputType, (v, c) -> converter.apply(v), typeName); + } + + /** + *

A constructor for {@link SimpleTypeConverter}.

* *

This is public in order to provide an implementation to use when registering {@link ValueConverter}s for the {@link Param.Strict} annotation * registry, and it's not intended way to register new {@link SimpleTypeConverter}

Use {@link #registerType(Class, Class, Function, String)} for * that.

* * @param inputType The required type for the input {@link Value} - * @param converter The function to convert an instance of inputType into R. + * @param converter The function to convert an instance of inputType into R, with the context of the call. + * @param typeName The name of the type for error messages */ public SimpleTypeConverter(final Class inputType, final BiFunction converter, final String typeName) { @@ -69,14 +85,6 @@ public final class SimpleTypeConverter implements ValueConve this.typeName = typeName; } - public SimpleTypeConverter(final Class inputType, final Function converter, final String typeName) - { - super(); - this.converter = (v, c) -> converter.apply(v); - this.valueClass = inputType; - this.typeName = typeName; - } - @Override public String getTypeName() { @@ -114,6 +122,26 @@ public final class SimpleTypeConverter implements ValueConve * when given {@link Value} cannot be converted to the {@code }, to follow the {@link ValueConverter} contract, but it * can also throw an {@link InternalExpressionException} by itself if really necessary. * @param typeName The name of the type, following the conventions of {@link ValueConverter#getTypeName()} + * + * @see #registerType(Class, Class, BiFunction, String) + */ + public static void registerType(final Class requiredInputType, final Class outputType, + final Function converter, final String typeName) + { + registerType(requiredInputType, outputType, (val, ctx) -> converter.apply(val), typeName); + } + + /** + *

Registers a new conversion from a {@link Value} subclass to a Java type, with the context of the call.

+ * + * @param The {@link Value} subtype required for this conversion, for automatic checked casting + * @param The type of the resulting object + * @param requiredInputType The {@link Class} of {@code } + * @param outputType The {@link Class} of {@code >} + * @param converter A function that converts from the given {@link Value} subtype to the given type. Should ideally return {@code null} + * when given {@link Value} cannot be converted to the {@code }, to follow the {@link ValueConverter} contract, but it + * can also throw an {@link InternalExpressionException} by itself if really necessary. + * @param typeName The name of the type, following the conventions of {@link ValueConverter#getTypeName()} */ public static void registerType(final Class requiredInputType, final Class outputType, final BiFunction converter, final String typeName) @@ -121,10 +149,4 @@ public final class SimpleTypeConverter implements ValueConve final SimpleTypeConverter type = new SimpleTypeConverter<>(requiredInputType, converter, typeName); byResult.put(outputType, type); } - - public static void registerType(final Class requiredInputType, final Class outputType, - final Function converter, final String typeName) - { - registerType(requiredInputType, outputType, (val, ctx) -> converter.apply(val), typeName); - } } diff --git a/src/main/java/carpet/script/annotation/ValueConverter.java b/src/main/java/carpet/script/annotation/ValueConverter.java index c7de8977..ba98f8b7 100644 --- a/src/main/java/carpet/script/annotation/ValueConverter.java +++ b/src/main/java/carpet/script/annotation/ValueConverter.java @@ -49,8 +49,8 @@ public interface ValueConverter *

Functions using the converter can use {@link #getTypeName()} to get the name of the type this was trying to convert to, in case they are not * trying to convert to anything else, where it would be recommended to tell the user the name of the final type instead.

* - * @param value The {@link Value} to convert - * @param context + * @param value The {@link Value} to convert + * @param context The {@link Context} of the call * @return The converted value, or {@code null} if the conversion failed in the process * @apiNote

While most implementations of this method should and will return the type from this method, implementations that require * parameters from {@link #checkAndConvert(Iterator, Context, Context.Type)} or that require multiple parameters may decide to throw @@ -65,10 +65,27 @@ public interface ValueConverter */ @Nullable R convert(Value value, final Context context); + /** + * Old version of {@link #convert(Value)} without taking a {@link Context}.

+ * + * This shouldn't be used given converters now take a context in the convert function to allow for converting + * values in lists or other places without using static state.

+ * + * @param value The value to convert + * @return A converted value + * @deprecated Calling this method instead of {@link #convert(Value, Context)} may not return values for some converters + */ @Deprecated(forRemoval = true) default R convert(final Value value) { - return convert(value, null); + try + { + return convert(value, null); + } + catch (NullPointerException e) + { + return null; + } } /** @@ -185,8 +202,7 @@ public interface ValueConverter * they may consume variable arguments, implement {@link #consumesVariableArgs()}

This method holds the same nullability * constraints as {@link #convert(Value, Context)}

* @param valueIterator An {@link Iterator} holding the {@link Value} to convert in next position - * @param context The {@link Context} this function has been called with. This was used when this passed lazy values instead in order to - * evaluate, now it's used to get the context + * @param context The {@link Context} this function has been called with * @param contextType The {@link Context.Type} that the original function was called with * @return The next {@link Value} (s) converted to the type {@code } of this {@link ValueConverter} * @implNote This method's default implementation runs the {@link #convert(Value, Context)} function in the next {@link Value} ignoring {@link Context} and