Skip to content

Commit

Permalink
Enhance TypeValidation: give advice for primitives
Browse files Browse the repository at this point in the history
  • Loading branch information
prdoyle committed May 1, 2024
1 parent 3687da9 commit 27eeceb
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 8 deletions.
4 changes: 4 additions & 0 deletions bosk-core/src/main/java/io/vena/bosk/TypeValidation.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import io.vena.bosk.exceptions.InvalidFieldTypeException;
import io.vena.bosk.exceptions.InvalidTypeException;
import java.lang.annotation.Annotation;
import java.lang.invoke.MethodType;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
Expand Down Expand Up @@ -50,6 +51,9 @@ private static void validateType(Type theType, Set<Type> alreadyValidated) throw
Class<?> theClass = rawClass(theType);
if (!isPublic(theClass.getModifiers())) {
throw new InvalidTypeException("Class is not public: " + theClass.getName());
} else if (theClass.isPrimitive()) {
Class<?> wrapped = MethodType.methodType(theClass).wrap().returnType();
throw new InvalidTypeException("Primitive types are not allowed in a bosk; use boxed " + wrapped.getSimpleName() + " instead of primitive " + theClass.getSimpleName());
} else if (isSimpleClass(theClass)) {
// All allowed
return;
Expand Down
55 changes: 47 additions & 8 deletions bosk-core/src/test/java/io/vena/bosk/TypeValidationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -134,14 +134,53 @@ public record BoxedPrimitives(
Double doubleObject
) implements StateTreeNode { }

public record BooleanPrimitive(boolean field) implements StateTreeNode {}
public record BytePrimitive(byte field) implements StateTreeNode {}
public record CharPrimitive(char field) implements StateTreeNode {}
public record ShortPrimitive(short field) implements StateTreeNode {}
public record IntegerPrimitive(int field) implements StateTreeNode {}
public record LongPrimitive(long field) implements StateTreeNode {}
public record FloatPrimitive(float field) implements StateTreeNode {}
public record DoublePrimitive(double field) implements StateTreeNode {}
public record BooleanPrimitive(boolean field) implements StateTreeNode {
static void testException(InvalidTypeException e) {
assertThat(e.getMessage(), containsStringIgnoringCase("primitive"));
}
}

public record BytePrimitive(byte field) implements StateTreeNode {
static void testException(InvalidTypeException e) {
assertThat(e.getMessage(), containsStringIgnoringCase("primitive"));
}
}

public record CharPrimitive(char field) implements StateTreeNode {
static void testException(InvalidTypeException e) {
assertThat(e.getMessage(), containsStringIgnoringCase("primitive"));
}
}

public record ShortPrimitive(short field) implements StateTreeNode {
static void testException(InvalidTypeException e) {
assertThat(e.getMessage(), containsStringIgnoringCase("primitive"));
}
}

public record IntegerPrimitive(int field) implements StateTreeNode {
static void testException(InvalidTypeException e) {
assertThat(e.getMessage(), containsStringIgnoringCase("primitive"));
}
}

public record LongPrimitive(long field) implements StateTreeNode {
static void testException(InvalidTypeException e) {
assertThat(e.getMessage(), containsStringIgnoringCase("primitive"));
}
}

public record FloatPrimitive(float field) implements StateTreeNode {
static void testException(InvalidTypeException e) {
assertThat(e.getMessage(), containsStringIgnoringCase("primitive"));
}
}

public record DoublePrimitive(double field) implements StateTreeNode {
static void testException(InvalidTypeException e) {
assertThat(e.getMessage(), containsStringIgnoringCase("primitive"));
}
}

@Getter @FieldDefaults(level=AccessLevel.PRIVATE, makeFinal=true) @RequiredArgsConstructor
public static final class SimpleTypes implements Entity {
Expand Down

0 comments on commit 27eeceb

Please sign in to comment.