| 1 package org.jicengine.builder; 2 3 import org.jicengine.operation.ReflectionUtils; 4 5 import org.jicengine.element.*; 6 7 import java.lang.reflect.InvocationTargetException ; 8 import java.util.*; 9 10 21 22 public class TypeManagerImpl implements TypeManager { 23 24 private Map typeNamesToClassesMap; 25 26 public TypeManagerImpl(Map elements) 27 { 28 this.typeNamesToClassesMap = elements; 29 } 30 31 public ElementCompiler createCompiler(String elementName, Location location, Type elementType) throws ElementException { 32 Class elementClass = (Class ) this.typeNamesToClassesMap.get(elementType.getName()); 33 if( elementClass == null ){ 34 throw new AttributeException("Type named '" + elementType.getName() + "' not supported. use " + this.typeNamesToClassesMap.keySet(), elementName, location); 35 } 36 37 String [] typeParameters = elementType.getParameters(); 38 39 Object [] parameters; 40 if( typeParameters.length > 0 ){ 41 parameters = new Object [3]; 42 parameters[0] = elementName; 43 parameters[1] = location; 44 parameters[2] = typeParameters; 45 } 46 else { 47 parameters = new Object [2]; 48 parameters[0] = elementName; 49 parameters[1] = location; 50 } 51 52 try { 53 ElementCompiler element = (ElementCompiler) org.jicengine.operation.ReflectionUtils.instantiate(elementClass, parameters); 54 return element; 55 } catch (InvocationTargetException e1){ 56 Throwable targetException = e1.getTargetException(); 57 if( targetException instanceof ElementException){ 58 throw (ElementException) targetException; 59 } 60 else { 61 throw new RuntimeException ("Failed to create element of type " + elementType.getSignature(),e1); 62 } 63 } catch (Exception e2){ 64 throw new RuntimeException ("Failed to create element of type " + elementType.getSignature(),e2); 65 } 66 } 67 } 68 | Popular Tags |