Skip to content

Commit

Permalink
WIP: move javanative type resolution into own class
Browse files Browse the repository at this point in the history
  • Loading branch information
kindlich committed Oct 18, 2024
1 parent 6e9bd58 commit a7cf3d7
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<String> name, int depth) {
if (depth >= name.size())
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,13 @@ public class JavaRuntimeTypeConverterImpl implements JavaRuntimeTypeConverter {
private final Map<Class<?>, TypeID> typeByClass = new HashMap<>();
private final Map<Class<?>, TypeID> unsignedByClass = new HashMap<>();
private final Map<Class<?>, Function<TypeID[], TypeID>> 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();
}
Expand Down Expand Up @@ -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<TypeSymbol> typeID = jdkJavaRuntimeConverter.resolveJavaType(cls);
if (typeID.isPresent()) {
return typeID.get();
}
if (cls == Object.class) {
TypeSymbol result = packageInfo.getRoot().getImport(Arrays.asList("stdlib", "Object"), 0);
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Class<?>, String> javaClassToZenCodeClassName = new HashMap<>();

public JdkJavaRuntimeConverter(ZSPackage rootPackage) {
this.rootPackage = rootPackage;
fillJavaClassToZenCodeClassName();
}

public Optional<TypeSymbol> 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);
}


}

0 comments on commit a7cf3d7

Please sign in to comment.