Java Code Examples for groovy.lang.Closure#DELEGATE_FIRST

The following examples show how to use groovy.lang.Closure#DELEGATE_FIRST . 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: ClosureBackedAction.java    From pushfish-android with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public ClosureBackedAction(Closure closure) {
    this(closure, Closure.DELEGATE_FIRST, true);
}
 
Example 2
Source File: ClosureBackedAction.java    From pushfish-android with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public ClosureBackedAction(Closure closure) {
    this(closure, Closure.DELEGATE_FIRST, true);
}
 
Example 3
Source File: TreeContext.java    From groovy with Apache License 2.0 4 votes vote down vote up
public boolean matches(@DelegatesTo(value=ASTNode.class, strategy=Closure.DELEGATE_FIRST) Closure<Boolean> predicate) {
    return MatcherUtils.cloneWithDelegate(predicate, node).call();
}
 
Example 4
Source File: TreeContext.java    From groovy with Apache License 2.0 4 votes vote down vote up
public void afterVisit(@DelegatesTo(value=TreeContext.class, strategy=Closure.DELEGATE_FIRST) Closure<?> action) {
    Closure<?> clone = MatcherUtils.cloneWithDelegate(action, this);
    afterVisit(DefaultGroovyMethods.asType(clone, TreeContextAction.class));
}
 
Example 5
Source File: ClosureBackedAction.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public ClosureBackedAction(Closure closure) {
    this(closure, Closure.DELEGATE_FIRST, true);
}
 
Example 6
Source File: ClosureBackedAction.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public ClosureBackedAction(Closure closure) {
    this(closure, Closure.DELEGATE_FIRST, true);
}
 
Example 7
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 8
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!");
}