1 18 package org.apache.geronimo.interop.rmi.iiop.compiler; 19 20 import java.lang.reflect.Method ; 21 import java.util.*; 22 import java.io.File ; 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 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 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 getClassLoader() { 88 return classLoader; 89 } 90 91 public JParameter[] getMethodParms(Method m) { 92 Class 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 getReadMethod(JVariable v) { 108 String rc = null; 109 110 if (v != null) { 111 rc = (String ) readMethods.get(v.getTypeDecl()); 112 } 113 114 return rc; 115 } 116 117 protected String getWriteMethod(JVariable v) { 118 String rc = null; 119 120 if (v != null) { 121 rc = (String ) writeMethods.get(v.getTypeDecl()); 122 } 123 124 return rc; 125 } 126 127 protected static void error( String errMsg ) { 128 System.err.println( "Error: " + errMsg ); 129 System.exit(1); 130 } 131 132 protected void error(String msg, Throwable t) { 133 error(msg); 134 t.printStackTrace(); 135 } 136 137 protected static void warn( String warnMsg ) { 138 System.out.println( "Warning: " + warnMsg ); 139 } 140 141 protected String adjustPath( String path ) 142 { 143 146 if (File.separatorChar == '/') { 147 return path.replace( '\\', '/' ); 149 } else { 150 return path.replace( '/', '\\' ); 152 } 153 } 154 155 protected void addMethodsToList( ArrayList list, Method [] 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 intfClass, boolean simpleIdl ) 164 { 165 Method myMethods[] = intfClass.getDeclaredMethods(); 166 167 if (!simpleIdl) 168 { 169 addMethodsToList( list, myMethods ); 170 } 171 172 Class myInterfaces[] = intfClass.getInterfaces(); 173 if (myInterfaces != null && myInterfaces.length > 0) 174 { 175 String opsName = intfClass.getName() + "Operations"; 176 177 for (int i = 0; i < myInterfaces.length; i++) 178 { 179 if (simpleIdl) 180 { 181 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 collectInterfaceMethods( list, myInterfaces[i], simpleIdl ); 197 } 198 } 199 } 200 } 201 202 protected Method [] getMethods(Class intfClass, boolean isSimpleIdl) 203 { 204 Method myMethods[] = intfClass.getDeclaredMethods(); 205 ArrayList list = new ArrayList( myMethods.length * 2 ); 206 207 collectInterfaceMethods( list, intfClass, isSimpleIdl ); 208 209 Object [] objs = list.toArray(); 210 Method [] methods = new Method [objs.length]; 211 System.arraycopy( objs, 0, methods, 0, objs.length ); 212 213 return methods; 214 } 215 216 public MethodOverload[] getMethodOverloads(Class intfCalss, boolean isSimpleIdl) { 217 Method [] methods = getMethods(intfCalss, isSimpleIdl); 218 MethodOverload[] methodOverloads = getMethodOverloads(methods); 219 return methodOverloads; 220 } 221 222 public MethodOverload[] getMethodOverloads( Method methods[] ) 223 { 224 HashMap hm = new HashMap( methods.length ); 225 226 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 m = (Method )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 m = (Method )al.remove(0); 257 overloadList.add( new MethodOverload( overloadMethodName(m), m ) ); 258 } 259 } 260 } 261 262 Object 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 overloadMethodName( Method m ) 270 { 271 Class parms[] = m.getParameterTypes(); 272 String 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 method; 283 public String iiop_name; 284 285 public MethodOverload( String iiop_name, Method 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 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 |