Skip to content

Commit

Permalink
bump patch + fix bug with primitive array signatures
Browse files Browse the repository at this point in the history
  • Loading branch information
Steanky committed Feb 10, 2024
1 parent 23bc87b commit 927e5d8
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
}

group 'com.github.steanky'
version '0.23.1'
version '0.23.2'

publishing {
publications {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ default boolean hasBuildingObject() {
* @param element the element from which to initialize the building object
* @return the initialized building object which will later be populated with data
*/
default @NotNull TReturn initBuildingObject(@NotNull ConfigElement element) {
default @NotNull Object initBuildingObject(@NotNull ConfigElement element) {
throw new UnsupportedOperationException("This signature does not support pre-initialized building objects");
}

Expand All @@ -136,7 +136,7 @@ default boolean hasBuildingObject() {
* @return a new object if this signature does not have a building object; otherwise, if a building object is
* provided, populates it
*/
@NotNull TReturn buildObject(@Nullable TReturn buildingObject, Object @NotNull [] args);
@NotNull Object buildObject(@Nullable Object buildingObject, Object @NotNull [] args);

/**
* Whether this signature should respect argument names when being matched.
Expand Down Expand Up @@ -312,7 +312,7 @@ private SignatureImpl(int priority, Collection<Map.Entry<String, SignatureParame

return new AbstractCollection<>() {
@Override
public Iterator<TypedObject> iterator() {
public @NotNull Iterator<TypedObject> iterator() {
return new Iterator<>() {
private final Iterator<Object> objectIterator = args.iterator();
private final Iterator<Map.Entry<String, SignatureParameter>> entryIterator = argumentTypes.iterator();
Expand Down Expand Up @@ -351,7 +351,7 @@ public boolean hasBuildingObject() {
}

@Override
public @NotNull T initBuildingObject(@NotNull ConfigElement element) {
public @NotNull Object initBuildingObject(@NotNull ConfigElement element) {
if (buildingObjectInitializer == null) {
//throws an exception
return Signature.super.initBuildingObject(element);
Expand All @@ -360,9 +360,10 @@ public boolean hasBuildingObject() {
return buildingObjectInitializer.apply(element);
}

@SuppressWarnings("unchecked")
@Override
public @NotNull T buildObject(@Nullable T buildingObject, Object @NotNull [] args) {
return constructor.apply(buildingObject, new Arguments(args));
public @NotNull T buildObject(@Nullable Object buildingObject, Object @NotNull [] args) {
return constructor.apply((T) buildingObject, new Arguments(args));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ private static Map.Entry<String, SignatureParameter> makeEntry(Parameter paramet

@SuppressWarnings("unchecked")
@Override
public @NotNull T buildObject(@Nullable T buildingObject, Object @NotNull [] args) {
public @NotNull T buildObject(@Nullable Object buildingObject, Object @NotNull [] args) {
if (buildingObject != null) {
throw new MapperException("ConstructorSignature does not support pre-initialized building objects");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public ArraySignature(@NotNull Token<T> componentType) {

return new AbstractCollection<>() {
@Override
public Iterator<TypedObject> iterator() {
public @NotNull Iterator<TypedObject> iterator() {
return new Iterator<>() {
private int i = 0;

Expand All @@ -59,21 +59,29 @@ public int size() {
};
}


@SuppressWarnings({"SuspiciousSystemArraycopy", "unchecked"})
@SuppressWarnings("SuspiciousSystemArraycopy")
@Override
public T @NotNull [] buildObject(@NotNull T @Nullable [] buildingObject, Object @NotNull [] args) {
public @NotNull Object buildObject(@Nullable Object buildingObject, Object @NotNull [] args) {
if (buildingObject == null) {
return (T[]) args;
buildingObject = Array.newInstance(containerType.componentType().rawType(), args.length);
}

if (containerType.componentType().rawType().isPrimitive()) {
//manual arraycopy when component type is primitive, we must unbox as args will be boxed
for (int i = 0; i < args.length; i++) {
Array.set(buildingObject, i, args[i]);
}

return buildingObject;
}

//in other cases, we can do a likely-faster arraycopy
System.arraycopy(args, 0, buildingObject, 0, args.length);
return buildingObject;
}

@SuppressWarnings("unchecked")
@Override
protected @NotNull T @NotNull [] makeBuildingObject(@NotNull ConfigContainer container) {
return (T[]) Array.newInstance(containerType.componentType().rawType(), container.elementCollection().size());
protected @NotNull Object makeBuildingObject(@NotNull ConfigContainer container) {
return Array.newInstance(containerType.componentType().rawType(), container.elementCollection().size());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public CollectionSignature(@NotNull Token<?> componentType, @NotNull Token<T> co

return new AbstractCollection<>() {
@Override
public Iterator<TypedObject> iterator() {
public @NotNull Iterator<TypedObject> iterator() {
return new Iterator<>() {
private final Iterator<Object> collectionIterator = objectCollection.iterator();

Expand All @@ -63,7 +63,7 @@ public int size() {

@SuppressWarnings("unchecked")
@Override
public @NotNull T buildObject(@Nullable T buildingObject, Object @NotNull [] args) {
public @NotNull T buildObject(@Nullable Object buildingObject, Object @NotNull [] args) {
if (buildingObject != null) {
Collection<Object> buildingCollection = (Collection<Object>) buildingObject;
buildingCollection.addAll(Arrays.asList(args));
Expand Down Expand Up @@ -92,9 +92,8 @@ private Collection<Object> makeNewCollection(int size) {
}
}

@SuppressWarnings("unchecked")
@Override
protected @NotNull T makeBuildingObject(@NotNull ConfigContainer container) {
return (T) makeNewCollection(container.entryCollection().size());
protected @NotNull Object makeBuildingObject(@NotNull ConfigContainer container) {
return makeNewCollection(container.entryCollection().size());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public boolean hasBuildingObject() {
}

@Override
public @NotNull T initBuildingObject(@NotNull ConfigElement element) {
public @NotNull Object initBuildingObject(@NotNull ConfigElement element) {
if (!element.isContainer()) {
throw new MapperException("Expected container, got '" + element.type() + "'");
}
Expand Down Expand Up @@ -165,7 +165,7 @@ public int length(@Nullable ConfigElement configElement) {
* @param container the {@link ConfigContainer} used to create the building object
* @return the new building object
*/
protected abstract @NotNull T makeBuildingObject(@NotNull ConfigContainer container);
protected abstract @NotNull Object makeBuildingObject(@NotNull ConfigContainer container);

/**
* Information about a constructor. Container constructors may be parameterless (in which case they take no
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.AbstractCollection;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.*;


/**
Expand All @@ -39,7 +36,7 @@ public MapSignature(@NotNull Token<?> keyType, @NotNull Token<?> valueType, @Not

return new AbstractCollection<>() {
@Override
public Iterator<TypedObject> iterator() {
public @NotNull Iterator<TypedObject> iterator() {
return new Iterator<>() {
private final Iterator<Map.Entry<Object, Object>> entryIterator = map.entrySet().iterator();

Expand All @@ -64,7 +61,7 @@ public int size() {

@SuppressWarnings("unchecked")
@Override
public @NotNull T buildObject(@Nullable T buildingObject, Object @NotNull [] args) {
public @NotNull T buildObject(@Nullable Object buildingObject, Object @NotNull [] args) {
if (buildingObject != null) {
Map<Object, Object> buildingMap = (Map<Object, Object>) buildingObject;
finishMap(buildingMap, args);
Expand Down Expand Up @@ -98,9 +95,8 @@ private Map<Object, Object> getMap(int size) {
}
}

@SuppressWarnings("unchecked")
@Override
protected @NotNull T makeBuildingObject(@NotNull ConfigContainer container) {
return (T) getMap(container.entryCollection().size());
protected @NotNull Object makeBuildingObject(@NotNull ConfigContainer container) {
return getMap(container.entryCollection().size());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ public boolean hasBuildingObject() {
}

@Override
public @NotNull T buildObject(@Nullable T buildingObject, Object @NotNull [] args) {
public @NotNull Object buildObject(@Nullable Object buildingObject, Object @NotNull [] args) {
try {
if (buildingObject != null) {
finishObject(buildingObject, args);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public RecordSignature(@NotNull Token<T> genericReturnType) {

@SuppressWarnings("unchecked")
@Override
public @NotNull T buildObject(@Nullable T buildingObject, Object @NotNull [] args) {
public @NotNull T buildObject(@Nullable Object buildingObject, Object @NotNull [] args) {
if (buildingObject != null) {
throw new MapperException("Pre-initialized building objects are not supported by this signature");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,19 @@ public record Child(double data) {}

public record Data(int x, Child child) {}

public record IntArrayContaining(int[] values) {

}

@Test
void intArray() throws ConfigProcessException {
ConfigProcessor<IntArrayContaining> processor = new MappingConfigProcessor<>(new Token<>() {
}, source, typeHinter, typeResolver, scalarSource, false);

IntArrayContaining containing = processor.dataFromElement(ConfigElement.of("{values=[0, 1, 2, 3, 4]}"));
assertArrayEquals(new int[] {0, 1, 2, 3, 4}, containing.values);
}

@Test
void upcastFloat() throws ConfigProcessException {
ConfigProcessor<Data> processor = new MappingConfigProcessor<>(new Token<>() {
Expand Down

0 comments on commit 927e5d8

Please sign in to comment.