KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jruby > runtime > CacheMap


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

28 package org.jruby.runtime;
29
30 import java.util.WeakHashMap JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import java.util.Map JavaDoc;
33 import org.jruby.Ruby;
34
35 import org.jruby.RubyModule;
36 import org.jruby.internal.runtime.methods.DynamicMethod;
37 import org.jruby.util.WeakIdentityHashMap;
38
39 /**
40  * This class represents mappings between methods that have been cached and the classes which
41  * have cached them. Methods within RubyModule will update this cacheMap as needed. Here is
42  * a list of scenarios when cached methods will become invalid:
43  *
44  * 1. Redefine a method in a base class
45  * 2. Add an alias in a superclass that is the same name as a cached method in a base class
46  * 3. Include a module that has a same-named method as one already caches in a base class
47  * 4. Remove a method definition
48  * 5. Add a same-named method in super class that has been cached in a super class
49  *
50  * Concurrency is another concern with managing this structure. Rather than synchronize this
51  * we are going to rely on synchronization further upstream. RubyModule methods that directly
52  * call this is responsible for synchronization.
53  */

54 public class CacheMap {
55     private final Map JavaDoc mappings = new WeakHashMap JavaDoc();
56     private final Ruby runtime;
57     
58     public CacheMap(Ruby runtime) {
59         this.runtime = runtime;
60     }
61
62     /**
63      * Add another class to the list of classes which are caching the method.
64      *
65      * @param method which is cached
66      * @param module which is caching method
67      */

68     public void add(DynamicMethod method, RubyModule module) {
69         Map JavaDoc classList = (Map JavaDoc) mappings.get(method);
70         
71         if (classList == null) {
72             classList = new WeakIdentityHashMap();
73             mappings.put(method, classList);
74         }
75         
76         classList.put(module,null);
77     }
78     
79     /**
80      * Remove all method caches associated with the provided method. This signature
81      * relies on having the methods valid name passed with it since the caching classes
82      * store the cache by name.
83      *
84      * @param name of the method to remove
85      * @param method to remove all caches of
86      */

87     public void remove(String JavaDoc name, DynamicMethod method) {
88         Map JavaDoc classList = (Map JavaDoc) mappings.remove(method);
89         
90         // Removed method has never been used so it has not been cached
91
if (classList == null) {
92             return;
93         }
94         
95         for(Iterator JavaDoc iter = classList.keySet().iterator(); iter.hasNext();) {
96             RubyModule module = (RubyModule) iter.next();
97             if (module != null) {
98                 module.removeCachedMethod(name);
99                 
100                 if (module.index != 0) {
101                     runtime.getSelectorTable().table[module.index][MethodIndex.getIndex(name)] = 0;
102                 }
103             }
104         }
105     }
106 }
107
Popular Tags