| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 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 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 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 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 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 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
public boolean associate(ScriptableObject topScope) |
| 68 |
{ |
| 69 |
if (topScope.getParentScope() != null) { |
| 70 |
|
| 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 |
|
| 82 |
|
| 83 |
public synchronized void clearCaches() |
| 84 |
{ |
| 85 |
classTable = null; |
| 86 |
classAdapterCache = null; |
| 87 |
interfaceAdapterCache = null; |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
public final boolean isCachingEnabled() |
| 95 |
{ |
| 96 |
return cachingIsEnabled; |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 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 |
|
| 128 |
|
| 129 |
Map<Class<?>,JavaMembers> getClassCacheMap() { |
| 130 |
if (classTable == null) { |
| 131 |
|
| 132 |
|
| 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 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
@Deprecated |
| 152 |
public boolean isInvokerOptimizationEnabled() |
| 153 |
{ |
| 154 |
return false; |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
@Deprecated |
| 167 |
public synchronized void setInvokerOptimizationEnabled(boolean enabled) |
| 168 |
{ |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 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 |
} |