KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > quercus > env > CompiledObjectValue


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

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 JavaDoc;
37 import java.io.ObjectInputStream JavaDoc;
38 import java.io.ObjectOutputStream JavaDoc;
39 import java.io.Serializable JavaDoc;
40 import java.util.ArrayList JavaDoc;
41 import java.util.Collection JavaDoc;
42 import java.util.IdentityHashMap JavaDoc;
43 import java.util.Map JavaDoc;
44 import java.util.Set JavaDoc;
45
46 /**
47  * Represents a compiled object value.
48  */

49 public class CompiledObjectValue extends ObjectValue
50   implements Serializable JavaDoc
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   /**
73    * Returns the class name.
74    */

75   public String JavaDoc getName()
76   {
77     return _cl.getName();
78   }
79
80   /**
81    * Returns the parent class
82    */

83   public String JavaDoc getParentName()
84   {
85     return _cl.getParentName();
86   }
87
88   /**
89    * Returns the type.
90    */

91   public String JavaDoc getType()
92   {
93     return "object";
94   }
95
96   /**
97    * Converts to a boolean.
98    */

99   public boolean toBoolean()
100   {
101     return true;
102   }
103
104   /**
105    * Returns true for an implementation of a class
106    */

107   public boolean isA(String JavaDoc name)
108   {
109     return _cl.isA(name);
110   }
111
112   /**
113    * Returns true for an object.
114    */

115   public boolean isObject()
116   {
117     return true;
118   }
119
120   /**
121    * Converts to a long.
122    */

123   public long toLong()
124   {
125     return 1;
126   }
127
128   /**
129    * Converts to a double.
130    */

131   public double toDouble()
132   {
133     return toLong();
134   }
135
136   public CompiledObjectValue toObjectValue()
137   {
138     return this;
139   }
140
141   /**
142    * Returns the number of entries.
143    */

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   /**
160    * Gets a new value.
161    */

