KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jruby > IncludedModuleWrapper


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) 2002-2004 Jan Arne Petersen <jpetersen@uni-bonn.de>
15  * Copyright (C) 2004-2006 Thomas E Enebo <enebo@acm.org>
16  * Copyright (C) 2005 Charles O Nutter <headius@headius.com>
17  * Copyright (C) 2006 Miguel Covarrubias <mlcovarrubias@gmail.com>
18  *
19  * Alternatively, the contents of this file may be used under the terms of
20  * either of the GNU General Public License Version 2 or later (the "GPL"),
21  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
22  * in which case the provisions of the GPL or the LGPL are applicable instead
23  * of those above. If you wish to allow use of your version of this file only
24  * under the terms of either the GPL or the LGPL, and not to allow others to
25  * use your version of this file under the terms of the CPL, indicate your
26  * decision by deleting the provisions above and replace them with the notice
27  * and other provisions required by the GPL or the LGPL. If you do not delete
28  * the provisions above, a recipient may use your version of this file under
29  * the terms of any one of the CPL, the GPL or the LGPL.
30  ***** END LICENSE BLOCK *****/

31 package org.jruby;
32
33 import java.util.Map JavaDoc;
34
35 /**
36  * This class is used to provide an intermediate superclass for modules and classes that include
37  * other modules. It inserts itself as the immediate superClass of the includer, but defers all
38  * module methods to the actual superclass. Multiple of these intermediate superclasses can be
39  * added for multiple included modules.
40  *
41  * This allows the normal superclass-based searches (searchMethod, getConstant, etc) to traverse
42  * the superclass ancestors as normal while the included modules do not actually show up in
43  * direct inheritance traversal.
44  *
45  * @see org.jruby.RubyModule
46  */

47 public final class IncludedModuleWrapper extends RubyClass {
48     private RubyModule delegate;
49
50     public IncludedModuleWrapper(Ruby runtime, RubyClass superClass, RubyModule delegate) {
51         super(runtime, superClass, null);
52         // FIXME: The null makes me nervous, but it makes sense that an included wrapper would never have an allocator
53

54         this.delegate = delegate;
55     }
56
57     /**
58      * Overridden newIncludeClass implementation to allow attaching future includes to the correct module
59      * (i.e. the one to which this is attached)
60      *
61      * @see org.jruby.RubyModule#newIncludeClass(RubyClass)
62      */

63     public IncludedModuleWrapper newIncludeClass(RubyClass superClass) {
64         IncludedModuleWrapper includedModule = new IncludedModuleWrapper(getRuntime(), superClass, getNonIncludedClass());
65         
66         // include its parent (and in turn that module's parents)
67
if (getSuperClass() != null) {
68             includedModule.includeModule(getSuperClass());
69         }
70         
71         return includedModule;
72     }
73
74     public boolean isModule() {
75         return false;
76     }
77
78     public boolean isClass() {
79         return false;
80     }
81
82     public boolean isIncluded() {
83         return true;
84     }
85     
86     public boolean isImmediate() {
87         return true;
88     }
89
90     public RubyClass getMetaClass() {
91         return delegate.getMetaClass();
92     }
93
94     public void setMetaClass(RubyClass newRubyClass) {
95         throw new UnsupportedOperationException JavaDoc("An included class is only a wrapper for a module");
96     }
97
98     public Map JavaDoc getMethods() {
99         return delegate.getMethods();
100     }
101
102     public void setMethods(Map JavaDoc newMethods) {
103         throw new UnsupportedOperationException JavaDoc("An included class is only a wrapper for a module");
104     }
105
106     public Map JavaDoc getInstanceVariables() {
107         return delegate.getInstanceVariables();
108     }
109
110     public void setInstanceVariables(Map JavaDoc newMethods) {
111         throw new UnsupportedOperationException JavaDoc("An included class is only a wrapper for a module");
112     }
113
114     public String JavaDoc getName() {
115         return delegate.getName();
116     }
117
118     public RubyModule getNonIncludedClass() {
119         return delegate;
120     }
121     
122     public RubyClass getRealClass() {
123         return getSuperClass().getRealClass();
124     }
125
126     public boolean isSame(RubyModule module) {
127         return delegate.isSame(module);
128     }
129     
130    /**
131     * We don't want to reveal ourselves to Ruby code, so delegate this
132     * operation.
133     */

134     public RubyFixnum id() {
135         return delegate.id();
136     }
137 }
138
Popular Tags