Search in sources :

Example 1 with MethodKey

use of org.codehaus.groovy.runtime.MethodKey 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 MethodKey

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

the class ExpandoMetaClass method addSuperMethodIfNotOverridden.

private void addSuperMethodIfNotOverridden(final MetaMethod metaMethodFromSuper) {
    performOperationOnMetaClass(new Callable() {

        public void call() {
            MetaMethod existing = null;
            try {
                existing = pickMethod(metaMethodFromSuper.getName(), metaMethodFromSuper.getNativeParameterTypes());
            } catch (GroovyRuntimeException e) {
            // ignore, this happens with overlapping method definitions
            }
            if (existing == null) {
                addMethodWithKey(metaMethodFromSuper);
            } else {
                boolean isGroovyMethod = getMetaMethods().contains(existing);
                if (isGroovyMethod) {
                    addMethodWithKey(metaMethodFromSuper);
                } else if (inheritedMetaMethods.contains(existing)) {
                    inheritedMetaMethods.remove(existing);
                    addMethodWithKey(metaMethodFromSuper);
                }
            }
        }

        private void addMethodWithKey(final MetaMethod metaMethodFromSuper) {
            inheritedMetaMethods.add(metaMethodFromSuper);
            if (metaMethodFromSuper instanceof ClosureMetaMethod) {
                ClosureMetaMethod closureMethod = (ClosureMetaMethod) metaMethodFromSuper;
                String name = metaMethodFromSuper.getName();
                final Class declaringClass = metaMethodFromSuper.getDeclaringClass().getTheClass();
                ClosureMetaMethod localMethod = ClosureMetaMethod.copy(closureMethod);
                addMetaMethod(localMethod);
                MethodKey key = new DefaultCachedMethodKey(declaringClass, name, localMethod.getParameterTypes(), false);
                checkIfGroovyObjectMethod(localMethod);
                expandoMethods.put(key, localMethod);
            }
        }
    });
}
Also used : ClosureStaticMetaMethod(org.codehaus.groovy.runtime.metaclass.ClosureStaticMetaMethod) MixinInstanceMetaMethod(org.codehaus.groovy.runtime.metaclass.MixinInstanceMetaMethod) ClosureMetaMethod(org.codehaus.groovy.runtime.metaclass.ClosureMetaMethod) ClosureMetaMethod(org.codehaus.groovy.runtime.metaclass.ClosureMetaMethod) OwnedMetaClass(org.codehaus.groovy.runtime.metaclass.OwnedMetaClass) MixedInMetaClass(org.codehaus.groovy.runtime.metaclass.MixedInMetaClass) CachedClass(org.codehaus.groovy.reflection.CachedClass) MixinInMetaClass(org.codehaus.groovy.reflection.MixinInMetaClass) DefaultCachedMethodKey(org.codehaus.groovy.runtime.DefaultCachedMethodKey) MethodKey(org.codehaus.groovy.runtime.MethodKey) DefaultCachedMethodKey(org.codehaus.groovy.runtime.DefaultCachedMethodKey)

Example 3 with MethodKey

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

the class ExpandoMetaClass method registerInstanceMethod.

/**
     * Registers a new instance method for the given method name and closure on this MetaClass
     *
     * @param metaMethod
     */
public void registerInstanceMethod(final MetaMethod metaMethod) {
    final boolean inited = this.initCalled;
    performOperationOnMetaClass(new Callable() {

        public void call() {
            String methodName = metaMethod.getName();
            checkIfGroovyObjectMethod(metaMethod);
            MethodKey key = new DefaultCachedMethodKey(theClass, methodName, metaMethod.getParameterTypes(), false);
            if (isInitialized()) {
                throw new RuntimeException("Already initialized, cannot add new method: " + metaMethod);
            }
            // we always adds meta methods to class itself
            addMetaMethodToIndex(metaMethod, metaMethodIndex.getHeader(theClass));
            dropMethodCache(methodName);
            expandoMethods.put(key, metaMethod);
            if (inited && isGetter(methodName, metaMethod.getParameterTypes())) {
                String propertyName = getPropertyForGetter(methodName);
                registerBeanPropertyForMethod(metaMethod, propertyName, true, false);
            } else if (inited && isSetter(methodName, metaMethod.getParameterTypes())) {
                String propertyName = getPropertyForSetter(methodName);
                registerBeanPropertyForMethod(metaMethod, propertyName, false, false);
            }
            performRegistryCallbacks();
        }
    });
}
Also used : DefaultCachedMethodKey(org.codehaus.groovy.runtime.DefaultCachedMethodKey) MethodKey(org.codehaus.groovy.runtime.MethodKey) DefaultCachedMethodKey(org.codehaus.groovy.runtime.DefaultCachedMethodKey)

