Skip to content

Commit

Permalink
fix: JavaEnums now have .name and .values getters
Browse files Browse the repository at this point in the history
  • Loading branch information
kindlich committed Nov 7, 2024
1 parent 3fea2d4 commit 799da9a
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@ public Optional<StaticCallable> findStaticMethod(String name) {

@Override
public Optional<StaticCallable> findStaticGetter(String name) {
return loadStatic(MethodID.getter(name));
return loadStatic(MethodID.staticGetter(name));
}

@Override
public Optional<StaticCallable> findStaticSetter(String name) {
return loadStatic(MethodID.setter(name));
return loadStatic(MethodID.staticSetter(name));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
import org.openzen.zenscript.codemodel.identifiers.instances.FieldInstance;
import org.openzen.zenscript.codemodel.ssa.CodeBlockStatement;
import org.openzen.zenscript.codemodel.ssa.SSAVariableCollector;
import org.openzen.zenscript.codemodel.type.ArrayTypeID;
import org.openzen.zenscript.codemodel.type.TypeID;
import org.openzen.zenscript.codemodel.type.builtin.BuiltinMethodSymbol;
import org.openzen.zenscript.javashared.JavaClass;
import org.openzen.zenscript.javashared.JavaModifiers;
import org.openzen.zenscript.javashared.JavaNativeField;
Expand All @@ -29,6 +31,7 @@
import java.lang.reflect.Modifier;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class JavaNativeTypeTemplate {
protected final JavaRuntimeClass class_;
Expand Down Expand Up @@ -206,6 +209,31 @@ private void loadMethods() {
class_.module.getCompiled().setMethodInfo(runtimeMethod, runtimeMethod);
}
}


if (class_.cls.isEnum()) {
Stream.of(
BuiltinMethodSymbol.ENUM_NAME,
BuiltinMethodSymbol.ENUM_ORDINAL,
//BuiltinMethodSymbol.ENUM_VALUES,
BuiltinMethodSymbol.ENUM_COMPARE
).forEach(method -> methods
.computeIfAbsent(method.getID(), x -> new ArrayList<>())
.add(method)
);

try {
MethodID id = MethodID.staticGetter("values");
FunctionHeader header = new FunctionHeader(new ArrayTypeID(target));
Method method = class_.cls.getMethod("values");
JavaRuntimeMethod runtimeMethod = new JavaRuntimeMethod(class_, target, method, id, header, false, false);
methods.computeIfAbsent(id, x -> new ArrayList<>()).add(runtimeMethod);
class_.module.getCompiled().setMethodInfo(runtimeMethod, runtimeMethod);
} catch (ReflectiveOperationException exception) {
throw new IllegalStateException("We found an enum class without values() method: " + class_.cls.getCanonicalName(), exception);
}

}
}

private void loadInnerTypes() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,34 @@ void EnumMembersMustBeRetrievableByTypeHintAndName() {
logger.assertPrintOutput(0, "A");
}

@Test
void EnumMembersMustBeRetrievableByFullName() {
ScriptBuilder.create()
.add("public class MyClass {")
.add(" public this(thing as test_module.MyEnum) {")
.add(" println(thing.name);")
.add(" }")
.add("}")
.add("new MyClass(test_module.MyEnum.A);")
.execute(this);

logger.assertPrintOutputSize(1);
logger.assertPrintOutput(0, "A");
}

@Test
void EnumMembersMustHaveValuesGetter() {
ScriptBuilder.create()
.add("var values = test_module.MyEnum.values;")
.add("var a = values[0];")
.add("var name = a.name;")
.add("println(a.name);")
.execute(this);

logger.assertPrintOutputSize(1);
logger.assertPrintOutput(0, "A");
}


@ZenCodeType.Name("test_module.MyEnum")
public enum MyEnum {
Expand Down

This file was deleted.

0 comments on commit 799da9a

Please sign in to comment.