1 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 45 public abstract class AbstractMetaClass extends RubyClass { 46 protected abstract class Meta { 47 protected abstract void initializeClass(); 48 49 53 69 protected Object [][] getPublicMethods() { 71 return new Object [][] {}; 72 } 73 74 protected Object [][] getDefineConstants() { 75 return new Object [][] {}; 76 } 77 78 protected Object [][] getSetConstants() { 79 return new Object [][] {}; 80 } 81 82 protected Object [][] getSingletonMethods() { 83 return new Object [][] {}; 84 } 85 86 protected String [][] getAliases() { 87 return new String [][] {}; 88 } 89 90 protected Object [][] getPrivateMethods() { 91 return new Object [][] {}; 92 } 93 94 protected String [] getIncludedModules() { 95 return new String [] {}; 96 } 97 98 protected String [] getUndefineMethods() { 99 return new String [] {}; 100 } 101 102 protected String [] getUndefineSingletonMethods() { 103 return new String [] {}; 104 } 105 106 140 public void undefineMethods(String [] 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 [][] constants, boolean singleton) { 151 for (int i = 0; i < constants.length; i++) { 152 if (singleton) { 153 getSingletonClass().defineConstant( 154 (String ) constants[i][0], 155 (IRubyObject) constants[i][1]); 156 } else { 157 defineConstant((String ) constants[i][0], 158 (IRubyObject) constants[i][1]); 159 } 160 } 161 } 162 163 public void setConstants(Object [][] constants, boolean singleton) { 164 for (int i = 0; i < constants.length; i++) { 165 if (singleton) { 166 getSingletonClass().setConstant((String ) constants[i][0], 167 (IRubyObject) constants[i][1]); 168 } else { 169 setConstant((String ) constants[i][0], 170 (IRubyObject) constants[i][1]); 171 } 172 } 173 } 174 175 public void includeModules(String [] includedModules) { 176 for (int i = 0; i < includedModules.length; i++) { 177 includeModule(getRuntime().getModule(includedModules[i])); 178 } 179 } 180 181 public void defineAliases(Object [][] aliases) { 182 for (int i = 0; i < aliases.length; i++) { 183 defineAlias((String ) aliases[i][0], (String ) aliases[i][1]); 184 } 185 } 186 }; 187 188 protected Meta getMeta() { 189 return null; 190 } 191 192 protected Class builtinClass; 193 protected MethodFactory mfactory = MethodFactory.createFactory(); 194 195 protected AbstractMetaClass(Ruby runtime, RubyClass metaClass, 197 RubyClass superClass, ObjectAllocator allocator, SinglyLinkedList parentCRef, String name, 198 Class builtinClass) { 199 super(runtime, metaClass, superClass, allocator, parentCRef, name); 200 201 this.builtinClass = builtinClass; 202 } 203 204 protected AbstractMetaClass(String name, Class builtinClass, RubyClass superClass, ObjectAllocator allocator) { 205 this(name, builtinClass, superClass, allocator, superClass.getRuntime().getClass( 206 "Object").getCRef(), true); 207 } 208 209 protected AbstractMetaClass(String name, Class builtinClass, RubyClass superClass, 210 ObjectAllocator allocator, SinglyLinkedList parentCRef) { 211 this(name, builtinClass, superClass, allocator, parentCRef, false); 212 } 213 214 protected AbstractMetaClass(String name, Class 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 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 name) { 241 super(runtime, metaClass, superClass, allocator, parentCRef, name); 242 } 243 244 public void defineMethod(String name, Arity arity) { 245 defineMethod(name, arity, name); 246 } 247 248 public void defineMethod(String name, Arity arity, String 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 name, Arity arity) { 261 addMethod(name, mfactory.getFullMethod(this, builtinClass, name, arity, 262 Visibility.PRIVATE)); 263 } 264 265 public void definePrivateMethod(String name, Arity arity, String java_name) { 266 addMethod(name, mfactory.getFullMethod(this, builtinClass, java_name, 267 arity, Visibility.PRIVATE)); 268 } 269 270 public void defineFastMethod(String name, Arity arity) { 271 defineFastMethod(name, arity, name); 272 } 273 274 public void defineFastMethod(String name, Arity arity, String 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 name, Arity arity) { 287 addMethod(name, mfactory.getSimpleMethod(this, builtinClass, name, arity, 288 Visibility.PRIVATE)); 289 } 290 291 public void defineFastPrivateMethod(String name, Arity arity, String java_name) { 292 addMethod(name, mfactory.getSimpleMethod(this, builtinClass, java_name, 293 arity, Visibility.PRIVATE)); 294 } 295 296 public void defineSingletonMethod(String name, Arity arity) { 297 defineSingletonMethod(name, arity, name); 298 } 299 300 public void defineSingletonMethod(String name, Arity arity, String 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 name, Arity arity) { 315 defineFastSingletonMethod(name, arity, name); 316 } 317 318 public void defineFastSingletonMethod(String name, Arity arity, String 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 339 public void initializeBootstrapClass() { 340 getMeta().initializeClass(); 341 } 342 } 343 | Popular Tags |