VariableAccessorVar0.java

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

  28. import org.jruby.RubyBasicObject;
  29. import org.jruby.RubyClass;
  30. import org.jruby.RubyObjectVar0;

  31. /**
  32.  * A variable accessor that accesses a var0 field directly;
  33.  */
  34. public class VariableAccessorVar0 extends FieldVariableAccessor {
  35.     /**
  36.      * Construct a new StampedVariableAccessor for the given "real" class,
  37.      * variable name, variable index, and class ID.
  38.      *
  39.      * @param realClass the "real" class
  40.      * @param name the variable's name
  41.      * @param index the variable's index
  42.      * @param classId the class's ID
  43.      */
  44.     public VariableAccessorVar0(RubyClass realClass, String name, int index, int classId) {
  45.         super(realClass, name, index, classId, 0);
  46.     }

  47.     /**
  48.      * Retrieve the variable's value from the given object.
  49.      *
  50.      * @param object the object from which to retrieve this variable
  51.      * @return the variable's value
  52.      */
  53.     public Object get(Object object) {
  54.         return ((RubyObjectVar0)object).var0;
  55.     }

  56.     /**
  57.      * Set this variable into the given object using Unsafe to ensure
  58.      * safe updating of the variable table.
  59.      *
  60.      * @param object the object into which to set this variable
  61.      * @param value the variable's value
  62.      */
  63.     public void set(Object object, Object value) {
  64.         ((RubyBasicObject)object).ensureInstanceVariablesSettable();
  65.         setVariable((RubyBasicObject)object, realClass, index, value);
  66.     }
  67.    
  68.     /**
  69.      * Set the given variable index into the specified object. The "real" class
  70.      * and index are pass in to provide functional access. This version checks
  71.      * if self has been frozen before proceeding to set the variable.
  72.      *
  73.      * @param self the object into which to set the variable
  74.      * @param realClass the "real" class for the object
  75.      * @param index the index of the variable
  76.      * @param value the variable's value
  77.      */
  78.     public static void setVariableChecked(RubyBasicObject self, RubyClass realClass, int index, Object value) {
  79.         self.ensureInstanceVariablesSettable();
  80.         setVariable(self, realClass, index, value);
  81.     }
  82.    
  83.     /**
  84.      * Set the given variable index into the specified object. The "real" class
  85.      * and index are pass in to provide functional access.
  86.      *
  87.      * @param self the object into which to set the variable
  88.      * @param realClass the "real" class for the object
  89.      * @param index the index of the variable
  90.      * @param value the variable's value
  91.      */
  92.     public static void setVariable(RubyBasicObject self, RubyClass realClass, int index, Object value) {
  93.         ((RubyObjectVar0)self).var0 = value;
  94.     }
  95. }