Search in sources :

Example 21 with MetaMethod

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);
    }
}
Also used : MetaMethod(groovy.lang.MetaMethod) GeneratedMetaMethod(org.codehaus.groovy.reflection.GeneratedMetaMethod) FastArray(org.codehaus.groovy.util.FastArray)

Example 22 with MetaMethod

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;
}
Also used : MetaMethod(groovy.lang.MetaMethod)

Example 23 with MetaMethod

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);
        }
    }
}
Also used : MixinInstanceMetaMethod(org.codehaus.groovy.runtime.metaclass.MixinInstanceMetaMethod) MetaMethod(groovy.lang.MetaMethod) NewInstanceMetaMethod(org.codehaus.groovy.runtime.metaclass.NewInstanceMetaMethod) HandleMetaClass(org.codehaus.groovy.runtime.HandleMetaClass) GroovyRuntimeException(groovy.lang.GroovyRuntimeException) ArrayList(java.util.ArrayList) MetaClass(groovy.lang.MetaClass) ExpandoMetaClass(groovy.lang.ExpandoMetaClass) MixedInMetaClass(org.codehaus.groovy.runtime.metaclass.MixedInMetaClass) HandleMetaClass(org.codehaus.groovy.runtime.HandleMetaClass) DelegatingMetaClass(groovy.lang.DelegatingMetaClass) MixinInstanceMetaProperty(org.codehaus.groovy.runtime.metaclass.MixinInstanceMetaProperty) MetaClass(groovy.lang.MetaClass) ExpandoMetaClass(groovy.lang.ExpandoMetaClass) MixedInMetaClass(org.codehaus.groovy.runtime.metaclass.MixedInMetaClass) HandleMetaClass(org.codehaus.groovy.runtime.HandleMetaClass) DelegatingMetaClass(groovy.lang.DelegatingMetaClass) ExpandoMetaClass(groovy.lang.ExpandoMetaClass) MixinInstanceMetaProperty(org.codehaus.groovy.runtime.metaclass.MixinInstanceMetaProperty) MetaProperty(groovy.lang.MetaProperty) DelegatingMetaClass(groovy.lang.DelegatingMetaClass) MixinInstanceMetaMethod(org.codehaus.groovy.runtime.metaclass.MixinInstanceMetaMethod)

Example 24 with 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;
}
Also used : MetaMethod(groovy.lang.MetaMethod) ArrayList(java.util.ArrayList) CachedClass(org.codehaus.groovy.reflection.CachedClass)

Example 25 with MetaMethod

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;
}
Also used : MetaMethod(groovy.lang.MetaMethod) MetaClass(groovy.lang.MetaClass) GroovyObject(groovy.lang.GroovyObject)

Aggregations

MetaMethod (groovy.lang.MetaMethod)35 GeneratedMetaMethod (org.codehaus.groovy.reflection.GeneratedMetaMethod)14 CachedClass (org.codehaus.groovy.reflection.CachedClass)13 FastArray (org.codehaus.groovy.util.FastArray)12 ArrayList (java.util.ArrayList)8 MetaClass (groovy.lang.MetaClass)5 LinkedList (java.util.LinkedList)4 NewInstanceMetaMethod (org.codehaus.groovy.runtime.metaclass.NewInstanceMetaMethod)4 DelegatingMetaClass (groovy.lang.DelegatingMetaClass)3 ExpandoMetaClass (groovy.lang.ExpandoMetaClass)3 GroovyRuntimeException (groovy.lang.GroovyRuntimeException)3 GroovyObject (groovy.lang.GroovyObject)2 MetaProperty (groovy.lang.MetaProperty)2 File (java.io.File)2 Constructor (java.lang.reflect.Constructor)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 HashSet (java.util.HashSet)2 CachedConstructor (org.codehaus.groovy.reflection.CachedConstructor)2 CachedMethod (org.codehaus.groovy.reflection.CachedMethod)2 ClassInfo (org.codehaus.groovy.reflection.ClassInfo)2