From a7cf3d721754faa68b51dd73585814a1886ee4aa Mon Sep 17 00:00:00 2001 From: kindlich Date: Fri, 18 Oct 2024 19:47:23 +0200 Subject: [PATCH] WIP: move javanative type resolution into own class --- .../codemodel/definition/ZSPackage.java | 10 ++-- .../JavaRuntimeTypeConverterImpl.java | 7 ++- .../conversion/JdkJavaRuntimeConverter.java | 58 +++++++++++++++++++ 3 files changed, 68 insertions(+), 7 deletions(-) create mode 100644 JavaIntegration/src/main/java/org/openzen/zencode/java/impl/conversion/JdkJavaRuntimeConverter.java diff --git a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/definition/ZSPackage.java b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/definition/ZSPackage.java index 08786def7..ca2ecf89c 100644 --- a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/definition/ZSPackage.java +++ b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/definition/ZSPackage.java @@ -1,15 +1,11 @@ package org.openzen.zenscript.codemodel.definition; import org.openzen.zenscript.codemodel.GenericName; -import org.openzen.zenscript.codemodel.identifiers.ExpansionSymbol; import org.openzen.zenscript.codemodel.identifiers.TypeSymbol; import org.openzen.zenscript.codemodel.type.DefinitionTypeID; import org.openzen.zenscript.codemodel.type.TypeID; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Optional; +import java.util.*; public class ZSPackage { public final String name; @@ -39,6 +35,10 @@ public boolean contains(String name) { return types.containsKey(name) || subPackages.containsKey(name); } + public TypeSymbol getImport(String[] name) { + return getImport(Arrays.asList(name), 0); + } + public TypeSymbol getImport(List name, int depth) { if (depth >= name.size()) return null; diff --git a/JavaIntegration/src/main/java/org/openzen/zencode/java/impl/conversion/JavaRuntimeTypeConverterImpl.java b/JavaIntegration/src/main/java/org/openzen/zencode/java/impl/conversion/JavaRuntimeTypeConverterImpl.java index 58e146f79..9a3ab1f03 100644 --- a/JavaIntegration/src/main/java/org/openzen/zencode/java/impl/conversion/JavaRuntimeTypeConverterImpl.java +++ b/JavaIntegration/src/main/java/org/openzen/zencode/java/impl/conversion/JavaRuntimeTypeConverterImpl.java @@ -45,11 +45,13 @@ public class JavaRuntimeTypeConverterImpl implements JavaRuntimeTypeConverter { private final Map, TypeID> typeByClass = new HashMap<>(); private final Map, TypeID> unsignedByClass = new HashMap<>(); private final Map, Function> specialTypes = new HashMap<>(); + private final JdkJavaRuntimeConverter jdkJavaRuntimeConverter; private JavaNativeHeaderConverter headerConverter; public JavaRuntimeTypeConverterImpl(JavaNativeModuleSpace nativeModuleSpace, JavaNativePackageInfo packageInfo) { this.nativeModuleSpace = nativeModuleSpace; this.packageInfo = packageInfo; + this.jdkJavaRuntimeConverter = new JdkJavaRuntimeConverter(packageInfo.getRoot()); fillClassMaps(); fillSpecialTypes(); } @@ -240,8 +242,9 @@ private TypeID loadWildcard() { private TypeSymbol findType(Class cls) { // ToDo: Have a custom type for Collection - if (cls == List.class || cls == Collection.class) { - return packageInfo.getRoot().getImport(Arrays.asList("stdlib", "List"), 0); + Optional typeID = jdkJavaRuntimeConverter.resolveJavaType(cls); + if (typeID.isPresent()) { + return typeID.get(); } if (cls == Object.class) { TypeSymbol result = packageInfo.getRoot().getImport(Arrays.asList("stdlib", "Object"), 0); diff --git a/JavaIntegration/src/main/java/org/openzen/zencode/java/impl/conversion/JdkJavaRuntimeConverter.java b/JavaIntegration/src/main/java/org/openzen/zencode/java/impl/conversion/JdkJavaRuntimeConverter.java new file mode 100644 index 000000000..ec53654f1 --- /dev/null +++ b/JavaIntegration/src/main/java/org/openzen/zencode/java/impl/conversion/JdkJavaRuntimeConverter.java @@ -0,0 +1,58 @@ +package org.openzen.zencode.java.impl.conversion; + +import org.openzen.zenscript.codemodel.definition.ZSPackage; +import org.openzen.zenscript.codemodel.identifiers.TypeSymbol; + +import java.util.*; + +public class JdkJavaRuntimeConverter { + + private final ZSPackage rootPackage; + private final Map, String> javaClassToZenCodeClassName = new HashMap<>(); + + public JdkJavaRuntimeConverter(ZSPackage rootPackage) { + this.rootPackage = rootPackage; + fillJavaClassToZenCodeClassName(); + } + + public Optional resolveJavaType(Class javaType) { + if (!javaClassToZenCodeClassName.containsKey(javaType)) { + return Optional.empty(); + } + + String zenCodeName = javaClassToZenCodeClassName.get(javaType); + TypeSymbol result = rootPackage.getImport(zenCodeName.split("\\.")); + if(result == null) { + throw new IllegalStateException("Could not resolve zen code type " + zenCodeName + " for java type " + javaType.getName() + ", make sure the required stdlib modules are registered as dependencies"); + } + + return Optional.of(result); + + } + + private void fillJavaClassToZenCodeClassName() { + fillMapping(List.class, "stdlib.List"); + fillMapping(Collection.class, "stdlib.List"); + fillMapping(Comparable.class, "stdlib.Comparable"); + fillMapping(Exception.class, "stdlib.Exception"); + fillMapping(IllegalArgumentException.class, "stdlib.IllegalArgumentException"); + fillMapping(Iterable.class, "stdlib.Iterable"); + fillMapping(Iterator.class, "stdlib.Iterator"); + fillMapping(StringBuilder.class, "stdlib.StringBuilder"); + + fillMapping(HashSet.class, "collections.HashSet"); + fillMapping(LinkedList.class, "collections.LinkedList"); + fillMapping(ArrayList.class, "collections.ArrayList"); + fillMapping(Queue.class, "collections.Queue"); + fillMapping(Set.class, "collections.Set"); + fillMapping(Stack.class, "collections.Stack"); + + fillMapping(UUID.class, "uuid.UUID"); + } + + private void fillMapping(Class javaType, String zenCodeClassName) { + javaClassToZenCodeClassName.put(javaType, zenCodeClassName); + } + + +}