]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.g3d/src/org/simantics/g3d/scl/SCLUtil.java
Compiler warning elimination
[simantics/3d.git] / org.simantics.g3d / src / org / simantics / g3d / scl / SCLUtil.java
1 package org.simantics.g3d.scl;
2
3 import java.util.HashMap;
4 import java.util.Map;
5
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;
16
17 @SuppressWarnings("restriction")
18 public class SCLUtil {
19         
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) {
23                 
24                 try {
25
26                         
27                         Failable<Module> failable = SCLOsgi.MODULE_REPOSITORY.getModule(moduleName);
28                         Module module = failable.getResult();
29                         
30                         RuntimeEnvironment env = SCLOsgi.MODULE_REPOSITORY.createRuntimeEnvironment(
31                                         EnvironmentSpecification.of(moduleName, ""), module.getParentClassLoader());
32
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);
37                         return clazz;
38                 } catch (ImportFailureException e) {
39                         return null;
40                 } catch (ClassNotFoundException e) {
41                         return null;
42                 }
43         }
44         
45         public static Class<?> possibleClass(TCon type) {
46                 Class<?> clazz = typeMap.get(type);
47                 if (clazz == null) {
48                         clazz = possibleFromDynamic(type, type.module);
49                         typeMap.put(type, clazz);
50                 }
51                 return clazz;
52         }
53         
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);
57                         if (clazz == null)
58                                 return false;
59                         if (!clazz.isAssignableFrom(o.getClass()))
60                                 return false;
61         
62                         return true;
63                 }
64                 return false;
65         }
66         
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);
70                         if (clazz == null)
71                                 return null;
72                         if (!clazz.isAssignableFrom(o.getClass()))
73                                 return null;
74         
75                         return o;
76                 }
77                 return null;
78         }
79
80 }