Debian Codegraph @

rhino/src/org/mozilla/javascript/ClassCache.java

raw symbols SCIP
1 /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 package org.mozilla.javascript;
8
9 import java.util.Map;
10 import java.util.concurrent.ConcurrentHashMap;
11 import java.io.Serializable;
12
13 /**
14 * Cache of generated classes and data structures to access Java runtime
15 * from JavaScript.
16 *
17 * @author Igor Bukanov
18 *
19 * @since Rhino 1.5 Release 5
20 */
21 public class ClassCache implements Serializable
22 {
23 private static final long serialVersionUID = -8866246036237312215L;
24 private static final Object AKEY = "ClassCache";
25 private volatile boolean cachingIsEnabled = true;
26 private transient Map<Class<?>,JavaMembers> classTable;
27 private transient Map<JavaAdapter.JavaAdapterSignature,Class<?>> classAdapterCache;
28 private transient Map<Class<?>,Object> interfaceAdapterCache;
29 private int generatedClassSerial;
30 private Scriptable associatedScope;
31
32 /**
33 * Search for ClassCache object in the given scope.
34 * The method first calls
35 * {@link ScriptableObject#getTopLevelScope(Scriptable scope)}
36 * to get the top most scope and then tries to locate associated
37 * ClassCache object in the prototype chain of the top scope.
38 *
39 * @param scope scope to search for ClassCache object.
40 * @return previously associated ClassCache object or a new instance of
41 * ClassCache if no ClassCache object was found.
42 *
43 * @see #associate(ScriptableObject topScope)
44 */
45 public static ClassCache get(Scriptable scope)
46 {
47 ClassCache cache = (ClassCache)
48 ScriptableObject.getTopScopeValue(scope, AKEY);
49 if (cache == null) {
50 throw new RuntimeException("Can't find top level scope for " +
51 "ClassCache.get");
52 }
53 return cache;
54 }
55
56 /**
57 * Associate ClassCache object with the given top-level scope.
58 * The ClassCache object can only be associated with the given scope once.
59 *
60 * @param topScope scope to associate this ClassCache object with.
61 * @return true if no previous ClassCache objects were embedded into
62 * the scope and this ClassCache were successfully associated
63 * or false otherwise.
64 *
65 * @see #get(Scriptable scope)
66 */
67 public boolean associate(ScriptableObject topScope)
68 {
69 if (topScope.getParentScope() != null) {
70 // Can only associate cache with top level scope
71 throw new IllegalArgumentException();
72 }
73 if (this == topScope.associateValue(AKEY, this)) {
74 associatedScope = topScope;
75 return true;
76 }
77 return false;
78 }
79
80 /**
81 * Empty caches of generated Java classes and Java reflection information.
82 */
83 public synchronized void clearCaches()
84 {
85 classTable = null;
86 classAdapterCache = null;
87 interfaceAdapterCache = null;
88 }
89
90 /**
91 * Check if generated Java classes and Java reflection information
92 * is cached.
93 */
94 public final boolean isCachingEnabled()
95 {
96 return cachingIsEnabled;
97 }
98
99 /**
100 * Set whether to cache some values.
101 * <p>
102 * By default, the engine will cache the results of
103 * <tt>Class.getMethods()</tt> and similar calls.
104 * This can speed execution dramatically, but increases the memory
105 * footprint. Also, with caching enabled, references may be held to
106 * objects past the lifetime of any real usage.
107 * <p>
108 * If caching is enabled and this method is called with a
109 * <code>false</code> argument, the caches will be emptied.
110 * <p>
111 * Caching is enabled by default.
112 *
113 * @param enabled if true, caching is enabled
114 *
115 * @see #clearCaches()
116 */
117 public synchronized void setCachingEnabled(boolean enabled)
118 {
119 if (enabled == cachingIsEnabled)
120 return;
121 if (!enabled)
122 clearCaches();
123 cachingIsEnabled = enabled;
124 }
125
126 /**
127 * @return a map from classes to associated JavaMembers objects
128 */
129 Map<Class<?>,JavaMembers> getClassCacheMap() {
130 if (classTable == null) {
131 // Use 1 as concurrency level here and for other concurrent hash maps
132 // as we don't expect high levels of sustained concurrent writes.
133 classTable = new ConcurrentHashMap<Class<?>,JavaMembers>(16, 0.75f, 1);
134 }
135 return classTable;
136 }
137
138 Map<JavaAdapter.JavaAdapterSignature,Class<?>> getInterfaceAdapterCacheMap()
139 {
140 if (classAdapterCache == null) {
141 classAdapterCache = new ConcurrentHashMap<JavaAdapter.JavaAdapterSignature,Class<?>>(16, 0.75f, 1);
142 }
143 return classAdapterCache;
144 }
145
146 /**
147 * @deprecated
148 * The method always returns false.
149 * @see #setInvokerOptimizationEnabled(boolean enabled)
150 */
151 @Deprecated
152 public boolean isInvokerOptimizationEnabled()
153 {
154 return false;
155 }
156
157 /**
158 * @deprecated
159 * The method does nothing.
160 * Invoker optimization is no longer used by Rhino.
161 * On modern JDK like 1.4 or 1.5 the disadvantages of the optimization
162 * like increased memory usage or longer initialization time overweight
163 * small speed increase that can be gained using generated proxy class
164 * to replace reflection.
165 */
166 @Deprecated
167 public synchronized void setInvokerOptimizationEnabled(boolean enabled)
168 {
169 }
170
171 /**
172 * Internal engine method to return serial number for generated classes
173 * to ensure name uniqueness.
174 */
175 public final synchronized int newClassSerialNumber()
176 {
177 return ++generatedClassSerial;
178 }
179
180 Object getInterfaceAdapter(Class<?> cl)
181 {
182 return interfaceAdapterCache == null
183 ? null
184 : interfaceAdapterCache.get(cl);
185 }
186
187 synchronized void cacheInterfaceAdapter(Class<?> cl, Object iadapter)
188 {
189 if (cachingIsEnabled) {
190 if (interfaceAdapterCache == null) {
191 interfaceAdapterCache = new ConcurrentHashMap<Class<?>,Object>(16, 0.75f, 1);
192 }
193 interfaceAdapterCache.put(cl, iadapter);
194 }
195 }
196
197 Scriptable getAssociatedScope() {
198 return associatedScope;
199 }
200 }