]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.reflection/src/org/simantics/scl/reflection/internal/typeRegistry/TypeRegistry.java
e7eb1486c88d9ae4e908c9116cc6bf8f67606c23
[simantics/platform.git] / bundles / org.simantics.scl.reflection / src / org / simantics / scl / reflection / internal / typeRegistry / TypeRegistry.java
1 package org.simantics.scl.reflection.internal.typeRegistry;
2
3 import gnu.trove.set.hash.THashSet;
4
5 import java.util.List;
6 import java.util.concurrent.ConcurrentHashMap;
7
8 import org.simantics.scl.compiler.types.TCon;
9 import org.simantics.scl.compiler.types.Types;
10 import org.simantics.scl.reflection.TypeNotFoundException;
11 import org.simantics.scl.reflection.internal.registry.BindingRegistry;
12
13 public class TypeRegistry {
14     private static final ConcurrentHashMap<TCon, Class<?>> map = 
15             new ConcurrentHashMap<TCon, Class<?>>();
16     
17     private static final THashSet<String> builtins = new THashSet<String>();
18     
19     static {
20         map.put(Types.BOOLEAN, Boolean.class);        
21         map.put(Types.BYTE, Byte.class);
22         map.put(Types.CHARACTER, Character.class);
23         map.put(Types.SHORT, Short.class);
24         map.put(Types.INTEGER, Integer.class);
25         map.put(Types.LONG, Long.class);
26         map.put(Types.FLOAT, Float.class);
27         map.put(Types.DOUBLE, Double.class);
28         map.put(Types.STRING, String.class);
29         map.put(Types.BYTE_ARRAY, byte[].class);
30         
31         map.put(Types.LIST, List.class);
32         for(int i=0;i<8;++i)
33             if(i != 1)
34                 try {
35                     map.put(Types.tupleConstructor(i), 
36                             Class.forName("org.simantics.scl.runtime.tuple.Tuple" + i));
37                 } catch(ClassNotFoundException e) {
38                     e.printStackTrace();
39                 }
40         
41         for(TCon con : map.keySet())
42             builtins.add(con.name);
43     }
44     
45     public static Class<?> getClass(TCon type) throws TypeNotFoundException {
46         Class<?> clazz = map.get(type);
47         if(clazz == null) {
48             try {
49                 clazz = BindingRegistry.getClass(type);
50             } catch(TypeNotFoundException e) {
51                 e.printStackTrace();
52                 throw e;
53             }
54             map.put(type, clazz);
55         }
56         return clazz;
57     }
58     
59     public static boolean isBuiltin(String name) {
60         return builtins.contains(name);
61     }
62 }