KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jruby > runtime > builtin > meta > AbstractMetaClass


1 /***** BEGIN LICENSE BLOCK *****
2  * Version: CPL 1.0/GPL 2.0/LGPL 2.1
3  *
4  * The contents of this file are subject to the Common Public
5  * License Version 1.0 (the "License"); you may not use this file
6  * except in compliance with the License. You may obtain a copy of
7  * the License at http://www.eclipse.org/legal/cpl-v10.html
8  *
9  * Software distributed under the License is distributed on an "AS
10  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
11  * implied. See the License for the specific language governing
12  * rights and limitations under the License.
13  *
14  * Copyright (C) 2005 Charles O Nutter <headius@headius.com>
15  *
16  * Alternatively, the contents of this file may be used under the terms of
17  * either of the GNU General Public License Version 2 or later (the "GPL"),
18  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
19  * in which case the provisions of the GPL or the LGPL are applicable instead
20  * of those above. If you wish to allow use of your version of this file only
21  * under the terms of either the GPL or the LGPL, and not to allow others to
22  * use your version of this file under the terms of the CPL, indicate your
23  * decision by deleting the provisions above and replace them with the notice
24  * and other provisions required by the GPL or the LGPL. If you do not delete
25  * the provisions above, a recipient may use your version of this file under
26  * the terms of any one of the CPL, the GPL or the LGPL.
27  ***** END LICENSE BLOCK *****/

28 package org.jruby.runtime.builtin.meta;
29
30 import org.jruby.Ruby;
31 import org.jruby.RubyClass;
32 import org.jruby.RubyModule;
33 import org.jruby.runtime.MethodFactory;
34 import org.jruby.runtime.Arity;
35 import org.jruby.runtime.ObjectAllocator;
36 import org.jruby.runtime.Visibility;
37 import org.jruby.runtime.builtin.IRubyObject;
38 import org.jruby.util.collections.SinglyLinkedList;
39
40 /**
41  * <p>
42  * The main meta class for all other meta classes.
43  * </p>
44  */

