Java Code Examples for groovy.lang.Closure#OWNER_FIRST
The following examples show how to use
groovy.lang.Closure#OWNER_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: RestrictiveGroovyInterceptor.java From cerberus-source with GNU General Public License v3.0 | 4 votes |
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 2
Source File: RestrictiveGroovyInterceptor.java From cerberus-source with GNU General Public License v3.0 | 4 votes |
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!"); }