Search in sources :

Example 1 with ClosureStaticMetaMethod

use of org.codehaus.groovy.runtime.metaclass.ClosureStaticMetaMethod in project groovy-core by groovy.

the class ExpandoMetaClass method registerStaticMethod.

/**
     * Registers a new static method for the given method name and closure on this MetaClass
     *
     * @param name     The method name
     * @param callable The callable Closure
     */
protected void registerStaticMethod(final String name, final Closure callable, final Class[] paramTypes) {
    performOperationOnMetaClass(new Callable() {

        public void call() {
            String methodName;
            if (name.equals(METHOD_MISSING))
                methodName = STATIC_METHOD_MISSING;
            else if (name.equals(PROPERTY_MISSING))
                methodName = STATIC_PROPERTY_MISSING;
            else
                methodName = name;
            ClosureStaticMetaMethod metaMethod = null;
            if (paramTypes != null) {
                metaMethod = new ClosureStaticMetaMethod(methodName, theClass, callable, paramTypes);
            } else {
                metaMethod = new ClosureStaticMetaMethod(methodName, theClass, callable);
            }
            if (methodName.equals(INVOKE_METHOD_METHOD) && callable.getParameterTypes().length == 2) {
                invokeStaticMethodMethod = metaMethod;
            } else {
                if (methodName.equals(METHOD_MISSING)) {
                    methodName = STATIC_METHOD_MISSING;
                }
                MethodKey key = new DefaultCachedMethodKey(theClass, methodName, metaMethod.getParameterTypes(), false);
                addMetaMethod(metaMethod);
                dropStaticMethodCache(methodName);
                if (isGetter(methodName, metaMethod.getParameterTypes())) {
                    String propertyName = getPropertyForGetter(methodName);
                    registerBeanPropertyForMethod(metaMethod, propertyName, true, true);
                } else if (isSetter(methodName, metaMethod.getParameterTypes())) {
                    String propertyName = getPropertyForSetter(methodName);
                    registerBeanPropertyForMethod(metaMethod, propertyName, false, true);
                }
                performRegistryCallbacks();
                expandoMethods.put(key, metaMethod);
            }
        }
    });
}
Also used : ClosureStaticMetaMethod(org.codehaus.groovy.runtime.metaclass.ClosureStaticMetaMethod) DefaultCachedMethodKey(org.codehaus.groovy.runtime.DefaultCachedMethodKey) MethodKey(org.codehaus.groovy.runtime.MethodKey) DefaultCachedMethodKey(org.codehaus.groovy.runtime.DefaultCachedMethodKey)

Example 2 with ClosureStaticMetaMethod

use of org.codehaus.groovy.runtime.metaclass.ClosureStaticMetaMethod in project groovy-core by groovy.

the class ExpandoMetaClass method refreshInheritedMethods.

private void refreshInheritedMethods(ExpandoMetaClass superExpando) {
    List<MetaMethod> metaMethods = superExpando.getExpandoMethods();
    for (MetaMethod metaMethod : metaMethods) {
        if (metaMethod.isStatic()) {
            if (superExpando.getTheClass() != getTheClass())
                // don't inherit static methods except our own
                continue;
            registerStaticMethod(metaMethod.getName(), (Closure) ((ClosureStaticMetaMethod) metaMethod).getClosure().clone());
        } else
            addSuperMethodIfNotOverridden(metaMethod);
    }
    Collection<MetaProperty> metaProperties = superExpando.getExpandoProperties();
    for (Object metaProperty : metaProperties) {
        MetaBeanProperty property = (MetaBeanProperty) metaProperty;
        expandoProperties.put(property.getName(), property);
        addMetaBeanProperty(property);
    }
}
Also used : ClosureStaticMetaMethod(org.codehaus.groovy.runtime.metaclass.ClosureStaticMetaMethod) MixinInstanceMetaMethod(org.codehaus.groovy.runtime.metaclass.MixinInstanceMetaMethod) ClosureMetaMethod(org.codehaus.groovy.runtime.metaclass.ClosureMetaMethod) ThreadManagedMetaBeanProperty(org.codehaus.groovy.runtime.metaclass.ThreadManagedMetaBeanProperty)

Example 3 with ClosureStaticMetaMethod

use of org.codehaus.groovy.runtime.metaclass.ClosureStaticMetaMethod in project groovy by apache.

the class ExpandoMetaClass method refreshInheritedMethods.

