Java Code Examples for groovy.lang.Closure#getOwner()

The following examples show how to use groovy.lang.Closure#getOwner() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BaseTemplate.java    From groovy with Apache License 2.0 6 votes vote down vote up
/**
 * Wraps a closure so that it can be used as a prototype for inclusion in layouts. This is useful when
 * you want to use a closure in a model, but that you don't want to render the result of the closure but instead
 * call it as if it was a specification of a template fragment.
 * @param cl the fragment to be wrapped
 * @return a wrapped closure returning an empty string
 */
public Closure contents(final Closure cl) {
    return new Closure(cl.getOwner(), cl.getThisObject()) {
        private static final long serialVersionUID = -5733727697043906478L;

        @Override
        public Object call() {
            cl.call();
            return "";
        }

        @Override
        public Object call(final Object... args) {
            cl.call(args);
            return "";
        }

        @Override
        public Object call(final Object arguments) {
            cl.call(arguments);
            return "";
        }
    };
}
 
Example 2
Source File: FactoryBuilderSupport.java    From groovy with Apache License 2.0 6 votes vote down vote up
/**
 * Assigns any existing properties to the node.<br>
 * It will call attributeDelegates before passing control to the factory
 * that built the node.
 *
 * @param node       the object returned by tne node factory
 * @param attributes the attributes for the node
 */
protected void handleNodeAttributes(Object node, Map attributes) {
    // first, short circuit
    if (node == null) {
        return;
    }

    for (Closure attrDelegate : getProxyBuilder().getAttributeDelegates()) {
        FactoryBuilderSupport builder = this;
        if (attrDelegate.getOwner() instanceof FactoryBuilderSupport) {
            builder = (FactoryBuilderSupport) attrDelegate.getOwner();
        } else if (attrDelegate.getDelegate() instanceof FactoryBuilderSupport) {
            builder = (FactoryBuilderSupport) attrDelegate.getDelegate();
        }

        attrDelegate.call(builder, node, attributes);
    }

    if (getProxyBuilder().getCurrentFactory().onHandleNodeAttributes(getProxyBuilder().getChildBuilder(), node, attributes)) {
        getProxyBuilder().setNodeAttributes(node, attributes);
    }
}
 
Example 3
Source File: Memoize.java    From groovy with Apache License 2.0 5 votes vote down vote up
MemoizeFunction(final MemoizeCache<Object, ?> cache, Closure<V> closure) {
    super(closure.getOwner());
    this.cache = coerce(cache);
    this.closure = closure;
    parameterTypes = closure.getParameterTypes();
    maximumNumberOfParameters = closure.getMaximumNumberOfParameters();
}
 
Example 4
Source File: AbstractNamedDomainObjectContainer.java    From pushfish-android with BSD 2-Clause "Simplified" License 4 votes vote down vote up
protected Object createConfigureDelegate(Closure configureClosure) {
    return new NamedDomainObjectContainerConfigureDelegate(configureClosure.getOwner(), this);
}
 
Example 5
Source File: TypedDomainObjectContainerWrapper.java    From pushfish-android with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public NamedDomainObjectContainer<U> configure(Closure configureClosure) {
    NamedDomainObjectContainerConfigureDelegate delegate = new NamedDomainObjectContainerConfigureDelegate(configureClosure.getOwner(), this);
    ConfigureUtil.configure(configureClosure, delegate);
    return this;
}
 
Example 6
Source File: AbstractPolymorphicDomainObjectContainer.java    From pushfish-android with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
protected Object createConfigureDelegate(Closure configureClosure) {
    return new PolymorphicDomainObjectContainerConfigureDelegate(configureClosure.getOwner(), this);
}
 
Example 7
Source File: AbstractNamedDomainObjectContainer.java    From pushfish-android with BSD 2-Clause "Simplified" License 4 votes vote down vote up
protected Object createConfigureDelegate(Closure configureClosure) {
    return new NamedDomainObjectContainerConfigureDelegate(configureClosure.getOwner(), this);
}
 
Example 8
Source File: AbstractPolymorphicDomainObjectContainer.java    From pushfish-android with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
protected Object createConfigureDelegate(Closure configureClosure) {
    return new PolymorphicDomainObjectContainerConfigureDelegate(configureClosure.getOwner(), this);
}
 
Example 9
Source File: AbstractNamedDomainObjectContainer.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 4 votes vote down vote up
protected Object createConfigureDelegate(Closure configureClosure) {
    return new NamedDomainObjectContainerConfigureDelegate(configureClosure.getOwner(), this);
}
 
Example 10
Source File: TypedDomainObjectContainerWrapper.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public NamedDomainObjectContainer<U> configure(Closure configureClosure) {
    NamedDomainObjectContainerConfigureDelegate delegate = new NamedDomainObjectContainerConfigureDelegate(configureClosure.getOwner(), this);
    ConfigureUtil.configure(configureClosure, delegate);
    return this;
}
 
