KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SOFA > Connector > EEG > EEM > Elements > LocalStubGen


1 /*
2  * LocalStubGen.java
3  *
4  * Created on 13. duben 2002, 21:08
5  */

6
7 package SOFA.Connector.EEG.EEM.Elements;
8
9 import SOFA.Connector.EEG.CodeWriter.JWriterDirectorException;
10 import SOFA.Connector.EEG.EEM.Actions.JIfaceCN;
11 import SOFA.Connector.EEG.EEM.Actions.JImplWriterDirectorBase;
12 import SOFA.SOFAnode.Made.TIR.OperationDef;
13
14 /**
15  *
16  * @author ghort
17  * @version
18  */

19 public class LocalStubGen extends JImplWriterDirectorBase {
20
21     /** Creates new LocalStubGen */
22     public LocalStubGen(String JavaDoc provIfaceCDL, SOFA.Connector.Property[] params) {
23         super(provIfaceCDL,params);
24     }
25
26     public String JavaDoc[] getImplements() {
27         return new String JavaDoc[] {"SOFA.Connector.EEG.Types.Stub"};
28     }
29     
30     public String JavaDoc getClassBody() {
31         String JavaDoc reqIfaceMapped=SOFA.Connector.Property.findFirst(params,"reqIfaceMapped");
32         return managerTieImpl() + "\n" +
33             "\tprotected "+reqIfaceMapped+" target;\n"+
34                      // "\tprotected Object dynTarget = null;\n"+
35
// "\tprotected java.lang.reflect.Method[] methods;\n"+
36
// "\tprotected java.util.Hashtable methodTable;\n"+
37
// "\tprotected java.util.Hashtable argsTable;\n\n"+
38
"\tpublic void link(SOFA.Connector.TaggedProfile target) {\n"+
39                      // "\t\tif (((SOFA.Connector.ECG.Profiles.LocalProfile)target).link instanceof "+reqIfaceMapped+") {\n"+
40
"\t\t\tthis.target=("+reqIfaceMapped+")((SOFA.Connector.ECG.Profiles.LocalProfile)target).link;\n"+
41                      // "\t\t\tSystem.out.println(\"KUK\");\n"+
42
// "\t\t} else {\n"+
43
// "\t\t\tdynTarget = ((SOFA.Connector.ECG.Profiles.LocalProfile)target).link;\n"+
44
// "\t\t\tmethods = dynTarget.getClass().getMethods();\n"+
45
// "\t\t\tmethodTable = SOFA.Connector.EEG.EEM.Elements.LocalStubGen.getMethodsTable(methods);\n"+
46
// "\t\t\targsTable = SOFA.Connector.EEG.EEM.Elements.LocalStubGen.getArgsTable(methods);\n"+
47
// "\t\t}\n"+
48
"\t}\n\n"+
49             "\tpublic String[] getSupportedProfiles() {\n"+
50             "\t\treturn new String[] {\"LOCAL\"};\n"+
51             "\t}\n\n"+
52                         "\tpublic org.objectweb.fractal.api.Type getFcItfType() { return null; }\n"+
53                         "\tpublic boolean isFcInternalItf() {return false; }\n"+
54                         "\tpublic String getFcItfName() { return this.target.getFcItfName(); }\n"+
55                         "\tpublic org.objectweb.fractal.api.Component getFcItfOwner() { return this.target.getFcItfOwner(); }\n\n";
56     }
57     
58     public String JavaDoc getMethodDef(OperationDef mt) throws JWriterDirectorException {
59         return JIfaceCN.getMethodDef(mt);
60     }
61
62     public String JavaDoc getMethodBody(OperationDef method) throws JWriterDirectorException {
63         return
64                     // "\t\tif (dynTarget == null) {\n"+
65
"\t\t"+declareRet(method,"ret")+"\n"+
66             "\t\t"+assignRet(method,"ret")+targetCall(method,JIfaceCN.targetCallParams(method))+";\n"+
67             "\t\t"+returnRet(method,"ret")+"\n";
68                     // "\t\t} else {\n"+
69
// getDynMethodBody(method)+
70
// "\t\t}\n";
71
}
72
73
74    /* public static java.util.Hashtable getMethodsTable(java.lang.reflect.Method[] m) {
75           java.util.Hashtable ret = new java.util.Hashtable();
76           for (int i=0; i<m.length; i++) {
77             ret.put(m[i].getName(), m[i]);
78           }
79           return ret;
80         }
81
82         public static java.util.Hashtable getArgsTable(java.lang.reflect.Method[] m) {
83           java.util.Hashtable ret = new java.util.Hashtable();
84           for (int i=0; i<m.length; i++) {
85             ret.put(m[i].getName(), new Object [m[i].getParameterTypes().length]);
86           }
87           return ret;
88         }
89
90         public String getDynMethodBody(OperationDef method) throws JWriterDirectorException {
91           StringBuffer ret = new StringBuffer();
92           try {
93             String methodName = method.get_identification().name();
94             ret.append("\t\tjava.lang.reflect.Method method = (java.lang.reflect.Method) methodTable.get(\""+methodName+"\");\n");
95             ret.append("\t\tObject[] args = (Object[]) argsTable.get(\""+methodName+"\");\n");
96             ParamDescr[] params = method.params();
97             int i;
98             for(i=0; i<params.length; i++) {
99               CDLType type = params[i].type();
100               ParamMode mode = params[i].mode();
101               String name = params[i].name();
102               ret.append("\t\targs["+Integer.toString(i)+"] = ");
103               if ( (type instanceof PrimitiveDef) & (mode.value() == ParamMode.PARAM_IN)) {
104                 ret.append(getPrimitParam(type, name)+";\n");
105               } else {
106                 ret.append(name+";\n");
107               }
108             }
109             ret.append("\t\targs["+Integer.toString(i)+"] = thr_id;");
110             ret.append("\t\ttry {\n");
111             StringBuffer methodCall = new StringBuffer("method.invoke(dynTarget, args)");
112             
113             
114             CDLType result = method.result();
115             if (result instanceof PrimitiveDef) {
116               ret.append(getPrimitResult(result, methodCall.toString()));
117             } else {
118               ret.append("\t\t\treturn ("+CDL2JavaMapping.getTypeName(result,ParamMode.PARAM_IN)+") "+methodCall.toString()+";\n" );
119             }
120             ret.append("\t\t} catch (IllegalAccessException _e) {\n\t\t\tthrow new SOFA.Connector.ConnectorTransportException(\"IllegalAccessException\", _e);\n\t\t} catch (java.lang.reflect.InvocationTargetException _e) {\n");
121             ExceptionDef[] exceptions = method.exceptions();
122             for ( i=0; i<exceptions.length; i++) {
123               String mapEx = CDL2JavaMapping.getTypeName(exceptions[i],ParamMode.PARAM_IN);
124               ret.append("\t\t\tif (_e.getTargetException() instanceof "+mapEx+") throw ("+mapEx+") _e.getTargetException();\n");
125             }
126             ret.append("\t\tthrow new SOFA.Connector.ConnectorTransportException(\"Unrecognized InvocationTargetException\", _e);\n");
127             ret.append("\t\t}\n");
128           } catch (java.rmi.RemoteException e) {
129             throw new JWriterDirectorException("Cannot access TIR.",e);
130           } catch (SOFA.SOFAnode.Made.TIR.TIRExceptLock e) {
131             throw new JWriterDirectorException("Cannot access TIR.",e);
132           } catch (SOFA.Connector.EEG.CodeWriter.CodeWriterException e) {
133             throw new JWriterDirectorException("Cannot map type.",e);
134           }
135           return ret.toString();
136         }
137
138         public String getPrimitParam(CDLType type, String name) throws java.rmi.RemoteException {
139           switch (((PrimitiveDef)type).kind().value()) {
140             case PrimitiveKind.pk_string:
141             case PrimitiveKind.pk_wstring:
142             case PrimitiveKind.pk_any:
143             case PrimitiveKind.pk_object:
144               return name;
145             case PrimitiveKind.pk_short:
146             case PrimitiveKind.pk_ushort:
147               return "new Short("+name+")";
148             case PrimitiveKind.pk_long:
149             case PrimitiveKind.pk_ulong:
150               return "new Integer("+name+")";
151             case PrimitiveKind.pk_longlong:
152             case PrimitiveKind.pk_ulonglong:
153               return "new Long("+name+")";
154             case PrimitiveKind.pk_float:
155               return "new Float("+name+")";
156             case PrimitiveKind.pk_double:
157               return "new Double("+name+")";
158             case PrimitiveKind.pk_boolean:
159               return "new Boolean("+name+")";
160             case PrimitiveKind.pk_char:
161             case PrimitiveKind.pk_wchar:
162               return "new Character("+name+")";
163             case PrimitiveKind.pk_octet:
164               return "new Byte("+name+")";
165           }
166           return name;
167         }
168
169         public String getPrimitResult(CDLType type, String methodCall) throws java.rmi.RemoteException {
170           switch (((PrimitiveDef)type).kind().value()) {
171             case PrimitiveKind.pk_void:
172               return ("\t\t\t"+methodCall+";\n");
173             case PrimitiveKind.pk_string:
174             case PrimitiveKind.pk_wstring:
175               return ("\t\t\treturn (String) "+methodCall+";\n");
176             case PrimitiveKind.pk_any:
177               return ("\t\t\treturn ("+SOFA.SOFAnode.Made.CodeGen.GeneratedObject.stdPackage+"Any)"+methodCall+";\n");
178             case PrimitiveKind.pk_object:
179               return ("\t\t\treturn ("+SOFA.SOFAnode.Made.CodeGen.GeneratedObject.stdPackage+"Object)"+methodCall+";\n");
180             case PrimitiveKind.pk_short:
181             case PrimitiveKind.pk_ushort:
182               return ("\t\t\treturn ((Short) "+methodCall+").shortValue();\n");
183             case PrimitiveKind.pk_long:
184             case PrimitiveKind.pk_ulong:
185               return ("\t\t\treturn ((Integer) "+methodCall+").intValue();\n");
186             case PrimitiveKind.pk_longlong:
187             case PrimitiveKind.pk_ulonglong:
188               return ("\t\t\treturn ((Long) "+methodCall+").longValue();\n");
189             case PrimitiveKind.pk_float:
190               return ("\t\t\treturn ((Float) "+methodCall+").floatValue();\n");
191             case PrimitiveKind.pk_double:
192               return ("\t\t\treturn ((Double) "+methodCall+").doubleValue();\n");
193             case PrimitiveKind.pk_boolean:
194               return ("\t\t\treturn ((Boolean) "+methodCall+").booleanValue();\n");
195             case PrimitiveKind.pk_char:
196             case PrimitiveKind.pk_wchar:
197               return ("\t\t\treturn ((Character) "+methodCall+").charValue();\n");
198             case PrimitiveKind.pk_octet:
199               return ("\t\t\treturn ((Byte) "+methodCall+").ByteValue();\n");
200           }
201           return ("\t\t\t"+methodCall+";\n");
202         } */

203 }
204
Popular Tags