1 package org.simantics.g3d.scl;
3 import java.util.HashMap;
6 import org.cojen.classfile.TypeDesc;
7 import org.simantics.scl.compiler.environment.specification.EnvironmentSpecification;
8 import org.simantics.scl.compiler.errors.Failable;
9 import org.simantics.scl.compiler.internal.codegen.types.JavaTypeTranslator;
10 import org.simantics.scl.compiler.module.Module;
11 import org.simantics.scl.compiler.module.repository.ImportFailureException;
12 import org.simantics.scl.compiler.runtime.RuntimeEnvironment;
13 import org.simantics.scl.compiler.types.TCon;
14 import org.simantics.scl.compiler.types.Type;
15 import org.simantics.scl.osgi.SCLOsgi;
17 public class SCLUtil {
19 static Map<TCon,Class<?>> typeMap = new HashMap<TCon, Class<?>>();
20 // copied from org.simantics.scl.db.SCLFunctions
21 public static Class<?> possibleFromDynamic(Type expectedType, String moduleName) {
26 Failable<Module> failable = SCLOsgi.MODULE_REPOSITORY.getModule(moduleName);
27 Module module = failable.getResult();
29 RuntimeEnvironment env = SCLOsgi.MODULE_REPOSITORY.createRuntimeEnvironment(
30 EnvironmentSpecification.of(moduleName, ""), module.getParentClassLoader());
32 JavaTypeTranslator tr = new JavaTypeTranslator(env.getEnvironment());
33 TypeDesc desc = tr.toTypeDesc(expectedType);
34 String className = desc.getFullName();
35 Class<?> clazz = env.getMutableClassLoader().loadClass(className);
37 } catch (ImportFailureException e) {
39 } catch (ClassNotFoundException e) {
44 public static Class<?> possibleClass(TCon type) {
45 Class<?> clazz = typeMap.get(type);
47 clazz = possibleFromDynamic(type, type.module);
48 typeMap.put(type, clazz);
53 public static boolean javaIsInstanceOf(org.simantics.scl.compiler.types.Type t, Object o) {
54 if (t instanceof TCon) {
55 Class<?> clazz = possibleClass((TCon)t);
58 if (!clazz.isAssignableFrom(o.getClass()))
66 public static Object javaSafeCoerce(org.simantics.scl.compiler.types.Type t, Object o) {
67 if (t instanceof TCon) {
68 Class<?> clazz = possibleClass((TCon)t);
71 if (!clazz.isAssignableFrom(o.getClass()))