162   @Override JavaDoc
163   public Value getField(Env env, String JavaDoc 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   /**
179    * Returns the array ref.
180    */

181   public Var getFieldRef(Env env, String JavaDoc 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   /**
202    * Returns the value as an argument which may be a reference.
203    */

204   public Value getFieldArg(Env env, String JavaDoc 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   /**
225    * Returns the value as an argument which may be a reference.
226    */

227   public Value getFieldArgRef(Env env, String JavaDoc 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   /**
248    * Returns field as an array.
249    */

250   public Value getFieldArray(Env env, String JavaDoc 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   /**
269    * Returns field as an object.
270    */

271   public Value getFieldObject(Env env, String JavaDoc 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   /**
290    * Gets a new value.
291    */

292   @Override JavaDoc
293   public Value get(Value key)
294   {
295     throw new UnsupportedOperationException JavaDoc();
296   }
297
298   public Value put(Value index, Value value)
299   {
300     throw new UnsupportedOperationException JavaDoc();
301   }
302
303   public Value put(Value value)
304   {
305     throw new UnsupportedOperationException JavaDoc();
306   }
307
308   /**
309    * Adds a new value.
310    */

311   public Value putField(Env env, String JavaDoc 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   /**
330    * Adds a new value.
331    */

332   public Value putField(String JavaDoc key, String JavaDoc value)
333   {
334     return putField(null, key, new StringValueImpl(value));
335   }
336
337   /**
338    * Adds a new value.
339    */

340   public Value putField(String JavaDoc key, long value)
341   {
342     return putField(null, key, LongValue.create(value));
343   }
344
345   /**
346    * Adds a new value.
347    */

348   public Value putField(String JavaDoc key, double value)
349   {
350     return putField(null, key, DoubleValue.create(value));
351   }
352
353   /**
354    * Removes a value.
355    */

356   public void removeField(String JavaDoc 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   /**
373    * Returns the key array
374    */

375   public Value []getKeyArray()
376   {
377     return new Value[0];
378   }
379
380   /**
381    * Returns the value array
382    */

383   public Value []getValueArray(Env env)
384   {
385     return new Value[0];
386   }
387
388   /**
389    * Returns the field values.
390    */

391   public Collection JavaDoc<Value> getIndices()
392   {
393     ArrayList JavaDoc<Value> indices = new ArrayList JavaDoc<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   /**
407    * Finds the method name.
408    */

409   public AbstractFunction findFunction(String JavaDoc methodName)
410   {
411     return _cl.findFunction(methodName);
412   }
413
414   /**
415    * Evaluates a method.
416    */

417   public Value callMethod(Env env, String JavaDoc methodName, Expr []args)
418   {
419     return _cl.getFunction(methodName).callMethod(env, this, args);
420   }
421
422   /**
423    * Evaluates a method.
424    */

425   public Value callMethod(Env env, String JavaDoc 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   /**
437    * Evaluates a method.
438    */

439   public Value callMethod(Env env, String JavaDoc methodName)
440   {
441     return _cl.getFunction(methodName).callMethod(env, this);
442   }
443
444   /**
445    * Evaluates a method.
446    */

447   public Value callMethod(Env env, String JavaDoc methodName, Value a0)
448   {
449     return _cl.getFunction(methodName).callMethod(env, this, a0);
450   }
451
452   /**
453    * Evaluates a method.
454    */

455   public Value callMethod(Env env, String JavaDoc methodName,
456                           Value a0, Value a1)
457   {
458     return _cl.getFunction(methodName).callMethod(env, this, a0, a1);
459   }
460
461   /**
462    * Evaluates a method.
463    */

464   public Value callMethod(Env env, String JavaDoc methodName,
465                           Value a0, Value a1, Value a2)
466   {
467     return _cl.getFunction(methodName).callMethod(env, this,
468                                                   a0, a1, a2);
469   }
470
471   /**
472    * Evaluates a method.
473    */

474   public Value callMethod(Env env, String JavaDoc 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   /**
482    * Evaluates a method.
483    */

484   public Value callMethod(Env env, String JavaDoc 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   /**
492    * Evaluates a method.
493    */

494   public Value callMethodRef(Env env, String JavaDoc methodName, Expr []args)
495   {
496     return _cl.getFunction(methodName).callMethodRef(env, this, args);
497   }
498
499   /**
500    * Evaluates a method.
501    */

502   public Value callMethodRef(Env env, String JavaDoc methodName, Value []args)
503   {
504     return _cl.getFunction(methodName).callMethodRef(env, this, args);
505   }
506
507   /**
508    * Evaluates a method.
509    */

510   public Value callMethodRef(Env env, String JavaDoc methodName)
511   {
512     return _cl.getFunction(methodName).callMethodRef(env, this);
513   }
514
515   /**
516    * Evaluates a method.
517    */

518   public Value callMethodRef(Env env, String JavaDoc methodName, Value a0)
519   {
520     return _cl.getFunction(methodName).callMethodRef(env, this, a0);
521   }
522
523   /**
524    * Evaluates a method.
525    */

526   public Value callMethodRef(Env env, String JavaDoc methodName,
527                              Value a0, Value a1)
528   {
529     return _cl.getFunction(methodName).callMethodRef(env, this, a0, a1);
530   }
531
532   /**
533    * Evaluates a method.
534    */

535   public Value callMethodRef(Env env, String JavaDoc methodName,
536                              Value a0, Value a1, Value a2)
537   {
538     return _cl.getFunction(methodName).callMethodRef(env, this,
539                                                      a0, a1, a2);
540   }
541
542   /**
543    * Evaluates a method.
544    */

545   public Value callMethodRef(Env env, String JavaDoc 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   /**
553    * Evaluates a method.
554    */

555   public Value callMethodRef(Env env, String JavaDoc 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   /**
563    * Evaluates a method.
564    */

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   /**
579    * Returns the value for the variable, creating an object if the var
580    * is unset.
581    */

582   public Value getObject(Env env)
583   {
584     return this;
585   }
586
587   /**
588    * Copy for assignment.
589    */

590   public Value copy()
591   {
592     return this;
593   }
594
595   /**
596    * Copy for serialization
597    */

598   public Value copy(Env env, IdentityHashMap JavaDoc<Value,Value> map)
599   {
600     Value oldValue = map.get(this);
601
602     if (oldValue != null)
603       return oldValue;
604
605     // XXX:
606
// return new ObjectExtValue(env, map, _cl, getArray());
607

608     return this;
609   }
610
611   /**
612    * Clone the object
613    */

614   public Value clone()
615   {
616     throw new UnsupportedOperationException JavaDoc();
617   }
618
619   // XXX: need to check the other copy, e.g. for sessions
620

621   /**
622    * Serializes the value.
623    */

624   public void serialize(StringBuilder JavaDoc 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 JavaDoc<String JavaDoc> names = _cl.getFieldNames();
635     
636     if (names != null) {
637       int index = 0;
638
639       for (int i = 0; i < names.size(); i++) {
640     String JavaDoc 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 JavaDoc<String JavaDoc,Value> mapEntry : _object.sortedEntrySet()) {
657     ObjectExtValue.Entry entry = (ObjectExtValue.Entry) mapEntry;
658
659     String JavaDoc 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   /**
675    * Converts to a string.
676    * @param env
677    */

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   /**
689    * Converts to a string.
690    * @param env
691    */

692   public void print(Env env)
693   {
694     env.print(toString(env));
695   }
696
697   /**
698    * Converts to an array.
699    */

700   public Value toArray()
701   {
702     ArrayValue array = new ArrayValueImpl();
703
704     for (Map.Entry JavaDoc<String JavaDoc,Value> entry : entrySet()) {
705       array.put(new StringValueImpl(entry.getKey()), entry.getValue());
706     }
707
708     return array;
709   }
710
711   /**
712    * Converts to an object.
713    */

714   public Value toObject(Env env)
715   {
716     return this;
717   }
718
719   /**
720    * Converts to an object.
721    */

722   public Object JavaDoc toJavaObject()
723   {
724     return this;
725   }
726
727   public Set JavaDoc<Map.Entry JavaDoc<String JavaDoc,Value>> entrySet()
728   {
729     throw new UnsupportedOperationException JavaDoc();
730     // return new EntrySet();
731
}
732
733   /**
734    * Returns a Set of entries, sorted by key.
735    */

736   public Set JavaDoc<Map.Entry JavaDoc<String JavaDoc,Value>> sortedEntrySet()
737   {
738     throw new UnsupportedOperationException JavaDoc();
739     //return new TreeSet<Map.Entry<String, Value>>(entrySet());
740
}
741
742   public String JavaDoc 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 JavaDoc<Value, String JavaDoc> valueSet)
751     throws IOException JavaDoc
752   {
753     out.println("object(" + getName() + ") (" + getSize() + ") {");
754
755     ArrayList JavaDoc<String JavaDoc> 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 JavaDoc<String JavaDoc,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 JavaDoc<Value, String JavaDoc> valueSet)
789     throws IOException JavaDoc
790   {
791     out.print(_cl.getName());
792     out.print(' ');
793     out.println("Object");
794     printDepth(out, 4 * depth);
795     out.println("(");
796
797     /*
798     for (Map.Entry<String,Value> mapEntry : sortedEntrySet()) {
799       ObjectValue.Entry entry = (ObjectValue.Entry) mapEntry;
800
801       entry.printRImpl(env, out, depth + 1, valueSet);
802     }
803     */

804
805     printDepth(out, 4 * depth);
806     out.println(")");
807   }
808   
809   //
810
// Java Serialization
811
//
812

813   private void writeObject(ObjectOutputStream JavaDoc out)
814     throws IOException JavaDoc
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 JavaDoc in)
824     throws ClassNotFoundException JavaDoc, IOException JavaDoc
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 JavaDoc name = (String JavaDoc) 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