private void refreshInheritedMethods(ExpandoMetaClass superExpando) {
    List<MetaMethod> metaMethods = superExpando.getExpandoMethods();
    for (MetaMethod metaMethod : metaMethods) {
        if (metaMethod.isStatic()) {
            if (superExpando.getTheClass() != getTheClass())
                // don't inherit static methods except our own
                continue;
            registerStaticMethod(metaMethod.getName(), (Closure) ((ClosureStaticMetaMethod) metaMethod).getClosure().clone());
        } else
            addSuperMethodIfNotOverridden(metaMethod);
    }
    Collection<MetaProperty> metaProperties = superExpando.getExpandoProperties();
    for (Object metaProperty : metaProperties) {
        MetaBeanProperty property = (MetaBeanProperty) metaProperty;
        expandoProperties.put(property.getName(), property);
        addMetaBeanProperty(property);
    }
}
Also used : ClosureStaticMetaMethod(org.codehaus.groovy.runtime.metaclass.ClosureStaticMetaMethod) MixinInstanceMetaMethod(org.codehaus.groovy.runtime.metaclass.MixinInstanceMetaMethod) ClosureMetaMethod(org.codehaus.groovy.runtime.metaclass.ClosureMetaMethod) ThreadManagedMetaBeanProperty(org.codehaus.groovy.runtime.metaclass.ThreadManagedMetaBeanProperty)

Example 4 with ClosureStaticMetaMethod

use of org.codehaus.groovy.runtime.metaclass.ClosureStaticMetaMethod in project groovy by apache.

the class ExpandoMetaClass method registerStaticMethod.

/**
     * Registers a new static method for the given method name and closure on this MetaClass
     *
     * @param name     The method name
     * @param callable The callable Closure
     */
protected void registerStaticMethod(final String name, final Closure callable, final Class[] paramTypes) {
    performOperationOnMetaClass(new Callable() {

        public void call() {
            String methodName;
            if (name.equals(METHOD_MISSING))
                methodName = STATIC_METHOD_MISSING;
            else if (name.equals(PROPERTY_MISSING))
                methodName = STATIC_PROPERTY_MISSING;
            else
                methodName = name;
            ClosureStaticMetaMethod metaMethod = null;
            if (paramTypes != null) {
                metaMethod = new ClosureStaticMetaMethod(methodName, theClass, callable, paramTypes);
            } else {
                metaMethod = new ClosureStaticMetaMethod(methodName, theClass, callable);
            }
            if (methodName.equals(INVOKE_METHOD_METHOD) && callable.getParameterTypes().length == 2) {
                invokeStaticMethodMethod = metaMethod;
            } else {
                if (methodName.equals(METHOD_MISSING)) {
                    methodName = STATIC_METHOD_MISSING;
                }
                MethodKey key = new DefaultCachedMethodKey(theClass, methodName, metaMethod.getParameterTypes(), false);
                addMetaMethod(metaMethod);
                dropStaticMethodCache(methodName);
                if (isGetter(methodName, metaMethod.getParameterTypes())) {
                    String propertyName = getPropertyForGetter(methodName);
                    registerBeanPropertyForMethod(metaMethod, propertyName, true, true);
                } else if (isSetter(methodName, metaMethod.getParameterTypes())) {
                    String propertyName = getPropertyForSetter(methodName);
                    registerBeanPropertyForMethod(metaMethod, propertyName, false, true);
                }
                performRegistryCallbacks();
                expandoMethods.put(key, metaMethod);
            }
        }
    });
}
Also used : ClosureStaticMetaMethod(org.codehaus.groovy.runtime.metaclass.ClosureStaticMetaMethod) DefaultCachedMethodKey(org.codehaus.groovy.runtime.DefaultCachedMethodKey) MethodKey(org.codehaus.groovy.runtime.MethodKey) DefaultCachedMethodKey(org.codehaus.groovy.runtime.DefaultCachedMethodKey)

Aggregations

ClosureStaticMetaMethod (org.codehaus.groovy.runtime.metaclass.ClosureStaticMetaMethod)4 DefaultCachedMethodKey (org.codehaus.groovy.runtime.DefaultCachedMethodKey)2 MethodKey (org.codehaus.groovy.runtime.MethodKey)2 ClosureMetaMethod (org.codehaus.groovy.runtime.metaclass.ClosureMetaMethod)2 MixinInstanceMetaMethod (org.codehaus.groovy.runtime.metaclass.MixinInstanceMetaMethod)2 ThreadManagedMetaBeanProperty (org.codehaus.groovy.runtime.metaclass.ThreadManagedMetaBeanProperty)2