use of groovy.lang.MetaMethod in project groovy by apache.
the class MetaMethodIndex method copyNonPrivateNonNewMetaMethods.
private void copyNonPrivateNonNewMetaMethods(Entry from, Header to) {
Object oldListOrMethod = from.methods;
if (oldListOrMethod == null)
return;
if (oldListOrMethod instanceof FastArray) {
FastArray oldList = (FastArray) oldListOrMethod;
Entry e = null;
int len1 = oldList.size();
Object[] list = oldList.getArray();
for (int j = 0; j != len1; ++j) {
MetaMethod method = (MetaMethod) list[j];
if (method instanceof NewMetaMethod || method.isPrivate())
continue;
if (e == null)
e = getOrPutMethods(from.name, to);
e.methods = addMethodToList(e.methods, method);
}
} else {
MetaMethod method = (MetaMethod) oldListOrMethod;
if (method instanceof NewMetaMethod || method.isPrivate())
return;
Entry e = getOrPutMethods(from.name, to);
e.methods = addMethodToList(e.methods, method);
}
}
use of groovy.lang.MetaMethod in project spock by spockframework.
the class AddSlotFactory method create.
@Override
public ISlot create(Object owner, Type ownerType, String name) {
String addMethodName = GroovyRuntimeUtil.propertyToMethodName("add", name);
MetaMethod addMethod = GroovyRuntimeUtil.getMetaClass(owner).pickMethod(addMethodName, new Class[] { Object.class });
return addMethod != null ? new SetterLikeSlot(owner, ownerType, addMethod) : null;
}
use of groovy.lang.MetaMethod in project groovy-core by groovy.
the class MixinInMetaClass method mixinClassesToMetaClass.
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<MetaMethod>();
for (Class categoryClass : categoryClasses) {
final CachedClass cachedCategoryClass = ReflectionCache.getCachedClass(categoryClass);
final MixinInMetaClass mixin = new MixinInMetaClass(mc, cachedCategoryClass);
final MetaClass metaClass = GroovySystem.getMetaClassRegistry().getMetaClass(categoryClass);
final List<MetaProperty> propList = metaClass.getProperties();
for (MetaProperty prop : propList) if (self.getMetaProperty(prop.getName()) == null) {
mc.registerBeanProperty(prop.getName(), new MixinInstanceMetaProperty(prop, mixin));
}
for (MetaProperty prop : cachedCategoryClass.getFields()) if (self.getMetaProperty(prop.getName()) == null) {
mc.registerBeanProperty(prop.getName(), new MixinInstanceMetaProperty(prop, mixin));
}
for (MetaMethod method : metaClass.getMethods()) {
final int mod = method.getModifiers();
if (!Modifier.isPublic(mod))
continue;
if (method instanceof CachedMethod && ((CachedMethod) method).getCachedMethod().isSynthetic())
continue;
if (Modifier.isStatic(mod)) {
if (method instanceof CachedMethod)
staticMethod(self, arr, (CachedMethod) method);
} else if (method.getDeclaringClass().getTheClass() != Object.class || method.getName().equals("toString")) {
// if (self.pickMethod(method.getName(), method.getNativeParameterTypes()) == null) {
final MixinInstanceMetaMethod metaMethod = new MixinInstanceMetaMethod(method, mixin);
arr.add(metaMethod);
// }
}
}
}
for (Object res : arr) {
final MetaMethod metaMethod = (MetaMethod) res;
if (metaMethod.getDeclaringClass().isAssignableFrom(selfClass))
mc.registerInstanceMethod(metaMethod);
else {
mc.registerSubclassInstanceMethod(metaMethod);
}
}
}
use of groovy.lang.MetaMethod in project groovy-core by groovy.
the class MethodRankHelper method rankMethods.
/**
* Returns a sorted(ranked) list of a selection of the methods among candidates which
* most closely resembles original.
*
* @param name
* @param original
* @param methods
* @return a sorted lists of Methods
*/
private static List<MetaMethod> rankMethods(String name, Object[] original, List<MetaMethod> methods) {
List<RankableMethod> rm = new ArrayList<RankableMethod>(methods.size());
if (original == null)
original = EMPTY_OBJECT_ARRAY;
Class[] ta = new Class[original.length];
Class nullC = NullObject.class;
for (int i = 0; i < original.length; i++) {
//All nulls have to be wrapped so that they can be compared
ta[i] = original[i] == null ? nullC : original[i].getClass();
}
for (MetaMethod m : methods) {
rm.add(new RankableMethod(name, ta, m));
}
Collections.sort(rm);
List<MetaMethod> l = new ArrayList<MetaMethod>(rm.size());
for (RankableMethod m : rm) {
if (l.size() > MAX_RECOMENDATIONS)
break;
if (m.score > MAX_METHOD_SCORE)
break;
l.add(m.m);
}
return l;
}
use of groovy.lang.MetaMethod in project groovy-core by groovy.
the class Inspector method getMetaMethods.
/**
* Get info about instance and class Methods that are dynamically added through Groovy.
*
* @return Array of StringArrays that can be indexed with the MEMBER_xxx_IDX constants
*/
public Object[] getMetaMethods() {
MetaClass metaClass = InvokerHelper.getMetaClass(objectUnderInspection);
List metaMethods = metaClass.getMetaMethods();
Object[] result = new Object[metaMethods.size()];
int i = 0;
for (Iterator iter = metaMethods.iterator(); iter.hasNext(); i++) {
MetaMethod metaMethod = (MetaMethod) iter.next();
result[i] = methodInfo(metaMethod);
}
return result;
}
Aggregations