gnu.trove3;bundle-version="3.0.0",
org.simantics.objmap2;bundle-version="1.0.0",
org.eclipse.nebula.widgets.tablecombo;bundle-version="1.0.0",
- org.simantics.ui;bundle-version="1.0.0"
+ org.simantics.ui;bundle-version="1.0.0",
+ org.simantics.scl.osgi;bundle-version="1.0.4"
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Bundle-ActivationPolicy: lazy
Export-Package: org.simantics.g3d,
--- /dev/null
+package org.simantics.g3d.scl;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.cojen.classfile.TypeDesc;
+import org.simantics.scl.compiler.environment.specification.EnvironmentSpecification;
+import org.simantics.scl.compiler.errors.Failable;
+import org.simantics.scl.compiler.internal.codegen.types.JavaTypeTranslator;
+import org.simantics.scl.compiler.module.Module;
+import org.simantics.scl.compiler.module.repository.ImportFailureException;
+import org.simantics.scl.compiler.runtime.RuntimeEnvironment;
+import org.simantics.scl.compiler.types.TCon;
+import org.simantics.scl.compiler.types.Type;
+import org.simantics.scl.osgi.SCLOsgi;
+
+public class SCLUtil {
+
+ static Map<TCon,Class<?>> typeMap = new HashMap<TCon, Class<?>>();
+ // copied from org.simantics.scl.db.SCLFunctions
+ public static Class<?> possibleFromDynamic(Type expectedType, String moduleName) {
+
+ try {
+
+
+ Failable<Module> failable = SCLOsgi.MODULE_REPOSITORY.getModule(moduleName);
+ Module module = failable.getResult();
+
+ RuntimeEnvironment env = SCLOsgi.MODULE_REPOSITORY.createRuntimeEnvironment(
+ EnvironmentSpecification.of(moduleName, ""), module.getParentClassLoader());
+
+ JavaTypeTranslator tr = new JavaTypeTranslator(env.getEnvironment());
+ TypeDesc desc = tr.toTypeDesc(expectedType);
+ String className = desc.getFullName();
+ Class<?> clazz = env.getMutableClassLoader().loadClass(className);
+ return clazz;
+ } catch (ImportFailureException e) {
+ return null;
+ } catch (ClassNotFoundException e) {
+ return null;
+ }
+ }
+
+ public static Class<?> possibleClass(TCon type) {
+ Class<?> clazz = typeMap.get(type);
+ if (clazz == null) {
+ clazz = possibleFromDynamic(type, type.module);
+ typeMap.put(type, clazz);
+ }
+ return clazz;
+ }
+
+ public static boolean javaIsInstanceOf(org.simantics.scl.compiler.types.Type t, Object o) {
+ if (t instanceof TCon) {
+ Class<?> clazz = possibleClass((TCon)t);
+ if (clazz == null)
+ return false;
+ if (!clazz.isAssignableFrom(o.getClass()))
+ return false;
+
+ return true;
+ }
+ return false;
+ }
+
+ public static Object javaSafeCoerce(org.simantics.scl.compiler.types.Type t, Object o) {
+ if (t instanceof TCon) {
+ Class<?> clazz = possibleClass((TCon)t);
+ if (clazz == null)
+ return null;
+ if (!clazz.isAssignableFrom(o.getClass()))
+ return null;
+
+ return o;
+ }
+ return null;
+ }
+
+}