/
MetaClassRegistryImpl.java
535 lines (470 loc) · 21.3 KB
/
MetaClassRegistryImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.codehaus.groovy.runtime.metaclass;
import groovy.lang.*;
import org.codehaus.groovy.classgen.Verifier;
import org.codehaus.groovy.reflection.*;
import org.codehaus.groovy.runtime.*;
import org.codehaus.groovy.runtime.m12n.ExtensionModule;
import org.codehaus.groovy.runtime.m12n.ExtensionModuleRegistry;
import org.codehaus.groovy.runtime.m12n.ExtensionModuleScanner;
import org.codehaus.groovy.vmplugin.VMPluginFactory;
import org.codehaus.groovy.util.FastArray;
import org.codehaus.groovy.util.ManagedLinkedList;
import org.codehaus.groovy.util.ReferenceBundle;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.*;
/**
* A registry of MetaClass instances which caches introspection &
* reflection information and allows methods to be dynamically added to
* existing classes at runtime
*
* @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
* @author John Wilson
* @author <a href="mailto:blackdrag@gmx.org">Jochen Theodorou</a>
* @author Graeme Rocher
* @author Alex Tkachman
*
* @version $Revision$
*/
public class MetaClassRegistryImpl implements MetaClassRegistry{
/**
* @deprecated Use {@link ExtensionModuleScanner#MODULE_META_INF_FILE instead}
*/
public static final String MODULE_META_INF_FILE = "META-INF/services/org.codehaus.groovy.runtime.ExtensionModule";
private final boolean useAccessible;
private final FastArray instanceMethods = new FastArray();
private final FastArray staticMethods = new FastArray();
private final LinkedList<MetaClassRegistryChangeEventListener> changeListenerList = new LinkedList<MetaClassRegistryChangeEventListener>();
private final LinkedList<MetaClassRegistryChangeEventListener> nonRemoveableChangeListenerList = new LinkedList<MetaClassRegistryChangeEventListener>();
private final ManagedLinkedList metaClassInfo = new ManagedLinkedList<MetaClass>(ReferenceBundle.getWeakBundle());
private final ExtensionModuleRegistry moduleRegistry = new ExtensionModuleRegistry();
public static final int LOAD_DEFAULT = 0;
public static final int DONT_LOAD_DEFAULT = 1;
private static MetaClassRegistry instanceInclude;
private static MetaClassRegistry instanceExclude;
public MetaClassRegistryImpl() {
this(LOAD_DEFAULT, true);
}
public MetaClassRegistryImpl(int loadDefault) {
this(loadDefault, true);
}
/**
* @param useAccessible defines whether or not the {@link java.lang.reflect.AccessibleObject#setAccessible(boolean)}
* method will be called to enable access to all methods when using reflection
*/
public MetaClassRegistryImpl(boolean useAccessible) {
this(LOAD_DEFAULT, useAccessible);
}
public MetaClassRegistryImpl(final int loadDefault, final boolean useAccessible) {
this.useAccessible = useAccessible;
if (loadDefault == LOAD_DEFAULT) {
final Map<CachedClass, List<MetaMethod>> map = new HashMap<CachedClass, List<MetaMethod>>();
// let's register the default methods
registerMethods(null, true, true, map);
final Class[] additionals = DefaultGroovyMethods.additionals;
for (int i = 0; i != additionals.length; ++i) {
createMetaMethodFromClass(map, additionals[i]);
}
Class[] pluginDGMs = VMPluginFactory.getPlugin().getPluginDefaultGroovyMethods();
for (Class plugin : pluginDGMs) {
registerMethods(plugin, false, true, map);
}
registerMethods(DefaultGroovyStaticMethods.class, false, false, map);
Class[] staticPluginDGMs = VMPluginFactory.getPlugin().getPluginStaticGroovyMethods();
for (Class plugin : staticPluginDGMs) {
registerMethods(plugin, false, false, map);
}
ExtensionModuleScanner scanner = new ExtensionModuleScanner(new DefaultModuleListener(map), this.getClass().getClassLoader());
scanner.scanClasspathModules();
refreshMopMethods(map);
}
installMetaClassCreationHandle();
final MetaClass emcMetaClass = metaClassCreationHandle.create(ExpandoMetaClass.class, this);
emcMetaClass.initialize();
ClassInfo.getClassInfo(ExpandoMetaClass.class).setStrongMetaClass(emcMetaClass);
addNonRemovableMetaClassRegistryChangeEventListener(new MetaClassRegistryChangeEventListener(){
public void updateConstantMetaClass(MetaClassRegistryChangeEvent cmcu) {
synchronized (metaClassInfo) {
metaClassInfo.add(cmcu.getNewMetaClass());
DefaultMetaClassInfo.getNewConstantMetaClassVersioning();
Class c = cmcu.getClassToUpdate();
DefaultMetaClassInfo.setPrimitiveMeta(c, cmcu.getNewMetaClass()==null);
Field sdyn;
try {
sdyn = c.getDeclaredField(Verifier.STATIC_METACLASS_BOOL);
sdyn.setBoolean(null, cmcu.getNewMetaClass()!=null);
} catch (Throwable e) {
//DO NOTHING
}
}
}
});
}
private void refreshMopMethods(final Map<CachedClass, List<MetaMethod>> map) {
for (Map.Entry<CachedClass, List<MetaMethod>> e : map.entrySet()) {
CachedClass cls = e.getKey();
cls.setNewMopMethods(e.getValue());
}
}
public void registerExtensionModuleFromProperties(final Properties properties, final ClassLoader classLoader, final Map<CachedClass, List<MetaMethod>> map) {
ExtensionModuleScanner scanner = new ExtensionModuleScanner(new DefaultModuleListener(map), classLoader);
scanner.scanExtensionModuleFromProperties(properties);
}
public ExtensionModuleRegistry getModuleRegistry() {
return moduleRegistry;
}
/**
* Looks for a class called 'groovy.runtime.metaclass.CustomMetaClassCreationHandle' and if it exists uses it as the MetaClassCreationHandle
* otherwise uses the default
*
* @see groovy.lang.MetaClassRegistry.MetaClassCreationHandle
*/
private void installMetaClassCreationHandle() {
try {
final Class customMetaClassHandle = Class.forName("groovy.runtime.metaclass.CustomMetaClassCreationHandle");
final Constructor customMetaClassHandleConstructor = customMetaClassHandle.getConstructor(new Class[]{});
this.metaClassCreationHandle = (MetaClassCreationHandle)customMetaClassHandleConstructor.newInstance();
} catch (final ClassNotFoundException e) {
this.metaClassCreationHandle = new MetaClassCreationHandle();
} catch (final Exception e) {
throw new GroovyRuntimeException("Could not instantiate custom Metaclass creation handle: "+ e, e);
}
}
private void registerMethods(final Class theClass, final boolean useMethodWrapper, final boolean useInstanceMethods, Map<CachedClass, List<MetaMethod>> map) {
if (useMethodWrapper) {
// Here we instantiate objects representing MetaMethods for DGM methods.
// Calls for such meta methods done without reflection, so more effectively.
try {
List<GeneratedMetaMethod.DgmMethodRecord> records = GeneratedMetaMethod.DgmMethodRecord.loadDgmInfo();
for (GeneratedMetaMethod.DgmMethodRecord record : records) {
Class[] newParams = new Class[record.parameters.length - 1];
System.arraycopy(record.parameters, 1, newParams, 0, record.parameters.length-1);
MetaMethod method = new GeneratedMetaMethod.Proxy(
record.className,
record.methodName,
ReflectionCache.getCachedClass(record.parameters[0]),
record.returnType,
newParams
);
final CachedClass declClass = method.getDeclaringClass();
List<MetaMethod> arr = map.get(declClass);
if (arr == null) {
arr = new ArrayList<MetaMethod>(4);
map.put(declClass, arr);
}
arr.add(method);
instanceMethods.add(method);
}
} catch (Throwable e) {
e.printStackTrace();
// we print the error, but we don't stop with an exception here
// since it is more comfortable this way for development
}
} else {
CachedMethod[] methods = ReflectionCache.getCachedClass(theClass).getMethods();
for (CachedMethod method : methods) {
final int mod = method.getModifiers();
if (Modifier.isStatic(mod) && Modifier.isPublic(mod) && method.getCachedMethod().getAnnotation(Deprecated.class) == null) {
CachedClass[] paramTypes = method.getParameterTypes();
if (paramTypes.length > 0) {
List<MetaMethod> arr = map.get(paramTypes[0]);
if (arr == null) {
arr = new ArrayList<MetaMethod>(4);
map.put(paramTypes[0], arr);
}
if (useInstanceMethods) {
final NewInstanceMetaMethod metaMethod = new NewInstanceMetaMethod(method);
arr.add(metaMethod);
instanceMethods.add(metaMethod);
} else {
final NewStaticMetaMethod metaMethod = new NewStaticMetaMethod(method);
arr.add(metaMethod);
staticMethods.add(metaMethod);
}
}
}
}
}
}
private void createMetaMethodFromClass(Map<CachedClass, List<MetaMethod>> map, Class aClass) {
try {
MetaMethod method = (MetaMethod) aClass.newInstance();
final CachedClass declClass = method.getDeclaringClass();
List<MetaMethod> arr = map.get(declClass);
if (arr == null) {
arr = new ArrayList<MetaMethod>(4);
map.put(declClass, arr);
}
arr.add(method);
instanceMethods.add(method);
} catch (InstantiationException e) { /* ignore */
} catch (IllegalAccessException e) { /* ignore */
}
}
public final MetaClass getMetaClass(Class theClass) {
return ClassInfo.getClassInfo(theClass).getMetaClass();
}
public MetaClass getMetaClass(Object obj) {
return ClassInfo.getClassInfo(obj.getClass()).getMetaClass(obj);
}
/**
* if oldMc is null, newMc will replace whatever meta class was used before.
* if oldMc is not null, then newMc will be used only if he stored mc is
* the same as oldMc
*/
private void setMetaClass(Class theClass, MetaClass oldMc, MetaClass newMc) {
final ClassInfo info = ClassInfo.getClassInfo(theClass);
MetaClass mc = null;
info.lock();
try {
mc = info.getStrongMetaClass();
info.setStrongMetaClass(newMc);
} finally {
info.unlock();
}
if ((oldMc == null && mc != newMc) || (oldMc != null && mc != newMc && mc != oldMc)) {
fireConstantMetaClassUpdate(null, theClass, mc, newMc);
}
}
public void removeMetaClass(Class theClass) {
setMetaClass(theClass, null, null);
}
/**
* Registers a new MetaClass in the registry to customize the type
*
* @param theClass
* @param theMetaClass
*/
public void setMetaClass(Class theClass, MetaClass theMetaClass) {
setMetaClass(theClass,null,theMetaClass);
}
public void setMetaClass(Object obj, MetaClass theMetaClass) {
Class theClass = obj.getClass ();
final ClassInfo info = ClassInfo.getClassInfo(theClass);
MetaClass oldMC = null;
info.lock();
try {
oldMC = info.getPerInstanceMetaClass(obj);
info.setPerInstanceMetaClass(obj, theMetaClass);
}
finally {
info.unlock();
}
fireConstantMetaClassUpdate(obj, theClass, oldMC, theMetaClass);
}
public boolean useAccessible() {
return useAccessible;
}
private volatile MetaClassCreationHandle metaClassCreationHandle = new MetaClassCreationHandle();
/**
* Gets a handle internally used to create MetaClass implementations
* WARNING: experimental code, likely to change soon
* @return the handle
*/
public MetaClassCreationHandle getMetaClassCreationHandler() {
return metaClassCreationHandle;
}
/**
* Sets a handle internally used to create MetaClass implementations.
* When replacing the handle with a custom version, you should
* reuse the old handle to keep custom logic and to use the
* default logic as fall back.
* WARNING: experimental code, likely to change soon
* @param handle the handle
*/
public void setMetaClassCreationHandle(MetaClassCreationHandle handle) {
if(handle == null) throw new IllegalArgumentException("Cannot set MetaClassCreationHandle to null value!");
ClassInfo.clearModifiedExpandos();
handle.setDisableCustomMetaClassLookup(metaClassCreationHandle.isDisableCustomMetaClassLookup());
metaClassCreationHandle = handle;
}
/**
* Adds a listener for constant meta classes.
* @param listener the listener
*/
public void addMetaClassRegistryChangeEventListener(MetaClassRegistryChangeEventListener listener) {
synchronized (changeListenerList) {
changeListenerList.add(listener);
}
}
/**
* Adds a listener for constant meta classes. This listener cannot be removed!
* @param listener the listener
*/
public void addNonRemovableMetaClassRegistryChangeEventListener(MetaClassRegistryChangeEventListener listener) {
synchronized (changeListenerList) {
nonRemoveableChangeListenerList.add(listener);
}
}
/**
* Removes a constant meta class listener.
* @param listener the listener
*/
public void removeMetaClassRegistryChangeEventListener(MetaClassRegistryChangeEventListener listener) {
synchronized (changeListenerList) {
changeListenerList.remove(listener);
}
}
/**
* Causes the execution of all registered listeners. This method is used mostly
* internal to kick of the listener notification. It can also be used by subclasses
* to achieve the same.
*
* @param obj object instance if the MetaClass change is on a per-instance metaclass (or null if global)
* @param c the class
* @param oldMC the old MetaClass
* @param newMc the new MetaClass
*/
protected void fireConstantMetaClassUpdate(Object obj, Class c, final MetaClass oldMC, MetaClass newMc) {
MetaClassRegistryChangeEventListener[] listener = getMetaClassRegistryChangeEventListeners();
MetaClassRegistryChangeEvent cmcu = new MetaClassRegistryChangeEvent(this, obj, c, oldMC, newMc);
for (int i = 0; i<listener.length; i++) {
listener[i].updateConstantMetaClass(cmcu);
}
}
/**
* Gets an array of of all registered ConstantMetaClassListener instances.
*/
public MetaClassRegistryChangeEventListener[] getMetaClassRegistryChangeEventListeners() {
synchronized (changeListenerList) {
ArrayList<MetaClassRegistryChangeEventListener> ret =
new ArrayList<MetaClassRegistryChangeEventListener>(changeListenerList.size()+nonRemoveableChangeListenerList.size());
ret.addAll(nonRemoveableChangeListenerList);
ret.addAll(changeListenerList);
return ret.toArray(new MetaClassRegistryChangeEventListener[ret.size()]);
}
}
/**
* Singleton of MetaClassRegistry.
*
* @param includeExtension
* @return the registry
*/
public static MetaClassRegistry getInstance(int includeExtension) {
if (includeExtension != DONT_LOAD_DEFAULT) {
if (instanceInclude == null) {
instanceInclude = new MetaClassRegistryImpl();
}
return instanceInclude;
} else {
if (instanceExclude == null) {
instanceExclude = new MetaClassRegistryImpl(DONT_LOAD_DEFAULT);
}
return instanceExclude;
}
}
public FastArray getInstanceMethods() {
return instanceMethods;
}
public FastArray getStaticMethods() {
return staticMethods;
}
/**
* Returns an iterator to iterate over all constant meta classes.
* This iterator can be seen as making a snapshot of the current state
* of the registry. The snapshot will include all meta classes that has
* been used unless they are already collected. Collected meta classes
* will be skipped automatically, so you can expect that each element
* of the iteration is not null. Calling this method is thread safe, the
* usage of the iterator is not.
*
* @return the iterator.
*/
public Iterator iterator() {
final MetaClass[] refs;
synchronized (metaClassInfo) {
refs = (MetaClass[]) metaClassInfo.toArray(new MetaClass[0]);
}
return new Iterator() {
// index in the ref array
private int index = 0;
// the current meta class
private MetaClass currentMeta;
// used to ensure that hasNext has been called
private boolean hasNextCalled = false;
// the cached hasNext call value
private boolean hasNext = false;
public boolean hasNext() {
if (hasNextCalled) return hasNext;
hasNextCalled = true;
if(index < refs.length) {
hasNext = true;
currentMeta = refs[index];
index++;
} else {
hasNext = false;
}
return hasNext;
}
private void ensureNext() {
// we ensure that hasNext has been called before
// next is called
hasNext();
hasNextCalled = false;
}
public Object next() {
ensureNext();
return currentMeta;
}
public void remove() {
ensureNext();
setMetaClass(currentMeta.getTheClass(), currentMeta, null);
currentMeta = null;
}
};
}
private class DefaultModuleListener implements ExtensionModuleScanner.ExtensionModuleListener {
private final Map<CachedClass, List<MetaMethod>> map;
public DefaultModuleListener(final Map<CachedClass, List<MetaMethod>> map) {
this.map = map;
}
public void onModule(final ExtensionModule module) {
if (moduleRegistry.hasModule(module.getName())) {
ExtensionModule loadedModule = moduleRegistry.getModule(module.getName());
if (loadedModule.getVersion().equals(module.getVersion())) {
// already registered
return;
} else {
throw new GroovyRuntimeException("Conflicting module versions. Module ["+module.getName()+" is loaded in version "+
loadedModule.getVersion()+" and you are trying to load version "+module.getVersion());
}
}
moduleRegistry.addModule(module);
// register MetaMethods
List<MetaMethod> metaMethods = module.getMetaMethods();
for (MetaMethod metaMethod : metaMethods) {
CachedClass cachedClass = metaMethod.getDeclaringClass();
List<MetaMethod> methods = map.get(cachedClass);
if (methods==null) {
methods = new ArrayList<MetaMethod>(4);
map.put(cachedClass, methods);
}
methods.add(metaMethod);
if (metaMethod.isStatic()) {
staticMethods.add(metaMethod);
} else {
instanceMethods.add(metaMethod);
}
}
}
}
}