KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jicengine > builder > TypeManagerImpl


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 JavaDoc;
8 import java.util.*;
9
10 /**
11  *
12  *
13  * <p>
14  * Copyright (C) 2004 Timo Laitinen
15  * </p>
16  * @author Timo Laitinen
17  * @created 2004-09-20
18  * @since JICE-0.10
19  *
20  */

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 JavaDoc elementName, Location location, Type elementType) throws ElementException {
32         Class JavaDoc elementClass = (Class JavaDoc) 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 JavaDoc[] typeParameters = elementType.getParameters();
38
39         Object JavaDoc[] parameters;
40         if( typeParameters.length > 0 ){
41             parameters = new Object JavaDoc[3];
42             parameters[0] = elementName;
43             parameters[1] = location;
44             parameters[2] = typeParameters;
45         }
46         else {
47             parameters = new Object JavaDoc[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 JavaDoc e1){
56       Throwable JavaDoc targetException = e1.getTargetException();
57       if( targetException instanceof ElementException){
58         throw (ElementException) targetException;
59       }
60       else {
61         throw new RuntimeException JavaDoc("Failed to create element of type " + elementType.getSignature(),e1);
62       }
63         } catch (Exception JavaDoc e2){
64             throw new RuntimeException JavaDoc("Failed to create element of type " + elementType.getSignature(),e2);
65         }
66     }
67 }
68
Popular Tags