IncludedModuleWrapper.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.  * Copyright (C) 2002-2004 Jan Arne Petersen <jpetersen@uni-bonn.de>
  16.  * Copyright (C) 2004-2006 Thomas E Enebo <enebo@acm.org>
  17.  * Copyright (C) 2005 Charles O Nutter <headius@headius.com>
  18.  * Copyright (C) 2006 Miguel Covarrubias <mlcovarrubias@gmail.com>
  19.  * Copyright (C) 2007 William N Dortch <bill.dortch@gmail.com>
  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 java.util.Collection;
  35. import java.util.HashSet;
  36. import java.util.List;
  37. import java.util.Map;
  38. import java.util.Set;

  39. import org.jruby.internal.runtime.methods.DynamicMethod;
  40. import org.jruby.runtime.Visibility;
  41. import org.jruby.runtime.builtin.IRubyObject;
  42. import org.jruby.runtime.builtin.Variable;

  43. /**
  44.  * This class is used to provide an intermediate superclass for modules and classes that include
  45.  * other modules. It inserts itself as the immediate superClass of the includer, but defers all
  46.  * module methods to the actual superclass. Multiple of these intermediate superclasses can be
  47.  * added for multiple included modules.
  48.  *
  49.  * This allows the normal superclass-based searches (searchMethod, getConstant, etc) to traverse
  50.  * the superclass ancestors as normal while the included modules do not actually show up in
  51.  * direct inheritance traversal.
  52.  *
  53.  * @see org.jruby.RubyModule
  54.  */
  55. public class IncludedModuleWrapper extends IncludedModule {
  56.     public IncludedModuleWrapper(Ruby runtime, RubyClass superClass, RubyModule origin) {
  57.         super(runtime, superClass, origin);
  58.         origin.addIncludingHierarchy(this);
  59.         if (origin.methodLocation != origin) this.methodLocation = origin.methodLocation;
  60.     }

  61.     /**
  62.      * Overridden newIncludeClass implementation to allow attaching future includes to the correct module
  63.      * (i.e. the one to which this is attached)
  64.      *
  65.      * @see org.jruby.RubyModule#newIncludeClass(RubyClass)
  66.      */
  67.     @Override
  68.     @Deprecated
  69.     public IncludedModuleWrapper newIncludeClass(RubyClass superClass) {
  70.         IncludedModuleWrapper includedModule = new IncludedModuleWrapper(getRuntime(), superClass, getNonIncludedClass());
  71.        
  72.         // include its parent (and in turn that module's parents)
  73.         if (getSuperClass() != null) {
  74.             includedModule.includeModule(getSuperClass());
  75.         }
  76.        
  77.         return includedModule;
  78.     }

  79.     @Override
  80.     public void addMethod(String name, DynamicMethod method) {
  81.         throw new UnsupportedOperationException("An included class is only a wrapper for a module");
  82.     }

  83.     public void setMethods(Map newMethods) {
  84.         throw new UnsupportedOperationException("An included class is only a wrapper for a module");
  85.     }

  86.     public RubyModule getDelegate() {
  87.         return origin;
  88.     }

  89.     @Override
  90.     public boolean isIncluded() {
  91.         return true;
  92.     }

  93.     @Override
  94.     public boolean isPrepended() {
  95.         return origin.hasPrepends();
  96.     }

  97.     @Override
  98.     protected boolean isSame(RubyModule module) {
  99.         return origin.isSame(module.getDelegate());
  100.     }

  101.     @Override
  102.     public Map<String, DynamicMethod> getMethods() {
  103.         return origin.getMethods();
  104.     }

  105.     @Override
  106.     public Map<String, DynamicMethod> getMethodsForWrite() {
  107.         return origin.getMethodsForWrite();
  108.     }

  109.     @Override
  110.     protected synchronized Map<String, IRubyObject> getClassVariables() {
  111.         return origin.getClassVariables();
  112.     }

  113.     @Override
  114.     protected Map<String, IRubyObject> getClassVariablesForRead() {
  115.         return origin.getClassVariablesForRead();
  116.     }

  117.     @Override
  118.     protected boolean variableTableContains(String name) {
  119.         return origin.variableTableContains(name);
  120.     }

  121.     @Override
  122.     protected Object variableTableFetch(String name) {
  123.         return origin.variableTableFetch(name);
  124.     }

  125.     @Override
  126.     protected Object variableTableStore(String name, Object value) {
  127.         return origin.variableTableStore(name, value);
  128.     }

  129.     @Override
  130.     protected Object variableTableRemove(String name) {
  131.         return origin.variableTableRemove(name);
  132.     }

  133.     @Override
  134.     protected void variableTableSync(List<Variable<Object>> vars) {
  135.         origin.variableTableSync(vars);
  136.     }

  137.     //
  138.     // CONSTANT TABLE METHODS - pass to origin
  139.     //

  140.     @Override
  141.     protected boolean constantTableContains(String name) {
  142.         return origin.constantTableContains(name);
  143.     }

  144.     @Override
  145.     protected IRubyObject constantTableFetch(String name) {
  146.         return origin.constantTableFetch(name);
  147.     }

  148.     @Override
  149.     protected ConstantEntry constantEntryFetch(String name) {
  150.         return origin.constantEntryFetch(name);
  151.     }

  152.     @Override
  153.     protected IRubyObject constantTableStore(String name, IRubyObject value) {
  154.         // FIXME: legal here? may want UnsupportedOperationException
  155.         return origin.constantTableStore(name, value);
  156.     }

  157.     @Override
  158.     protected IRubyObject constantTableRemove(String name) {
  159.         // this _is_ legal (when removing an undef)
  160.         return origin.constantTableRemove(name);
  161.     }
  162.    
  163.     @Override
  164.     @Deprecated
  165.     public List<String> getStoredConstantNameList() {
  166.         return origin.getStoredConstantNameList();
  167.     }
  168.    
  169.     @Override
  170.     public Collection<String> getConstantNames() {
  171.         return origin.getConstantNames();
  172.     }

  173.     @Override
  174.     public Collection<String> getConstantNames(boolean includePrivate) {
  175.         return origin.getConstantNames(includePrivate);
  176.     }

  177.     @Override
  178.     public IRubyObject getAutoloadConstant(String name) {
  179.         return origin.getAutoloadConstant(name);
  180.     }
  181. }