Example 11
Source File: AbstractPolymorphicDomainObjectContainer.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
protected Object createConfigureDelegate(Closure configureClosure) {
    return new PolymorphicDomainObjectContainerConfigureDelegate(configureClosure.getOwner(), this);
}
 
Example 12
Source File: AbstractNamedDomainObjectContainer.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 4 votes vote down vote up
protected Object createConfigureDelegate(Closure configureClosure) {
    return new NamedDomainObjectContainerConfigureDelegate(configureClosure.getOwner(), this);
}
 
Example 13
Source File: AbstractPolymorphicDomainObjectContainer.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
protected Object createConfigureDelegate(Closure configureClosure) {
    return new PolymorphicDomainObjectContainerConfigureDelegate(configureClosure.getOwner(), this);
}
 
Example 14
Source File: RestrictiveGroovyInterceptor.java    From cerberus-source with GNU General Public License v3.0 4 votes vote down vote up
private boolean checkMethodCall(Object receiver, String method)
        throws GroovyRestrictionException {
    if (receiver instanceof Closure) {
        // Closure method names were tested before.
        Closure<?> closure = (Closure<?>) receiver;
        Object owner = closure.getOwner();
        Object delegate = closure.getDelegate();
        int rs = closure.getResolveStrategy();
        // Check owner first.
        if (rs == Closure.OWNER_FIRST || rs == Closure.OWNER_ONLY) {
            if (checkMethodCall(owner, method)) {
                return true;
            }
        }
        // Check delegate first/second.
        if (rs == Closure.OWNER_FIRST || rs == Closure.DELEGATE_FIRST
                || rs == Closure.DELEGATE_ONLY) {
            if (delegate != null && delegate != closure) {
                if (checkMethodCall(delegate, method)) {
                    return true;
                }
            }
        }
        // Check owner second.
        if (rs == Closure.DELEGATE_FIRST) {
            if (checkMethodCall(owner, method)) {
                return true;
            }
        }

        // Cannot be 100% sure whether the call will be handled by
        // delegation to this closure.
        return false;
    } else if (isAllowedClass(receiver.getClass())) {
        checkExecute(receiver, method);
        return instanceAllAllowedClasses.contains(receiver.getClass())
                || !InvokerHelper.getMetaClass(receiver).respondsTo(receiver, method).isEmpty();
    } else if (isScriptClass(receiver.getClass()) && !disallowedScriptMethods.contains(method)) {
        return !InvokerHelper.getMetaClass(receiver).respondsTo(receiver, method).isEmpty();
    }
    throw new GroovyRestrictionException("Possible access of method " + method + " on class "
            + receiver.getClass().getSimpleName()
            + " is not allowed in Groovy transformations!");
}
 
Example 15
Source File: RestrictiveGroovyInterceptor.java    From cerberus-source with GNU General Public License v3.0 4 votes vote down vote up
private boolean checkPropertyAccess(Object receiver, String property, boolean set)
        throws GroovyRestrictionException {
    if (receiver instanceof Closure) {
        // Closure properties were tested before.
        Closure<?> closure = (Closure<?>) receiver;
        Object owner = closure.getOwner();
        Object delegate = closure.getDelegate();
        int rs = closure.getResolveStrategy();
        // Check owner first.
        if (rs == Closure.OWNER_FIRST || rs == Closure.OWNER_ONLY) {
            if (checkPropertyAccess(owner, property, set)) {
                return true;
            }
        }
        // Check delegate first/second.
        if (rs == Closure.OWNER_FIRST || rs == Closure.DELEGATE_FIRST
                || rs == Closure.DELEGATE_ONLY) {
            if (delegate != null && delegate != closure) {
                if (checkPropertyAccess(delegate, property, set)) {
                    return true;
                }
            }
        }
        // Check owner second.
        if (rs == Closure.DELEGATE_FIRST) {
            if (checkPropertyAccess(owner, property, set)) {
                return true;
            }
        }
        // Cannot be 100% sure whether the property will be handled by
        // delegation to this closure.
        return false;
    } else if (instanceAllAllowedClasses.contains(receiver.getClass())) {
        return true;
    } else if (isAllowedClass(receiver.getClass())) {
        return hasProperty(receiver, property);
    } else if (isScriptClass(receiver.getClass())
            && (!set || !disallowedScriptWriteProperties.contains(property))) {
        return hasProperty(receiver, property);
    }
    throw new GroovyRestrictionException("Possible " + (set ? "write " : "")
            + "access of property " + property + " on class "
            + receiver.getClass().getSimpleName()
            + " is not allowed in Groovy transformations!");
}