Skip to content

Commit e303d2e

Browse files
kindlichclaude
andcommitted
Support raw generic types in native headers
A raw class reference (e.g. a `GenClass` parameter) was translated by looking up the class's own type variables in the current context, which only works when the class refers to itself. From anywhere else this threw "Could not find type variable T". Raw types are now erased the way Java does it: each type argument becomes `out <erasure of its first bound>`, so a raw `Something<T extends Parent>` exposes Parent's members. As in JavaRuntimeClass.translateTypeParameters, the type variables are put into a child TypeVariableContext before their bounds are resolved, so a bound leading back to the class being loaded (e.g. `T extends Node<T>`) is detected and becomes Object. Fixes RawTypeInHeaderTest; adds RawBoundedTypeInHeaderTest and RawRecursiveBoundTypeInHeaderTest. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 2bb5fbc commit e303d2e

3 files changed

Lines changed: 177 additions & 0 deletions

File tree

‎JavaIntegration/src/main/java/org/openzen/zencode/java/impl/conversion/JavaRuntimeTypeConverterImpl.java‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,12 @@ private TypeID loadClass(TypeVariableContext context, Class<?> type) {
196196
// ((JavaRuntimeClass) definition).translateTypeParameters(context);
197197
//}
198198

199+
// Type variables not in context mean a raw type used from outside the class,
200+
// e.g. a parameter declared as `Foo` instead of `Foo<String>`. Inside Foo itself, T is in context.
201+
if (!context.containsAll(type.getTypeParameters())) {
202+
return loadRawClass(context, type, definition);
203+
}
204+
199205
final List<TypeID> typeParameters = new ArrayList<>();
200206
for (TypeVariable<? extends Class<?>> typeParameter : type.getTypeParameters()) {
201207
typeParameters.add(new GenericTypeID(context.get(typeParameter)));
@@ -204,6 +210,36 @@ private TypeID loadClass(TypeVariableContext context, Class<?> type) {
204210
return DefinitionTypeID.create(definition, typeParameters.toArray(TypeID.NONE));
205211
}
206212

213+
// Raw types are treated the way Java erases them: each type argument becomes "out <erasure of its first bound>".
214+
// Like JavaRuntimeClass.translateTypeParameters, the type variables are put into the context before their bounds
215+
// are resolved, so a bound leading back to a class being loaded (e.g. T extends Node<T>) is detected and becomes Object.
216+
private TypeID loadRawClass(TypeVariableContext context, Class<?> type, TypeSymbol definition) {
217+
final TypeVariable<? extends Class<?>>[] javaTypeParameters = type.getTypeParameters();
218+
final TypeVariableContext rawContext = context.createChildContext();
219+
for (TypeVariable<? extends Class<?>> typeVariable : javaTypeParameters) {
220+
rawContext.put(typeVariable, new TypeParameter(CodePosition.NATIVE, typeVariable.getName()));
221+
}
222+
223+
final TypeID[] typeArguments = new TypeID[javaTypeParameters.length];
224+
for (int i = 0; i < javaTypeParameters.length; i++) {
225+
Class<?> bound = erase(javaTypeParameters[i].getBounds()[0]);
226+
boolean boundIsLoading = bound.getTypeParameters().length > 0 && rawContext.containsAll(bound.getTypeParameters());
227+
typeArguments[i] = new WildcardOutTypeID(loadClass(rawContext, boundIsLoading ? Object.class : bound));
228+
}
229+
230+
return DefinitionTypeID.create(definition, typeArguments);
231+
}
232+
233+
private static Class<?> erase(Type type) {
234+
if (type instanceof Class)
235+
return (Class<?>) type;
236+
if (type instanceof ParameterizedType)
237+
return erase(((ParameterizedType) type).getRawType());
238+
if (type instanceof TypeVariable)
239+
return erase(((TypeVariable<?>) type).getBounds()[0]);
240+
return Object.class;
241+
}
242+
207243
private TypeID loadGenericArray(TypeVariableContext context, GenericArrayType type) {
208244
final JavaAnnotatedType componentType = JavaAnnotatedType.of(type.getGenericComponentType());
209245
final TypeID baseType = this.loadType(context, componentType);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package org.openzen.zenscript.scriptingexample.tests.actual_test.java_native;
2+
3+
import org.junit.jupiter.api.Test;
4+
import org.openzen.zencode.java.ZenCodeGlobals;
5+
import org.openzen.zencode.java.ZenCodeType;
6+
import org.openzen.zenscript.scriptingexample.tests.helpers.ZenCodeTest;
7+
8+
import java.util.List;
9+
10+
public class RawBoundedTypeInHeaderTest extends ZenCodeTest {
11+
12+
@Override
13+
public List<Class<?>> getRequiredClasses() {
14+
final List<Class<?>> requiredClasses = super.getRequiredClasses();
15+
requiredClasses.add(Parent.class);
16+
requiredClasses.add(Child.class);
17+
requiredClasses.add(BoundedGenClass.class);
18+
requiredClasses.add(TestGlobals.class);
19+
return requiredClasses;
20+
}
21+
22+
@Test
23+
void rawBoundedTypeAcceptsParameterizedValue() {
24+
addScript("acceptRaw(new test_module.java_native.BoundedGenClass<test_module.java_native.Child>(new test_module.java_native.Child()));");
25+
26+
executeEngine();
27+
28+
logger.assertNoErrors();
29+
logger.assertNoWarnings();
30+
}
31+
32+
@Test
33+
void rawBoundedTypeExposesBoundMembers() {
34+
addScript("println(makeRaw().unwrap().greet());");
35+
36+
executeEngine();
37+
38+
logger.assertNoErrors();
39+
logger.printlnOutputs().assertLinesInOrder("Hi from child");
40+
}
41+
42+
@ZenCodeType.Name("test_module.java_native.RawBoundedTestGlobals")
43+
public static final class TestGlobals {
44+
@ZenCodeGlobals.Global
45+
public static void acceptRaw(BoundedGenClass clazz) {
46+
}
47+
48+
@ZenCodeGlobals.Global
49+
public static BoundedGenClass makeRaw() {
50+
return new BoundedGenClass<>(new Child());
51+
}
52+
}
53+
54+
@ZenCodeType.Name("test_module.java_native.Parent")
55+
public static class Parent {
56+
@ZenCodeType.Method
57+
public String greet() {
58+
return "Hi from parent";
59+
}
60+
}
61+
62+
@ZenCodeType.Name("test_module.java_native.Child")
63+
public static class Child extends Parent {
64+
@ZenCodeType.Constructor
65+
public Child() {
66+
}
67+
68+
@Override
69+
@ZenCodeType.Method
70+
public String greet() {
71+
return "Hi from child";
72+
}
73+
}
74+
75+
@ZenCodeType.Name("test_module.java_native.BoundedGenClass")
76+
public static final class BoundedGenClass<T extends Parent> {
77+
private final T value;
78+
79+
@ZenCodeType.Constructor
80+
public BoundedGenClass(T value) {
81+
this.value = value;
82+
}
83+
84+
@ZenCodeType.Method
85+
public T unwrap() {
86+
return value;
87+
}
88+
}
89+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package org.openzen.zenscript.scriptingexample.tests.actual_test.java_native;
2+
3+
import org.junit.jupiter.api.Test;
4+
import org.openzen.zencode.java.ZenCodeGlobals;
5+
import org.openzen.zencode.java.ZenCodeType;
6+
import org.openzen.zenscript.scriptingexample.tests.helpers.ZenCodeTest;
7+
8+
import java.util.List;
9+
10+
/**
11+
* Raw types whose type parameter bound leads back to the type itself (T extends SelfBoundedGenClass<T>).
12+
* Without the guard in JavaRuntimeTypeConverterImpl.loadRawClass, the bound would either recurse or leak the
13+
* guard's placeholder type parameter, so the round trip below would no longer type-check.
14+
* No other test registers these types, so a failure here points at that guard.
15+
*/
16+
public class RawRecursiveBoundTypeInHeaderTest extends ZenCodeTest {
17+
18+
@Override
19+
public List<Class<?>> getRequiredClasses() {
20+
final List<Class<?>> requiredClasses = super.getRequiredClasses();
21+
requiredClasses.add(SelfBoundedGenClass.class);
22+
requiredClasses.add(TestGlobals.class);
23+
return requiredClasses;
24+
}
25+
26+
@Test
27+
void rawSelfBoundedTypeRoundTrips() {
28+
addScript("println(acceptRawSelfBounded(makeRawSelfBounded()));");
29+
30+
executeEngine();
31+
32+
logger.assertNoErrors();
33+
logger.printlnOutputs().assertLinesInOrder("self bounded");
34+
}
35+
36+
@ZenCodeType.Name("test_module.java_native.RawRecursiveBoundTestGlobals")
37+
public static final class TestGlobals {
38+
@ZenCodeGlobals.Global
39+
public static SelfBoundedGenClass makeRawSelfBounded() {
40+
return new SelfBoundedGenClass<>();
41+
}
42+
43+
@ZenCodeGlobals.Global
44+
public static String acceptRawSelfBounded(SelfBoundedGenClass clazz) {
45+
return "self bounded";
46+
}
47+
}
48+
49+
@ZenCodeType.Name("test_module.java_native.SelfBoundedGenClass")
50+
public static final class SelfBoundedGenClass<T extends SelfBoundedGenClass<T>> {
51+
}
52+
}

0 commit comments

Comments
 (0)