45 public abstract class AbstractMetaClass extends RubyClass {
46     protected abstract class Meta {
47         protected abstract void initializeClass();
48         
49         /**
50          * Base implementation uses the data-driven approach not used currently but
51          * possibly revisited in the future.
52          */

53 // protected void initializeClass() {
54
// includeModules(getIncludedModules());
55
//
56
// defineMethods(Visibility.PUBLIC, getSingletonMethods(), true);
57
// defineMethods(Visibility.PUBLIC, getPublicMethods(), false);
58
// defineMethods(Visibility.PRIVATE, getPrivateMethods(), false);
59
//
60
// defineAliases(getAliases());
61
//
62
// undefineMethods(getUndefineMethods(), false);
63
// undefineMethods(getUndefineSingletonMethods(), true);
64
//
65
// defineConstants(getDefineConstants(), false);
66
// setConstants(getSetConstants(), false);
67
// }
68

69         // Empty impls
70
protected Object JavaDoc[][] getPublicMethods() {
71             return new Object JavaDoc[][] {};
72         }
73
74         protected Object JavaDoc[][] getDefineConstants() {
75             return new Object JavaDoc[][] {};
76         }
77
78         protected Object JavaDoc[][] getSetConstants() {
79             return new Object JavaDoc[][] {};
80         }
81
82         protected Object JavaDoc[][] getSingletonMethods() {
83             return new Object JavaDoc[][] {};
84         }
85
86         protected String JavaDoc[][] getAliases() {
87             return new String JavaDoc[][] {};
88         }
89
90         protected Object JavaDoc[][] getPrivateMethods() {
91             return new Object JavaDoc[][] {};
92         }
93
94         protected String JavaDoc[] getIncludedModules() {
95             return new String JavaDoc[] {};
96         }
97
98         protected String JavaDoc[] getUndefineMethods() {
99             return new String JavaDoc[] {};
100         }
101
102         protected String JavaDoc[] getUndefineSingletonMethods() {
103             return new String JavaDoc[] {};
104         }
105
106 // public void defineMethods(Visibility visibility, Object[][] methods,
107
// boolean singleton) {
108
// for (int i = 0; i < methods.length; i++) {
109
// String name = (String) methods[i][0];
110
// Arity arity = (Arity) methods[i][1];
111
// String java_name = null;
112
// switch (methods[i].length) {
113
// case 2:
114
// java_name = (String) methods[i][0];
115
// break;
116
// case 3:
117
// java_name = (String) methods[i][2];
118
// break;
119
// }
120
//
121
// assert name != null;
122
// assert arity != null;
123
// assert java_name != null;
124
//
125
// visibility = name.equals("initialize") ? Visibility.PRIVATE
126
// : Visibility.PUBLIC;
127
//
128
// if (singleton) {
129
// getSingletonClass().addMethod(
130
// name,
131
// new ReflectedMethod(AbstractMetaClass.this, AbstractMetaClass.this
132
// .getClass(), java_name, arity, visibility));
133
// } else {
134
// addMethod(name, new ReflectedMethod(AbstractMetaClass.this,
135
// builtinClass, java_name, arity, visibility));
136
// }
137
// }
138
// }
139

140         public void undefineMethods(String JavaDoc[] undefineMethods, boolean singleton) {
141             for (int i = 0; i < undefineMethods.length; i++) {
142                 if (singleton) {
143                     getSingletonClass().undefineMethod(undefineMethods[i]);
144                 } else {
145                     undefineMethod(undefineMethods[i]);
146                 }
147             }
148         }
149
150         public void defineConstants(Object JavaDoc[][] constants, boolean singleton) {
151             for (int i = 0; i < constants.length; i++) {
152                 if (singleton) {
153                     getSingletonClass().defineConstant(
154                             (String JavaDoc) constants[i][0],
155                             (IRubyObject) constants[i][1]);
156                 } else {
157                     defineConstant((String JavaDoc) constants[i][0],
158                             (IRubyObject) constants[i][1]);
159                 }
160             }
161         }
162
163         public void setConstants(Object JavaDoc[][] constants, boolean singleton) {
164             for (int i = 0; i < constants.length; i++) {
165                 if (singleton) {
166                     getSingletonClass().setConstant((String JavaDoc) constants[i][0],
167                             (IRubyObject) constants[i][1]);
168                 } else {
169                     setConstant((String JavaDoc) constants[i][0],
170                             (IRubyObject) constants[i][1]);
171                 }
172             }
173         }
174
175         public void includeModules(String JavaDoc[] includedModules) {
176             for (int i = 0; i < includedModules.length; i++) {
177                 includeModule(getRuntime().getModule(includedModules[i]));
178             }
179         }
180
181         public void defineAliases(Object JavaDoc[][] aliases) {
182             for (int i = 0; i < aliases.length; i++) {
183                 defineAlias((String JavaDoc) aliases[i][0], (String JavaDoc) aliases[i][1]);
184             }
185         }
186     };
187
188     protected Meta getMeta() {
189         return null;
190     }
191
192     protected Class JavaDoc builtinClass;
193     protected MethodFactory mfactory = MethodFactory.createFactory();
194
195     // Only for other core modules/classes
196
protected AbstractMetaClass(Ruby runtime, RubyClass metaClass,
197             RubyClass superClass, ObjectAllocator allocator, SinglyLinkedList parentCRef, String JavaDoc name,
198             Class JavaDoc builtinClass) {
199         super(runtime, metaClass, superClass, allocator, parentCRef, name);
200
201         this.builtinClass = builtinClass;
202     }
203
204     protected AbstractMetaClass(String JavaDoc name, Class JavaDoc builtinClass, RubyClass superClass, ObjectAllocator allocator) {
205         this(name, builtinClass, superClass, allocator, superClass.getRuntime().getClass(
206                 "Object").getCRef(), true);
207     }
208
209     protected AbstractMetaClass(String JavaDoc name, Class JavaDoc builtinClass, RubyClass superClass,
210             ObjectAllocator allocator, SinglyLinkedList parentCRef) {
211         this(name, builtinClass, superClass, allocator, parentCRef, false);
212     }
213
214     protected AbstractMetaClass(String JavaDoc name, Class JavaDoc builtinClass, RubyClass superClass,
215             ObjectAllocator allocator, SinglyLinkedList parentCRef, boolean init) {
216         super(superClass.getRuntime(), superClass.getRuntime()
217                 .getClass("Class"), superClass, allocator, parentCRef, name);
218
219         assert name != null;
220         assert builtinClass != null;
221         //assert RubyObject.class.isAssignableFrom(builtinClass) || RubyObject.class == builtinClass: "builtinClass have to be a subclass of RubyObject.";
222
assert superClass != null;
223
224         this.builtinClass = builtinClass;
225
226         makeMetaClass(superClass.getMetaClass(), superClass.getRuntime()
227                 .getCurrentContext().peekCRef());
228         inheritedBy(superClass);
229
230                 if(name != null) {
231                     ((RubyModule)parentCRef.getValue()).setConstant(name, this);
232                 }
233
234         if (init) {
235             getMeta().initializeClass();
236         }
237     }
238
239     public AbstractMetaClass(Ruby runtime, RubyClass metaClass, RubyClass superClass,
240             ObjectAllocator allocator, SinglyLinkedList parentCRef, String JavaDoc name) {
241         super(runtime, metaClass, superClass, allocator, parentCRef, name);
242     }
243
244     public void defineMethod(String JavaDoc name, Arity arity) {
245         defineMethod(name, arity, name);
246     }
247
248     public void defineMethod(String JavaDoc name, Arity arity, String JavaDoc java_name) {
249         assert name != null;
250         assert arity != null;
251         assert java_name != null;
252
253         Visibility visibility = name.equals("initialize") ? Visibility.PRIVATE
254                 : Visibility.PUBLIC;
255
256         addMethod(name, mfactory.getFullMethod(this, builtinClass, java_name,
257                 arity, visibility));
258     }
259
260     public void definePrivateMethod(String JavaDoc name, Arity arity) {
261         addMethod(name, mfactory.getFullMethod(this, builtinClass, name, arity,
262                 Visibility.PRIVATE));
263     }
264
265     public void definePrivateMethod(String JavaDoc name, Arity arity, String JavaDoc java_name) {
266         addMethod(name, mfactory.getFullMethod(this, builtinClass, java_name,
267                 arity, Visibility.PRIVATE));
268     }
269
270     public void defineFastMethod(String JavaDoc name, Arity arity) {
271         defineFastMethod(name, arity, name);
272     }
273
274     public void defineFastMethod(String JavaDoc name, Arity arity, String JavaDoc java_name) {
275         assert name != null;
276         assert arity != null;
277         assert java_name != null;
278
279         Visibility visibility = name.equals("initialize") ? Visibility.PRIVATE
280                 : Visibility.PUBLIC;
281
282         addMethod(name, mfactory.getSimpleMethod(this, builtinClass, java_name,
283                 arity, visibility));
284     }
285
286     public void defineFastPrivateMethod(String JavaDoc name, Arity arity) {
287         addMethod(name, mfactory.getSimpleMethod(this, builtinClass, name, arity,
288                 Visibility.PRIVATE));
289     }
290
291     public void defineFastPrivateMethod(String JavaDoc name, Arity arity, String JavaDoc java_name) {
292         addMethod(name, mfactory.getSimpleMethod(this, builtinClass, java_name,
293                 arity, Visibility.PRIVATE));
294     }
295
296     public void defineSingletonMethod(String JavaDoc name, Arity arity) {
297         defineSingletonMethod(name, arity, name);
298     }
299
300     public void defineSingletonMethod(String JavaDoc name, Arity arity, String JavaDoc java_name) {
301         assert name != null;
302         assert arity != null;
303         assert java_name != null;
304
305         Visibility visibility = name.equals("initialize") ? Visibility.PRIVATE
306                 : Visibility.PUBLIC;
307
308         getSingletonClass().addMethod(
309                 name,
310                 mfactory.getFullMethod(this, getClass(), java_name, arity,
311                         visibility));
312     }
313
314     public void defineFastSingletonMethod(String JavaDoc name, Arity arity) {
315         defineFastSingletonMethod(name, arity, name);
316     }
317
318     public void defineFastSingletonMethod(String JavaDoc name, Arity arity, String JavaDoc java_name) {
319         assert name != null;
320         assert arity != null;
321         assert java_name != null;
322
323         Visibility visibility = name.equals("initialize") ? Visibility.PRIVATE
324                 : Visibility.PUBLIC;
325
326         getSingletonClass().addMethod(
327                 name,
328                 mfactory.getSimpleMethod(this, getClass(), java_name, arity,
329                         visibility));
330     }
331
332     /**
333      * Only intended to be called by ModuleMetaClass and ClassMetaClass. Seems
334      * like a waste to subclass over this and there seems little risk of
335      * programmer error. We cannot define methods for there two classes since
336      * there is a circular dependency between them and ObjectMetaClass. We defer
337      * initialization until after construction and meta classes are made.
338      */

339     public void initializeBootstrapClass() {
340         getMeta().initializeClass();
341     }
342 }
343
Popular Tags