Skip to content

Commit 86b7e35

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. Bounds that lead back to a raw type currently being loaded (e.g. `T extends Node<T>`) fall back to Object. Fixes RawTypeInHeaderTest; adds RawBoundedTypeInHeaderTest. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 2bb5fbc commit 86b7e35

2 files changed

Lines changed: 149 additions & 0 deletions

File tree

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,10 @@ private TypeID loadAnnotatedType(TypeVariableContext context, AnnotatedType type
180180
}
181181

182182
private TypeID loadClass(TypeVariableContext context, Class<?> type) {
183+
return loadClass(context, type, new HashSet<>());
184+
}
185+
186+
private TypeID loadClass(TypeVariableContext context, Class<?> type, Set<Class<?>> loadingRawTypes) {
183187
if (type.isArray()) {
184188
final TypeID baseType = this.loadType(context, JavaAnnotatedType.of(type.getComponentType()));
185189
return new ArrayTypeID(baseType);
@@ -196,6 +200,11 @@ private TypeID loadClass(TypeVariableContext context, Class<?> type) {
196200
// ((JavaRuntimeClass) definition).translateTypeParameters(context);
197201
//}
198202

203+
// Type variables not in context mean a raw type used from outside the class (e.g. a GenClass parameter)
204+
if (!context.containsAll(type.getTypeParameters())) {
205+
return loadRawClass(context, type, definition, loadingRawTypes);
206+
}
207+
199208
final List<TypeID> typeParameters = new ArrayList<>();
200209
for (TypeVariable<? extends Class<?>> typeParameter : type.getTypeParameters()) {
201210
typeParameters.add(new GenericTypeID(context.get(typeParameter)));
@@ -204,6 +213,34 @@ private TypeID loadClass(TypeVariableContext context, Class<?> type) {
204213
return DefinitionTypeID.create(definition, typeParameters.toArray(TypeID.NONE));
205214
}
206215

216+
// Raw types are treated the way Java erases them: each type argument becomes "out <erasure of its first bound>".
217+
// Bounds that lead back to a raw type currently being loaded (e.g. T extends Node<T>) fall back to Object.
218+
private TypeID loadRawClass(TypeVariableContext context, Class<?> type, TypeSymbol definition, Set<Class<?>> loadingRawTypes) {
219+
loadingRawTypes.add(type);
220+
final TypeVariable<? extends Class<?>>[] javaTypeParameters = type.getTypeParameters();
221+
final TypeID[] typeArguments = new TypeID[javaTypeParameters.length];
222+
for (int i = 0; i < javaTypeParameters.length; i++) {
223+
Class<?> bound = erase(javaTypeParameters[i].getBounds()[0]);
224+
TypeID boundType = loadingRawTypes.contains(bound) ? loadClass(context, Object.class) : loadClass(context, bound, loadingRawTypes);
225+
typeArguments[i] = new WildcardOutTypeID(boundType);
226+
}
227+
loadingRawTypes.remove(type);
228+
229+
return DefinitionTypeID.create(definition, typeArguments);
230+
}
231+
232+
private static Class<?> erase(Type type) {
233+
if (type instanceof Class)
234+
return (Class<?>) type;
235+
if (type instanceof ParameterizedType)
236+
return erase(((ParameterizedType) type).getRawType());
237+
if (type instanceof TypeVariable)
238+
return erase(((TypeVariable<?>) type).getBounds()[0]);
239+
if (type instanceof GenericArrayType)
240+
return java.lang.reflect.Array.newInstance(erase(((GenericArrayType) type).getGenericComponentType()), 0).getClass();
241+
return Object.class;
242+
}
243+
207244
private TypeID loadGenericArray(TypeVariableContext context, GenericArrayType type) {
208245
final JavaAnnotatedType componentType = JavaAnnotatedType.of(type.getGenericComponentType());
209246
final TypeID baseType = this.loadType(context, componentType);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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(SelfBoundedGenClass.class);
19+
requiredClasses.add(TestGlobals.class);
20+
return requiredClasses;
21+
}
22+
23+
@Test
24+
void rawBoundedTypeAcceptsParameterizedValue() {
25+
addScript("acceptRaw(new test_module.java_native.BoundedGenClass<test_module.java_native.Child>(new test_module.java_native.Child()));");
26+
27+
executeEngine();
28+
29+
logger.assertNoErrors();
30+
logger.assertNoWarnings();
31+
}
32+
33+
@Test
34+
void rawBoundedTypeExposesBoundMembers() {
35+
addScript("println(makeRaw().unwrap().greet());");
36+
37+
executeEngine();
38+
39+
logger.assertNoErrors();
40+
logger.printlnOutputs().assertLinesInOrder("Hi from child");
41+
}
42+
43+
@Test
44+
void rawSelfBoundedTypeDoesNotRecurse() {
45+
// Header conversion of acceptRawSelfBounded happens on registration, the script itself doesn't matter
46+
addScript("println('ok');");
47+
48+
executeEngine();
49+
50+
logger.assertNoErrors();
51+
logger.printlnOutputs().assertLinesInOrder("ok");
52+
}
53+
54+
@ZenCodeType.Name("test_module.java_native.RawBoundedTestGlobals")
55+
public static final class TestGlobals {
56+
@ZenCodeGlobals.Global
57+
public static void acceptRaw(BoundedGenClass clazz) {
58+
}
59+
60+
@ZenCodeGlobals.Global
61+
public static BoundedGenClass makeRaw() {
62+
return new BoundedGenClass<>(new Child());
63+
}
64+
65+
@ZenCodeGlobals.Global
66+
public static void acceptRawSelfBounded(SelfBoundedGenClass clazz) {
67+
}
68+
}
69+
70+
@ZenCodeType.Name("test_module.java_native.Parent")
71+
public static class Parent {
72+
@ZenCodeType.Method
73+
public String greet() {
74+
return "Hi from parent";
75+
}
76+
}
77+
78+
@ZenCodeType.Name("test_module.java_native.Child")
79+
public static class Child extends Parent {
80+
@ZenCodeType.Constructor
81+
public Child() {
82+
}
83+
84+
@Override
85+
@ZenCodeType.Method
86+
public String greet() {
87+
return "Hi from child";
88+
}
89+
}
90+
91+
@ZenCodeType.Name("test_module.java_native.BoundedGenClass")
92+
public static final class BoundedGenClass<T extends Parent> {
93+
private final T value;
94+
95+
@ZenCodeType.Constructor
96+
public BoundedGenClass(T value) {
97+
this.value = value;
98+
}
99+
100+
@ZenCodeType.Method
101+
public T unwrap() {
102+
return value;
103+
}
104+
}
105+
106+
@ZenCodeType.Name("test_module.java_native.SelfBoundedGenClass")
107+
public static final class SelfBoundedGenClass<T extends SelfBoundedGenClass<T>> {
108+
@ZenCodeType.Constructor
109+
public SelfBoundedGenClass() {
110+
}
111+
}
112+
}

0 commit comments

Comments
 (0)