KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > interop > rmi > iiop > compiler > Compiler


1 /**
2  *
3  * Copyright 2004-2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.geronimo.interop.rmi.iiop.compiler;
19
20 import java.lang.reflect.Method JavaDoc;
21 import java.util.*;
22 import java.io.File JavaDoc;
23
24 import org.apache.geronimo.interop.generator.GenOptions;
25 import org.apache.geronimo.interop.generator.JParameter;
26 import org.apache.geronimo.interop.generator.JVariable;
27
28 public class Compiler {
29     protected GenOptions genOptions;
30
31     private ClassLoader JavaDoc classLoader;
32
33     private static HashMap readMethods;
34     private static HashMap writeMethods;
35     private static HashMap overloadTypes;
36
37     static {
38         readMethods = new HashMap();
39         readMethods.put("boolean", "readBoolean");
40         readMethods.put("char", "readChar");
41         readMethods.put("byte", "readByte");
42         readMethods.put("short", "readShort");
43         readMethods.put("int", "readInt");
44         readMethods.put("long", "readLong");
45         readMethods.put("float", "readFloat");
46         readMethods.put("double", "readDouble");
47
48         writeMethods = new HashMap();
49         writeMethods.put("boolean", "writeBoolean");
50         writeMethods.put("char", "writeChar");
51         writeMethods.put("byte", "writeByte");
52         writeMethods.put("short", "writeShort");
53         writeMethods.put("int", "writeInt");
54         writeMethods.put("long", "writeLong");
55         writeMethods.put("float", "writeFloat");
56         writeMethods.put("double", "writeDouble");
57
58         overloadTypes = new HashMap();
59         overloadTypes.put("boolean", "boolean");
60         overloadTypes.put("byte", "octet");
61         overloadTypes.put("char", "wchar");
62         overloadTypes.put("double", "double");
63         overloadTypes.put("float", "float");
64         overloadTypes.put("int", "long");
65         overloadTypes.put("long", "long_long");
66         overloadTypes.put("short", "short");
67         overloadTypes.put("java.lang.Class", "javax_rmi_CORBA.ClassDesc");
68         overloadTypes.put("java.lang.String", "CORBA.WStringValue");
69         overloadTypes.put("org.omg.CORBA.Object", "Object");
70         overloadTypes.put("org.omg.CORBA.Any", "org_omg_boxedIDL_CORBA.Any");
71         overloadTypes.put("org.omg.CORBA.TypeCode", "org_omg_boxedIDL_CORBA.TypeCode");
72     }
73
74     public Compiler(GenOptions go, ClassLoader JavaDoc cl) {
75         classLoader = cl;
76         if (classLoader == null) {
77             classLoader = ClassLoader.getSystemClassLoader();
78         }
79
80         genOptions = go;
81     }
82
83     public GenOptions getGenOptions() {
84         return genOptions;
85     }
86
87     public ClassLoader JavaDoc getClassLoader() {
88         return classLoader;
89     }
90
91     public JParameter[] getMethodParms(Method JavaDoc m) {
92         Class JavaDoc p[] = m.getParameterTypes();
93         JParameter parms[] = null;
94
95         if (p != null) {
96             parms = new JParameter[p.length];
97
98             int i;
99             for (i = 0; i < p.length; i++) {
100                 parms[i] = new JParameter(p[i], "p" + i);
101             }
102         }
103
104         return parms;
105     }
106
107     protected String JavaDoc getReadMethod(JVariable v) {
108         String JavaDoc rc = null;
109
110         if (v != null) {
111             rc = (String JavaDoc) readMethods.get(v.getTypeDecl());
112         }
113
114         return rc;
115     }
116
117     protected String JavaDoc getWriteMethod(JVariable v) {
118         String JavaDoc rc = null;
119
120         if (v != null) {
121             rc = (String JavaDoc) writeMethods.get(v.getTypeDecl());
122         }
123
124         return rc;
125     }
126
127     protected static void error( String JavaDoc errMsg ) {
128         System.err.println( "Error: " + errMsg );
129         System.exit(1);
130     }
131
132     protected void error(String JavaDoc msg, Throwable JavaDoc t) {
133         error(msg);
134         t.printStackTrace();
135     }
136
137     protected static void warn( String JavaDoc warnMsg ) {
138         System.out.println( "Warning: " + warnMsg );
139     }
140
141     protected String JavaDoc adjustPath( String JavaDoc path )
142     {
143         // Maybe it would be easier if GenOptions just made sure that platform path
144
// separators and file separators were as required on the platform?
145

146         if (File.separatorChar == '/') {
147             // We're under Unix, change '\\' to '/'
148
return path.replace( '\\', '/' );
149         } else {
150             // We're under Windows, change '/' to '\\'
151
return path.replace( '/', '\\' );
152         }
153     }
154
155     protected void addMethodsToList( ArrayList list, Method JavaDoc[] methods )
156     {
157         for(int i=0; list != null && methods != null && i < methods.length; i++ )
158         {
159             list.add( methods[i] );
160         }
161     }
162
163     protected void collectInterfaceMethods( ArrayList list, Class JavaDoc intfClass, boolean simpleIdl )
164     {
165         Method JavaDoc myMethods[] = intfClass.getDeclaredMethods();
166
167         if (!simpleIdl)
168         {
169             addMethodsToList( list, myMethods );
170         }
171
172         Class JavaDoc myInterfaces[] = intfClass.getInterfaces();
173         if (myInterfaces != null && myInterfaces.length > 0)
174         {
175             String JavaDoc opsName = intfClass.getName() + "Operations";
176
177             for (int i = 0; i < myInterfaces.length; i++)
178             {
179                 if (simpleIdl)
180                 {
181                     // add interface and its Operations, only if there is a coresponding Operations
182
if (myInterfaces[i].getName().equals(opsName))
183                     {
184                         addMethodsToList( list, myMethods );
185                         addMethodsToList( list, myInterfaces[i].getDeclaredMethods() );
186                         continue;
187                     }
188                     else
189                     {
190                         collectInterfaceMethods( list, myInterfaces[i], simpleIdl );
191                     }
192                 }
193                 else
194                 {
195                     // Collect the interface methods for all interfaces ..
196
collectInterfaceMethods( list, myInterfaces[i], simpleIdl );
197                 }
198             }
199         }
200     }
201
202     protected Method JavaDoc[] getMethods(Class JavaDoc intfClass, boolean isSimpleIdl)
203     {
204         Method JavaDoc myMethods[] = intfClass.getDeclaredMethods();
205         ArrayList list = new ArrayList( myMethods.length * 2 );
206
207         collectInterfaceMethods( list, intfClass, isSimpleIdl );
208
209         Object JavaDoc[] objs = list.toArray();
210         Method JavaDoc[] methods = new Method JavaDoc[objs.length];
211         System.arraycopy( objs, 0, methods, 0, objs.length );
212
213         return methods;
214     }
215
216     public MethodOverload[] getMethodOverloads(Class JavaDoc intfCalss, boolean isSimpleIdl) {
217         Method JavaDoc[] methods = getMethods(intfCalss, isSimpleIdl);
218         MethodOverload[] methodOverloads = getMethodOverloads(methods);
219         return methodOverloads;
220     }
221
222     public MethodOverload[] getMethodOverloads( Method JavaDoc methods[] )
223     {
224         HashMap hm = new HashMap( methods.length );
225
226         // Put all the methods into the hashmap
227
for( int i=0; methods != null && i < methods.length; i++ )
228         {
229             ArrayList al = (ArrayList)hm.get( methods[i].getName() );
230             if (al == null)
231             {
232                 al = new ArrayList( methods.length );
233                 al.add( methods[i] );
234                 hm.put( methods[i].getName(), al );
235             }
236             else
237             {
238                 al.add( methods[i] );
239             }
240         }
241
242         Set keySet = hm.keySet();
243         ArrayList overloadList = new ArrayList( methods.length );
244         for (Iterator keyIt = keySet.iterator(); keyIt != null && keyIt.hasNext(); )
245         {
246             ArrayList al = (ArrayList)hm.get( keyIt.next() );
247             if (al.size() == 1)
248             {
249                 Method JavaDoc m = (Method JavaDoc)al.remove(0);
250                 overloadList.add( new MethodOverload( m.getName(), m ) );
251             }
252             else
253             {
254                 for( int i=0; i<=al.size(); i++ )
255                 {
256                     Method JavaDoc m = (Method JavaDoc)al.remove(0);
257                     overloadList.add( new MethodOverload( overloadMethodName(m), m ) );
258                 }
259             }
260         }
261
262         Object JavaDoc obj[] = overloadList.toArray();
263         MethodOverload m[] = new MethodOverload[ obj.length ];
264         System.arraycopy( obj, 0, m, 0, obj.length );
265
266         return m;
267     }
268
269     protected String JavaDoc overloadMethodName( Method JavaDoc m )
270     {
271         Class JavaDoc parms[] = m.getParameterTypes();
272         String JavaDoc name = m.getName() + "_";
273         for( int i=0; i<parms.length; i++ )
274         {
275             name += "_" + parms[i].getName();
276         }
277         return name.replace( '.', '_' );
278     }
279
280     public class MethodOverload
281     {
282         public Method JavaDoc method;
283         public String JavaDoc iiop_name;
284
285         public MethodOverload( String JavaDoc iiop_name, Method JavaDoc method )
286         {
287             this.method = method;
288             this.iiop_name = iiop_name;
289         }
290
291         public int hashCode()
292         {
293             return iiop_name.hashCode();
294         }
295
296         public boolean equals( Object JavaDoc other )
297         {
298             if (other instanceof MethodOverload)
299             {
300                 MethodOverload mother = (MethodOverload)other;
301                 if (iiop_name != null)
302                 {
303                     return iiop_name.equals( mother.iiop_name );
304                 }
305                 else
306                 {
307                     return iiop_name == mother.iiop_name;
308                 }
309             }
310
311             return false;
312         }
313     }
314 }
315
Popular Tags