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 @SuppressWarnings("restriction")
18 public class SCLUtil {
20 static Map<TCon,Class<?>> typeMap = new HashMap<TCon, Class<?>>();
21 // copied from org.simantics.scl.db.SCLFunctions
22 public static Class<?> possibleFromDynamic(Type expectedType, String moduleName) {
27 Failable<Module> failable = SCLOsgi.MODULE_REPOSITORY.getModule(moduleName);
28 Module module = failable.getResult();
30 RuntimeEnvironment env = SCLOsgi.MODULE_REPOSITORY.createRuntimeEnvironment(
31 EnvironmentSpecification.of(moduleName, ""), module.getParentClassLoader());
33 JavaTypeTranslator tr = new JavaTypeTranslator(env.getEnvironment());
34 TypeDesc desc = tr.toTypeDesc(expectedType);
35 String className = desc.getFullName();
36 Class<?> clazz = env.getMutableClassLoader().loadClass(className);
38 } catch (ImportFailureException e) {
40 } catch (ClassNotFoundException e) {
45 public static Class<?> possibleClass(TCon type) {
46 Class<?> clazz = typeMap.get(type);
48 clazz = possibleFromDynamic(type, type.module);
49 typeMap.put(type, clazz);
54 public static boolean javaIsInstanceOf(org.simantics.scl.compiler.types.Type t, Object o) {
55 if (t instanceof TCon) {
56 Class<?> clazz = possibleClass((TCon)t);
59 if (!clazz.isAssignableFrom(o.getClass()))
67 public static Object javaSafeCoerce(org.simantics.scl.compiler.types.Type t, Object o) {
68 if (t instanceof TCon) {
69 Class<?> clazz = possibleClass((TCon)t);
72 if (!clazz.isAssignableFrom(o.getClass()))