RubyNil.java

  1. /***** BEGIN LICENSE BLOCK *****
  2.  * Version: EPL 1.0/GPL 2.0/LGPL 2.1
  3.  *
  4.  * The contents of this file are subject to the Eclipse 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/epl-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) 2001 Alan Moore <alan_moore@gmx.net>
  15.  * Copyright (C) 2001-2004 Jan Arne Petersen <jpetersen@uni-bonn.de>
  16.  * Copyright (C) 2002-2004 Anders Bengtsson <ndrsbngtssn@yahoo.se>
  17.  * Copyright (C) 2004 Thomas E Enebo <enebo@acm.org>
  18.  * Copyright (C) 2004 Charles O Nutter <headius@headius.com>
  19.  * Copyright (C) 2004 Stefan Matthias Aust <sma@3plus4.de>
  20.  *
  21.  * Alternatively, the contents of this file may be used under the terms of
  22.  * either of the GNU General Public License Version 2 or later (the "GPL"),
  23.  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  24.  * in which case the provisions of the GPL or the LGPL are applicable instead
  25.  * of those above. If you wish to allow use of your version of this file only
  26.  * under the terms of either the GPL or the LGPL, and not to allow others to
  27.  * use your version of this file under the terms of the EPL, indicate your
  28.  * decision by deleting the provisions above and replace them with the notice
  29.  * and other provisions required by the GPL or the LGPL. If you do not delete
  30.  * the provisions above, a recipient may use your version of this file under
  31.  * the terms of any one of the EPL, the GPL or the LGPL.
  32.  ***** END LICENSE BLOCK *****/
  33. package org.jruby;

  34. import org.jruby.anno.JRubyMethod;
  35. import org.jruby.anno.JRubyClass;
  36. import org.jruby.compiler.Constantizable;
  37. import org.jruby.runtime.ClassIndex;
  38. import org.jruby.runtime.ObjectAllocator;
  39. import org.jruby.runtime.ThreadContext;
  40. import org.jruby.runtime.builtin.IRubyObject;
  41. import org.jruby.runtime.opto.OptoFactory;

  42. /**
  43.  *
  44.  * @author  jpetersen
  45.  */
  46. @JRubyClass(name="NilClass")
  47. public class RubyNil extends RubyObject implements Constantizable {

  48.     private final int hashCode;
  49.     private final Object constant;

  50.     public RubyNil(Ruby runtime) {
  51.         super(runtime, runtime.getNilClass(), false);
  52.         flags |= NIL_F | FALSE_F | FROZEN_F;

  53.         if (RubyInstanceConfig.CONSISTENT_HASHING_ENABLED) {
  54.             // default to a fixed value
  55.             this.hashCode = 34;
  56.         } else {
  57.             // save the object id based hash code;
  58.             this.hashCode = System.identityHashCode(this);
  59.         }

  60.         constant = OptoFactory.newConstantWrapper(IRubyObject.class, this);
  61.     }
  62.    
  63.     public static final ObjectAllocator NIL_ALLOCATOR = new ObjectAllocator() {
  64.         @Override
  65.         public IRubyObject allocate(Ruby runtime, RubyClass klass) {
  66.             return runtime.getNil();
  67.         }
  68.     };
  69.    
  70.     public static RubyClass createNilClass(Ruby runtime) {
  71.         RubyClass nilClass = runtime.defineClass("NilClass", runtime.getObject(), NIL_ALLOCATOR);
  72.         runtime.setNilClass(nilClass);
  73.         nilClass.setClassIndex(ClassIndex.NIL);
  74.         nilClass.setReifiedClass(RubyNil.class);
  75.        
  76.         nilClass.defineAnnotatedMethods(RubyNil.class);
  77.        
  78.         nilClass.getMetaClass().undefineMethod("new");
  79.        
  80.         // FIXME: This is causing a verification error for some reason
  81.         //nilClass.dispatcher = callbackFactory.createDispatcher(nilClass);
  82.        
  83.         return nilClass;
  84.     }
  85.    
  86.     @Override
  87.     public ClassIndex getNativeClassIndex() {
  88.         return ClassIndex.NIL;
  89.     }

  90.     @Override
  91.     public boolean isImmediate() {
  92.         return true;
  93.     }

  94.     @Override
  95.     public RubyClass getSingletonClass() {
  96.         return metaClass;
  97.     }
  98.    
  99.     @Override
  100.     public Class<?> getJavaClass() {
  101.         return void.class;
  102.     }

  103.     /**
  104.      * @see org.jruby.compiler.Constantizable
  105.      */
  106.     @Override
  107.     public Object constant() {
  108.         return constant;
  109.     }
  110.    
  111.     // Methods of the Nil Class (nil_*):
  112.    
  113.     /** nil_to_i
  114.      *
  115.      */
  116.     @JRubyMethod
  117.     public static RubyFixnum to_i(ThreadContext context, IRubyObject recv) {
  118.         return RubyFixnum.zero(recv.getRuntime());
  119.     }
  120.    
  121.     /**
  122.      * nil_to_f
  123.      *
  124.      */
  125.     @JRubyMethod
  126.     public static RubyFloat to_f(ThreadContext context, IRubyObject recv) {
  127.         return RubyFloat.newFloat(context.runtime, 0.0D);
  128.     }
  129.    
  130.     /** nil_to_s
  131.      *
  132.      */
  133.     @JRubyMethod
  134.     public static RubyString to_s(ThreadContext context, IRubyObject recv) {
  135.         return RubyString.newEmptyString(context.runtime);
  136.     }
  137.    
  138.     /** nil_to_a
  139.      *
  140.      */
  141.     @JRubyMethod
  142.     public static RubyArray to_a(ThreadContext context, IRubyObject recv) {
  143.         return context.runtime.newEmptyArray();
  144.     }
  145.    
  146.     @JRubyMethod
  147.     public static RubyHash to_h(ThreadContext context, IRubyObject recv) {
  148.         return RubyHash.newSmallHash(context.runtime);
  149.     }
  150.    
  151.     /** nil_inspect
  152.      *
  153.      */
  154.     @JRubyMethod
  155.     public static RubyString inspect(ThreadContext context, IRubyObject recv) {
  156.         return RubyString.newUSASCIIString(context.runtime, "nil");
  157.     }
  158.    
  159.     /** nil_and
  160.      *
  161.      */
  162.     @JRubyMethod(name = "&", required = 1)
  163.     public static RubyBoolean op_and(ThreadContext context, IRubyObject recv, IRubyObject obj) {
  164.         return context.runtime.getFalse();
  165.     }
  166.    
  167.     /** nil_or
  168.      *
  169.      */
  170.     @JRubyMethod(name = "|", required = 1)
  171.     public static RubyBoolean op_or(ThreadContext context, IRubyObject recv, IRubyObject obj) {
  172.         return context.runtime.newBoolean(obj.isTrue());
  173.     }
  174.    
  175.     /** nil_xor
  176.      *
  177.      */
  178.     @JRubyMethod(name = "^", required = 1)
  179.     public static RubyBoolean op_xor(ThreadContext context, IRubyObject recv, IRubyObject obj) {
  180.         return context.runtime.newBoolean(obj.isTrue());
  181.     }

  182.     @JRubyMethod(name = "nil?")
  183.     public IRubyObject nil_p() {
  184.         return getRuntime().getTrue();
  185.     }

  186.     @JRubyMethod
  187.     public RubyFixnum hash(ThreadContext context) {
  188.         return context.runtime.newFixnum(hashCode());
  189.     }

  190.     @Override
  191.     public int hashCode() {
  192.         return hashCode;
  193.     }

  194.     @Override
  195.     public RubyFixnum id() {
  196.         return getRuntime().newFixnum(8);
  197.     }
  198.    
  199.     @Override
  200.     public IRubyObject taint(ThreadContext context) {
  201.         return this;
  202.     }

  203.     /** nilclass_to_c
  204.      *
  205.      */
  206.     @JRubyMethod
  207.     public static IRubyObject to_c(ThreadContext context, IRubyObject recv) {
  208.         return RubyComplex.newComplexCanonicalize(context, RubyFixnum.zero(context.runtime));
  209.     }
  210.    
  211.     /** nilclass_to_r
  212.      *
  213.      */
  214.     @JRubyMethod
  215.     public static IRubyObject to_r(ThreadContext context, IRubyObject recv) {
  216.         return RubyRational.newRationalCanonicalize(context, RubyFixnum.zero(context.runtime));
  217.     }

  218.     /** nilclass_rationalize
  219.      *
  220.      */
  221.     @JRubyMethod(optional = 1)
  222.     public static IRubyObject rationalize(ThreadContext context, IRubyObject recv, IRubyObject[] args) {
  223.         return to_r(context, recv);
  224.     }

  225.     @Override
  226.     public Object toJava(Class target) {
  227.         if (target.isPrimitive()) {
  228.             if (target == Boolean.TYPE) {
  229.                 return Boolean.FALSE;
  230.             } else if (target == Byte.TYPE) {
  231.                 return (byte)0;
  232.             } else if (target == Short.TYPE) {
  233.                 return (short)0;
  234.             } else if (target == Character.TYPE) {
  235.                 return (char)0;
  236.             } else if (target == Integer.TYPE) {
  237.                 return 0;
  238.             } else if (target == Long.TYPE) {
  239.                 return (long)0;
  240.             } else if (target == Float.TYPE) {
  241.                 return (float)0;
  242.             } else if (target == Double.TYPE) {
  243.                 return (double)0;
  244.             }
  245.         }
  246.         return null;
  247.     }
  248. }