|
Groovy example source code file (MixinInMetaClass.java)
The Groovy MixinInMetaClass.java source code
/*
* Copyright 2003-2009 the original author or authors.
*
* Licensed 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.reflection;
import groovy.lang.DelegatingMetaClass;
import groovy.lang.ExpandoMetaClass;
import groovy.lang.GroovyRuntimeException;
import groovy.lang.GroovySystem;
import groovy.lang.MetaClass;
import groovy.lang.MetaMethod;
import groovy.lang.MetaProperty;
import org.codehaus.groovy.runtime.HandleMetaClass;
import org.codehaus.groovy.runtime.MetaClassHelper;
import org.codehaus.groovy.runtime.metaclass.MixedInMetaClass;
import org.codehaus.groovy.runtime.metaclass.MixinInstanceMetaMethod;
import org.codehaus.groovy.runtime.metaclass.MixinInstanceMetaProperty;
import org.codehaus.groovy.runtime.metaclass.NewInstanceMetaMethod;
import org.codehaus.groovy.util.ManagedConcurrentMap;
import org.codehaus.groovy.util.ReferenceBundle;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;
public class MixinInMetaClass extends ManagedConcurrentMap {
final ExpandoMetaClass emc;
final CachedClass mixinClass;
final CachedConstructor constructor;
private static ReferenceBundle softBundle = ReferenceBundle.getSoftBundle();
public MixinInMetaClass(ExpandoMetaClass emc, CachedClass mixinClass) {
super(softBundle);
this.emc = emc;
this.mixinClass = mixinClass;
constructor = findDefaultConstructor(mixinClass);
emc.addMixinClass(this);
}
private CachedConstructor findDefaultConstructor(CachedClass mixinClass) {
for (CachedConstructor constr : mixinClass.getConstructors()) {
if (!Modifier.isPublic(constr.getModifiers()))
continue;
CachedClass[] classes = constr.getParameterTypes();
if (classes.length == 0)
return constr;
}
throw new GroovyRuntimeException("No default constructor for class " + mixinClass.getName() + "! Can't be mixed in.");
}
public synchronized Object getMixinInstance(Object object) {
Object mixinInstance = get(object);
if (mixinInstance == null) {
mixinInstance = constructor.invoke(MetaClassHelper.EMPTY_ARRAY);
new MixedInMetaClass(mixinInstance, object);
put(object, mixinInstance);
}
return mixinInstance;
}
public synchronized void setMixinInstance(Object object, Object mixinInstance) {
if (mixinInstance == null) {
remove(object);
} else {
put(object, mixinInstance);
}
}
public CachedClass getInstanceClass() {
return emc.getTheCachedClass();
}
public CachedClass getMixinClass() {
return mixinClass;
}
public static void mixinClassesToMetaClass(MetaClass self, List<Class> categoryClasses) {
final Class selfClass = self.getTheClass();
if (self instanceof HandleMetaClass) {
self = (MetaClass) ((HandleMetaClass) self).replaceDelegate();
}
if (!(self instanceof ExpandoMetaClass)) {
if (self instanceof DelegatingMetaClass && ((DelegatingMetaClass) self).getAdaptee() instanceof ExpandoMetaClass) {
self = ((DelegatingMetaClass) self).getAdaptee();
} else {
throw new GroovyRuntimeException("Can't mixin methods to meta class: " + self);
}
}
ExpandoMetaClass mc = (ExpandoMetaClass) self;
List<MetaMethod> arr = new ArrayList
Other Groovy examples (source code examples)Here is a short list of links related to this Groovy MixinInMetaClass.java source code file: |
Other websites by Alvin Alexander:
Life/living in Alaska (OneMansAlaska.com)
How I Sold My Business (HowISoldMyBusiness.com)
Copyright 1998-2011 Alvin Alexander, devdaily.com
All Rights Reserved.