1 package org.jacorb.idl; 2 3 22 23 24 import java.io.File ; 25 import java.io.PrintWriter ; 26 27 import java.util.*; 28 29 33 34 public class ValueBoxDecl 35 extends Value 36 { 37 private boolean written = false; 38 private boolean parsed = false; 39 40 TypeSpec typeSpec; 41 42 public ValueBoxDecl(int num) 43 { 44 super(num); 45 pack_name = ""; 46 } 47 48 public Object clone() 49 { 50 return null; 51 } 52 53 public TypeDeclaration declaration() 54 { 55 return this; 56 }; 57 58 public String typeName() 59 { 60 if (typeName == null) 61 setPrintPhaseNames(); 62 63 if (typeSpec.typeSpec() instanceof BaseType) 64 return typeName; 65 else 66 return typeSpec.typeSpec().typeName(); 67 } 68 69 public String boxTypeName() 70 { 71 if (typeName == null) 72 setPrintPhaseNames(); 73 return typeName; 74 } 75 76 public boolean basic() 77 { 78 return false; 79 } 80 81 82 public void set_included(boolean i) 83 { 84 included = i; 85 } 86 87 88 public void setPackage(String s) 89 { 90 s = parser.pack_replace(s); 91 if (pack_name.length() > 0) 92 pack_name = s + "." + pack_name; 93 else 94 pack_name = s; 95 96 typeSpec.setPackage(s); 97 } 98 99 public void setEnclosingSymbol(IdlSymbol s) 100 { 101 if (enclosing_symbol != null && enclosing_symbol != s) 102 { 103 logger.error("was " + enclosing_symbol.getClass().getName() + 104 " now: " + s.getClass().getName()); 105 throw new RuntimeException ("Compiler Error: trying to reassign container for " + name); 106 } 107 enclosing_symbol = s; 108 } 109 110 111 public String toString() 112 { 113 return typeName(); 114 } 115 116 117 public void parse() 118 { 119 if (parsed) 120 throw new RuntimeException ("Compiler error: Value box already parsed!"); 121 122 escapeName(); 123 124 typeSpec.parse(); 125 126 try 127 { 128 ConstrTypeSpec ctspec = new ConstrTypeSpec(new_num()); 129 ctspec.c_type_spec = this; 130 131 NameTable.define(full_name(), "type"); 132 TypeMap.typedef(full_name(), ctspec); 133 } 134 catch (NameAlreadyDefined nad) 135 { 136 parser.error("Value box " + typeName() + " already defined", token); 137 } 138 139 parsed = true; 140 } 141 142 143 public String className() 144 { 145 String fullName = typeName(); 146 if (fullName.indexOf('.') > 0) 147 { 148 return fullName.substring(fullName.lastIndexOf('.') + 1); 149 } 150 else 151 { 152 return fullName; 153 } 154 } 155 156 public String printReadExpression(String streamname) 157 { 158 return "(" + typeName() + ")((org.omg.CORBA_2_3.portable.InputStream)" + streamname + ").read_value (new " + helperName() + "())"; 159 } 160 161 public String printWriteStatement(String var_name, String streamname) 162 { 163 return "((org.omg.CORBA_2_3.portable.OutputStream)" + streamname + ").write_value (" + var_name + ", new " + helperName() + "());"; 164 } 165 166 public String holderName() 167 { 168 return boxTypeName() + "Holder"; 169 } 170 171 public String helperName() 172 { 173 return boxTypeName() + "Helper"; 174 } 175 176 181 182 public String getTypeCodeExpression(Set knownTypes) 183 { 184 if (knownTypes.contains(this)) 185 { 186 return this.getRecursiveTypeCodeExpression(); 187 } 188 else 189 { 190 knownTypes.add(this); 191 StringBuffer sb = new StringBuffer (); 192 String className = boxTypeName(); 193 if (className.indexOf('.') > 0) 194 className = className.substring(className.lastIndexOf('.') + 1); 195 sb.append("org.omg.CORBA.ORB.init().create_value_box_tc(" + 196 helperName() + ".id(),\"" + className + "\"," + 197 typeSpec.typeSpec().getTypeCodeExpression() + ")"); 198 199 return sb.toString(); 200 } 201 } 202 203 public String getTypeCodeExpression() 204 { 205 return this.getTypeCodeExpression(new HashSet()); 206 } 207 208 private void printHolderClass(String className, PrintWriter ps) 209 { 210 if (Environment.JAVA14 && pack_name.equals("")) 211 lexer.emit_warn 212 ("No package defined for " + className + " - illegal in JDK1.4", token); 213 if (!pack_name.equals("")) 214 ps.println("package " + pack_name + ";"); 215 216 ps.println("public" + parser.getFinalString() + " class " + className + "Holder"); 217 ps.println("\timplements org.omg.CORBA.portable.Streamable"); 218 ps.println("{"); 219 220 ps.println("\tpublic " + typeName() + " value;\n"); 221 222 ps.println("\tpublic " + className + "Holder ()"); 223 ps.println("\t{"); 224 ps.println("\t}"); 225 226 ps.println("\tpublic " + className + "Holder (final " + typeName() + " initial)"); 227 ps.println("\t{"); 228 ps.println("\t\tvalue = initial;"); 229 ps.println("\t}"); 230 231 ps.println("\tpublic org.omg.CORBA.TypeCode _type ()"); 232 ps.println("\t{"); 233 ps.println("\t\treturn " + helperName() + ".type ();"); 234 ps.println("\t}"); 235 236 ps.println("\tpublic void _read (final org.omg.CORBA.portable.InputStream _in)"); 237 ps.println("\t{"); 238 ps.println("\t\tvalue = " + helperName() + ".read (_in);"); 239 ps.println("\t}"); 240 241 ps.println("\tpublic void _write (final org.omg.CORBA.portable.OutputStream _out)"); 242 ps.println("\t{"); 243 ps.println("\t\t" + helperName() + ".write (_out,value);"); 244 ps.println("\t}"); 245 246 ps.println("}"); 247 } 248 249 250 private void printHelperClass(String className, PrintWriter ps) 251 { 252 if (Environment.JAVA14 && pack_name.equals("")) 253 lexer.emit_warn 254 ("No package defined for " + className + " - illegal in JDK1.4", token); 255 if (!pack_name.equals("")) 256 ps.println("package " + pack_name + ";"); 257 258 ps.println("public" + parser.getFinalString() + " class " + className + "Helper"); 259 ps.println("\timplements org.omg.CORBA.portable.BoxedValueHelper"); 260 ps.println("{"); 261 ps.println("\tprivate static org.omg.CORBA.TypeCode _type = " + getTypeCodeExpression() + ";"); 262 263 String type = typeName(); 264 265 TypeSpec.printHelperClassMethods( ps, type); 266 267 printIdMethod(ps); 269 270 ps.println("\tpublic static " + type + 271 " read (final org.omg.CORBA.portable.InputStream in)"); 272 ps.println("\t{"); 273 274 if (typeSpec.typeSpec() instanceof BaseType) 275 { 276 ps.println("\t\t" + type + " result = new " + type + 277 "(" + typeSpec.typeSpec().printReadExpression("in") + ");"); 278 } 279 else 280 { 281 ps.println("\t\t" + type + " result;"); 282 ps.println("\t\t" + typeSpec.typeSpec().printReadStatement("result", "in")); 283 } 284 ps.println("\t\treturn result;"); 285 ps.println("\t}"); 286 287 288 ps.println("\tpublic static void write (final org.omg.CORBA.portable.OutputStream out, final " + type + " s)"); 289 ps.println("\t{"); 290 if (typeSpec.typeSpec() instanceof BaseType) 291 { 292 ps.println("\t\t" + typeSpec.typeSpec().printWriteStatement("s.value", "out")); 293 } 294 else 295 ps.println("\t\t" + typeSpec.typeSpec().printWriteStatement("s", "out")); 296 ps.println("\t}"); 297 298 ps.println("\tpublic java.io.Serializable read_value (final org.omg.CORBA.portable.InputStream is)"); 299 ps.println("\t{"); 300 ps.println("\t\treturn " + helperName() + ".read (is);"); 301 ps.println("\t}"); 302 303 ps.println("\tpublic void write_value (final org.omg.CORBA.portable.OutputStream os, final java.io.Serializable value)"); 304 ps.println("\t{"); 305 ps.println("\t\t" + helperName() + ".write (os, (" + type + ")value);"); 306 ps.println("\t}"); 307 308 ps.println("\tpublic java.lang.String get_id()"); 309 ps.println("\t{"); 310 ps.println("\t\treturn " + helperName() + ".id();"); 311 ps.println("\t}"); 312 ps.println("}"); 313 } 314 315 private void printValueClass(String className, PrintWriter ps) 316 { 317 String fullClassName = className; 318 319 if (Environment.JAVA14 && pack_name.equals("")) 320 lexer.emit_warn 321 ("No package defined for " + className + " - illegal in JDK1.4", token); 322 if (!pack_name.equals("")) 323 { 324 fullClassName = pack_name + "." + className; 325 326 ps.println("package " + pack_name + ";"); 327 } 328 329 ps.println("public class " + className); 330 ps.println("\timplements org.omg.CORBA.portable.ValueBase"); 331 ps.println("{"); 332 ps.println("\tpublic " + typeSpec.typeName() + " value;"); 333 ps.println("\tprivate static String[] _ids = { " + className + "Helper.id() };"); 334 335 ps.println("\tpublic " + className + "(" + typeSpec.typeName() + " initial)"); 336 ps.println("\t{"); 337 ps.println("\t\tvalue = initial;"); 338 ps.println("\t}"); 339 340 ps.println("\tpublic String[] _truncatable_ids()"); 341 ps.println("\t{"); 342 ps.println("\t\treturn _ids;"); 343 ps.println("\t}"); 344 345 ps.println("}"); 346 } 347 348 349 350 public void print(PrintWriter ps) 351 { 352 setPrintPhaseNames(); 353 354 if (included && !generateIncluded()) 356 return; 357 358 360 if (!written) 361 { 362 try 363 { 364 String className = boxTypeName(); 365 if (className.indexOf('.') > 0) 366 className = className.substring(className.lastIndexOf('.') + 1); 367 368 String path = parser.out_dir + fileSeparator + 369 pack_name.replace('.', fileSeparator); 370 371 File dir = new File (path); 372 if (!dir.exists()) 373 if (!dir.mkdirs()) 374 { 375 org.jacorb.idl.parser.fatal_error("Unable to create " + path, null); 376 } 377 378 PrintWriter decl_ps; 380 String fname = className + ".java"; 381 File f = new File (dir, fname); 382 383 if (typeSpec.typeSpec() instanceof BaseType) 384 { 385 386 if (GlobalInputStream.isMoreRecentThan(f)) 387 { 388 decl_ps = new PrintWriter (new java.io.FileWriter (f)); 389 printValueClass(className, decl_ps); 390 decl_ps.close(); 391 } 392 } 393 394 396 fname = className + "Holder.java"; 397 f = new File (dir, fname); 398 399 if (GlobalInputStream.isMoreRecentThan(f)) 400 { 401 decl_ps = new PrintWriter (new java.io.FileWriter (f)); 402 printHolderClass(className, decl_ps); 403 decl_ps.close(); 404 } 405 406 408 fname = className + "Helper.java"; 409 f = new File (dir, fname); 410 411 if (GlobalInputStream.isMoreRecentThan(f)) 412 { 413 decl_ps = new PrintWriter (new java.io.FileWriter (f)); 414 printHelperClass(className, decl_ps); 415 decl_ps.close(); 416 } 417 418 written = true; 419 } 420 catch (java.io.IOException i) 421 { 422 throw new RuntimeException ("File IO error" + i); 423 } 424 } 425 } 426 427 public void accept(IDLTreeVisitor visitor) 428 { 429 visitor.visitValue(this); 430 } 431 } 432 | Popular Tags |