1 29 30 package com.caucho.quercus.env; 31 32 import com.caucho.quercus.expr.Expr; 33 import com.caucho.quercus.program.AbstractFunction; 34 import com.caucho.vfs.WriteStream; 35 36 import java.io.IOException ; 37 import java.io.ObjectInputStream ; 38 import java.io.ObjectOutputStream ; 39 import java.io.Serializable ; 40 import java.util.ArrayList ; 41 import java.util.Collection ; 42 import java.util.IdentityHashMap ; 43 import java.util.Map ; 44 import java.util.Set ; 45 46 49 public class CompiledObjectValue extends ObjectValue 50 implements Serializable 51 { 52 private static final StringValue TO_STRING = new StringValueImpl("__toString"); 53 private static final Value []NULL_FIELDS = new Value[0]; 54 55 private QuercusClass _cl; 56 57 public Value []_fields; 58 59 private ObjectExtValue _object; 60 61 public CompiledObjectValue(QuercusClass cl) 62 { 63 _cl = cl; 64 65 int size = cl.getFieldSize(); 66 if (size != 0) 67 _fields = new Value[cl.getFieldSize()]; 68 else 69 _fields = NULL_FIELDS; 70 } 71 72 75 public String getName() 76 { 77 return _cl.getName(); 78 } 79 80 83 public String getParentName() 84 { 85 return _cl.getParentName(); 86 } 87 88 91 public String getType() 92 { 93 return "object"; 94 } 95 96 99 public boolean toBoolean() 100 { 101 return true; 102 } 103 104 107 public boolean isA(String name) 108 { 109 return _cl.isA(name); 110 } 111 112 115 public boolean isObject() 116 { 117 return true; 118 } 119 120 123 public long toLong() 124 { 125 return 1; 126 } 127 128 131 public double toDouble() 132 { 133 return toLong(); 134 } 135 136 public CompiledObjectValue toObjectValue() 137 { 138 return this; 139 } 140 141 144 public int getSize() 145 { 146 int size = 0; 147 148 for (int i = 0; i < _fields.length; i++) { 149 if (_fields[i] != UnsetValue.UNSET) 150 size++; 151 } 152 153 if (_object != null) 154 size += _object.getSize(); 155 156 return size; 157 } 158 159 162 @Override 163 public Value getField(Env env, String key) 164 { 165 if (_fields.length > 0) { 166 int index = _cl.findFieldIndex(key); 167 168 if (index >= 0) 169 return _fields[index].toValue(); 170 } 171 172 if (_object != null) 173 return _object.getField(env, key); 174 else 175 return UnsetValue.UNSET; 176 } 177 178 181 public Var getFieldRef(Env env, String key) 182 { 183 if (_fields.length > 0) { 184 int index = _cl.findFieldIndex(key); 185 186 if (index >= 0) { 187 Var var = _fields[index].toRefVar(); 188 189 _fields[index] = var; 190 191 return var; 192 } 193 } 194 195 if (_object == null) 196 _object = new ObjectExtValue(_cl); 197 198 return _object.getFieldRef(env, key); 199 } 200 201 204 public Value getFieldArg(Env env, String key) 205 { 206 if (_fields.length > 0) { 207 int index = _cl.findFieldIndex(key); 208 209 if (index >= 0) { 210 Var var = _fields[index].toRefVar(); 211 212 _fields[index] = var; 213 214 return var; 215 } 216 } 217 218 if (_object == null) 219 _object = new ObjectExtValue(_cl); 220 221 return _object.getFieldArg(env, key); 222 } 223 224 227 public Value getFieldArgRef(Env env, String key) 228 { 229 if (_fields.length > 0) { 230 int index = _cl.findFieldIndex(key); 231 232 if (index >= 0) { 233 Var var = _fields[index].toRefVar(); 234 235 _fields[index] = var; 236 237 return var; 238 } 239 } 240 241 if (_object == null) 242 _object = new ObjectExtValue(_cl); 243 244 return _object.getFieldArgRef(env, key); 245 } 246 247 250 public Value getFieldArray(Env env, String key) 251 { 252 if (_fields.length > 0) { 253 int index = _cl.findFieldIndex(key); 254 255 if (index >= 0) { 256 _fields[index] = _fields[index].toAutoArray(); 257 258 return _fields[index]; 259 } 260 } 261 262 if (_object == null) 263 _object = new ObjectExtValue(_cl); 264 265 return _object.getFieldArray(env, key); 266 } 267 268 271 public Value getFieldObject(Env env, String key) 272 { 273 if (_fields.length > 0) { 274 int index = _cl.findFieldIndex(key); 275 276 if (index >= 0) { 277 _fields[index] = _fields[index].toAutoObject(env); 278 279 return _fields[index]; 280 } 281 } 282 283 if (_object == null) 284 _object = new ObjectExtValue(_cl); 285 286 return _object.getFieldObject(env, key); 287 } 288 289 292 @Override 293 public Value get(Value key) 294 { 295 throw new UnsupportedOperationException (); 296 } 297 298 public Value put(Value index, Value value) 299 { 300 throw new UnsupportedOperationException (); 301 } 302 303 public Value put(Value value) 304 { 305 throw new UnsupportedOperationException (); 306 } 307 308 311 public Value putField(Env env, String key, Value value) 312 { 313 if (_fields.length > 0) { 314 int index = _cl.findFieldIndex(key); 315 316 if (index >= 0) { 317 _fields[index] = _fields[index].set(value); 318 319 return value; 320 } 321 } 322 323 if (_object == null) 324 _object = new ObjectExtValue(_cl); 325 326 return _object.putField(env, key, value); 327 } 328 329 332 public Value putField(String key, String value) 333 { 334 return putField(null, key, new StringValueImpl(value)); 335 } 336 337 340 public Value putField(String key, long value) 341 { 342 return putField(null, key, LongValue.create(value)); 343 } 344 345 348 public Value putField(String key, double value) 349 { 350 return putField(null, key, DoubleValue.create(value)); 351 } 352 353 356 public void removeField(String key) 357 { 358 if (_fields.length > 0) { 359 int index = _cl.findFieldIndex(key); 360 361 if (index >= 0) { 362 _fields[index] = UnsetValue.UNSET; 363 364 return; 365 } 366 } 367 368 if (_object != null) 369 _object.removeField(key); 370 } 371 372 375 public Value []getKeyArray() 376 { 377 return new Value[0]; 378 } 379 380 383 public Value []getValueArray(Env env) 384 { 385 return new Value[0]; 386 } 387 388 391 public Collection <Value> getIndices() 392 { 393 ArrayList <Value> indices = new ArrayList <Value>(); 394 395 for (int i = 0; i < _fields.length; i++) { 396 if (_fields[i] != UnsetValue.UNSET) 397 indices.add(_fields[i]); 398 } 399 400 if (_object != null) 401 indices.addAll(_object.getIndices()); 402 403 return indices; 404 } 405 406 409 public AbstractFunction findFunction(String methodName) 410 { 411 return _cl.findFunction(methodName); 412 } 413 414 417 public Value callMethod(Env env, String methodName, Expr []args) 418 { 419 return _cl.getFunction(methodName).callMethod(env, this, args); 420 } 421 422 425 public Value callMethod(Env env, String methodName, Value []args) 426 { 427 AbstractFunction fun = _cl.findFunction(methodName); 428 429 if (fun != null) 430 return fun.callMethod(env, this, args); 431 else 432 return env.error(L.l("Call to undefined method {0}::{1}()", 433 _cl.getName(), methodName)); 434 } 435 436 439 public Value callMethod(Env env, String methodName) 440 { 441 return _cl.getFunction(methodName).callMethod(env, this); 442 } 443 444 447 public Value callMethod(Env env, String methodName, Value a0) 448 { 449 return _cl.getFunction(methodName).callMethod(env, this, a0); 450 } 451 452 455 public Value callMethod(Env env, String methodName, 456 Value a0, Value a1) 457 { 458 return _cl.getFunction(methodName).callMethod(env, this, a0, a1); 459 } 460 461 464 public Value callMethod(Env env, String methodName, 465 Value a0, Value a1, Value a2) 466 { 467 return _cl.getFunction(methodName).callMethod(env, this, 468 a0, a1, a2); 469 } 470 471 474 public Value callMethod(Env env, String methodName, 475 Value a0, Value a1, Value a2, Value a3) 476 { 477 return _cl.getFunction(methodName).callMethod(env, this, 478 a0, a1, a2, a3); 479 } 480 481 484 public Value callMethod(Env env, String methodName, 485 Value a0, Value a1, Value a2, Value a3, Value a4) 486 { 487 return _cl.getFunction(methodName).callMethod(env, this, 488 a0, a1, a2, a3, a4); 489 } 490 491 494 public Value callMethodRef(Env env, String methodName, Expr []args) 495 { 496 return _cl.getFunction(methodName).callMethodRef(env, this, args); 497 } 498 499 502 public Value callMethodRef(Env env, String methodName, Value []args) 503 { 504 return _cl.getFunction(methodName).callMethodRef(env, this, args); 505 } 506 507 510 public Value callMethodRef(Env env, String methodName) 511 { 512 return _cl.getFunction(methodName).callMethodRef(env, this); 513 } 514 515 518 public Value callMethodRef(Env env, String methodName, Value a0) 519 { 520 return _cl.getFunction(methodName).callMethodRef(env, this, a0); 521 } 522 523 526 public Value callMethodRef(Env env, String methodName, 527 Value a0, Value a1) 528 { 529 return _cl.getFunction(methodName).callMethodRef(env, this, a0, a1); 530 } 531 532 535 public Value callMethodRef(Env env, String methodName, 536 Value a0, Value a1, Value a2) 537 { 538 return _cl.getFunction(methodName).callMethodRef(env, this, 539 a0, a1, a2); 540 } 541 542 545 public Value callMethodRef(Env env, String methodName, 546 Value a0, Value a1, Value a2, Value a3) 547 { 548 return _cl.getFunction(methodName).callMethodRef(env, this, 549 a0, a1, a2, a3); 550 } 551 552 555 public Value callMethodRef(Env env, String methodName, 556 Value a0, Value a1, Value a2, Value a3, Value a4) 557 { 558 return _cl.getFunction(methodName).callMethodRef(env, this, 559 a0, a1, a2, a3, a4); 560 } 561 562 565 public Value callClassMethod(Env env, AbstractFunction fun, Value []args) 566 { 567 Value oldThis = env.getThis(); 568 569 try { 570 env.setThis(this); 571 572 return fun.call(env, args); 573 } finally { 574 env.setThis(oldThis); 575 } 576 } 577 578 582 public Value getObject(Env env) 583 { 584 return this; 585 } 586 587 590 public Value copy() 591 { 592 return this; 593 } 594 595 598 public Value copy(Env env, IdentityHashMap <Value,Value> map) 599 { 600 Value oldValue = map.get(this); 601 602 if (oldValue != null) 603 return oldValue; 604 605 608 return this; 609 } 610 611 614 public Value clone() 615 { 616 throw new UnsupportedOperationException (); 617 } 618 619 621 624 public void serialize(StringBuilder sb) 625 { 626 sb.append("O:"); 627 sb.append(_cl.getName().length()); 628 sb.append(":\""); 629 sb.append(_cl.getName()); 630 sb.append("\":"); 631 sb.append(getSize()); 632 sb.append(":{"); 633 634 ArrayList <String > names = _cl.getFieldNames(); 635 636 if (names != null) { 637 int index = 0; 638 639 for (int i = 0; i < names.size(); i++) { 640 String key = names.get(i); 641 642 if (_fields[i] == UnsetValue.UNSET) 643 continue; 644 645 sb.append("s:"); 646 sb.append(key.length()); 647 sb.append(":\""); 648 sb.append(key); 649 sb.append("\";"); 650 651 _fields[i].serialize(sb); 652 } 653 } 654 655 if (_object != null) { 656 for (Map.Entry <String ,Value> mapEntry : _object.sortedEntrySet()) { 657 ObjectExtValue.Entry entry = (ObjectExtValue.Entry) mapEntry; 658 659 String key = entry.getKey(); 660 661 sb.append("s:"); 662 sb.append(key.length()); 663 sb.append(":\""); 664 sb.append(key); 665 sb.append("\";"); 666 667 entry.getValue().serialize(sb); 668 } 669 } 670 671 sb.append("}"); 672 } 673 674 678 public StringValue toString(Env env) 679 { 680 AbstractFunction fun = _cl.findFunction("__toString"); 681 682 if (fun != null) 683 return fun.callMethod(env, this, new Expr[0]).toString(env); 684 else 685 return new StringBuilderValue().append(_cl.getName()).append("[]"); 686 } 687 688 692 public void print(Env env) 693 { 694 env.print(toString(env)); 695 } 696 697 700 public Value toArray() 701 { 702 ArrayValue array = new ArrayValueImpl(); 703 704 for (Map.Entry <String ,Value> entry : entrySet()) { 705 array.put(new StringValueImpl(entry.getKey()), entry.getValue()); 706 } 707 708 return array; 709 } 710 711 714 public Value toObject(Env env) 715 { 716 return this; 717 } 718 719 722 public Object toJavaObject() 723 { 724 return this; 725 } 726 727 public Set <Map.Entry <String ,Value>> entrySet() 728 { 729 throw new UnsupportedOperationException (); 730 } 732 733 736 public Set <Map.Entry <String ,Value>> sortedEntrySet() 737 { 738 throw new UnsupportedOperationException (); 739 } 741 742 public String toString() 743 { 744 return "CompiledObjectValue@" + System.identityHashCode(this) + "[" + _cl.getName() + "]"; 745 } 746 747 public void varDumpImpl(Env env, 748 WriteStream out, 749 int depth, 750 IdentityHashMap <Value, String > valueSet) 751 throws IOException 752 { 753 out.println("object(" + getName() + ") (" + getSize() + ") {"); 754 755 ArrayList <String > names = _cl.getFieldNames(); 756 757 if (names != null) { 758 int index = 0; 759 760 for (int i = 0; i < names.size(); i++) { 761 if (_fields[i] == UnsetValue.UNSET) 762 continue; 763 764 printDepth(out, 2 * depth + 2); 765 out.println("[\"" + names.get(i) + "\"]=>"); 766 printDepth(out, 2 * depth + 2); 767 _fields[i].varDumpImpl(env, out, depth + 1, valueSet); 768 out.println(); 769 } 770 } 771 772 if (_object != null) { 773 for (Map.Entry <String ,Value> mapEntry : _object.sortedEntrySet()) { 774 ObjectExtValue.Entry entry = (ObjectExtValue.Entry) mapEntry; 775 776 entry.varDumpImpl(env, out, depth + 1, valueSet); 777 } 778 } 779 780 printDepth(out, 2 * depth); 781 782 out.print("}"); 783 } 784 785 protected void printRImpl(Env env, 786 WriteStream out, 787 int depth, 788 IdentityHashMap <Value, String > valueSet) 789 throws IOException 790 { 791 out.print(_cl.getName()); 792 out.print(' '); 793 out.println("Object"); 794 printDepth(out, 4 * depth); 795 out.println("("); 796 797 804 805 printDepth(out, 4 * depth); 806 out.println(")"); 807 } 808 809 813 private void writeObject(ObjectOutputStream out) 814 throws IOException 815 { 816 System.err.println("CompiledObjectArray->writeObject()"); 817 818 out.writeObject(_fields); 819 out.writeObject(_object); 820 out.writeObject(_cl.getName()); 821 } 822 823 private void readObject(ObjectInputStream in) 824 throws ClassNotFoundException , IOException 825 { 826 System.err.println("CompiledObjectArray->readObject()"); 827 828 _fields = (Value []) in.readObject(); 829 _object = (ObjectExtValue) in.readObject(); 830 831 Env env = Env.getInstance(); 832 String name = (String ) in.readObject(); 833 834 _cl = env.findClass(name); 835 836 if (_cl != null) { 837 } 838 else { 839 _cl = env.getQuercus().getStdClass(); 840 841 putField(env, 842 "__Quercus_Class_Definition_Not_Found", 843 new StringValueImpl(name)); 844 } 845 } 846 } 847 848 | Popular Tags |