KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > python > core > PySuper


1 package org.python.core;
2
3 public class PySuper extends PyObject implements PyType.Newstyle {
4     /* type info */
5
6     public static final String JavaDoc exposed_name="super";
7
8     public static void typeSetup(PyObject dict,PyType.Newstyle marker) {
9         dict.__setitem__("__thisclass__",new PyGetSetDescr("__thisclass__",PySuper.class,"getThisClass",null));
10         dict.__setitem__("__self__",new PyGetSetDescr("__self__",PySuper.class,"getSelf",null));
11         dict.__setitem__("__self_class__",new PyGetSetDescr("__self_class__",PySuper.class,"getSelfClass",null));
12         class exposed___getattribute__ extends PyBuiltinFunctionNarrow {
13
14             private PySuper self;
15
16             public PyObject getSelf() {
17                 return self;
18             }
19
20             exposed___getattribute__(PySuper self,PyBuiltinFunction.Info info) {
21                 super(info);
22                 this.self=self;
23             }
24
25             public PyBuiltinFunction makeBound(PyObject self) {
26                 return new exposed___getattribute__((PySuper)self,info);
27             }
28
29             public PyObject __call__(PyObject arg0) {
30                 try {
31                     String JavaDoc name=(arg0.asName(0));
32                     PyObject ret=self.super___findattr__(name);
33                     if (ret==null)
34                         self.noAttributeError(name);
35                     return ret;
36                 } catch (PyObject.ConversionException e) {
37                     String JavaDoc msg;
38                     switch (e.index) {
39                     case 0:
40                         msg="attribute name must be string";
41                         break;
42                     default:
43                         msg="xxx";
44                     }
45                     throw Py.TypeError(msg);
46                 }
47             }
48
49             public PyObject inst_call(PyObject gself,PyObject arg0) {
50                 PySuper self=(PySuper)gself;
51                 try {
52                     String JavaDoc name=(arg0.asName(0));
53                     PyObject ret=self.super___findattr__(name);
54                     if (ret==null)
55                         self.noAttributeError(name);
56                     return ret;
57                 } catch (PyObject.ConversionException e) {
58                     String JavaDoc msg;
59                     switch (e.index) {
60                     case 0:
61                         msg="attribute name must be string";
62                         break;
63                     default:
64                         msg="xxx";
65                     }
66                     throw Py.TypeError(msg);
67                 }
68             }
69
70         }
71         dict.__setitem__("__getattribute__",new PyMethodDescr("__getattribute__",PySuper.class,1,1,new exposed___getattribute__(null,null)));
72         class exposed___get__ extends PyBuiltinFunctionNarrow {
73
74             private PySuper self;
75
76             public PyObject getSelf() {
77                 return self;
78             }
79
80             exposed___get__(PySuper self,PyBuiltinFunction.Info info) {
81                 super(info);
82                 this.self=self;
83             }
84
85             public PyBuiltinFunction makeBound(PyObject self) {
86                 return new exposed___get__((PySuper)self,info);
87             }
88
89             public PyObject __call__(PyObject arg0,PyObject arg1) {
90                 PyObject obj=(arg0==Py.None)?null:arg1;
91                 PyObject type=(arg1==Py.None)?null:arg0;
92                 return self.super___get__(obj,type);
93             }
94
95             public PyObject inst_call(PyObject gself,PyObject arg0,PyObject arg1) {
96                 PySuper self=(PySuper)gself;
97                 PyObject obj=(arg0==Py.None)?null:arg1;
98                 PyObject type=(arg1==Py.None)?null:arg0;
99                 return self.super___get__(obj,type);
100             }
101
102             public PyObject __call__(PyObject arg0) {
103                 PyObject obj=(arg0==Py.None)?null:(null);
104                 PyObject type=((null)==Py.None)?null:arg0;
105                 return self.super___get__(obj,type);
106             }
107
108             public PyObject inst_call(PyObject gself,PyObject arg0) {
109                 PySuper self=(PySuper)gself;
110                 PyObject obj=(arg0==Py.None)?null:(null);
111                 PyObject type=((null)==Py.None)?null:arg0;
112                 return self.super___get__(obj,type);
113             }
114
115         }
116         dict.__setitem__("__get__",new PyMethodDescr("__get__",PySuper.class,1,2,new exposed___get__(null,null)));
117         class exposed___init__ extends PyBuiltinFunctionWide {
118
119             private PySuper self;
120
121             public PyObject getSelf() {
122                 return self;
123             }
124
125             exposed___init__(PySuper self,PyBuiltinFunction.Info info) {
126                 super(info);
127                 this.self=self;
128             }
129
130             public PyBuiltinFunction makeBound(PyObject self) {
131                 return new exposed___init__((PySuper)self,info);
132             }
133
134             public PyObject inst_call(PyObject self,PyObject[]args) {
135                 return inst_call(self,args,Py.NoKeywords);
136             }
137
138             public PyObject __call__(PyObject[]args) {
139                 return __call__(args,Py.NoKeywords);
140             }
141
142             public PyObject __call__(PyObject[]args,String JavaDoc[]keywords) {
143                 self.super_init(args,keywords);
144                 return Py.None;
145             }
146
147             public PyObject inst_call(PyObject gself,PyObject[]args,String JavaDoc[]keywords) {
148                 PySuper self=(PySuper)gself;
149                 self.super_init(args,keywords);
150                 return Py.None;
151             }
152
153         }
154         dict.__setitem__("__init__",new PyMethodDescr("__init__",PySuper.class,-1,-1,new exposed___init__(null,null)));
155         dict.__setitem__("__new__",new PyNewWrapper(PySuper.class,"__new__",-1,-1) {
156
157                                                                                        public PyObject new_impl(boolean init,PyType subtype,PyObject[]args,String JavaDoc[]keywords) {
158                                                                                            PySuper newobj;
159                                                                                            if (for_type==subtype) {
160                                                                                                newobj=new PySuper();
161                                                                                                if (init)
162                                                                                                    newobj.super_init(args,keywords);
163                                                                                            } else {
164                                                                                                newobj=null; // xxx new PySuperDerived(subtype);
165
}
166                                                                                            return newobj;
167                                                                                        }
168
169                                                                                    });
170     }
171    
172     protected PyType thisClass;
173     protected PyObject self;
174     protected PyType selfClass;
175
176     private PyType supercheck(PyType type,PyObject obj) {
177         if (obj instanceof PyType && ((PyType)obj).isSubType(type)) {
178             return (PyType)obj;
179         }
180         PyType obj_type = obj.getType();
181         if (obj_type.isSubType(type))
182             return obj_type;
183         throw Py.TypeError("super(type, obj): "+
184                 "obj must be an instance or subtype of type");
185     }
186     
187     public void super_init(PyObject[] args, String JavaDoc[] keywords) {
188         if (keywords.length != 0
189                 || !PyBuiltinFunction.DefaultInfo.check(args.length, 1, 2)) {
190             throw PyBuiltinFunction.DefaultInfo.unexpectedCall(args.length,
191                     keywords.length != 0, "super", 1, 2);
192         }
193         if (!(args[0] instanceof PyType)) {
194             throw Py.TypeError("super: argument 1 must be type");
195         }
196         PyType type = (PyType)args[0];
197         PyObject obj = null;
198         PyType obj_type = null;
199         if (args.length == 2 && args[1] != Py.None)
200             obj = args[1];
201         if (obj != null) {
202             obj_type = supercheck(type, obj);
203         }
204         this.thisClass = type;
205         this.self = obj;
206         this.selfClass = obj_type;
207     }
208     
209     public PySuper() {
210     }
211     
212     public PyObject getSelf() {
213         return self;
214     }
215     public PyType getSelfClass() {
216         return selfClass;
217     }
218     public PyType getThisClass() {
219         return thisClass;
220     }
221     
222     public PyObject __findattr__(String JavaDoc name) {
223         return super___findattr__(name);
224     }
225
226     final PyObject super___findattr__(String JavaDoc name) {
227         if (selfClass != null && name != "__class__") {
228             PyObject descr = selfClass.super_lookup(thisClass, name);
229             return descr.__get__(selfClass == self ? null : self, selfClass);
230         }
231         return super.__findattr__(name);
232     }
233
234     public PyObject __get__(PyObject obj, PyObject type) {
235         return super___get__(obj,type);
236     }
237
238     final PyObject super___get__(PyObject obj, PyObject type) { //xxx subtype case!
239
if (obj == null || obj == Py.None || self != null)
240             return this;
241         PyType obj_type = supercheck(this.thisClass, obj);
242         PySuper newsuper = new PySuper();
243         newsuper.thisClass = this.thisClass;
244         newsuper.self = obj;
245         newsuper.selfClass = obj_type;
246         return newsuper;
247     }
248
249 }
250
Popular Tags