Example 4 with MethodKey

use of org.codehaus.groovy.runtime.MethodKey in project groovy by apache.

the class ExpandoMetaClass method addSuperMethodIfNotOverridden.

private void addSuperMethodIfNotOverridden(final MetaMethod metaMethodFromSuper) {
    performOperationOnMetaClass(new Callable() {

        public void call() {
            MetaMethod existing = null;
            try {
                existing = pickMethod(metaMethodFromSuper.getName(), metaMethodFromSuper.getNativeParameterTypes());
            } catch (GroovyRuntimeException e) {
            // ignore, this happens with overlapping method definitions
            }
            if (existing == null) {
                addMethodWithKey(metaMethodFromSuper);
            } else {
                boolean isGroovyMethod = getMetaMethods().contains(existing);
                if (isGroovyMethod) {
                    addMethodWithKey(metaMethodFromSuper);
                } else if (inheritedMetaMethods.contains(existing)) {
                    inheritedMetaMethods.remove(existing);
                    addMethodWithKey(metaMethodFromSuper);
                }
            }
        }

        private void addMethodWithKey(final MetaMethod metaMethodFromSuper) {
            inheritedMetaMethods.add(metaMethodFromSuper);
            if (metaMethodFromSuper instanceof ClosureMetaMethod) {
                ClosureMetaMethod closureMethod = (ClosureMetaMethod) metaMethodFromSuper;
                String name = metaMethodFromSuper.getName();
                final Class declaringClass = metaMethodFromSuper.getDeclaringClass().getTheClass();
                ClosureMetaMethod localMethod = ClosureMetaMethod.copy(closureMethod);
                addMetaMethod(localMethod);
                MethodKey key = new DefaultCachedMethodKey(declaringClass, name, localMethod.getParameterTypes(), false);
                checkIfGroovyObjectMethod(localMethod);
                expandoMethods.put(key, localMethod);
            }
        }
    });
}
Also used : ClosureStaticMetaMethod(org.codehaus.groovy.runtime.metaclass.ClosureStaticMetaMethod) MixinInstanceMetaMethod(org.codehaus.groovy.runtime.metaclass.MixinInstanceMetaMethod) ClosureMetaMethod(org.codehaus.groovy.runtime.metaclass.ClosureMetaMethod) ClosureMetaMethod(org.codehaus.groovy.runtime.metaclass.ClosureMetaMethod) OwnedMetaClass(org.codehaus.groovy.runtime.metaclass.OwnedMetaClass) MixedInMetaClass(org.codehaus.groovy.runtime.metaclass.MixedInMetaClass) CachedClass(org.codehaus.groovy.reflection.CachedClass) MixinInMetaClass(org.codehaus.groovy.reflection.MixinInMetaClass) DefaultCachedMethodKey(org.codehaus.groovy.runtime.DefaultCachedMethodKey) MethodKey(org.codehaus.groovy.runtime.MethodKey) DefaultCachedMethodKey(org.codehaus.groovy.runtime.DefaultCachedMethodKey)

Example 5 with MethodKey

use of org.codehaus.groovy.runtime.MethodKey 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

DefaultCachedMethodKey (org.codehaus.groovy.runtime.DefaultCachedMethodKey)6 MethodKey (org.codehaus.groovy.runtime.MethodKey)6 ClosureStaticMetaMethod (org.codehaus.groovy.runtime.metaclass.ClosureStaticMetaMethod)4 CachedClass (org.codehaus.groovy.reflection.CachedClass)2 MixinInMetaClass (org.codehaus.groovy.reflection.MixinInMetaClass)2 ClosureMetaMethod (org.codehaus.groovy.runtime.metaclass.ClosureMetaMethod)2 MixedInMetaClass (org.codehaus.groovy.runtime.metaclass.MixedInMetaClass)2 MixinInstanceMetaMethod (org.codehaus.groovy.runtime.metaclass.MixinInstanceMetaMethod)2 OwnedMetaClass (org.codehaus.groovy.runtime.metaclass.OwnedMetaClass)2