implements PLN-0121
This commit is contained in:
+121
@@ -11,11 +11,16 @@ import p.studio.compiler.source.tables.IntrinsicReference;
|
||||
import p.studio.compiler.source.tables.ModuleReference;
|
||||
import p.studio.utilities.structures.ReadOnlyList;
|
||||
|
||||
import java.lang.reflect.ParameterizedType;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.function.Function;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
@@ -151,6 +156,57 @@ class IRBackendExecutableContractTest {
|
||||
"public IRBackend contract must not expose PBS frontend types: " + violations);
|
||||
}
|
||||
|
||||
@Test
|
||||
void publicIRBackendContractMustExposeOnlySerializableShapes() {
|
||||
final var violations = new ArrayList<String>();
|
||||
|
||||
for (final var contractType : PUBLIC_CONTRACT_TYPES) {
|
||||
inspectSerializableContractType(contractType.getName(), contractType, violations);
|
||||
for (final var field : contractType.getFields()) {
|
||||
inspectSerializableContractType(
|
||||
contractType.getName() + "#" + field.getName(),
|
||||
field.getGenericType(),
|
||||
violations);
|
||||
}
|
||||
for (final var constructor : contractType.getConstructors()) {
|
||||
for (final var parameterType : constructor.getGenericParameterTypes()) {
|
||||
inspectSerializableContractType(
|
||||
contractType.getName() + "#<init>",
|
||||
parameterType,
|
||||
violations);
|
||||
}
|
||||
}
|
||||
for (final var method : contractType.getMethods()) {
|
||||
if (method.getDeclaringClass().equals(Object.class)
|
||||
|| !Modifier.isPublic(method.getModifiers())) {
|
||||
continue;
|
||||
}
|
||||
inspectSerializableContractType(
|
||||
contractType.getName() + "#" + method.getName(),
|
||||
method.getGenericReturnType(),
|
||||
violations);
|
||||
for (final var parameterType : method.getGenericParameterTypes()) {
|
||||
inspectSerializableContractType(
|
||||
contractType.getName() + "#" + method.getName(),
|
||||
parameterType,
|
||||
violations);
|
||||
}
|
||||
}
|
||||
if (contractType.isRecord()) {
|
||||
for (final var component : contractType.getRecordComponents()) {
|
||||
inspectSerializableContractType(
|
||||
contractType.getName() + "#" + component.getName(),
|
||||
component.getGenericType(),
|
||||
violations);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
assertTrue(
|
||||
violations.isEmpty(),
|
||||
"public IRBackend contract must expose only serializable-by-design shapes: " + violations);
|
||||
}
|
||||
|
||||
@Test
|
||||
void callInstructionMustRequireCategorySpecificMetadata() {
|
||||
assertThrows(IllegalArgumentException.class, () -> new IRBackendExecutableFunction.Instruction(
|
||||
@@ -515,4 +571,69 @@ class IRBackendExecutableContractTest {
|
||||
violations.add(owner + " -> " + typeName);
|
||||
}
|
||||
}
|
||||
|
||||
private static void inspectSerializableContractType(
|
||||
final String owner,
|
||||
final Type type,
|
||||
final ArrayList<String> violations) {
|
||||
if (type == null) {
|
||||
return;
|
||||
}
|
||||
if (type instanceof ParameterizedType parameterizedType) {
|
||||
inspectSerializableContractType(owner, parameterizedType.getRawType(), violations);
|
||||
for (final var argument : parameterizedType.getActualTypeArguments()) {
|
||||
inspectSerializableContractType(owner, argument, violations);
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (!(type instanceof Class<?> clazz)) {
|
||||
return;
|
||||
}
|
||||
if (clazz.isPrimitive()
|
||||
|| clazz.isEnum()
|
||||
|| AUDITED_SUPPORT_TYPES.contains(clazz)
|
||||
|| PUBLIC_CONTRACT_TYPES.contains(clazz)) {
|
||||
return;
|
||||
}
|
||||
if (isForbiddenFrontendOwnedType(clazz)) {
|
||||
violations.add(owner + " exposes frontend-owned type " + clazz.getName());
|
||||
}
|
||||
if (isForbiddenCallbackShape(clazz)) {
|
||||
violations.add(owner + " exposes callback shape " + clazz.getName());
|
||||
}
|
||||
if (isForbiddenMutableOrUnorderedCollection(clazz)) {
|
||||
violations.add(owner + " exposes mutable or unordered collection " + clazz.getName());
|
||||
}
|
||||
if (isForbiddenServiceShape(clazz)) {
|
||||
violations.add(owner + " exposes service-like shape " + clazz.getName());
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean isForbiddenFrontendOwnedType(final Class<?> clazz) {
|
||||
final var packageName = clazz.getPackageName();
|
||||
return packageName.startsWith("p.studio.compiler.pbs")
|
||||
|| packageName.contains(".parser")
|
||||
|| packageName.contains(".semantic")
|
||||
|| packageName.contains(".editor");
|
||||
}
|
||||
|
||||
private static boolean isForbiddenCallbackShape(final Class<?> clazz) {
|
||||
return clazz.equals(Runnable.class)
|
||||
|| Callable.class.isAssignableFrom(clazz)
|
||||
|| Function.class.isAssignableFrom(clazz)
|
||||
|| clazz.getPackageName().startsWith("java.util.function");
|
||||
}
|
||||
|
||||
private static boolean isForbiddenMutableOrUnorderedCollection(final Class<?> clazz) {
|
||||
return !clazz.equals(ReadOnlyList.class)
|
||||
&& (Collection.class.isAssignableFrom(clazz) || Map.class.isAssignableFrom(clazz));
|
||||
}
|
||||
|
||||
private static boolean isForbiddenServiceShape(final Class<?> clazz) {
|
||||
final var simpleName = clazz.getSimpleName();
|
||||
return simpleName.endsWith("Service")
|
||||
|| simpleName.endsWith("Registry")
|
||||
|| simpleName.endsWith("Visitor")
|
||||
|| simpleName.endsWith("Provider");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user