-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP: move javanative type resolution into own class
- Loading branch information
Showing
3 changed files
with
68 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
...ation/src/main/java/org/openzen/zencode/java/impl/conversion/JdkJavaRuntimeConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
|
||
} |